KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > gbean > TransportConnectorGBeanImpl


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.activemq.gbean;
18
19 import java.net.InetSocketAddress JavaDoc;
20 import java.net.URI JavaDoc;
21 import java.net.URISyntaxException JavaDoc;
22
23 import org.apache.activemq.broker.TransportConnector;
24 import org.apache.activemq.gbean.ActiveMQConnector;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.geronimo.gbean.GBeanInfo;
28 import org.apache.geronimo.gbean.GBeanInfoBuilder;
29 import org.apache.geronimo.gbean.GBeanLifecycle;
30 import org.apache.geronimo.gbean.GConstructorInfo;
31
32 /**
33  * Default implementation of the ActiveMQ connector
34  *
35  * @version $Revision: 478875 $ $Date: 2006-11-24 08:28:42 -0500 (Fri, 24 Nov 2006) $
36  */

37 public class TransportConnectorGBeanImpl implements GBeanLifecycle, ActiveMQConnector {
38     private Log log = LogFactory.getLog(getClass().getName());
39
40     private TransportConnector transportConnector;
41     private BrokerServiceGBean brokerServiceGBean;
42     
43     private String JavaDoc protocol;
44     private String JavaDoc host;
45     private int port;
46     private String JavaDoc path;
47     private String JavaDoc query;
48     private String JavaDoc urlAsStarted;
49     private ClassLoader JavaDoc classLoader;
50
51     public TransportConnectorGBeanImpl(BrokerServiceGBean brokerServiceGBean, String JavaDoc protocol, String JavaDoc host, int port) {
52         this.brokerServiceGBean = brokerServiceGBean;
53         this.protocol = protocol;
54         this.host = host;
55         this.port = port;
56     }
57
58     public String JavaDoc getProtocol() {
59         return protocol;
60     }
61
62     public void setProtocol(String JavaDoc protocol) {
63         this.protocol = protocol;
64     }
65
66     public String JavaDoc getHost() {
67         return host;
68     }
69
70     public void setHost(String JavaDoc host) {
71         this.host = host;
72     }
73
74     public int getPort() {
75         return port;
76     }
77
78     public void setPort(int port) {
79         this.port = port;
80     }
81
82     public String JavaDoc getPath() {
83         return path;
84     }
85
86     public void setPath(String JavaDoc path) {
87         this.path = path;
88     }
89
90     public String JavaDoc getQuery() {
91         return query;
92     }
93
94     public void setQuery(String JavaDoc query) {
95         this.query = query;
96     }
97
98     public String JavaDoc getUrl() {
99         try {
100             return new URI JavaDoc(protocol, null, host, port, path, query, null).toString();
101         } catch (URISyntaxException JavaDoc e) {
102             throw new IllegalStateException JavaDoc("Attributes don't form a valid URI: "+protocol+"://"+host+":"+port+"/"+path+"?"+query);
103         }
104     }
105
106     public InetSocketAddress JavaDoc getListenAddress() {
107         try {
108             return transportConnector.getServer().getSocketAddress();
109         } catch (Throwable JavaDoc e) {
110             log.debug("Failure to determine ListenAddress: "+e,e);
111             return null;
112         }
113     }
114
115     public synchronized void doStart() throws Exception JavaDoc {
116         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
117         Thread.currentThread().setContextClassLoader(getClassLoader());
118         try {
119             if (transportConnector == null) {
120                 urlAsStarted = getUrl();
121                 transportConnector = createBrokerConnector(urlAsStarted);
122                 transportConnector.start();
123             }
124         } finally {
125             Thread.currentThread().setContextClassLoader(old);
126         }
127     }
128
129     public synchronized void doStop() throws Exception JavaDoc {
130         if (transportConnector != null) {
131             TransportConnector temp = transportConnector;
132             transportConnector = null;
133             temp.stop();
134         }
135     }
136
137     public synchronized void doFail() {
138         if (transportConnector != null) {
139             TransportConnector temp = transportConnector;
140             transportConnector = null;
141             try {
142                 temp.stop();
143             }
144             catch (Exception JavaDoc e) {
145                 log.info("Caught while closing due to failure: " + e, e);
146             }
147         }
148     }
149
150     protected TransportConnector createBrokerConnector(String JavaDoc url) throws Exception JavaDoc {
151         return brokerServiceGBean.getBrokerContainer().addConnector(url);
152     }
153
154     public ClassLoader JavaDoc getClassLoader() {
155         if( classLoader == null ) {
156             classLoader = this.getClass().getClassLoader();
157         }
158         return classLoader;
159     }
160
161     public void setClassLoader(ClassLoader JavaDoc classLoader) {
162         this.classLoader = classLoader;
163     }
164
165     public static final GBeanInfo GBEAN_INFO;
166
167     static {
168         GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic("ActiveMQ Transport Connector", TransportConnectorGBeanImpl.class, CONNECTOR_J2EE_TYPE);
169         infoBuilder.addAttribute("classLoader", ClassLoader JavaDoc.class, false);
170         infoBuilder.addAttribute("url", String JavaDoc.class.getName(), false);
171         infoBuilder.addReference("brokerService", BrokerServiceGBean.class);
172         infoBuilder.addInterface(ActiveMQConnector.class, new String JavaDoc[]{"host","port","protocol","path","query"},
173                 new String JavaDoc[]{"host","port"});
174         infoBuilder.setConstructor(new GConstructorInfo(new String JavaDoc[]{"brokerService", "protocol", "host", "port"}));
175         GBEAN_INFO = infoBuilder.getBeanInfo();
176     }
177
178     public static GBeanInfo getGBeanInfo() {
179         return GBEAN_INFO;
180     }
181 }
182
Popular Tags