1 17 18 package org.apache.activemq.gbean; 19 20 import java.net.URI ; 21 22 import javax.sql.DataSource ; 23 import javax.jms.JMSException ; 24 25 import org.apache.activemq.broker.BrokerFactory; 26 import org.apache.activemq.broker.BrokerService; 27 import org.apache.activemq.store.DefaultPersistenceAdapterFactory; 28 import org.apache.activemq.transport.TransportDisposedIOException; 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.apache.geronimo.connector.outbound.ConnectionFactorySource; 32 import org.apache.geronimo.gbean.GBeanInfo; 33 import org.apache.geronimo.gbean.GBeanInfoBuilder; 34 import org.apache.geronimo.gbean.GBeanLifecycle; 35 import org.apache.geronimo.management.geronimo.JMSManager; 36 import org.apache.geronimo.management.geronimo.NetworkConnector; 37 import org.apache.geronimo.system.serverinfo.ServerInfo; 38 39 44 public class BrokerServiceGBeanImpl implements GBeanLifecycle, BrokerServiceGBean { 45 46 private Log log = LogFactory.getLog(getClass()); 47 48 private String brokerName; 49 private String brokerUri; 50 private BrokerService brokerService; 51 private ServerInfo serverInfo; 52 private String dataDirectory; 53 private ConnectionFactorySource dataSource; 54 private ClassLoader classLoader; 55 private String objectName; 56 private JMSManager manager; 57 private boolean useShutdownHook; 58 59 public BrokerServiceGBeanImpl() { 60 } 61 62 public synchronized BrokerService getBrokerContainer() { 63 return brokerService; 64 } 65 66 public synchronized void doStart() throws Exception { 67 ClassLoader old = Thread.currentThread().getContextClassLoader(); 68 Thread.currentThread().setContextClassLoader(getClassLoader()); 69 try { 70 if (brokerService == null) { 71 if (brokerUri != null) { 72 brokerService = BrokerFactory.createBroker(new URI (brokerUri)); 73 brokerName = brokerService.getBrokerName(); 74 } 75 else { 76 brokerService = new BrokerService(); 77 if (brokerName != null) { 78 brokerService.setBrokerName(brokerName); 79 } 80 else { 81 brokerName = brokerService.getBrokerName(); 82 } 83 } 84 } 85 86 brokerService.setUseShutdownHook(isUseShutdownHook()); 88 89 DefaultPersistenceAdapterFactory persistenceFactory = (DefaultPersistenceAdapterFactory) brokerService.getPersistenceFactory(); 91 persistenceFactory.setDataDirectoryFile(serverInfo.resolve(dataDirectory)); 92 persistenceFactory.setDataSource((DataSource ) dataSource.$getResource()); 93 94 brokerService.start(); 95 } 96 finally { 97 Thread.currentThread().setContextClassLoader(old); 98 } 99 } 100 101 public synchronized void doStop() throws Exception { 102 if (brokerService != null) { 103 BrokerService temp = brokerService; 104 brokerService = null; 105 try { 106 temp.stop(); 107 } catch (JMSException ignored) { 108 if (!(ignored.getCause() instanceof TransportDisposedIOException)) { 110 throw ignored; 111 } 112 } 113 } 114 } 115 116 public synchronized void doFail() { 117 if (brokerService != null) { 118 BrokerService temp = brokerService; 119 brokerService = null; 120 try { 121 temp.stop(); 122 } catch (JMSException ignored) { 123 if (!(ignored.getCause() instanceof TransportDisposedIOException)) { 125 log.warn("Caught while closing due to failure: " + ignored, ignored); 126 } 127 } catch (Exception e) { 128 log.warn("Caught while closing due to failure: " + e, e); 129 } 130 } 131 } 132 133 public static final GBeanInfo GBEAN_INFO; 134 135 static { 136 GBeanInfoBuilder infoBuilder = new GBeanInfoBuilder("ActiveMQ Message Broker", BrokerServiceGBeanImpl.class, "JMSServer"); 137 infoBuilder.addReference("serverInfo", ServerInfo.class); 138 infoBuilder.addAttribute("classLoader", ClassLoader .class, false); 139 infoBuilder.addAttribute("brokerName", String .class, true); 140 infoBuilder.addAttribute("brokerUri", String .class, true); 141 infoBuilder.addAttribute("useShutdownHook", Boolean.TYPE, true); 142 infoBuilder.addAttribute("dataDirectory", String .class, true); 143 infoBuilder.addReference("dataSource", ConnectionFactorySource.class); 144 infoBuilder.addAttribute("objectName", String .class, false); 145 infoBuilder.addReference("manager", JMSManager.class); 146 infoBuilder.addInterface(BrokerServiceGBean.class); 147 GBEAN_INFO = infoBuilder.getBeanInfo(); 149 } 150 151 public static GBeanInfo getGBeanInfo() { 152 return GBEAN_INFO; 153 } 154 155 158 public String getBrokerName() { 159 return brokerName; 160 } 161 162 public String getBrokerUri() { 163 return brokerUri; 164 } 165 166 public void setBrokerName(String brokerName) { 167 this.brokerName = brokerName; 168 } 169 170 public void setBrokerUri(String brokerUri) { 171 this.brokerUri = brokerUri; 172 } 173 174 public ServerInfo getServerInfo() { 175 return serverInfo; 176 } 177 178 public void setServerInfo(ServerInfo serverInfo) { 179 this.serverInfo = serverInfo; 180 } 181 182 public String getDataDirectory() { 183 return dataDirectory; 184 } 185 186 public void setDataDirectory(String dataDir) { 187 this.dataDirectory = dataDir; 188 } 189 190 public ConnectionFactorySource getDataSource() { 191 return dataSource; 192 } 193 194 public void setDataSource(ConnectionFactorySource dataSource) { 195 this.dataSource = dataSource; 196 } 197 198 public String getObjectName() { 199 return objectName; 200 } 201 202 public boolean isStateManageable() { 203 return true; 204 } 205 206 public boolean isStatisticsProvider() { 207 return false; } 209 210 public boolean isEventProvider() { 211 return true; 212 } 213 214 public NetworkConnector[] getConnectors() { 215 return manager.getConnectorsForContainer(this); 216 } 217 218 public NetworkConnector[] getConnectors(String protocol) { 219 return manager.getConnectorsForContainer(this, protocol); 220 } 221 222 public JMSManager getManager() { 223 return manager; 224 } 225 226 public void setManager(JMSManager manager) { 227 this.manager = manager; 228 } 229 230 public void setObjectName(String objectName) { 231 this.objectName = objectName; 232 } 233 234 public ClassLoader getClassLoader() { 235 if( classLoader == null ) { 236 classLoader = this.getClass().getClassLoader(); 237 } 238 return classLoader; 239 } 240 241 public void setClassLoader(ClassLoader classLoader) { 242 this.classLoader = classLoader; 243 } 244 245 public boolean isUseShutdownHook() { 246 return useShutdownHook; 247 } 248 249 public void setUseShutdownHook(final boolean useShutdownHook) { 250 this.useShutdownHook = useShutdownHook; 251 } 252 } | Popular Tags |