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: 547376 $ $Date: 2007-06-14 15:38:13 -0400 (Thu, 14 Jun 2007) $
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 this.host = host;
057 this.port = port;
058 }
059
060 public String getProtocol() {
061 return protocol;
062 }
063
064 public void setProtocol(String protocol) {
065 this.protocol = protocol;
066 }
067
068 public String getHost() {
069 return host;
070 }
071
072 public void setHost(String host) {
073 this.host = host;
074 }
075
076 public int getPort() {
077 return port;
078 }
079
080 public void setPort(int port) {
081 this.port = port;
082 }
083
084 public String getPath() {
085 return path;
086 }
087
088 public void setPath(String path) {
089 this.path = path;
090 }
091
092 public String getQuery() {
093 return query;
094 }
095
096 public void setQuery(String query) {
097 this.query = query;
098 }
099
100 public String getUrl() {
101 try {
102 return new URI(protocol, null, host, port, path, query, null).toString();
103 } catch (URISyntaxException e) {
104 throw new IllegalStateException("Attributes don't form a valid URI: "+protocol+"://"+host+":"+port+"/"+path+"?"+query, e);
105 }
106 }
107
108 public InetSocketAddress getListenAddress() {
109 try {
110 return transportConnector.getServer().getSocketAddress();
111 } catch (Throwable e) {
112 log.debug("Failure to determine ListenAddress: "+e,e);
113 return null;
114 }
115 }
116
117 public synchronized void doStart() throws Exception {
118 ClassLoader old = Thread.currentThread().getContextClassLoader();
119 Thread.currentThread().setContextClassLoader(getClassLoader());
120 try {
121 if (transportConnector == null) {
122 urlAsStarted = getUrl();
123 transportConnector = createBrokerConnector(urlAsStarted);
124 transportConnector.start();
125 }
126 } finally {
127 Thread.currentThread().setContextClassLoader(old);
128 }
129 }
130
131 public synchronized void doStop() throws Exception {
132 if (transportConnector != null) {
133 TransportConnector temp = transportConnector;
134 transportConnector = null;
135 temp.stop();
136 }
137 }
138
139 public synchronized void doFail() {
140 if (transportConnector != null) {
141 TransportConnector temp = transportConnector;
142 transportConnector = null;
143 try {
144 temp.stop();
145 }
146 catch (Exception e) {
147 log.info("Caught while closing due to failure: " + e, e);
148 }
149 }
150 }
151
152 protected TransportConnector createBrokerConnector(String url) throws Exception {
153 return brokerServiceGBean.getBrokerContainer().addConnector(url);
154 }
155
156 public ClassLoader getClassLoader() {
157 if( classLoader == null ) {
158 classLoader = this.getClass().getClassLoader();
159 }
160 return classLoader;
161 }
162
163 public void setClassLoader(ClassLoader classLoader) {
164 this.classLoader = classLoader;
165 }
166
167 public static final GBeanInfo GBEAN_INFO;
168
169 static {
170 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic("ActiveMQ Transport Connector", TransportConnectorGBeanImpl.class, CONNECTOR_J2EE_TYPE);
171 infoBuilder.addAttribute("classLoader", ClassLoader.class, false);
172 infoBuilder.addAttribute("url", String.class.getName(), false);
173 infoBuilder.addReference("brokerService", BrokerServiceGBean.class);
174 infoBuilder.addInterface(ActiveMQConnector.class, new String[]{"host","port","protocol","path","query"},
175 new String[]{"host","port"});
176 infoBuilder.setConstructor(new GConstructorInfo(new String[]{"brokerService", "protocol", "host", "port"}));
177 GBEAN_INFO = infoBuilder.getBeanInfo();
178 }
179
180 public static GBeanInfo getGBeanInfo() {
181 return GBEAN_INFO;
182 }
183 }