View Javadoc

1   /**
2    *
3    *  Licensed to the Apache Software Foundation (ASF) under one or more
4    *  contributor license agreements.  See the NOTICE file distributed with
5    *  this work for additional information regarding copyright ownership.
6    *  The ASF licenses this file to You under the Apache License, Version 2.0
7    *  (the "License"); you may not use this file except in compliance with
8    *  the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  
19  package org.apache.activemq.gbean;
20  
21  import java.net.URI;
22  
23  import javax.sql.DataSource;
24  import javax.jms.JMSException;
25  
26  import org.apache.activemq.broker.BrokerFactory;
27  import org.apache.activemq.broker.BrokerService;
28  import org.apache.activemq.store.DefaultPersistenceAdapterFactory;
29  import org.apache.activemq.transport.TransportDisposedIOException;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.geronimo.connector.outbound.ConnectionFactorySource;
33  import org.apache.geronimo.gbean.GBeanInfo;
34  import org.apache.geronimo.gbean.GBeanInfoBuilder;
35  import org.apache.geronimo.gbean.GBeanLifecycle;
36  import org.apache.geronimo.management.geronimo.JMSManager;
37  import org.apache.geronimo.management.geronimo.NetworkConnector;
38  import org.apache.geronimo.system.serverinfo.ServerInfo;
39  
40  /**
41   * Default implementation of the ActiveMQ Message Server
42   *
43   * @version $Revision: 470597 $
44   */
45  public class BrokerServiceGBeanImpl implements GBeanLifecycle, BrokerServiceGBean {
46  
47      private Log log = LogFactory.getLog(getClass());
48  
49      private String brokerName;
50      private String brokerUri;
51      private BrokerService brokerService;
52      private ServerInfo serverInfo;
53      private String dataDirectory;
54      private ConnectionFactorySource dataSource;
55      private ClassLoader classLoader;
56      private String objectName;
57      private JMSManager manager;
58      private boolean useShutdownHook;
59  
60      public BrokerServiceGBeanImpl() {
61      }
62  
63      public synchronized BrokerService getBrokerContainer() {
64          return brokerService;
65      }
66  
67      public synchronized void doStart() throws Exception {
68          ClassLoader old = Thread.currentThread().getContextClassLoader();
69          Thread.currentThread().setContextClassLoader(getClassLoader());
70          try {
71              if (brokerService == null) {
72                  if (brokerUri != null) {
73                      brokerService = BrokerFactory.createBroker(new URI(brokerUri));
74                      brokerName = brokerService.getBrokerName();
75                  }
76                  else {
77                      brokerService = new BrokerService();
78                      if (brokerName != null) {
79                          brokerService.setBrokerName(brokerName);
80                      }
81                      else {
82                          brokerName = brokerService.getBrokerName();
83                      }
84                  }
85              }
86  
87              // Do not allow the broker to use a shutown hook, the kernel will stop it
88              brokerService.setUseShutdownHook(isUseShutdownHook());
89  
90              // Setup the persistence adapter to use the right datasource and directory
91              DefaultPersistenceAdapterFactory persistenceFactory = (DefaultPersistenceAdapterFactory) brokerService.getPersistenceFactory();
92              persistenceFactory.setDataDirectoryFile(serverInfo.resolve(dataDirectory));
93              persistenceFactory.setDataSource((DataSource) dataSource.$getResource());
94  
95              brokerService.start();
96          }
97          finally {
98              Thread.currentThread().setContextClassLoader(old);
99          }
100     }
101 
102     public synchronized void doStop() throws Exception {
103         if (brokerService != null) {
104             BrokerService temp = brokerService;
105             brokerService = null;
106             try {
107                 temp.stop();
108             } catch (JMSException ignored) {
109                 // just a lame exception ActiveMQ likes to throw on shutdown
110                 if (!(ignored.getCause() instanceof TransportDisposedIOException)) {
111                     throw ignored;
112                 }
113             }
114         }
115     }
116 
117     public synchronized void doFail() {
118         if (brokerService != null) {
119             BrokerService temp = brokerService;
120             brokerService = null;
121             try {
122                 temp.stop();
123             } catch (JMSException ignored) {
124                 // just a lame exception ActiveMQ likes to throw on shutdown
125                 if (!(ignored.getCause() instanceof TransportDisposedIOException)) {
126                     log.warn("Caught while closing due to failure: " + ignored, ignored);
127                 }
128             } catch (Exception e) {
129                 log.warn("Caught while closing due to failure: " + e, e);
130             }
131         }
132     }
133 
134     public static final GBeanInfo GBEAN_INFO;
135 
136     static {
137         GBeanInfoBuilder infoBuilder = new GBeanInfoBuilder("ActiveMQ Message Broker", BrokerServiceGBeanImpl.class, "JMSServer");
138         infoBuilder.addReference("serverInfo", ServerInfo.class);
139         infoBuilder.addAttribute("classLoader", ClassLoader.class, false);
140         infoBuilder.addAttribute("brokerName", String.class, true);
141         infoBuilder.addAttribute("brokerUri", String.class, true);
142         infoBuilder.addAttribute("useShutdownHook", Boolean.TYPE, true);
143         infoBuilder.addAttribute("dataDirectory", String.class, true);
144         infoBuilder.addReference("dataSource", ConnectionFactorySource.class);
145         infoBuilder.addAttribute("objectName", String.class, false);
146         infoBuilder.addReference("manager", JMSManager.class);
147         infoBuilder.addInterface(BrokerServiceGBean.class);
148         // infoFactory.setConstructor(new String[]{"brokerName, brokerUri"});
149         GBEAN_INFO = infoBuilder.getBeanInfo();
150     }
151 
152     public static GBeanInfo getGBeanInfo() {
153         return GBEAN_INFO;
154     }
155 
156 	/**
157 	 * @return Returns the brokerName.
158 	 */
159 	public String getBrokerName() {
160 		return brokerName;
161 	}
162 
163     public String getBrokerUri() {
164         return brokerUri;
165     }
166 
167     public void setBrokerName(String brokerName) {
168         this.brokerName = brokerName;
169     }
170 
171     public void setBrokerUri(String brokerUri) {
172         this.brokerUri = brokerUri;
173     }
174 
175     public ServerInfo getServerInfo() {
176         return serverInfo;
177     }
178 
179     public void setServerInfo(ServerInfo serverInfo) {
180         this.serverInfo = serverInfo;
181     }
182 
183     public String getDataDirectory() {
184         return dataDirectory;
185     }
186 
187     public void setDataDirectory(String dataDir) {
188         this.dataDirectory = dataDir;
189     }
190 
191     public ConnectionFactorySource getDataSource() {
192         return dataSource;
193     }
194 
195     public void setDataSource(ConnectionFactorySource dataSource) {
196         this.dataSource = dataSource;
197     }
198 
199     public String getObjectName() {
200         return objectName;
201     }
202 
203     public boolean isStateManageable() {
204         return true;
205     }
206 
207     public boolean isStatisticsProvider() {
208         return false; // todo: return true once stats are integrated
209     }
210 
211     public boolean isEventProvider() {
212         return true;
213     }
214 
215     public NetworkConnector[] getConnectors() {
216         return manager.getConnectorsForContainer(this);
217     }
218 
219     public NetworkConnector[] getConnectors(String protocol) {
220         return manager.getConnectorsForContainer(this, protocol);
221     }
222 
223     public JMSManager getManager() {
224         return manager;
225     }
226 
227     public void setManager(JMSManager manager) {
228         this.manager = manager;
229     }
230 
231     public void setObjectName(String objectName) {
232         this.objectName = objectName;
233     }
234 
235     public ClassLoader getClassLoader() {
236         if( classLoader == null ) {
237             classLoader = this.getClass().getClassLoader();
238         }
239         return classLoader;
240     }
241 
242     public void setClassLoader(ClassLoader classLoader) {
243         this.classLoader = classLoader;
244     }
245 
246     public boolean isUseShutdownHook() {
247         return useShutdownHook;
248     }
249 
250     public void setUseShutdownHook(final boolean useShutdownHook) {
251         this.useShutdownHook = useShutdownHook;
252     }
253 }