001    /**
002     *
003     * Copyright 2005-2006 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  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    package org.apache.activemq.gbean;
018    
019    import java.net.InetSocketAddress;
020    import java.net.URI;
021    import java.net.URISyntaxException;
022    
023    import org.apache.activemq.broker.TransportConnector;
024    import org.apache.activemq.gbean.ActiveMQConnector;
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
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 $Revision: 437254 $
036     */
037    public class TransportConnectorGBeanImpl implements GBeanLifecycle, ActiveMQConnector {
038        private Log log = LogFactory.getLog(getClass().getName());
039    
040        private TransportConnector transportConnector;
041        private BrokerServiceGBean brokerServiceGBean;
042        
043        private String protocol;
044        private String host;
045        private int port;
046        private String path;
047        private String query;
048        private String urlAsStarted;
049        private ClassLoader classLoader;
050    
051        public TransportConnectorGBeanImpl(BrokerServiceGBean brokerServiceGBean, String protocol, String host, int port) {
052            this.brokerServiceGBean = brokerServiceGBean;
053            this.protocol = protocol;
054            this.host = host;
055            this.port = port;
056        }
057    
058        public String getProtocol() {
059            return protocol;
060        }
061    
062        public void setProtocol(String protocol) {
063            this.protocol = protocol;
064        }
065    
066        public String getHost() {
067            return host;
068        }
069    
070        public void setHost(String host) {
071            this.host = host;
072        }
073    
074        public int getPort() {
075            return port;
076        }
077    
078        public void setPort(int port) {
079            this.port = port;
080        }
081    
082        public String getPath() {
083            return path;
084        }
085    
086        public void setPath(String path) {
087            this.path = path;
088        }
089    
090        public String getQuery() {
091            return query;
092        }
093    
094        public void setQuery(String query) {
095            this.query = query;
096        }
097    
098        public String getUrl() {
099            try {
100                return new URI(protocol, null, host, port, path, query, null).toString();
101            } catch (URISyntaxException e) {
102                throw new IllegalStateException("Attributes don't form a valid URI: "+protocol+"://"+host+":"+port+"/"+path+"?"+query);
103            }
104        }
105    
106        public InetSocketAddress getListenAddress() {
107            try {
108                return transportConnector.getServer().getSocketAddress();
109            } catch (Throwable e) {
110                log.debug("Failure to determine ListenAddress: "+e,e);
111                return null;
112            }
113        }
114    
115        public synchronized void doStart() throws Exception {
116            ClassLoader 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 {
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 e) {
145                    log.info("Caught while closing due to failure: " + e, e);
146                }
147            }
148        }
149    
150        protected TransportConnector createBrokerConnector(String url) throws Exception {
151            return brokerServiceGBean.getBrokerContainer().addConnector(url);
152        }
153    
154        public ClassLoader getClassLoader() {
155            if( classLoader == null ) {
156                classLoader = this.getClass().getClassLoader();
157            }
158            return classLoader;
159        }
160    
161        public void setClassLoader(ClassLoader classLoader) {
162            this.classLoader = classLoader;
163        }
164    
165        public static final GBeanInfo GBEAN_INFO;
166    
167        static {
168            GBeanInfoBuilder infoBuilder = new GBeanInfoBuilder("ActiveMQ Transport Connector", TransportConnectorGBeanImpl.class, CONNECTOR_J2EE_TYPE);
169            infoBuilder.addAttribute("classLoader", ClassLoader.class, false);
170            infoBuilder.addAttribute("url", String.class.getName(), false);
171            infoBuilder.addReference("brokerService", BrokerServiceGBean.class);
172            infoBuilder.addInterface(ActiveMQConnector.class, new String[]{"host","port","protocol","path","query"},
173                    new String[]{"host","port"});
174            infoBuilder.setConstructor(new GConstructorInfo(new String[]{"brokerService", "protocol", "host", "port"}));
175            GBEAN_INFO = infoBuilder.getBeanInfo();
176        }
177    
178        public static GBeanInfo getGBeanInfo() {
179            return GBEAN_INFO;
180        }
181    }