KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > server > JmsServer


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2000-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: JmsServer.java,v 1.3 2005/06/07 14:24:39 tanderson Exp $
44  */

45 package org.exolab.jms.server;
46
47 import java.io.PrintStream JavaDoc;
48 import javax.naming.NamingException JavaDoc;
49
50 import org.apache.log4j.xml.DOMConfigurator;
51
52 import org.exolab.jms.authentication.AuthenticationMgr;
53 import org.exolab.jms.config.Configuration;
54 import org.exolab.jms.config.ConfigurationManager;
55 import org.exolab.jms.config.LoggerConfiguration;
56 import org.exolab.jms.events.BasicEventManager;
57 import org.exolab.jms.gc.GarbageCollectionService;
58 import org.exolab.jms.lease.LeaseManager;
59 import org.exolab.jms.messagemgr.DestinationManager;
60 import org.exolab.jms.messagemgr.MessageMgr;
61 import org.exolab.jms.persistence.DatabaseService;
62 import org.exolab.jms.scheduler.Scheduler;
63 import org.exolab.jms.service.ServiceException;
64 import org.exolab.jms.service.ServiceManager;
65 import org.exolab.jms.threads.ThreadPoolManager;
66 import org.exolab.jms.util.CommandLine;
67 import org.exolab.jms.util.Version;
68
69
70 /**
71  * This class contains the main line for instantiating the JMS Server. It
72  * dynamically detemrines, from the configuration information which of the
73  * servers to implement and then calls the init method on them.
74  *
75  * @author <a HREF="mailto:jima@exoffice.com">Jim Alateras</a>
76  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
77  * @version $Revision: 1.3 $ $Date: 2005/06/07 14:24:39 $
78  * @see ConfigurationManager
79  */

80 public class JmsServer {
81
82     /**
83      * The service manager.
84      */

85     protected ServiceManager _services = null;
86
87     /**
88      * The configuration of this server.
89      */

90     private Configuration _config;
91
92
93     /**
94      * Construct a new <code>JmsServer</code>.
95      *
96      * @param config the server configuration
97      * @throws ServerException if the server cannot be created
98      */

99     public JmsServer(Configuration config) throws ServerException {
100         version();
101         ConfigurationManager.setConfig(config);
102     }
103
104     /**
105      * Construct a new <code>JmsServer</code>, configured from the specified
106      * configuration file.
107      *
108      * @param file configuration file name
109      * @throws ServerException if the server cannot be created
110      */

111     public JmsServer(String JavaDoc file) throws ServerException {
112         version();
113
114         try {
115             // initialise the configuration manager
116
ConfigurationManager.setConfig(file);
117             _config = ConfigurationManager.getConfig();
118         } catch (Exception JavaDoc exception) {
119             throw new FailedToCreateServerException(
120                     "Failed to read configuration: " + file, exception);
121         }
122     }
123
124     /**
125      * Initialise the server
126      *
127      * @throws NamingException if administered objects cannot be bound in JNDI
128      * @throws ServerException if the server cannot be initialised
129      */

130     public void init() throws NamingException JavaDoc, ServerException {
131         // initialise the logger
132
LoggerConfiguration log = _config.getLoggerConfiguration();
133
134         // @todo - need to do this in main(), to allow pluggable log factories
135
// when embedding OpenJMS
136
DOMConfigurator.configure(log.getFile());
137
138         // initialise the service manager
139
_services = ServiceManager.instance();
140
141         // initialise the embedded JNDI server if required
142
if (_config.getServerConfiguration().getEmbeddedJNDI()) {
143             EmbeddedNameService.getInstance();
144         }
145
146         // initialise the services, and start them
147
try {
148             registerServices();
149             _services.startAll();
150         } catch (ServiceException exception) {
151             throw new ServerException("Failed to start services", exception);
152         }
153
154         // create and bind administered destinations
155
DestinationManager.instance()
156                 .registerConfiguredAdministeredDestinations();
157
158         // create and start the connector service
159
try {
160             ConnectorService connectors = new ConnectorService(_config);
161             _services.add(connectors.getName(), connectors);
162             connectors.start();
163         } catch (ServerException exception) {
164             throw exception;
165         } catch (ServiceException exception) {
166             throw new ServerException(exception.getMessage(), exception);
167         }
168     }
169
170     /**
171      * This is the main line for the JMS Server. It processes the command line
172      * argument, instantiates an instance of the server class and calls the init
173      * routine on it.
174      */

175     public static void main(String JavaDoc[] args) {
176         try {
177             CommandLine cmdline = new CommandLine(args);
178
179             boolean helpSet = cmdline.exists("help");
180             boolean versionSet = cmdline.exists("version");
181             boolean configSet = cmdline.exists("config");
182
183             if (helpSet) {
184                 usage();
185             } else if (versionSet) {
186                 version();
187             } else if (!configSet && args.length != 0) {
188                 // invalid argument specified
189
usage();
190             } else {
191                 String JavaDoc config = cmdline.value("config",
192                                               getOpenJMSHome()
193                                               + "/config/openjms.xml");
194                 JmsServer server = new JmsServer(config);
195                 server.init();
196             }
197         } catch (Exception JavaDoc exception) {
198             exception.printStackTrace();
199             // force termination of any threads
200
System.exit(-1);
201         }
202     }
203
204     public static void version() {
205         System.err.println(Version.TITLE + " " + Version.VERSION);
206         System.err.println(Version.COPYRIGHT);
207         System.err.println(Version.VENDOR_URL);
208     }
209
210     /**
211      * Print out information on running this sevice
212      */

213     protected static void usage() {
214         PrintStream JavaDoc out = System.out;
215
216         out.println("\n\n");
217         out.println("=====================================================");
218         out.println("Usage information for org.exolab.jms.server.JmsServer");
219         out.println("=====================================================");
220         out.println("\norg.exolab.jms.server.JmsServer");
221         out.println(" [-config <xml config file> | -version | -help]\n");
222         out.println("\t-config file name of xml-based config file\n");
223         out.println("\t-version displays OpenJMS version information\n");
224         out.println("\t-help displays this screen\n");
225     }
226
227     /**
228      * Initialise the services
229      */

230     protected void registerServices() throws ServiceException {
231         // add the thread pool manager
232
_services.add(ThreadPoolManager.instance().getName(),
233                       ThreadPoolManager.instance());
234
235         // add the event manager
236
_services.add(BasicEventManager.instance().getName(),
237                       BasicEventManager.instance());
238
239         // add the database service
240
_services.add(DatabaseService.instance().getName(),
241                       DatabaseService.instance());
242
243         // add the scheduler
244
_services.add(Scheduler.createInstance().getName(),
245                       Scheduler.instance());
246
247         // add the lease manager
248
_services.add(LeaseManager.instance().getName(),
249                       LeaseManager.instance());
250
251         // add the transaction manager
252
//_services.add (TransactionService.instance().getName(),
253
// TransactionService.instance());
254

255         // add the garbage collection service
256
_services.add(GarbageCollectionService.instance().getName(),
257                       GarbageCollectionService.instance());
258
259         // add the authentication manager
260
_services.add(AuthenticationMgr.createInstance().getName(),
261                       AuthenticationMgr.instance());
262
263         // add the resource manager service
264
//_services.add(ResourceManager.instance().getName(),
265
// ResourceManager.instance());
266

267         // add the message manager
268
_services.add(MessageMgr.createInstance().getName(),
269                       MessageMgr.instance());
270     }
271
272     /**
273      * Returns the value of the <code>openjms.home</code> system property. If
274      * none is set, returns the value of the <code>user.dir</code> property.
275      *
276      * @return the value of the openjms.home system property
277      */

278     private static String JavaDoc getOpenJMSHome() {
279         return System.getProperty("openjms.home",
280                                   System.getProperty("user.dir"));
281     }
282
283 }
284
Popular Tags