You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2013/06/26 09:40:34 UTC
[3/3] git commit: CAMEL-6488: camel-netty-http allow to share port in
OSGi environment. Work in progress.
CAMEL-6488: camel-netty-http allow to share port in OSGi environment. Work in progress.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/c90f9244
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/c90f9244
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/c90f9244
Branch: refs/heads/master
Commit: c90f924412bbcba14678d3eed8b7c32a57e95c05
Parents: 1f8d47a
Author: Claus Ibsen <da...@apache.org>
Authored: Wed Jun 26 09:39:24 2013 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed Jun 26 09:39:24 2013 +0200
----------------------------------------------------------------------
.../http/DefaultNettySharedHttpServer.java | 83 ++++++++++
.../http/DefaultSharedNettyHttpServer.java | 78 ---------
.../netty/http/HttpServerPipelineFactory.java | 77 ++++-----
.../http/HttpServerSharedPipelineFactory.java | 166 +++++++++++++++++++
.../netty/http/NettyHttpComponent.java | 14 ++
.../component/netty/http/NettyHttpConsumer.java | 10 --
.../component/netty/http/NettyHttpEndpoint.java | 17 +-
.../netty/http/NettySharedHttpServer.java | 59 +++++++
.../netty/http/SharedNettyHttpServer.java | 53 ------
.../netty/http/NettySharedHttpServerTest.java | 18 +-
10 files changed, 370 insertions(+), 205 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/c90f9244/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettySharedHttpServer.java
----------------------------------------------------------------------
diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettySharedHttpServer.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettySharedHttpServer.java
new file mode 100644
index 0000000..0917d51
--- /dev/null
+++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettySharedHttpServer.java
@@ -0,0 +1,83 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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 org.apache.camel.component.netty.http;
+
+import org.apache.camel.component.netty.NettyServerBootstrapConfiguration;
+import org.apache.camel.component.netty.NettyServerBootstrapFactory;
+import org.apache.camel.component.netty.http.handlers.HttpServerMultiplexChannelHandler;
+import org.apache.camel.spi.ClassResolver;
+import org.apache.camel.support.ServiceSupport;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.ServiceHelper;
+import org.jboss.netty.channel.ChannelPipelineFactory;
+
+/**
+ * A default {@link NettySharedHttpServer} to make sharing Netty server in Camel applications easier.
+ */
+public class DefaultNettySharedHttpServer extends ServiceSupport implements NettySharedHttpServer {
+
+ private NettyServerBootstrapConfiguration configuration;
+ private HttpServerConsumerChannelFactory channelFactory;
+ private HttpServerBootstrapFactory bootstrapFactory;
+ private ClassResolver classResolver;
+
+ public void setNettyServerBootstrapConfiguration(NettyServerBootstrapConfiguration configuration) {
+ this.configuration = configuration;
+ }
+
+ public void setClassResolver(ClassResolver classResolver) {
+ this.classResolver = classResolver;
+ }
+
+ public int getPort() {
+ return configuration != null ? configuration.getPort() : -1;
+ }
+
+ public HttpServerConsumerChannelFactory getConsumerChannelFactory() {
+ return channelFactory;
+ }
+
+ public NettyServerBootstrapFactory getServerBootstrapFactory() {
+ return bootstrapFactory;
+ }
+
+ protected void doStart() throws Exception {
+ ObjectHelper.notNull(configuration, "setNettyServerBootstrapConfiguration() must be called with a NettyServerBootstrapConfiguration instance", this);
+
+ // port must be set
+ if (configuration.getPort() <= 0) {
+ throw new IllegalArgumentException("Port must be configured on NettyServerBootstrapConfiguration " + configuration);
+ }
+
+ // force using tcp as the underlying transport
+ configuration.setProtocol("tcp");
+
+ channelFactory = new HttpServerMultiplexChannelHandler();
+ channelFactory.init(configuration.getPort());
+
+ ChannelPipelineFactory pipelineFactory = new HttpServerSharedPipelineFactory(configuration, channelFactory, classResolver);
+
+ // create bootstrap factory and disable compatible check as its shared among the consumers
+ bootstrapFactory = new HttpServerBootstrapFactory(channelFactory, false);
+ bootstrapFactory.init(null, configuration, pipelineFactory);
+ }
+
+ @Override
+ protected void doStop() throws Exception {
+ ServiceHelper.stopServices(bootstrapFactory, channelFactory);
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/c90f9244/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultSharedNettyHttpServer.java
----------------------------------------------------------------------
diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultSharedNettyHttpServer.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultSharedNettyHttpServer.java
deleted file mode 100644
index ff24819..0000000
--- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultSharedNettyHttpServer.java
+++ /dev/null
@@ -1,78 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF 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 org.apache.camel.component.netty.http;
-
-import org.apache.camel.component.netty.DefaultServerPipelineFactory;
-import org.apache.camel.component.netty.NettyServerBootstrapConfiguration;
-import org.apache.camel.component.netty.NettyServerBootstrapFactory;
-import org.apache.camel.component.netty.http.handlers.HttpServerMultiplexChannelHandler;
-import org.apache.camel.support.ServiceSupport;
-import org.apache.camel.util.ObjectHelper;
-import org.apache.camel.util.ServiceHelper;
-import org.jboss.netty.channel.ChannelPipelineFactory;
-
-/**
- * A default {@link SharedNettyHttpServer} to make sharing Netty server in Camel applications easier.
- */
-public class DefaultSharedNettyHttpServer extends ServiceSupport implements SharedNettyHttpServer {
-
- private NettyServerBootstrapConfiguration configuration;
- private HttpServerConsumerChannelFactory channelFactory;
- private HttpServerBootstrapFactory bootstrapFactory;
-
- public void setNettyServerBootstrapConfiguration(NettyServerBootstrapConfiguration configuration) {
- this.configuration = configuration;
- }
-
- public int getPort() {
- return configuration != null ? configuration.getPort() : -1;
- }
-
- public HttpServerConsumerChannelFactory getConsumerChannelFactory() {
- return channelFactory;
- }
-
- public NettyServerBootstrapFactory getServerBootstrapFactory() {
- return bootstrapFactory;
- }
-
- protected void doStart() throws Exception {
- ObjectHelper.notNull(configuration, "setNettyServerBootstrapConfiguration() must be called with a NettyServerBootstrapConfiguration instance", this);
-
- // port must be set
- if (configuration.getPort() <= 0) {
- throw new IllegalArgumentException("Port must be configured on NettyServerBootstrapConfiguration " + configuration);
- }
-
- // force using tcp as the underlying transport
- configuration.setProtocol("tcp");
- // TODO: ChannelPipelineFactory should be a shared to handle adding consumers
- ChannelPipelineFactory pipelineFactory = new HttpServerPipelineFactory(configuration);
-
- channelFactory = new HttpServerMultiplexChannelHandler();
- channelFactory.init(configuration.getPort());
-
- // create bootstrap factory and disable compatible check as its shared among the consumers
- bootstrapFactory = new HttpServerBootstrapFactory(channelFactory, false);
- bootstrapFactory.init(null, configuration, pipelineFactory);
- }
-
- @Override
- protected void doStop() throws Exception {
- ServiceHelper.stopServices(bootstrapFactory, channelFactory);
- }
-}
http://git-wip-us.apache.org/repos/asf/camel/blob/c90f9244/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerPipelineFactory.java
----------------------------------------------------------------------
diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerPipelineFactory.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerPipelineFactory.java
index 9772460..6eae81c 100644
--- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerPipelineFactory.java
+++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerPipelineFactory.java
@@ -23,6 +23,7 @@ import org.apache.camel.component.netty.NettyConsumer;
import org.apache.camel.component.netty.NettyServerBootstrapConfiguration;
import org.apache.camel.component.netty.ServerPipelineFactory;
import org.apache.camel.component.netty.ssl.SSLEngineFactory;
+import org.apache.camel.spi.ClassResolver;
import org.apache.camel.util.ObjectHelper;
import org.jboss.netty.channel.ChannelHandler;
import org.jboss.netty.channel.ChannelPipeline;
@@ -41,28 +42,17 @@ import org.slf4j.LoggerFactory;
public class HttpServerPipelineFactory extends ServerPipelineFactory {
private static final Logger LOG = LoggerFactory.getLogger(HttpServerPipelineFactory.class);
- private NettyHttpConsumer consumer;
- private SSLContext sslContext;
+ protected NettyHttpConsumer consumer;
+ protected SSLContext sslContext;
+ protected NettyServerBootstrapConfiguration configuration;
public HttpServerPipelineFactory() {
// default constructor needed
}
- public HttpServerPipelineFactory(NettyServerBootstrapConfiguration configuration) {
- this.consumer = null;
- try {
- this.sslContext = createSSLContext(configuration);
- } catch (Exception e) {
- throw ObjectHelper.wrapRuntimeCamelException(e);
- }
-
- if (sslContext != null) {
- LOG.info("Created SslContext {}", sslContext);
- }
- }
-
public HttpServerPipelineFactory(NettyHttpConsumer nettyConsumer) {
this.consumer = nettyConsumer;
+ this.configuration = nettyConsumer.getConfiguration();
try {
this.sslContext = createSSLContext(consumer.getConfiguration());
} catch (Exception e) {
@@ -84,8 +74,7 @@ public class HttpServerPipelineFactory extends ServerPipelineFactory {
// Create a default pipeline implementation.
ChannelPipeline pipeline = Channels.pipeline();
- // TODO: on demand use configuration
- SslHandler sslHandler = configureServerSSLOnDemand();
+ SslHandler sslHandler = configureServerSSLOnDemand(configuration);
if (sslHandler != null) {
LOG.debug("Server SSL handler configured and added as an interceptor against the ChannelPipeline: {}", sslHandler);
pipeline.addLast("ssl", sslHandler);
@@ -101,15 +90,8 @@ public class HttpServerPipelineFactory extends ServerPipelineFactory {
pipeline.addLast("deflater", new HttpContentCompressor());
}
- // TODO: shared netty http server in ctr
- // handler to route Camel messages
- ChannelHandler handler;
- if (consumer.getSharedNettyHttpServer() != null) {
- handler = consumer.getSharedNettyHttpServer().getConsumerChannelFactory().getChannelHandler();
- } else {
- int port = consumer.getConfiguration().getPort();
- handler = consumer.getEndpoint().getComponent().getMultiplexChannelHandler(port).getChannelHandler();
- }
+ int port = consumer.getConfiguration().getPort();
+ ChannelHandler handler = consumer.getEndpoint().getComponent().getMultiplexChannelHandler(port).getChannelHandler();
pipeline.addLast("handler", handler);
return pipeline;
@@ -129,47 +111,48 @@ public class HttpServerPipelineFactory extends ServerPipelineFactory {
return null;
}
- private SslHandler configureServerSSLOnDemand() throws Exception {
- if (!consumer.getConfiguration().isSsl()) {
+ private SslHandler configureServerSSLOnDemand(NettyServerBootstrapConfiguration configuration) throws Exception {
+ if (!configuration.isSsl()) {
return null;
}
- if (consumer.getConfiguration().getSslHandler() != null) {
- return consumer.getConfiguration().getSslHandler();
+ if (configuration.getSslHandler() != null) {
+ return configuration.getSslHandler();
} else if (sslContext != null) {
SSLEngine engine = sslContext.createSSLEngine();
engine.setUseClientMode(false);
- engine.setNeedClientAuth(consumer.getConfiguration().isNeedClientAuth());
+ engine.setNeedClientAuth(configuration.isNeedClientAuth());
return new SslHandler(engine);
} else {
- if (consumer.getConfiguration().getKeyStoreFile() == null && consumer.getConfiguration().getKeyStoreResource() == null) {
+ if (configuration.getKeyStoreFile() == null && configuration.getKeyStoreResource() == null) {
LOG.debug("keystorefile is null");
}
- if (consumer.getConfiguration().getTrustStoreFile() == null && consumer.getConfiguration().getTrustStoreResource() == null) {
+ if (configuration.getTrustStoreFile() == null && configuration.getTrustStoreResource() == null) {
LOG.debug("truststorefile is null");
}
- if (consumer.getConfiguration().getPassphrase().toCharArray() == null) {
+ if (configuration.getPassphrase().toCharArray() == null) {
LOG.debug("passphrase is null");
}
SSLEngineFactory sslEngineFactory;
- if (consumer.getConfiguration().getKeyStoreFile() != null || consumer.getConfiguration().getTrustStoreFile() != null) {
+ if (configuration.getKeyStoreFile() != null || configuration.getTrustStoreFile() != null) {
sslEngineFactory = new SSLEngineFactory(
- consumer.getConfiguration().getKeyStoreFormat(),
- consumer.getConfiguration().getSecurityProvider(),
- consumer.getConfiguration().getKeyStoreFile(),
- consumer.getConfiguration().getTrustStoreFile(),
- consumer.getConfiguration().getPassphrase().toCharArray());
+ configuration.getKeyStoreFormat(),
+ configuration.getSecurityProvider(),
+ configuration.getKeyStoreFile(),
+ configuration.getTrustStoreFile(),
+ configuration.getPassphrase().toCharArray());
} else {
- sslEngineFactory = new SSLEngineFactory(consumer.getContext().getClassResolver(),
- consumer.getConfiguration().getKeyStoreFormat(),
- consumer.getConfiguration().getSecurityProvider(),
- consumer.getConfiguration().getKeyStoreResource(),
- consumer.getConfiguration().getTrustStoreResource(),
- consumer.getConfiguration().getPassphrase().toCharArray());
+ ClassResolver resolver = consumer != null ? consumer.getContext().getClassResolver() : null;
+ sslEngineFactory = new SSLEngineFactory(resolver,
+ configuration.getKeyStoreFormat(),
+ configuration.getSecurityProvider(),
+ configuration.getKeyStoreResource(),
+ configuration.getTrustStoreResource(),
+ configuration.getPassphrase().toCharArray());
}
SSLEngine sslEngine = sslEngineFactory.createServerSSLEngine();
sslEngine.setUseClientMode(false);
- sslEngine.setNeedClientAuth(consumer.getConfiguration().isNeedClientAuth());
+ sslEngine.setNeedClientAuth(configuration.isNeedClientAuth());
return new SslHandler(sslEngine);
}
}
http://git-wip-us.apache.org/repos/asf/camel/blob/c90f9244/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerSharedPipelineFactory.java
----------------------------------------------------------------------
diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerSharedPipelineFactory.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerSharedPipelineFactory.java
new file mode 100644
index 0000000..d82143d
--- /dev/null
+++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerSharedPipelineFactory.java
@@ -0,0 +1,166 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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 org.apache.camel.component.netty.http;
+
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLEngine;
+
+import org.apache.camel.component.netty.NettyConsumer;
+import org.apache.camel.component.netty.NettyServerBootstrapConfiguration;
+import org.apache.camel.component.netty.ServerPipelineFactory;
+import org.apache.camel.component.netty.ssl.SSLEngineFactory;
+import org.apache.camel.spi.ClassResolver;
+import org.apache.camel.util.ObjectHelper;
+import org.jboss.netty.channel.ChannelPipeline;
+import org.jboss.netty.channel.Channels;
+import org.jboss.netty.handler.codec.http.HttpChunkAggregator;
+import org.jboss.netty.handler.codec.http.HttpContentCompressor;
+import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
+import org.jboss.netty.handler.codec.http.HttpResponseEncoder;
+import org.jboss.netty.handler.ssl.SslHandler;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A shared {@link org.apache.camel.component.netty.ServerPipelineFactory} for a shared Netty HTTP server.
+ *
+ * @see NettySharedHttpServer
+ */
+public class HttpServerSharedPipelineFactory extends HttpServerPipelineFactory {
+
+ private static final Logger LOG = LoggerFactory.getLogger(HttpServerSharedPipelineFactory.class);
+ private final NettyServerBootstrapConfiguration configuration;
+ private final HttpServerConsumerChannelFactory channelFactory;
+ private final ClassResolver classResolver;
+ private SSLContext sslContext;
+
+ public HttpServerSharedPipelineFactory(NettyServerBootstrapConfiguration configuration, HttpServerConsumerChannelFactory channelFactory,
+ ClassResolver classResolver) {
+ this.configuration = configuration;
+ this.channelFactory = channelFactory;
+ this.classResolver = classResolver;
+ try {
+ this.sslContext = createSSLContext(configuration);
+ } catch (Exception e) {
+ throw ObjectHelper.wrapRuntimeCamelException(e);
+ }
+
+ if (sslContext != null) {
+ LOG.info("Created SslContext {}", sslContext);
+ }
+ }
+
+ @Override
+ public ServerPipelineFactory createPipelineFactory(NettyConsumer nettyConsumer) {
+ throw new UnsupportedOperationException("Should not call this operation");
+ }
+
+ @Override
+ public ChannelPipeline getPipeline() throws Exception {
+ // Create a default pipeline implementation.
+ ChannelPipeline pipeline = Channels.pipeline();
+
+ SslHandler sslHandler = configureServerSSLOnDemand(configuration);
+ if (sslHandler != null) {
+ LOG.debug("Server SSL handler configured and added as an interceptor against the ChannelPipeline: {}", sslHandler);
+ pipeline.addLast("ssl", sslHandler);
+ }
+
+ pipeline.addLast("decoder", new HttpRequestDecoder());
+ // Uncomment the following line if you don't want to handle HttpChunks.
+ if (supportChunked()) {
+ pipeline.addLast("aggregator", new HttpChunkAggregator(1048576));
+ }
+ pipeline.addLast("encoder", new HttpResponseEncoder());
+ if (supportCompressed()) {
+ pipeline.addLast("deflater", new HttpContentCompressor());
+ }
+
+ pipeline.addLast("handler", channelFactory.getChannelHandler());
+
+ return pipeline;
+ }
+
+ private SSLContext createSSLContext(NettyServerBootstrapConfiguration configuration) throws Exception {
+ if (!configuration.isSsl()) {
+ return null;
+ }
+
+ // create ssl context once
+ if (configuration.getSslContextParameters() != null) {
+ return configuration.getSslContextParameters().createSSLContext();
+ }
+
+ return null;
+ }
+
+ private SslHandler configureServerSSLOnDemand(NettyServerBootstrapConfiguration configuration) throws Exception {
+ if (!configuration.isSsl()) {
+ return null;
+ }
+
+ if (configuration.getSslHandler() != null) {
+ return configuration.getSslHandler();
+ } else if (sslContext != null) {
+ SSLEngine engine = sslContext.createSSLEngine();
+ engine.setUseClientMode(false);
+ engine.setNeedClientAuth(configuration.isNeedClientAuth());
+ return new SslHandler(engine);
+ } else {
+ if (configuration.getKeyStoreFile() == null && configuration.getKeyStoreResource() == null) {
+ LOG.debug("keystorefile is null");
+ }
+ if (configuration.getTrustStoreFile() == null && configuration.getTrustStoreResource() == null) {
+ LOG.debug("truststorefile is null");
+ }
+ if (configuration.getPassphrase().toCharArray() == null) {
+ LOG.debug("passphrase is null");
+ }
+ SSLEngineFactory sslEngineFactory;
+ if (configuration.getKeyStoreFile() != null || configuration.getTrustStoreFile() != null) {
+ sslEngineFactory = new SSLEngineFactory(
+ configuration.getKeyStoreFormat(),
+ configuration.getSecurityProvider(),
+ configuration.getKeyStoreFile(),
+ configuration.getTrustStoreFile(),
+ configuration.getPassphrase().toCharArray());
+ } else {
+ sslEngineFactory = new SSLEngineFactory(classResolver,
+ configuration.getKeyStoreFormat(),
+ configuration.getSecurityProvider(),
+ configuration.getKeyStoreResource(),
+ configuration.getTrustStoreResource(),
+ configuration.getPassphrase().toCharArray());
+ }
+ SSLEngine sslEngine = sslEngineFactory.createServerSSLEngine();
+ sslEngine.setUseClientMode(false);
+ sslEngine.setNeedClientAuth(configuration.isNeedClientAuth());
+ return new SslHandler(sslEngine);
+ }
+ }
+
+ private boolean supportChunked() {
+ // TODO: options on bootstrap
+ return true;
+ }
+
+ private boolean supportCompressed() {
+ // TODO: options on bootstrap
+ return false;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/c90f9244/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpComponent.java
----------------------------------------------------------------------
diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpComponent.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpComponent.java
index 1542c9e..6d5c4c3 100644
--- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpComponent.java
+++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpComponent.java
@@ -31,12 +31,16 @@ import org.apache.camel.util.IntrospectionSupport;
import org.apache.camel.util.ServiceHelper;
import org.apache.camel.util.URISupport;
import org.apache.camel.util.UnsafeUriCharactersEncoder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* Netty HTTP based component.
*/
public class NettyHttpComponent extends NettyComponent implements HeaderFilterStrategyAware {
+ private static final Logger LOG = LoggerFactory.getLogger(NettyHttpComponent.class);
+
// factories which is created by this component and therefore manage their lifecycles
private final Map<Integer, HttpServerConsumerChannelFactory> multiplexChannelHandlers = new HashMap<Integer, HttpServerConsumerChannelFactory>();
private final Map<String, HttpServerBootstrapFactory> bootstrapFactories = new HashMap<String, HttpServerBootstrapFactory>();
@@ -73,6 +77,14 @@ public class NettyHttpComponent extends NettyComponent implements HeaderFilterSt
// validate config
config.validateConfiguration();
+ // are we using a shared http server?
+ NettySharedHttpServer shared = resolveAndRemoveReferenceParameter(parameters, "nettySharedHttpServer", NettySharedHttpServer.class);
+ if (shared != null) {
+ // use port number from the shared http server
+ LOG.debug("Using NettySharedHttpServer: {} with port: {}", shared, shared.getPort());
+ config.setPort(shared.getPort());
+ }
+
NettyHttpEndpoint answer = new NettyHttpEndpoint(remaining, this, config);
answer.setTimer(getTimer());
setProperties(answer.getConfiguration(), parameters);
@@ -90,6 +102,8 @@ public class NettyHttpComponent extends NettyComponent implements HeaderFilterSt
if (answer.getHeaderFilterStrategy() == null) {
answer.setHeaderFilterStrategy(getHeaderFilterStrategy());
}
+
+ answer.setNettySharedHttpServer(shared);
return answer;
}
http://git-wip-us.apache.org/repos/asf/camel/blob/c90f9244/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConsumer.java
----------------------------------------------------------------------
diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConsumer.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConsumer.java
index e4f9b40..2c27204 100644
--- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConsumer.java
+++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConsumer.java
@@ -26,8 +26,6 @@ import org.apache.camel.util.ObjectHelper;
*/
public class NettyHttpConsumer extends NettyConsumer {
- private SharedNettyHttpServer sharedNettyHttpServer;
-
public NettyHttpConsumer(NettyHttpEndpoint nettyEndpoint, Processor processor, NettyConfiguration configuration) {
super(nettyEndpoint, processor, configuration);
}
@@ -42,14 +40,6 @@ public class NettyHttpConsumer extends NettyConsumer {
return (NettyHttpConfiguration) super.getConfiguration();
}
- public SharedNettyHttpServer getSharedNettyHttpServer() {
- return sharedNettyHttpServer;
- }
-
- public void setSharedNettyHttpServer(SharedNettyHttpServer sharedNettyHttpServer) {
- this.sharedNettyHttpServer = sharedNettyHttpServer;
- }
-
@Override
protected void doStart() throws Exception {
super.doStart();
http://git-wip-us.apache.org/repos/asf/camel/blob/c90f9244/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpEndpoint.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpEndpoint.java
index aac08bd..18565b4 100644
--- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpEndpoint.java
+++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpEndpoint.java
@@ -45,7 +45,7 @@ public class NettyHttpEndpoint extends NettyEndpoint implements HeaderFilterStra
private HeaderFilterStrategy headerFilterStrategy;
private boolean traceEnabled;
private String httpMethodRestrict;
- private SharedNettyHttpServer sharedNettyHttpServer;
+ private NettySharedHttpServer nettySharedHttpServer;
public NettyHttpEndpoint(String endpointUri, NettyHttpComponent component, NettyConfiguration configuration) {
super(endpointUri, component, configuration);
@@ -61,10 +61,9 @@ public class NettyHttpEndpoint extends NettyEndpoint implements HeaderFilterStra
NettyHttpConsumer answer = new NettyHttpConsumer(this, processor, getConfiguration());
configureConsumer(answer);
- if (sharedNettyHttpServer != null) {
- answer.setSharedNettyHttpServer(sharedNettyHttpServer);
- answer.setNettyServerBootstrapFactory(sharedNettyHttpServer.getServerBootstrapFactory());
- LOG.debug("Created NettyHttpConsumer: {} using SharedNettyHttpServer: {}", answer, sharedNettyHttpServer);
+ if (nettySharedHttpServer != null) {
+ answer.setNettyServerBootstrapFactory(nettySharedHttpServer.getServerBootstrapFactory());
+ LOG.debug("Created NettyHttpConsumer: {} using NettySharedHttpServer: {}", answer, nettySharedHttpServer);
} else {
// reuse pipeline factory for the same address
HttpServerBootstrapFactory factory = getComponent().getOrCreateHttpNettyServerBootstrapFactory(answer);
@@ -160,12 +159,12 @@ public class NettyHttpEndpoint extends NettyEndpoint implements HeaderFilterStra
this.uriParameters = uriParameters;
}
- public SharedNettyHttpServer getSharedNettyHttpServer() {
- return sharedNettyHttpServer;
+ public NettySharedHttpServer getNettySharedHttpServer() {
+ return nettySharedHttpServer;
}
- public void setSharedNettyHttpServer(SharedNettyHttpServer sharedNettyHttpServer) {
- this.sharedNettyHttpServer = sharedNettyHttpServer;
+ public void setNettySharedHttpServer(NettySharedHttpServer nettySharedHttpServer) {
+ this.nettySharedHttpServer = nettySharedHttpServer;
}
@Override
http://git-wip-us.apache.org/repos/asf/camel/blob/c90f9244/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettySharedHttpServer.java
----------------------------------------------------------------------
diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettySharedHttpServer.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettySharedHttpServer.java
new file mode 100644
index 0000000..8e41d40
--- /dev/null
+++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettySharedHttpServer.java
@@ -0,0 +1,59 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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 org.apache.camel.component.netty.http;
+
+import org.apache.camel.Service;
+import org.apache.camel.component.netty.NettyServerBootstrapConfiguration;
+import org.apache.camel.component.netty.NettyServerBootstrapFactory;
+import org.apache.camel.spi.ClassResolver;
+
+/**
+ * A single interface to easily configure and setup a shared Netty HTTP server
+ * to be re-used among other Camel applications.
+ * <p/>
+ * To use this, just define a {@link NettyServerBootstrapConfiguration} configuration, and
+ * set this using {@link #setNettyServerBootstrapConfiguration(org.apache.camel.component.netty.NettyServerBootstrapConfiguration)}.
+ * Then call the {@link #start()} to initialize this shared server.
+ */
+public interface NettySharedHttpServer extends Service {
+
+ /**
+ * Sets the bootstrap configuration to use by this shared Netty HTTP server.
+ */
+ void setNettyServerBootstrapConfiguration(NettyServerBootstrapConfiguration configuration);
+
+ /**
+ * To use a custom {@link ClassResolver} for loading resource on the classpath.
+ */
+ void setClassResolver(ClassResolver classResolver);
+
+ /**
+ * Gets the port number this Netty HTTP server uses.
+ */
+ int getPort();
+
+ /**
+ * Gets the {@link HttpServerConsumerChannelFactory} to use.
+ */
+ HttpServerConsumerChannelFactory getConsumerChannelFactory();
+
+ /**
+ * Gets the {@link NettyServerBootstrapFactory} to use.
+ */
+ NettyServerBootstrapFactory getServerBootstrapFactory();
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/c90f9244/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/SharedNettyHttpServer.java
----------------------------------------------------------------------
diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/SharedNettyHttpServer.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/SharedNettyHttpServer.java
deleted file mode 100644
index 66244bb..0000000
--- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/SharedNettyHttpServer.java
+++ /dev/null
@@ -1,53 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF 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 org.apache.camel.component.netty.http;
-
-import org.apache.camel.Service;
-import org.apache.camel.component.netty.NettyServerBootstrapConfiguration;
-import org.apache.camel.component.netty.NettyServerBootstrapFactory;
-
-/**
- * A single interface to easily configure and setup a shared Netty HTTP server
- * to be re-used among other Camel applications.
- * <p/>
- * To use this, just define a {@link NettyServerBootstrapConfiguration} configuration, and
- * set this using {@link #setNettyServerBootstrapConfiguration(org.apache.camel.component.netty.NettyServerBootstrapConfiguration)}.
- * Then call the {@link #start()} to initialize this shared server.
- */
-public interface SharedNettyHttpServer extends Service {
-
- /**
- * Sets the bootstrap configuration to use by this shared Netty HTTP server.
- */
- void setNettyServerBootstrapConfiguration(NettyServerBootstrapConfiguration configuration);
-
- /**
- * Gets the port number this Netty HTTP server uses.
- */
- int getPort();
-
- /**
- * Gets the {@link HttpServerConsumerChannelFactory} to use.
- */
- HttpServerConsumerChannelFactory getConsumerChannelFactory();
-
- /**
- * Gets the {@link NettyServerBootstrapFactory} to use.
- */
- NettyServerBootstrapFactory getServerBootstrapFactory();
-
-}
http://git-wip-us.apache.org/repos/asf/camel/blob/c90f9244/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettySharedHttpServerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettySharedHttpServerTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettySharedHttpServerTest.java
index 67bbff4..ca887ca 100644
--- a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettySharedHttpServerTest.java
+++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettySharedHttpServerTest.java
@@ -23,29 +23,29 @@ import org.junit.Test;
public class NettySharedHttpServerTest extends BaseNettyTest {
- private SharedNettyHttpServer sharedNettyHttpServer;
+ private NettySharedHttpServer nettySharedHttpServer;
@Override
protected JndiRegistry createRegistry() throws Exception {
- sharedNettyHttpServer = new DefaultSharedNettyHttpServer();
+ nettySharedHttpServer = new DefaultNettySharedHttpServer();
NettyServerBootstrapConfiguration configuration = new NettyServerBootstrapConfiguration();
configuration.setPort(getPort());
configuration.setHost("localhost");
configuration.setBacklog(20);
configuration.setKeepAlive(true);
- sharedNettyHttpServer.setNettyServerBootstrapConfiguration(configuration);
+ nettySharedHttpServer.setNettyServerBootstrapConfiguration(configuration);
- sharedNettyHttpServer.start();
+ nettySharedHttpServer.start();
JndiRegistry jndi = super.createRegistry();
- jndi.bind("myNettyServer", sharedNettyHttpServer);
+ jndi.bind("myNettyServer", nettySharedHttpServer);
return jndi;
}
@Override
public void tearDown() throws Exception {
- sharedNettyHttpServer.stop();
+ nettySharedHttpServer.stop();
super.tearDown();
}
@@ -68,11 +68,13 @@ public class NettySharedHttpServerTest extends BaseNettyTest {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
- from("netty-http:http://0.0.0.0:{{port}}/foo?sharedNettyHttpServer=#myNettyServer")
+ // we are using a shared netty http server, so the port number is not needed to be defined in the uri
+ from("netty-http:http://localhost/foo?nettySharedHttpServer=#myNettyServer")
.to("mock:foo")
.transform().constant("Bye World");
- from("netty-http:http://0.0.0.0:{{port}}/bar?sharedNettyHttpServer=#myNettyServer")
+ // we are using a shared netty http server, so the port number is not needed to be defined in the uri
+ from("netty-http:http://localhost/bar?nettySharedHttpServer=#myNettyServer")
.to("mock:bar")
.transform().constant("Bye Camel");
}