Move AbstractBootstrap.ChannelFactory out of AbstractBootstrap and hide AbstractBootstrap from a user
- Fixes #998 - Also generified ChannelFactory
This commit is contained in:
parent
86135a4080
commit
23438de66f
@ -35,10 +35,10 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
* method-chaining to provide an easy way to configure the {@link AbstractBootstrap}.
|
||||
*
|
||||
*/
|
||||
public abstract class AbstractBootstrap<B extends AbstractBootstrap<?>> implements Cloneable {
|
||||
abstract class AbstractBootstrap<B extends AbstractBootstrap<?, C>, C extends Channel> implements Cloneable {
|
||||
|
||||
private EventLoopGroup group;
|
||||
private ChannelFactory channelFactory;
|
||||
private ChannelFactory<? extends C> channelFactory;
|
||||
private SocketAddress localAddress;
|
||||
private final Map<ChannelOption<?>, Object> options = new ConcurrentHashMap<ChannelOption<?>, Object>();
|
||||
private final Map<AttributeKey<?>, Object> attrs = new ConcurrentHashMap<AttributeKey<?>, Object>();
|
||||
@ -48,7 +48,7 @@ public abstract class AbstractBootstrap<B extends AbstractBootstrap<?>> implemen
|
||||
// Disallow extending from a different package.
|
||||
}
|
||||
|
||||
AbstractBootstrap(AbstractBootstrap<B> bootstrap) {
|
||||
AbstractBootstrap(AbstractBootstrap<B, C> bootstrap) {
|
||||
group = bootstrap.group;
|
||||
channelFactory = bootstrap.channelFactory;
|
||||
handler = bootstrap.handler;
|
||||
@ -78,11 +78,11 @@ public abstract class AbstractBootstrap<B extends AbstractBootstrap<?>> implemen
|
||||
* You either use this or {@link #channelFactory(ChannelFactory)} if your
|
||||
* {@link Channel} implementation has no no-args constructor.
|
||||
*/
|
||||
public B channel(Class<? extends Channel> channelClass) {
|
||||
public B channel(Class<? extends C> channelClass) {
|
||||
if (channelClass == null) {
|
||||
throw new NullPointerException("channelClass");
|
||||
}
|
||||
return channelFactory(new BootstrapChannelFactory(channelClass));
|
||||
return channelFactory(new BootstrapChannelFactory<C>(channelClass));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -93,7 +93,7 @@ public abstract class AbstractBootstrap<B extends AbstractBootstrap<?>> implemen
|
||||
* simplify your code.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public B channelFactory(ChannelFactory channelFactory) {
|
||||
public B channelFactory(ChannelFactory<? extends C> channelFactory) {
|
||||
if (channelFactory == null) {
|
||||
throw new NullPointerException("channelFactory");
|
||||
}
|
||||
@ -267,7 +267,7 @@ public abstract class AbstractBootstrap<B extends AbstractBootstrap<?>> implemen
|
||||
return localAddress;
|
||||
}
|
||||
|
||||
final ChannelFactory channelFactory() {
|
||||
final ChannelFactory<? extends C> channelFactory() {
|
||||
return channelFactory;
|
||||
}
|
||||
|
||||
@ -331,15 +331,15 @@ public abstract class AbstractBootstrap<B extends AbstractBootstrap<?>> implemen
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
private static final class BootstrapChannelFactory implements ChannelFactory {
|
||||
private final Class<? extends Channel> clazz;
|
||||
private static final class BootstrapChannelFactory<T extends Channel> implements ChannelFactory<T> {
|
||||
private final Class<? extends T> clazz;
|
||||
|
||||
BootstrapChannelFactory(Class<? extends Channel> clazz) {
|
||||
BootstrapChannelFactory(Class<? extends T> clazz) {
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Channel newChannel() {
|
||||
public T newChannel() {
|
||||
try {
|
||||
return clazz.newInstance();
|
||||
} catch (Throwable t) {
|
||||
@ -352,16 +352,4 @@ public abstract class AbstractBootstrap<B extends AbstractBootstrap<?>> implemen
|
||||
return clazz.getSimpleName() + ".class";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory that is responsible to create new {@link Channel}'s on {@link AbstractBootstrap#bind()}
|
||||
* requests.
|
||||
*
|
||||
*/
|
||||
public interface ChannelFactory {
|
||||
/**
|
||||
* {@link Channel} to use in the {@link AbstractBootstrap}
|
||||
*/
|
||||
Channel newChannel();
|
||||
}
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ import java.util.Map.Entry;
|
||||
* for clients.
|
||||
*
|
||||
*/
|
||||
public final class Bootstrap extends AbstractBootstrap<Bootstrap> {
|
||||
public final class Bootstrap extends AbstractBootstrap<Bootstrap, Channel> {
|
||||
|
||||
private static final InternalLogger logger = InternalLoggerFactory.getInstance(Bootstrap.class);
|
||||
|
||||
|
@ -0,0 +1,29 @@
|
||||
/*
|
||||
* 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.bootstrap;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
|
||||
/**
|
||||
* Factory that creates a new {@link Channel} on {@link Bootstrap#bind()}, {@link Bootstrap#connect()}, and
|
||||
* {@link ServerBootstrap#bind()}.
|
||||
*/
|
||||
public interface ChannelFactory<T extends Channel> {
|
||||
/**
|
||||
* Creates a new channel.
|
||||
*/
|
||||
T newChannel();
|
||||
}
|
@ -43,7 +43,7 @@ import java.util.Map.Entry;
|
||||
* {@link Bootstrap} sub-class which allows easy bootstrap of {@link ServerChannel}
|
||||
*
|
||||
*/
|
||||
public final class ServerBootstrap extends AbstractBootstrap<ServerBootstrap> {
|
||||
public final class ServerBootstrap extends AbstractBootstrap<ServerBootstrap, ServerChannel> {
|
||||
|
||||
private static final InternalLogger logger = InternalLoggerFactory.getInstance(ServerBootstrap.class);
|
||||
|
||||
@ -94,21 +94,6 @@ public final class ServerBootstrap extends AbstractBootstrap<ServerBootstrap> {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The {@link Class} which is used to create the {@link ServerChannel} from (for the acceptor).
|
||||
*/
|
||||
@Override
|
||||
public ServerBootstrap channel(Class<? extends Channel> channelClass) {
|
||||
if (channelClass == null) {
|
||||
throw new NullPointerException("channelClass");
|
||||
}
|
||||
if (!ServerChannel.class.isAssignableFrom(channelClass)) {
|
||||
throw new IllegalArgumentException(
|
||||
"channelClass must be subtype of " + ServerChannel.class.getSimpleName() + '.');
|
||||
}
|
||||
return super.channel(channelClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allow to specify a {@link ChannelOption} which is used for the {@link Channel} instances once they get created
|
||||
* (after the acceptor accepted the {@link Channel}). Use a value of {@code null} to remove a previous set
|
||||
|
Loading…
Reference in New Issue
Block a user