import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;
import io.netty.util.concurrent.EventExecutorGroup;
import java.net.InetSocketAddress;
import java.util.Arrays;
import javax.net.ssl.SSLContext;
import org.restexpress.RestExpress;
import org.restexpress.ServerBootstrapFactory;
import org.restexpress.pipeline.PipelineInitializer;
import org.restexpress.settings.ServerSettings;
public class BindableAddressRestExpress extends RestExpress {
private String bindAddress;
public String getBindAddress() {
return bindAddress;
}
public BindAddressRestExpress setBindAddress(String bindAddress) {
this.bindAddress = bindAddress;
return this;
}
@Override
public Channel bind(int port) {
setPort(port);
ServerBootstrapFactory bootstrapFactory = ReflectionUtils.getFieldInObject(this, "bootstrapFactory", ServerBootstrapFactory.class);
ServerBootstrap bootstrap = bootstrapFactory.newServerBootstrap(getIoThreadCount());
bootstrap.childHandler(new PipelineInitializer()
.setExecutionHandler(ReflectionUtils.callHierarchicalMethodOnObject("initializeExecutorGroup", this, EventExecutorGroup.class,Arrays.asList() ,null))
.addRequestHandler(buildRequestHandler())
.setSSLContext(ReflectionUtils.getFieldInObject(this, "sslContext", SSLContext.class))
.setMaxContentLength(ReflectionUtils.getFieldInObject(this, "serverSettings", ServerSettings.class).getMaxContentSize()));
ReflectionUtils.callHierarchicalMethodOnObject("setBootstrapOptions", this, void.class, Arrays.asList(ServerBootstrap.class),bootstrap);
// Bind and start to accept incoming connections.
if (shouldUseSystemOut())
{
System.out.println(getName() + " server listening on port " + port);
}
Channel channel = bootstrap.bind(new InetSocketAddress(bindAddress, port)).channel();
ReflectionUtils.getFieldInObject(this, "allChannels", ChannelGroup.class).add(channel);
ReflectionUtils.callHierarchicalMethodOnObject("bindPlugins", this, void.class,Arrays.asList(), null);
return channel;
}
}