KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > gbean > BrokerServiceGBeanImpl


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.activemq.gbean;
19
20 import java.net.URI JavaDoc;
21
22 import javax.sql.DataSource JavaDoc;
23 import javax.jms.JMSException JavaDoc;
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 /**
40  * Default implementation of the ActiveMQ Message Server
41  *
42  * @version $Revision: 476049 $
43  */

44 public class BrokerServiceGBeanImpl implements GBeanLifecycle, BrokerServiceGBean {
45
46     private Log log = LogFactory.getLog(getClass());
47
48     private String JavaDoc brokerName;
49     private String JavaDoc brokerUri;
50     private BrokerService brokerService;
51     private ServerInfo serverInfo;
52     private String JavaDoc dataDirectory;
53     private ConnectionFactorySource dataSource;
54     private ClassLoader JavaDoc classLoader;
55     private String JavaDoc 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 JavaDoc {
67         ClassLoader JavaDoc 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 JavaDoc(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             // Do not allow the broker to use a shutown hook, the kernel will stop it
87
brokerService.setUseShutdownHook(isUseShutdownHook());
88
89             // Setup the persistence adapter to use the right datasource and directory
90
DefaultPersistenceAdapterFactory persistenceFactory = (DefaultPersistenceAdapterFactory) brokerService.getPersistenceFactory();
91             persistenceFactory.setDataDirectoryFile(serverInfo.resolve(dataDirectory));
92             persistenceFactory.setDataSource((DataSource JavaDoc) dataSource.$getResource());
93
94             brokerService.start();
95         }
96         finally {
97             Thread.currentThread().setContextClassLoader(old);
98         }
99     }
100
101     public synchronized void doStop() throws Exception JavaDoc {
102         if (brokerService != null) {
103             BrokerService temp = brokerService;
104             brokerService = null;
105             try {
106                 temp.stop();
107             } catch (JMSException JavaDoc ignored) {
108                 // just a lame exception ActiveMQ likes to throw on shutdown
109
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 JavaDoc ignored) {
123                 // just a lame exception ActiveMQ likes to throw on shutdown
124
if (!(ignored.getCause() instanceof TransportDisposedIOException)) {
125                     log.warn("Caught while closing due to failure: " + ignored, ignored);
126                 }
127             } catch (Exception JavaDoc 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 JavaDoc.class, false);
139         infoBuilder.addAttribute("brokerName", String JavaDoc.class, true);
140         infoBuilder.addAttribute("brokerUri", String JavaDoc.class, true);
141         infoBuilder.addAttribute("useShutdownHook", Boolean.TYPE, true);
142         infoBuilder.addAttribute("dataDirectory", String JavaDoc.class, true);
143         infoBuilder.addReference("dataSource", ConnectionFactorySource.class);
144         infoBuilder.addAttribute("objectName", String JavaDoc.class, false);
145         infoBuilder.addReference("manager", JMSManager.class);
146         infoBuilder.addInterface(BrokerServiceGBean.class);
147         // infoFactory.setConstructor(new String[]{"brokerName, brokerUri"});
148
GBEAN_INFO = infoBuilder.getBeanInfo();
149     }
150
151     public static GBeanInfo getGBeanInfo() {
152         return GBEAN_INFO;
153     }
154
155     /**
156      * @return Returns the brokerName.
157      */

158     public String JavaDoc getBrokerName() {
159         return brokerName;
160     }
161
162     public String JavaDoc getBrokerUri() {
163         return brokerUri;
164     }
165
166     public void setBrokerName(String JavaDoc brokerName) {
167         this.brokerName = brokerName;
168     }
169
170     public void setBrokerUri(String JavaDoc 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 JavaDoc getDataDirectory() {
183         return dataDirectory;
184     }
185
186     public void setDataDirectory(String JavaDoc 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 JavaDoc getObjectName() {
199         return objectName;
200     }
201
202     public boolean isStateManageable() {
203         return true;
204     }
205
206     public boolean isStatisticsProvider() {
207         return false; // todo: return true once stats are integrated
208
}
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 JavaDoc 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 JavaDoc objectName) {
231         this.objectName = objectName;
232     }
233
234     public ClassLoader JavaDoc getClassLoader() {
235         if( classLoader == null ) {
236             classLoader = this.getClass().getClassLoader();
237         }
238         return classLoader;
239     }
240
241     public void setClassLoader(ClassLoader JavaDoc 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