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: */package org.apache.geronimo.activemq;
017:
018: import java.net.InetSocketAddress;
019: import java.net.URI;
020: import java.net.URISyntaxException;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024:
025: import org.apache.activemq.broker.TransportConnector;
026: import org.apache.geronimo.activemq.ActiveMQConnector;
027: import org.apache.geronimo.gbean.GBeanInfo;
028: import org.apache.geronimo.gbean.GBeanInfoBuilder;
029: import org.apache.geronimo.gbean.GBeanLifecycle;
030: import org.apache.geronimo.gbean.GConstructorInfo;
031:
032: /**
033: * Default implementation of the ActiveMQ connector
034: *
035: * @version $Rev: 547376 $ $Date: 2007-06-14 12:38:13 -0700 (Thu, 14 Jun 2007) $
036: */
037: public class TransportConnectorGBeanImpl implements GBeanLifecycle,
038: ActiveMQConnector {
039: private Log log = LogFactory.getLog(getClass().getName());
040:
041: private TransportConnector transportConnector;
042: private BrokerServiceGBean brokerServiceGBean;
043:
044: private String protocol;
045: private String host;
046: private int port;
047: private String path;
048: private String query;
049: private String urlAsStarted;
050: private ClassLoader classLoader;
051:
052: public TransportConnectorGBeanImpl(
053: BrokerServiceGBean brokerServiceGBean, String protocol,
054: String host, int port) {
055: this .brokerServiceGBean = brokerServiceGBean;
056: this .protocol = protocol;
057: this .host = host;
058: this .port = port;
059: }
060:
061: public String getProtocol() {
062: return protocol;
063: }
064:
065: public void setProtocol(String protocol) {
066: this .protocol = protocol;
067: }
068:
069: public String getHost() {
070: return host;
071: }
072:
073: public void setHost(String host) {
074: this .host = host;
075: }
076:
077: public int getPort() {
078: return port;
079: }
080:
081: public void setPort(int port) {
082: this .port = port;
083: }
084:
085: public String getPath() {
086: return path;
087: }
088:
089: public void setPath(String path) {
090: this .path = path;
091: }
092:
093: public String getQuery() {
094: return query;
095: }
096:
097: public void setQuery(String query) {
098: this .query = query;
099: }
100:
101: public String getUrl() {
102: try {
103: return new URI(protocol, null, host, port, path, query,
104: null).toString();
105: } catch (URISyntaxException e) {
106: throw new IllegalStateException(
107: "Attributes don't form a valid URI: " + protocol
108: + "://" + host + ":" + port + "/" + path
109: + "?" + 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()
124: .getContextClassLoader();
125: Thread.currentThread().setContextClassLoader(getClassLoader());
126: try {
127: if (transportConnector == null) {
128: urlAsStarted = getUrl();
129: transportConnector = createBrokerConnector(urlAsStarted);
130: transportConnector.start();
131: }
132: } finally {
133: Thread.currentThread().setContextClassLoader(old);
134: }
135: }
136:
137: public synchronized void doStop() throws Exception {
138: if (transportConnector != null) {
139: TransportConnector temp = transportConnector;
140: transportConnector = null;
141: temp.stop();
142: }
143: }
144:
145: public synchronized void doFail() {
146: if (transportConnector != null) {
147: TransportConnector temp = transportConnector;
148: transportConnector = null;
149: try {
150: temp.stop();
151: } catch (Exception e) {
152: log
153: .info("Caught while closing due to failure: "
154: + e, e);
155: }
156: }
157: }
158:
159: protected TransportConnector createBrokerConnector(String url)
160: throws Exception {
161: return brokerServiceGBean.getBrokerContainer()
162: .addConnector(url);
163: }
164:
165: public ClassLoader getClassLoader() {
166: if (classLoader == null) {
167: classLoader = this .getClass().getClassLoader();
168: }
169: return classLoader;
170: }
171:
172: public void setClassLoader(ClassLoader classLoader) {
173: this .classLoader = classLoader;
174: }
175:
176: public static final GBeanInfo GBEAN_INFO;
177:
178: static {
179: GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(
180: "ActiveMQ Transport Connector",
181: TransportConnectorGBeanImpl.class, CONNECTOR_J2EE_TYPE);
182: infoBuilder.addAttribute("classLoader", ClassLoader.class,
183: false);
184: infoBuilder.addAttribute("url", String.class.getName(), false);
185: infoBuilder.addReference("brokerService",
186: BrokerServiceGBean.class);
187: infoBuilder.addInterface(ActiveMQConnector.class, new String[] {
188: "host", "port", "protocol", "path", "query" },
189: new String[] { "host", "port" });
190: infoBuilder.setConstructor(new GConstructorInfo(new String[] {
191: "brokerService", "protocol", "host", "port" }));
192: GBEAN_INFO = infoBuilder.getBeanInfo();
193: }
194:
195: public static GBeanInfo getGBeanInfo() {
196: return GBEAN_INFO;
197: }
198: }
|