View Javadoc

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.6 2006/02/23 11:17:40 tanderson Exp $
44   */
45  package org.exolab.jms.server;
46  
47  import java.io.PrintStream;
48  import javax.naming.NamingException;
49  
50  import org.apache.log4j.xml.DOMConfigurator;
51  
52  import org.exolab.jms.authentication.AuthenticationMgr;
53  import org.exolab.jms.authentication.UserManager;
54  import org.exolab.jms.config.Configuration;
55  import org.exolab.jms.config.ConfigurationLoader;
56  import org.exolab.jms.config.LoggerConfiguration;
57  import org.exolab.jms.events.BasicEventManager;
58  import org.exolab.jms.gc.GarbageCollectionService;
59  import org.exolab.jms.lease.LeaseManager;
60  import org.exolab.jms.messagemgr.ConsumerManagerImpl;
61  import org.exolab.jms.messagemgr.DestinationCacheFactory;
62  import org.exolab.jms.messagemgr.DestinationConfigurator;
63  import org.exolab.jms.messagemgr.DestinationManagerImpl;
64  import org.exolab.jms.messagemgr.MessageMgr;
65  import org.exolab.jms.messagemgr.ResourceManager;
66  import org.exolab.jms.messagemgr.DestinationBinder;
67  import org.exolab.jms.persistence.DatabaseService;
68  import org.exolab.jms.scheduler.Scheduler;
69  import org.exolab.jms.service.ServiceException;
70  import org.exolab.jms.service.ServiceManager;
71  import org.exolab.jms.service.Services;
72  import org.exolab.jms.service.ServiceThreadListener;
73  import org.exolab.jms.util.CommandLine;
74  import org.exolab.jms.util.Version;
75  import org.exolab.jms.common.threads.DefaultThreadPoolFactory;
76  
77  
78  /***
79   * This class contains the main line for instantiating the JMS Server. It
80   * dynamically detemrines, from the configuration information which of the
81   * servers to implement and then calls the init method on them.
82   *
83   * @author <a href="mailto:jima@exoffice.com">Jim Alateras</a>
84   * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
85   * @version $Revision: 1.6 $ $Date: 2006/02/23 11:17:40 $
86   */
87  public class JmsServer {
88  
89      /***
90       * The service manager.
91       */
92      private Services _services = new ServiceManager();
93  
94      /***
95       * The configuration of this server.
96       */
97      private Configuration _config;
98  
99  
100     /***
101      * Construct a new <code>JmsServer</code>.
102      *
103      * @param config the server configuration
104      */
105     public JmsServer(Configuration config) {
106         version();
107         _config = config;
108     }
109 
110     /***
111      * Construct a new <code>JmsServer</code>, configured from the specified
112      * configuration file.
113      *
114      * @param file configuration file name
115      * @throws ServerException if the server cannot be created
116      */
117     public JmsServer(String file) throws ServerException {
118         version();
119 
120         ConfigurationLoader loader = new ConfigurationLoader();
121         try {
122             _config = loader.load(file);
123         } catch (Exception exception) {
124             throw new ServerException("Failed to read configuration: " + file,
125                                       exception);
126         }
127     }
128 
129     /***
130      * Initialise the server
131      *
132      * @throws NamingException  if administered objects cannot be bound in JNDI
133      * @throws ServiceException if the server cannot be initialised
134      */
135     public void init() throws NamingException, ServiceException {
136         // initialise the logger
137         LoggerConfiguration log = _config.getLoggerConfiguration();
138 
139         // @todo - need to do this in main(), to allow pluggable log factories
140         // when embedding OpenJMS
141         DOMConfigurator.configure(log.getFile());
142 
143         // initialise the services, and start them
144         try {
145             registerServices();
146             _services.start();
147         } catch (ServiceException exception) {
148             throw new ServerException(
149                     "Failed to start services", exception);
150         }
151     }
152 
153     /***
154      * This is the main line for the JMS Server. It processes the command line
155      * argument, instantiates an instance of the server class and calls the init
156      * routine on it.
157      */
158     public static void main(String[] args) {
159         try {
160             CommandLine cmdline = new CommandLine(args);
161 
162             boolean helpSet = cmdline.exists("help");
163             boolean versionSet = cmdline.exists("version");
164             boolean configSet = cmdline.exists("config");
165 
166             if (helpSet) {
167                 usage();
168             } else if (versionSet) {
169                 version();
170             } else if (!configSet && args.length != 0) {
171                 // invalid argument specified
172                 usage();
173             } else {
174                 String config = cmdline.value("config",
175                                               getOpenJMSHome()
176                                               + "/config/openjms.xml");
177                 JmsServer server = new JmsServer(config);
178                 server.init();
179             }
180         } catch (Exception exception) {
181             exception.printStackTrace();
182             // force termination of any threads
183             System.exit(-1);
184         }
185     }
186 
187     public static void version() {
188         System.err.println(Version.TITLE + " " + Version.VERSION);
189         System.err.println(Version.COPYRIGHT);
190         System.err.println(Version.VENDOR_URL);
191     }
192 
193     /***
194      * Print out information on running this sevice
195      */
196     protected static void usage() {
197         PrintStream out = System.out;
198 
199         out.println("\n\n");
200         out.println("=====================================================");
201         out.println("Usage information for org.exolab.jms.server.JmsServer");
202         out.println("=====================================================");
203         out.println("\norg.exolab.jms.server.JmsServer");
204         out.println("    [-config <xml config file> | -version | -help]\n");
205         out.println("\t-config  file name of xml-based config file\n");
206         out.println("\t-version displays OpenJMS version information\n");
207         out.println("\t-help    displays this screen\n");
208     }
209 
210     /***
211      * Returns the services
212      *
213      * @return ServiceManager
214      * @deprecated no replacement
215      */
216     protected Services getServices() {
217         return _services;
218     }
219 
220     /***
221      * Initialise the services
222      */
223     protected void registerServices() throws ServiceException {
224         _services.addService(_config);
225         _services.addService(ServiceThreadListener.class);
226         _services.addService(DefaultThreadPoolFactory.class);
227         _services.addService(BasicEventManager.class);
228         _services.addService(DatabaseService.class);
229         _services.addService(Scheduler.class);
230         _services.addService(LeaseManager.class);
231         _services.addService(GarbageCollectionService.class);
232         _services.addService(AuthenticationMgr.class);
233         _services.addService(UserManager.class);
234         _services.addService(DestinationCacheFactory.class);
235         _services.addService(DestinationManagerImpl.class);
236         _services.addService(ConsumerManagerImpl.class);
237         _services.addService(ServerConnectionManagerImpl.class);
238         _services.addService(ResourceManager.class);
239         _services.addService(AdminConnectionFactory.class);
240         _services.addService(AdminConnectionManager.class);
241         _services.addService(MessageMgr.class);
242         _services.addService(NameService.class);
243         _services.addService(DestinationBinder.class);
244         _services.addService(DestinationConfigurator.class);
245         _services.addService(ConnectorService.class);
246     }
247 
248     /***
249      * Returns the value of the <code>openjms.home</code> system property. If
250      * none is set, returns the value of the <code>user.dir</code> property.
251      *
252      * @return the value of the openjms.home system property
253      */
254     private static String getOpenJMSHome() {
255         return System.getProperty("openjms.home",
256                                   System.getProperty("user.dir"));
257     }
258 
259 }