001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. 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: */package org.apache.geronimo.activemq;
017:
018: import java.net.URI;
019:
020: import javax.jms.JMSException;
021: import javax.resource.ResourceException;
022: import javax.sql.DataSource;
023:
024: import org.apache.activemq.broker.BrokerFactory;
025: import org.apache.activemq.broker.BrokerService;
026: import org.apache.activemq.broker.jmx.ManagementContext;
027: import org.apache.activemq.store.DefaultPersistenceAdapterFactory;
028: import org.apache.activemq.transport.TransportDisposedIOException;
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.apache.geronimo.gbean.GBeanInfo;
032: import org.apache.geronimo.gbean.GBeanInfoBuilder;
033: import org.apache.geronimo.gbean.GBeanLifecycle;
034: import org.apache.geronimo.naming.ResourceSource;
035: import org.apache.geronimo.management.geronimo.JMSManager;
036: import org.apache.geronimo.management.geronimo.NetworkConnector;
037: import org.apache.geronimo.system.jmx.MBeanServerReference;
038: import org.apache.geronimo.system.serverinfo.ServerInfo;
039:
040: /**
041: * Default implementation of the ActiveMQ Message Server
042: *
043: * @version $Rev: 607943 $ $Date: 2008-01-01 15:07:17 -0800 (Tue, 01 Jan 2008) $
044: */
045: public class BrokerServiceGBeanImpl implements GBeanLifecycle,
046: BrokerServiceGBean {
047:
048: private Log log = LogFactory.getLog(getClass());
049:
050: private String brokerName;
051: private String brokerUri;
052: private BrokerService brokerService;
053: private ServerInfo serverInfo;
054: private String dataDirectory;
055: private ResourceSource<ResourceException> dataSource;
056: private ClassLoader classLoader;
057: private String objectName;
058: private JMSManager manager;
059: private boolean useShutdownHook;
060: private MBeanServerReference mbeanServerReference;
061:
062: public BrokerServiceGBeanImpl() {
063: }
064:
065: public synchronized BrokerService getBrokerContainer() {
066: return brokerService;
067: }
068:
069: public void setMbeanServerReference(
070: MBeanServerReference mbeanServerReference) {
071: this .mbeanServerReference = mbeanServerReference;
072: }
073:
074: public synchronized void doStart() throws Exception {
075: ClassLoader old = Thread.currentThread()
076: .getContextClassLoader();
077: Thread.currentThread().setContextClassLoader(getClassLoader());
078: try {
079: if (brokerService == null) {
080: if (brokerUri != null) {
081: brokerService = BrokerFactory.createBroker(new URI(
082: brokerUri));
083: brokerName = brokerService.getBrokerName();
084: } else {
085: brokerService = new BrokerService();
086: if (brokerName != null) {
087: brokerService.setBrokerName(brokerName);
088: } else {
089: brokerName = brokerService.getBrokerName();
090: }
091: }
092: }
093:
094: // Do not allow creation of another ConnectorServer
095: ManagementContext mgmtctx = new ManagementContext(
096: mbeanServerReference != null ? mbeanServerReference
097: .getMBeanServer() : null);
098: mgmtctx.setCreateConnector(false);
099: brokerService.setManagementContext(mgmtctx);
100:
101: // Do not allow the broker to use a shutown hook, the kernel will stop it
102: brokerService.setUseShutdownHook(isUseShutdownHook());
103:
104: // Setup the persistence adapter to use the right datasource and directory
105: DefaultPersistenceAdapterFactory persistenceFactory = (DefaultPersistenceAdapterFactory) brokerService
106: .getPersistenceFactory();
107: persistenceFactory.setDataDirectoryFile(serverInfo
108: .resolveServer(dataDirectory));
109: persistenceFactory.setDataSource((DataSource) dataSource
110: .$getResource());
111:
112: brokerService.start();
113: } finally {
114: Thread.currentThread().setContextClassLoader(old);
115: }
116: }
117:
118: public synchronized void doStop() throws Exception {
119: if (brokerService != null) {
120: BrokerService temp = brokerService;
121: brokerService = null;
122: try {
123: temp.stop();
124: } catch (JMSException ignored) {
125: // just a lame exception ActiveMQ likes to throw on shutdown
126: if (!(ignored.getCause() instanceof TransportDisposedIOException)) {
127: throw ignored;
128: }
129: }
130: }
131: }
132:
133: public synchronized void doFail() {
134: if (brokerService != null) {
135: BrokerService temp = brokerService;
136: brokerService = null;
137: try {
138: temp.stop();
139: } catch (JMSException ignored) {
140: // just a lame exception ActiveMQ likes to throw on shutdown
141: if (!(ignored.getCause() instanceof TransportDisposedIOException)) {
142: log.warn("Caught while closing due to failure: "
143: + ignored, ignored);
144: }
145: } catch (Exception e) {
146: log
147: .warn("Caught while closing due to failure: "
148: + e, e);
149: }
150: }
151: }
152:
153: public static final GBeanInfo GBEAN_INFO;
154:
155: static {
156: GBeanInfoBuilder infoBuilder = new GBeanInfoBuilder(
157: "ActiveMQ Message Broker",
158: BrokerServiceGBeanImpl.class, "JMSServer");
159: infoBuilder.addReference("serverInfo", ServerInfo.class);
160: infoBuilder.addReference("mbeanServerReference",
161: MBeanServerReference.class);
162: infoBuilder.addAttribute("classLoader", ClassLoader.class,
163: false);
164: infoBuilder.addAttribute("brokerName", String.class, true);
165: infoBuilder.addAttribute("brokerUri", String.class, true);
166: infoBuilder.addAttribute("useShutdownHook", Boolean.TYPE, true);
167: infoBuilder.addAttribute("dataDirectory", String.class, true);
168: infoBuilder.addReference("dataSource", ResourceSource.class);
169: infoBuilder.addAttribute("objectName", String.class, false);
170: infoBuilder.addReference("manager", JMSManager.class);
171: infoBuilder.addInterface(BrokerServiceGBean.class);
172: // infoFactory.setConstructor(new String[]{"brokerName, brokerUri"});
173: GBEAN_INFO = infoBuilder.getBeanInfo();
174: }
175:
176: public static GBeanInfo getGBeanInfo() {
177: return GBEAN_INFO;
178: }
179:
180: /**
181: * @return Returns the brokerName.
182: */
183: public String getBrokerName() {
184: return brokerName;
185: }
186:
187: public String getBrokerUri() {
188: return brokerUri;
189: }
190:
191: public void setBrokerName(String brokerName) {
192: this .brokerName = brokerName;
193: }
194:
195: public void setBrokerUri(String brokerUri) {
196: this .brokerUri = brokerUri;
197: }
198:
199: public ServerInfo getServerInfo() {
200: return serverInfo;
201: }
202:
203: public void setServerInfo(ServerInfo serverInfo) {
204: this .serverInfo = serverInfo;
205: }
206:
207: public String getDataDirectory() {
208: return dataDirectory;
209: }
210:
211: public void setDataDirectory(String dataDir) {
212: this .dataDirectory = dataDir;
213: }
214:
215: public ResourceSource<ResourceException> getDataSource() {
216: return dataSource;
217: }
218:
219: public void setDataSource(
220: ResourceSource<ResourceException> dataSource) {
221: this .dataSource = dataSource;
222: }
223:
224: public String getObjectName() {
225: return objectName;
226: }
227:
228: public boolean isStateManageable() {
229: return true;
230: }
231:
232: public boolean isStatisticsProvider() {
233: return false; // todo: return true once stats are integrated
234: }
235:
236: public boolean isEventProvider() {
237: return true;
238: }
239:
240: public NetworkConnector[] getConnectors() {
241: return manager.getConnectorsForContainer(this );
242: }
243:
244: public NetworkConnector[] getConnectors(String protocol) {
245: return manager.getConnectorsForContainer(this , protocol);
246: }
247:
248: public JMSManager getManager() {
249: return manager;
250: }
251:
252: public void setManager(JMSManager manager) {
253: this .manager = manager;
254: }
255:
256: public void setObjectName(String objectName) {
257: this .objectName = objectName;
258: }
259:
260: public ClassLoader getClassLoader() {
261: if (classLoader == null) {
262: classLoader = this .getClass().getClassLoader();
263: }
264: return classLoader;
265: }
266:
267: public void setClassLoader(ClassLoader classLoader) {
268: this .classLoader = classLoader;
269: }
270:
271: public boolean isUseShutdownHook() {
272: return useShutdownHook;
273: }
274:
275: public void setUseShutdownHook(final boolean useShutdownHook) {
276: this.useShutdownHook = useShutdownHook;
277: }
278: }
|