From ba2447e39ad513ff1a37833887cfd2e9bf49a0b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Baptiste=20Onofr=C3=A9?= Date: Thu, 11 Oct 2018 09:18:32 +0200 Subject: [PATCH] [AMQ-7063] Be able to configure ActiveMQ http transport connector by providing a jetty.xml --- .../transport/WebTransportServerSupport.java | 67 ++++++++++++++++--- .../transport/http/HttpTransportFactory.java | 2 + .../http/HttpJettyConfigurationTest.java | 49 ++++++++++++++ activemq-http/src/test/resources/jetty.xml | 22 ++++++ 4 files changed, 132 insertions(+), 8 deletions(-) create mode 100644 activemq-http/src/test/java/org/apache/activemq/transport/http/HttpJettyConfigurationTest.java create mode 100644 activemq-http/src/test/resources/jetty.xml diff --git a/activemq-http/src/main/java/org/apache/activemq/transport/WebTransportServerSupport.java b/activemq-http/src/main/java/org/apache/activemq/transport/WebTransportServerSupport.java index 7f2ce7e896..eba63abeb4 100644 --- a/activemq-http/src/main/java/org/apache/activemq/transport/WebTransportServerSupport.java +++ b/activemq-http/src/main/java/org/apache/activemq/transport/WebTransportServerSupport.java @@ -16,6 +16,7 @@ */ package org.apache.activemq.transport; +import java.io.File; import java.net.InetAddress; import java.net.URI; import java.util.Map; @@ -27,15 +28,21 @@ import org.eclipse.jetty.security.ConstraintSecurityHandler; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.util.security.Constraint; +import org.eclipse.jetty.xml.XmlConfiguration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; abstract public class WebTransportServerSupport extends TransportServerSupport { + private final static Logger LOG = LoggerFactory.getLogger(WebTransportServerSupport.class); + protected URI bindAddress; protected Server server; protected Connector connector; protected SocketConnectorFactory socketConnectorFactory; protected String host; protected final HttpOptions httpOptions = new HttpOptions(); + protected final JettyOptions jettyOptions = new JettyOptions(); public WebTransportServerSupport(URI location) { super(location); @@ -46,7 +53,22 @@ abstract public class WebTransportServerSupport extends TransportServerSupport { } protected void createServer() { - server = new Server(); + LOG.info("Starting Jetty server"); + if (jettyOptions.getConfig() != null) { + try { + LOG.info("Configuring Jetty server using {}", jettyOptions.getConfig()); + File file = new File(jettyOptions.getConfig()); + if (!file.exists()) { + throw new IllegalArgumentException("Jetty XML not found: " + file.getAbsolutePath()); + } + XmlConfiguration xmlConfiguration = new XmlConfiguration(file.toURI().toURL()); + server = (Server) xmlConfiguration.configure(); + } catch (Throwable t) { + throw new IllegalStateException("Jetty configuration can't be loaded", t); + } + } else { + server = new Server(); + } try { server.getClass().getMethod("setStopTimeout", Long.TYPE).invoke(server, 500l); } catch (Throwable t) { @@ -55,21 +77,31 @@ abstract public class WebTransportServerSupport extends TransportServerSupport { } public URI bind() throws Exception { - URI bind = getBindLocation(); - String bindHost = bind.getHost(); bindHost = (bindHost == null || bindHost.length() == 0) ? "localhost" : bindHost; InetAddress addr = InetAddress.getByName(bindHost); host = addr.getCanonicalHostName(); - - setConnectorProperty("Host", String.class, host); - setConnectorProperty("Port", Integer.TYPE, bindAddress.getPort()); - server.addConnector(connector); + if (server.getConnectors().length == 0) { + LOG.info("Creating Jetty connector"); + setConnectorProperty("Host", String.class, host); + setConnectorProperty("Port", Integer.TYPE, bindAddress.getPort()); + server.addConnector(connector); + } else { + LOG.info("Using Jetty configured connector"); + connector = server.getConnectors()[0]; + for (Connector c : server.getConnectors()) { + if (c.getName() != null && c.getName().equalsIgnoreCase("activemq")) { + connector = c; + } + } + setConnectorProperty("Host", String.class, host); + setConnectorProperty("Port", Integer.TYPE, bindAddress.getPort()); + server.addConnector(connector); + } if (addr.isAnyLocalAddress()) { host = InetAddressUtil.getLocalHostName(); } - URI boundUri = new URI(bind.getScheme(), bind.getUserInfo(), host, bindAddress.getPort(), bind.getPath(), bind.getQuery(), bind.getFragment()); setConnectURI(boundUri); return boundUri; @@ -94,6 +126,12 @@ abstract public class WebTransportServerSupport extends TransportServerSupport { } } + public void setJettyOptions(Map options) { + if (options != null) { + IntrospectionSupport.setProperties(this.jettyOptions, options); + } + } + protected static class HttpOptions { private boolean enableTrace = false; @@ -105,4 +143,17 @@ abstract public class WebTransportServerSupport extends TransportServerSupport { this.enableTrace = enableTrace; } } + + protected static class JettyOptions { + private String config; + + public String getConfig() { + return config; + } + + public void setConfig(String config) { + this.config = config; + } + } + } diff --git a/activemq-http/src/main/java/org/apache/activemq/transport/http/HttpTransportFactory.java b/activemq-http/src/main/java/org/apache/activemq/transport/http/HttpTransportFactory.java index 8332a9514b..02ecf77180 100644 --- a/activemq-http/src/main/java/org/apache/activemq/transport/http/HttpTransportFactory.java +++ b/activemq-http/src/main/java/org/apache/activemq/transport/http/HttpTransportFactory.java @@ -45,8 +45,10 @@ public class HttpTransportFactory extends TransportFactory { try { Map options = new HashMap(URISupport.parseParameters(location)); HttpTransportServer result = new HttpTransportServer(location, this); + Map jettyOptions = IntrospectionSupport.extractProperties(options, "jetty."); Map httpOptions = IntrospectionSupport.extractProperties(options, "http."); Map transportOptions = IntrospectionSupport.extractProperties(options, "transport."); + result.setJettyOptions(jettyOptions); result.setTransportOption(transportOptions); result.setHttpOptions(httpOptions); return result; diff --git a/activemq-http/src/test/java/org/apache/activemq/transport/http/HttpJettyConfigurationTest.java b/activemq-http/src/test/java/org/apache/activemq/transport/http/HttpJettyConfigurationTest.java new file mode 100644 index 0000000000..363dd4e841 --- /dev/null +++ b/activemq-http/src/test/java/org/apache/activemq/transport/http/HttpJettyConfigurationTest.java @@ -0,0 +1,49 @@ +/** + * 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.activemq.transport.http; + +import org.apache.activemq.broker.BrokerService; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class HttpJettyConfigurationTest { + + private BrokerService brokerService; + + @Before + public void setUp() throws Exception { + brokerService = new BrokerService(); + brokerService.setPersistent(false); + brokerService.setUseJmx(false); + brokerService.deleteAllMessages(); + brokerService.addConnector("http://0.0.0.0:0?jetty.config=src/test/resources/jetty.xml"); + brokerService.start(); + } + + @After + public void tearDown() throws Exception { + brokerService.stop(); + } + + @Test + public void test() throws Exception { + // nothing to do + } + +} diff --git a/activemq-http/src/test/resources/jetty.xml b/activemq-http/src/test/resources/jetty.xml new file mode 100644 index 0000000000..e6764fd93f --- /dev/null +++ b/activemq-http/src/test/resources/jetty.xml @@ -0,0 +1,22 @@ + + + + + + + 10 + 1000 + + + + + + + + + + + + + + \ No newline at end of file