From cb895f01b79a6e104435db68729a7ad7ace99434 Mon Sep 17 00:00:00 2001 From: Bosanac Dejan Date: Mon, 9 Jul 2012 14:43:30 +0000 Subject: [PATCH] https://issues.apache.org/jira/browse/AMQ-3918 - transport connector urls exposed using JMX git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1359194 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/activemq/broker/BrokerService.java | 4 +- .../activemq/broker/jmx/BrokerView.java | 35 ++++++++++++ .../activemq/broker/jmx/BrokerViewMBean.java | 32 +++++++++-- .../transport/TransportServerSupport.java | 2 + .../apache/activemq/broker/jmx/MBeanTest.java | 3 +- .../transport/WebTransportServerSupport.java | 56 +++++++++++++++++++ .../transport/http/HttpTransportServer.java | 16 +++--- .../transport/ws/WSTransportServer.java | 20 +++---- 8 files changed, 142 insertions(+), 26 deletions(-) create mode 100644 activemq-optional/src/main/java/org/apache/activemq/transport/WebTransportServerSupport.java diff --git a/activemq-core/src/main/java/org/apache/activemq/broker/BrokerService.java b/activemq-core/src/main/java/org/apache/activemq/broker/BrokerService.java index b9be63f2f5..cb0e960d38 100644 --- a/activemq-core/src/main/java/org/apache/activemq/broker/BrokerService.java +++ b/activemq-core/src/main/java/org/apache/activemq/broker/BrokerService.java @@ -2328,11 +2328,11 @@ public class BrokerService implements Service { if (policy != null) { connector.setMessageAuthorizationPolicy(policy); } + connector.getStatistics().setEnabled(enableStatistics); + connector.start(); if (isUseJmx()) { connector = registerConnectorMBean(connector); } - connector.getStatistics().setEnabled(enableStatistics); - connector.start(); return connector; } diff --git a/activemq-core/src/main/java/org/apache/activemq/broker/jmx/BrokerView.java b/activemq-core/src/main/java/org/apache/activemq/broker/jmx/BrokerView.java index b91943eeaf..7f21ca5f39 100755 --- a/activemq-core/src/main/java/org/apache/activemq/broker/jmx/BrokerView.java +++ b/activemq-core/src/main/java/org/apache/activemq/broker/jmx/BrokerView.java @@ -23,6 +23,8 @@ import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; +import java.util.HashMap; +import java.util.Map; import java.util.NoSuchElementException; import java.util.concurrent.atomic.AtomicInteger; import javax.management.ObjectName; @@ -364,21 +366,54 @@ public class BrokerView implements BrokerViewMBean { } } + public Map getTransportConnectors() { + Map answer = new HashMap(); + try { + for (TransportConnector connector : brokerService.getTransportConnectors()) { + answer.put(connector.getName(), connector.getConnectUri().toString()); + } + } catch (Exception e) { + LOG.debug("Failed to read URI to build transport connectors map", e); + } + return answer; + } + + @Override + public String getTransportConnectorByType(String type) { + return brokerService.getTransportConnectorURIsAsMap().get(type); + } + + @Deprecated + /** + * @deprecated use {@link #getTransportConnectors()} or {@link #getTransportConnectorByType(String)} + */ public String getOpenWireURL() { String answer = brokerService.getTransportConnectorURIsAsMap().get("tcp"); return answer != null ? answer : ""; } + @Deprecated + /** + * @deprecated use {@link #getTransportConnectors()} or {@link #getTransportConnectorByType(String)} + */ public String getStompURL() { String answer = brokerService.getTransportConnectorURIsAsMap().get("stomp"); return answer != null ? answer : ""; } + @Deprecated + /** + * @deprecated use {@link #getTransportConnectors()} or {@link #getTransportConnectorByType(String)} + */ public String getSslURL() { String answer = brokerService.getTransportConnectorURIsAsMap().get("ssl"); return answer != null ? answer : ""; } + @Deprecated + /** + * @deprecated use {@link #getTransportConnectors()} or {@link #getTransportConnectorByType(String)} + */ public String getStompSslURL() { String answer = brokerService.getTransportConnectorURIsAsMap().get("stomp+ssl"); return answer != null ? answer : ""; diff --git a/activemq-core/src/main/java/org/apache/activemq/broker/jmx/BrokerViewMBean.java b/activemq-core/src/main/java/org/apache/activemq/broker/jmx/BrokerViewMBean.java index ef69f1ffca..b25a50fe7c 100755 --- a/activemq-core/src/main/java/org/apache/activemq/broker/jmx/BrokerViewMBean.java +++ b/activemq-core/src/main/java/org/apache/activemq/broker/jmx/BrokerViewMBean.java @@ -19,6 +19,8 @@ package org.apache.activemq.broker.jmx; import javax.management.ObjectName; import org.apache.activemq.Service; +import java.util.Map; + /** * @author David Martin Clavo david(dot)martin(dot)clavo(at)gmail.com (for the reloadLog4jProperties method) @@ -249,21 +251,43 @@ public interface BrokerViewMBean extends Service { @MBeanInfo(value="Reloads log4j.properties from the classpath.") public void reloadLog4jProperties() throws Throwable; - @MBeanInfo("The url of the openwire connector") + /** + * @deprecated use {@link #getTransportConnectors()} or {@link #getTransportConnectorByType(String)} + */ + @Deprecated + @MBeanInfo("The url of the openwire connector - deprecated, use getTransportConnectors or getTransportConnectorByType instead") String getOpenWireURL(); - @MBeanInfo("The url of the stomp connector") + /** + * @deprecated use {@link #getTransportConnectors()} or {@link #getTransportConnectorByType(String)} + */ + @Deprecated + @MBeanInfo("The url of the stomp connector - deprecated, use getTransportConnectors or getTransportConnectorByType instead") String getStompURL(); - @MBeanInfo("The url of the SSL connector") + /** + * @deprecated use {@link #getTransportConnectors()} or {@link #getTransportConnectorByType(String)} + */ + @Deprecated + @MBeanInfo("The url of the SSL connector - deprecated, use getTransportConnectors or getTransportConnectorByType instead") String getSslURL(); - @MBeanInfo("The url of the Stomp SSL connector") + /** + * @deprecated use {@link #getTransportConnectors()} or {@link #getTransportConnectorByType(String)} + */ + @Deprecated + @MBeanInfo("The url of the Stomp SSL connector - deprecated, use getTransportConnectors or getTransportConnectorByType instead") String getStompSslURL(); @MBeanInfo("The url of the VM connector") String getVMURL(); + @MBeanInfo("The map of all defined transport connectors, with transport name as a key") + Map getTransportConnectors(); + + @MBeanInfo("The url of transport connector by it's type; e.g. tcp, stomp, ssl, etc.") + String getTransportConnectorByType(String type); + @MBeanInfo("The location of the data directory") public String getDataDirectory(); diff --git a/activemq-core/src/main/java/org/apache/activemq/transport/TransportServerSupport.java b/activemq-core/src/main/java/org/apache/activemq/transport/TransportServerSupport.java index 4fb61a4ccf..fba80634bc 100644 --- a/activemq-core/src/main/java/org/apache/activemq/transport/TransportServerSupport.java +++ b/activemq-core/src/main/java/org/apache/activemq/transport/TransportServerSupport.java @@ -88,4 +88,6 @@ public abstract class TransportServerSupport extends ServiceSupport implements T public void setTransportOption(Map transportOptions) { this.transportOptions = transportOptions; } + + } diff --git a/activemq-core/src/test/java/org/apache/activemq/broker/jmx/MBeanTest.java b/activemq-core/src/test/java/org/apache/activemq/broker/jmx/MBeanTest.java index 348a20a4e4..2bd27323bd 100644 --- a/activemq-core/src/test/java/org/apache/activemq/broker/jmx/MBeanTest.java +++ b/activemq-core/src/test/java/org/apache/activemq/broker/jmx/MBeanTest.java @@ -90,8 +90,9 @@ public class MBeanTest extends EmbeddedBrokerTestSupport { public void testConnectors() throws Exception{ ObjectName brokerName = assertRegisteredObjectName(domain + ":Type=Broker,BrokerName=localhost"); BrokerViewMBean broker = (BrokerViewMBean)MBeanServerInvocationHandler.newProxyInstance(mbeanServer, brokerName, BrokerViewMBean.class, true); + assertEquals("openwire URL port doesn't equal bind Address", - new URI(broker.getOpenWireURL()).getPort(), + new URI(broker.getTransportConnectorByType("tcp")).getPort(), new URI(this.broker.getTransportConnectors().get(0).getPublishableConnectString()).getPort()); } diff --git a/activemq-optional/src/main/java/org/apache/activemq/transport/WebTransportServerSupport.java b/activemq-optional/src/main/java/org/apache/activemq/transport/WebTransportServerSupport.java new file mode 100644 index 0000000000..2e9c128da5 --- /dev/null +++ b/activemq-optional/src/main/java/org/apache/activemq/transport/WebTransportServerSupport.java @@ -0,0 +1,56 @@ +/** + * 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; + +import org.apache.activemq.util.InetAddressUtil; +import org.eclipse.jetty.server.Connector; +import org.eclipse.jetty.server.Server; + +import java.net.InetAddress; +import java.net.URI; + +abstract public class WebTransportServerSupport extends TransportServerSupport { + + protected URI bindAddress; + protected Server server; + protected Connector connector; + protected SocketConnectorFactory socketConnectorFactory; + protected String host; + + public WebTransportServerSupport(URI location) { + super(location); + } + + public void 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(); + if (addr.isAnyLocalAddress()) { + host = InetAddressUtil.getLocalHostName(); + } + + connector.setHost(host); + connector.setPort(bindAddress.getPort()); + connector.setServer(server); + server.addConnector(connector); + + } +} diff --git a/activemq-optional/src/main/java/org/apache/activemq/transport/http/HttpTransportServer.java b/activemq-optional/src/main/java/org/apache/activemq/transport/http/HttpTransportServer.java index 2a4ad088fb..6fcd0badbe 100755 --- a/activemq-optional/src/main/java/org/apache/activemq/transport/http/HttpTransportServer.java +++ b/activemq-optional/src/main/java/org/apache/activemq/transport/http/HttpTransportServer.java @@ -19,6 +19,7 @@ package org.apache.activemq.transport.http; import org.apache.activemq.command.BrokerInfo; import org.apache.activemq.transport.SocketConnectorFactory; import org.apache.activemq.transport.TransportServerSupport; +import org.apache.activemq.transport.WebTransportServerSupport; import org.apache.activemq.transport.util.TextWireFormat; import org.apache.activemq.transport.xstream.XStreamWireFormat; import org.apache.activemq.util.ServiceStopper; @@ -28,18 +29,15 @@ import org.eclipse.jetty.server.handler.GzipHandler; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.URI; import java.util.Map; -public class HttpTransportServer extends TransportServerSupport { +public class HttpTransportServer extends WebTransportServerSupport { - private URI bindAddress; private TextWireFormat wireFormat; - private Server server; - private Connector connector; private HttpTransportFactory transportFactory; - protected SocketConnectorFactory socketConnectorFactory; public HttpTransportServer(URI uri, HttpTransportFactory factory) { super(uri); @@ -79,10 +77,9 @@ public class HttpTransportServer extends TransportServerSupport { if (connector == null) { connector = socketConnectorFactory.createConnector(); } - connector.setHost(bindAddress.getHost()); - connector.setPort(bindAddress.getPort()); - connector.setServer(server); - server.addConnector(connector); + + URI bind = getBindLocation(); + bind(); ServletContextHandler contextHandler = new ServletContextHandler(server, "/", ServletContextHandler.NO_SECURITY); @@ -100,6 +97,7 @@ public class HttpTransportServer extends TransportServerSupport { contextHandler.setHandler(gzipHandler); server.start(); + setConnectURI(new URI(bind.getScheme(), bind.getUserInfo(), host, connector.getLocalPort(), bind.getPath(), bind.getQuery(), bind.getFragment())); } protected void doStop(ServiceStopper stopper) throws Exception { diff --git a/activemq-optional/src/main/java/org/apache/activemq/transport/ws/WSTransportServer.java b/activemq-optional/src/main/java/org/apache/activemq/transport/ws/WSTransportServer.java index cbab9b1980..3a837f7dc1 100644 --- a/activemq-optional/src/main/java/org/apache/activemq/transport/ws/WSTransportServer.java +++ b/activemq-optional/src/main/java/org/apache/activemq/transport/ws/WSTransportServer.java @@ -20,6 +20,8 @@ package org.apache.activemq.transport.ws; import org.apache.activemq.command.BrokerInfo; import org.apache.activemq.transport.SocketConnectorFactory; import org.apache.activemq.transport.TransportServerSupport; +import org.apache.activemq.transport.WebTransportServerSupport; +import org.apache.activemq.util.InetAddressUtil; import org.apache.activemq.util.ServiceStopper; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Server; @@ -27,6 +29,7 @@ import org.eclipse.jetty.server.nio.SelectChannelConnector; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.URI; import java.util.Map; @@ -35,12 +38,7 @@ import java.util.Map; * Creates a web server and registers web socket server * */ -public class WSTransportServer extends TransportServerSupport { - - private URI bindAddress; - private Server server; - private Connector connector; - protected SocketConnectorFactory socketConnectorFactory; +public class WSTransportServer extends WebTransportServerSupport { public WSTransportServer(URI location) { super(location); @@ -50,13 +48,14 @@ public class WSTransportServer extends TransportServerSupport { protected void doStart() throws Exception { server = new Server(); + if (connector == null) { connector = socketConnectorFactory.createConnector(); } - connector.setHost(bindAddress.getHost()); - connector.setPort(bindAddress.getPort()); - connector.setServer(server); - server.addConnector(connector); + + URI bind = getBindLocation(); + + bind(); ServletContextHandler contextHandler = new ServletContextHandler(server, "/", ServletContextHandler.NO_SECURITY); @@ -68,6 +67,7 @@ public class WSTransportServer extends TransportServerSupport { contextHandler.setAttribute("acceptListener", getAcceptListener()); server.start(); + setConnectURI(new URI(bind.getScheme(), bind.getUserInfo(), host, connector.getLocalPort(), bind.getPath(), bind.getQuery(), bind.getFragment())); } protected void doStop(ServiceStopper stopper) throws Exception {