001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.geronimo.activemq;
019
020 import java.net.InetSocketAddress;
021 import java.net.URI;
022 import java.net.URISyntaxException;
023
024 import org.apache.commons.logging.Log;
025 import org.apache.commons.logging.LogFactory;
026
027 import org.apache.activemq.broker.TransportConnector;
028 import org.apache.geronimo.activemq.ActiveMQConnector;
029 import org.apache.geronimo.gbean.GBeanInfo;
030 import org.apache.geronimo.gbean.GBeanInfoBuilder;
031 import org.apache.geronimo.gbean.GBeanLifecycle;
032 import org.apache.geronimo.gbean.GConstructorInfo;
033
034 /**
035 * Default implementation of the ActiveMQ connector
036 *
037 * @version $Rev: 718350 $ $Date: 2008-11-18 03:50:04 +0800 (Tue, 18 Nov 2008) $
038 */
039 public class TransportConnectorGBeanImpl implements GBeanLifecycle, ActiveMQConnector {
040 private Log log = LogFactory.getLog(getClass().getName());
041
042 private TransportConnector transportConnector;
043 private BrokerServiceGBean brokerServiceGBean;
044
045 private String protocol;
046 private String host;
047 private int port;
048 private String path;
049 private String query;
050 private String urlAsStarted;
051 private ClassLoader classLoader;
052
053 public TransportConnectorGBeanImpl(BrokerServiceGBean brokerServiceGBean, String protocol, String host, int port) {
054 this.brokerServiceGBean = brokerServiceGBean;
055 this.protocol = protocol;
056 //force localhost to 127.0.0.1 to avoid activemq amplify localhost to 0.0.0.0
057 if (host.equalsIgnoreCase("localhost")) {
058 this.host = "127.0.0.1";
059 } else {
060 this.host = host;
061 }
062 this.port = port;
063 }
064
065 public String getProtocol() {
066 return protocol;
067 }
068
069 public void setProtocol(String protocol) {
070 this.protocol = protocol;
071 }
072
073 public String getHost() {
074 return host;
075 }
076
077 public void setHost(String host) {
078 this.host = host;
079 }
080
081 public int getPort() {
082 return port;
083 }
084
085 public void setPort(int port) {
086 this.port = port;
087 }
088
089 public String getPath() {
090 return path;
091 }
092
093 public void setPath(String path) {
094 this.path = path;
095 }
096
097 public String getQuery() {
098 return query;
099 }
100
101 public void setQuery(String query) {
102 this.query = query;
103 }
104
105 public String getUrl() {
106 try {
107 return new URI(protocol, null, host, port, path, query, null).toString();
108 } catch (URISyntaxException e) {
109 throw new IllegalStateException("Attributes don't form a valid URI: "+protocol+"://"+host+":"+port+"/"+path+"?"+query, e);
110 }
111 }
112
113 public InetSocketAddress getListenAddress() {
114 try {
115 return transportConnector.getServer().getSocketAddress();
116 } catch (Throwable e) {
117 log.debug("Failure to determine ListenAddress: "+e,e);
118 return null;
119 }
120 }
121
122 public synchronized void doStart() throws Exception {
123 ClassLoader old = Thread.currentThread().getContextClassLoader();
124 Thread.currentThread().setContextClassLoader(getClassLoader());
125 try {
126 if (transportConnector == null) {
127 urlAsStarted = getUrl();
128 transportConnector = createBrokerConnector(urlAsStarted);
129 transportConnector.start();
130 }
131 } finally {
132 Thread.currentThread().setContextClassLoader(old);
133 }
134 }
135
136 public synchronized void doStop() throws Exception {
137 if (transportConnector != null) {
138 TransportConnector temp = transportConnector;
139 transportConnector = null;
140 temp.stop();
141 }
142 }
143
144 public synchronized void doFail() {
145 if (transportConnector != null) {
146 TransportConnector temp = transportConnector;
147 transportConnector = null;
148 try {
149 temp.stop();
150 }
151 catch (Exception e) {
152 log.info("Caught while closing due to failure: " + e, e);
153 }
154 }
155 }
156
157 protected TransportConnector createBrokerConnector(String url) throws Exception {
158 return brokerServiceGBean.getBrokerContainer().addConnector(url);
159 }
160
161 public ClassLoader getClassLoader() {
162 if( classLoader == null ) {
163 classLoader = this.getClass().getClassLoader();
164 }
165 return classLoader;
166 }
167
168 public void setClassLoader(ClassLoader classLoader) {
169 this.classLoader = classLoader;
170 }
171
172 public static final GBeanInfo GBEAN_INFO;
173
174 static {
175 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic("ActiveMQ Transport Connector", TransportConnectorGBeanImpl.class, CONNECTOR_J2EE_TYPE);
176 infoBuilder.addAttribute("classLoader", ClassLoader.class, false);
177 infoBuilder.addAttribute("url", String.class.getName(), false);
178 infoBuilder.addReference("brokerService", BrokerServiceGBean.class);
179 infoBuilder.addInterface(ActiveMQConnector.class, new String[]{"host","port","protocol","path","query"},
180 new String[]{"host","port"});
181 infoBuilder.setConstructor(new GConstructorInfo(new String[]{"brokerService", "protocol", "host", "port"}));
182 GBEAN_INFO = infoBuilder.getBeanInfo();
183 }
184
185 public static GBeanInfo getGBeanInfo() {
186 return GBEAN_INFO;
187 }
188 }