forked from netty/netty
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace UniqueName with Constant and ConstantPool
- Proposed fix for netty#1824 UniqueName and its subtypes do not allow getting the previously registered instance. For example, let's assume that a user is running his/her application in an OSGi container with Netty bundles and his server bundle. Whenever the server bundle is reloaded, the server will try to create a new AttributeKey instance with the same name. However, Netty bundles were not reloaded at all, so AttributeKey will complain that the name is taken already (by the previously loaded bundle.) To fix this problem: - Replaced UniqueName with Constant, AbstractConstant, and ConstantPool. Better name and better design. - Sctp/Udt/RxtxChannelOption is not a ChannelOption anymore. They are just constant providers and ChannelOption is final now. It's because caching anything that's from outside of netty-transport will lead to ClassCastException on reload, because ChannelOption's constant pool will keep all option objects for reuse. - Signal implements Constant because we can't ensure its uniqueness anymore by relying on the exception raised by UniqueName's constructor.
- Loading branch information
Showing
23 changed files
with
413 additions
and
394 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2012 The Netty Project | ||
* | ||
* The Netty Project licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package io.netty.util; | ||
|
||
/** | ||
* Base implementation of {@link Constant}. | ||
*/ | ||
public abstract class AbstractConstant<T extends AbstractConstant<T>> implements Constant<T> { | ||
|
||
private final int id; | ||
private final String name; | ||
|
||
/** | ||
* Creates a new instance. | ||
*/ | ||
protected AbstractConstant(int id, String name) { | ||
this.id = id; | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public final String name() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public final int id() { | ||
return id; | ||
} | ||
|
||
@Override | ||
public final int hashCode() { | ||
return super.hashCode(); | ||
} | ||
|
||
@Override | ||
public final boolean equals(Object o) { | ||
return super.equals(o); | ||
} | ||
|
||
@Override | ||
public final int compareTo(T other) { | ||
if (this == other) { | ||
return 0; | ||
} | ||
|
||
int returnCode = name.compareTo(other.name()); | ||
if (returnCode != 0) { | ||
return returnCode; | ||
} | ||
|
||
return ((Integer) id).compareTo(other.id()); | ||
} | ||
|
||
@Override | ||
public final String toString() { | ||
return name(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2012 The Netty Project | ||
* | ||
* The Netty Project licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package io.netty.util; | ||
|
||
/** | ||
* A singleton which is safe to compare via the {@code ==} operator. Created and managed by {@link ConstantPool}. | ||
*/ | ||
public interface Constant<T extends Constant<T>> extends Comparable<T> { | ||
|
||
/** | ||
* Returns the unique number assigned to this {@link Constant}. | ||
*/ | ||
int id(); | ||
|
||
/** | ||
* Returns the name of this {@link Constant}. | ||
*/ | ||
String name(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2013 The Netty Project | ||
* | ||
* The Netty Project licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package io.netty.util; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* A pool of {@link Constant}s. | ||
* | ||
* @param <T> the type of the constant | ||
*/ | ||
public abstract class ConstantPool<T extends Constant<T>> { | ||
|
||
private final Map<String, T> constants = new HashMap<String, T>(); | ||
|
||
private int nextId = 1; | ||
|
||
/** | ||
* Creates a new or the existing {@link Constant} mapped to the specified {@code name}. | ||
*/ | ||
public T valueOf(String name) { | ||
if (name == null) { | ||
throw new NullPointerException("name"); | ||
} | ||
|
||
synchronized (constants) { | ||
T c = constants.get(name); | ||
if (c == null) { | ||
c = newConstant(nextId, name); | ||
constants.put(name, c); | ||
nextId ++; | ||
} | ||
|
||
return c; | ||
} | ||
} | ||
|
||
protected abstract T newConstant(int id, String name); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.