1 17 package org.apache.activemq.gbean; 18 19 import java.net.InetSocketAddress ; 20 import java.net.URI ; 21 import java.net.URISyntaxException ; 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 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 protocol; 44 private String host; 45 private int port; 46 private String path; 47 private String query; 48 private String urlAsStarted; 49 private ClassLoader classLoader; 50 51 public TransportConnectorGBeanImpl(BrokerServiceGBean brokerServiceGBean, String protocol, String host, int port) { 52 this.brokerServiceGBean = brokerServiceGBean; 53 this.protocol = protocol; 54 this.host = host; 55 this.port = port; 56 } 57 58 public String getProtocol() { 59 return protocol; 60 } 61 62 public void setProtocol(String protocol) { 63 this.protocol = protocol; 64 } 65 66 public String getHost() { 67 return host; 68 } 69 70 public void setHost(String 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 getPath() { 83 return path; 84 } 85 86 public void setPath(String path) { 87 this.path = path; 88 } 89 90 public String getQuery() { 91 return query; 92 } 93 94 public void setQuery(String query) { 95 this.query = query; 96 } 97 98 public String getUrl() { 99 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 = GBeanInfoBuilder.createStatic("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 } 182 | Popular Tags |