KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > Runner


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * Runner.java
20  *
21  * This class is responsible for instanciating the coadunation environment properly.
22  */

23
24 package com.rift.coad;
25
26 // log 4 j imports
27
import org.apache.log4j.Logger;
28 import org.apache.log4j.PropertyConfigurator;
29 import org.apache.log4j.xml.DOMConfigurator;
30 import org.apache.log4j.BasicConfigurator;
31
32 // coadunation imports
33
import com.rift.coad.lib.cache.CacheRegistry;
34 import com.rift.coad.lib.configuration.Configuration;
35 import com.rift.coad.lib.configuration.ConfigurationFactory;
36 import com.rift.coad.lib.db.DBSourceManager;
37 import com.rift.coad.lib.deployment.DeploymentLoader;
38 import com.rift.coad.lib.deployment.DeploymentManager;
39 import com.rift.coad.lib.deployment.test.TestMonitor;
40 import com.rift.coad.lib.deployment.bean.BeanManager;
41 import com.rift.coad.lib.deployment.bean.BeanConnector;
42 import com.rift.coad.lib.deployment.jmxbean.JMXBeanManager;
43 import com.rift.coad.lib.deployment.jmxbean.JMXBeanConnector;
44 import com.rift.coad.lib.deployment.webservice.WebServiceManager;
45 import com.rift.coad.lib.deployment.webservice.WebServiceConnector;
46 import com.rift.coad.lib.httpd.HttpDaemon;
47 import com.rift.coad.lib.interceptor.InterceptorFactory;
48 import com.rift.coad.lib.loader.MasterClassLoader;
49 import com.rift.coad.lib.naming.NamingDirector;
50 import com.rift.coad.lib.security.ThreadsPermissionContainer;
51 import com.rift.coad.lib.security.ThreadPermissionSession;
52 import com.rift.coad.lib.security.ThreadsPermissionContainerAccessor;
53 import com.rift.coad.lib.security.user.UserSessionManager;
54 import com.rift.coad.lib.security.user.UserSessionManagerAccessor;
55 import com.rift.coad.lib.security.user.UserStoreManager;
56 import com.rift.coad.lib.security.user.UserStoreManagerAccessor;
57 import com.rift.coad.lib.security.login.handlers.PasswordInfoHandler;
58 import com.rift.coad.lib.security.SessionManager;
59 import com.rift.coad.lib.security.RoleManager;
60 import com.rift.coad.lib.security.Validator;
61 import com.rift.coad.lib.security.login.LoginManager;
62 import com.rift.coad.lib.thread.CoadunationThreadGroup;
63 import com.rift.coad.lib.thread.BasicThread;
64 import com.rift.coad.lib.thirdparty.axis.AxisManager;
65 import com.rift.coad.lib.transaction.TransactionDirector;
66
67 /**
68  *
69  * @author Brett Chaldecott
70  */

71 public class Runner {
72     
73     /**
74      * The implementation of the shut down hook. This object will get run when
75      * this program is terminated.
76      */

77     public static class ShutdownHook extends Thread JavaDoc {
78         /**
79          * The default constructor of the shutdown hook.
80          */

81         public ShutdownHook() {
82         }
83         
84         
85         /**
86          * This method will get called to shut down the coadunation base.
87          */

88         public void run() {
89             // alert the waiting thread
90
alert();
91             synchronized (this){
92                 try {
93                     wait();
94                 } catch (Exception JavaDoc ex) {
95                     
96                 }
97             }
98         }
99         
100         
101         /**
102          * This method will alert the hook to the fact that this object is being
103          * shut down.
104          */

105         private synchronized void alert() {
106             notify();
107         }
108         
109         /**
110          * This method will monitor
111          */

112         public synchronized void monitor() {
113             try {
114                 wait();
115             } catch (Exception JavaDoc ex) {
116                 // do nothing
117
}
118         }
119         
120         /**
121          * Notify the caller thread to inform them of complete shut down.
122          */

123         public synchronized void notifyOfCompletion() {
124             notifyAll();
125         }
126     }
127     
128     // class constants
129
public static final String JavaDoc RUNNER_USER = "runner_user";
130     
131     
132     // private member variables
133
protected static Logger log =
134             Logger.getLogger(Runner.class.getName());
135     private ThreadsPermissionContainer permissionContainer = null;
136     private UserStoreManager userStoreManager = null;
137     private UserSessionManager sessionManager = null;
138     private CoadunationThreadGroup threadGroup = null;
139     private BeanManager beanManager = null;
140     private JMXBeanManager jmxBeanManager = null;
141     private WebServiceManager webServiceManager = null;
142     private DeploymentManager deploymentManager = null;
143     private HttpDaemon httpDaemon = null;
144     
145     /**
146      * Creates a new instance of Main
147      */

148     public Runner() throws CoadException {
149         // Validate the class loader
150
System.out.println("Check the class loader");
151         if (!(this.getClass().getClassLoader() instanceof
152                 com.rift.coad.BaseClassLoader)) {
153             log.error("Invalid class loader");
154             System.exit(-1);
155         }
156         System.out.println("Try and init");
157         try {
158             Configuration config = ConfigurationFactory.getInstance().getConfig(
159                     Runner.class);
160             
161             // instanciate the user permissions
162
log.info("Init the master class loader");
163             MasterClassLoader.init();
164             
165             // instanciate the user permissions
166
log.info("Init thread permissions");
167             permissionContainer =
168                     new ThreadsPermissionContainer();
169             ThreadsPermissionContainerAccessor.init(permissionContainer);
170             log.info("Init session manager");
171             SessionManager.init(permissionContainer);
172             log.info("Init user store");
173             userStoreManager = new UserStoreManager();
174             UserStoreManagerAccessor.init(userStoreManager);
175             log.info("Init user session manager");
176             sessionManager = new UserSessionManager(
177                     permissionContainer,userStoreManager);
178             sessionManager.startCleanup();
179             UserSessionManagerAccessor.init(sessionManager);
180             log.info("Init login module");
181             LoginManager.init(sessionManager,userStoreManager);
182             
183             // add a user to the session for the current thread
184
log.info("Init roles");
185             RoleManager.getInstance().startBackgroundThread();
186             
187             // setup a default user for the current thread
188
log.info("Init the default user for the runner");
189             Long JavaDoc threadId = new Long JavaDoc(Thread.currentThread().getId());
190             permissionContainer.putSession(threadId,
191                     new ThreadPermissionSession(threadId,userStoreManager.
192                     getUserInfo(config.getString(RUNNER_USER))));
193             
194             // instanciate the thread manager
195
log.info("Init thread group");
196             threadGroup = new CoadunationThreadGroup(sessionManager,
197                     userStoreManager);
198             
199             // init the interceptor factory
200
log.info("Init the interceptor factory");
201             InterceptorFactory.init(permissionContainer,sessionManager,
202                 userStoreManager);
203             
204             // setup the current thread class loader
205
log.info("Init the naming director");
206             NamingDirector.init(threadGroup);
207             
208             log.info("Init the transaction director");
209             TransactionDirector.init();
210             
211             // instanciate the cache registry
212
log.info("Init the cache registry");
213             CacheRegistry.init(threadGroup);
214             
215             // instanciate the database sources
216
log.info("Init data stources");
217             DBSourceManager.init();
218             
219             // instanciate the bean manager
220
log.info("Init coadunation beans");
221             beanManager = new BeanManager(permissionContainer,
222                     threadGroup);
223             BeanConnector.init(beanManager);
224             
225             // instanciate the jmx bean manager
226
log.info("Init JMX Beans");
227             jmxBeanManager = new JMXBeanManager(permissionContainer,
228                     threadGroup);
229             JMXBeanConnector.init(jmxBeanManager);
230             
231             // instanciate the axis engine
232
log.info("Init AXIS");
233             AxisManager.init();
234             
235             // instanciate the web service manager
236
log.info("Init Web Service management");
237             webServiceManager = new WebServiceManager();
238             WebServiceConnector.init(webServiceManager);
239             
240             // instanciate the thread manager
241
log.info("Init Deployment Loader");
242             deploymentManager = new DeploymentManager(
243                     threadGroup,beanManager,jmxBeanManager, webServiceManager);
244             
245             // instanciate the http daemon
246
log.info("Init Web Service HTTPD");
247             httpDaemon = new HttpDaemon(threadGroup);
248             
249         } catch (Exception JavaDoc ex) {
250             System.out.println("Failed to start coadunation : " + ex.getMessage());
251             ex.printStackTrace(System.out);
252             log.error("Failed to start coadunation : " + ex.getMessage(), ex);
253             throw new CoadException(
254                     "Failed start the Coadunation base because : " +
255                     ex.getMessage(),ex);
256         }
257     }
258     
259     
260     /**
261      * This method will shut down the coadunation base.
262      */

263     public void shutdown() {
264         try {
265             log.info("Shutting down HTTPD");
266             httpDaemon.shutdown();
267             log.info("Shutting down deployment manager");
268             deploymentManager.shutdown();
269             log.info("Shutting down the cache registry");
270             CacheRegistry.getInstance().shutdown();
271             log.info("Stopping the transaction director");
272             TransactionDirector.getInstance().stop();
273             log.info("Shut down the naming director");
274             NamingDirector.getInstance().shutdown();
275             log.info("Terminating the local thread group");
276             threadGroup.terminate();
277             log.info("Terminate the back ground thread");
278             RoleManager.getInstance().terminateBackgroundThread();
279             log.info("Terminating the session manager");
280             sessionManager.shutdown();
281             
282         } catch (Exception JavaDoc ex) {
283             log.error("Shutdown failed : " + ex.getMessage(),ex);
284         }
285     }
286     
287     /**
288      * The main method responsible for starting the coadunation base.
289      *
290      * @param args the command line arguments
291      */

292     public static void main() {
293         try {
294             String JavaDoc logFile = System.getProperty("Log.File");
295             if (logFile.endsWith("properties")) {
296                 System.out.println("Initing the log file from properties.");
297                 PropertyConfigurator.configure(logFile);
298             } else if (logFile.endsWith("xml")) {
299                 System.out.println("Initing the log file from xml.");
300                 DOMConfigurator.configure(logFile);
301             } else {
302                 System.out.println("Using the basic configuration.");
303                 BasicConfigurator.configure();
304             }
305             System.out.println("Start");
306             Runner runner = new Runner();
307             ShutdownHook shutdownHook = new ShutdownHook();
308             Runtime.getRuntime().addShutdownHook(shutdownHook);
309             System.out.println("Initialization complete");
310             shutdownHook.monitor();
311             runner.shutdown();
312             shutdownHook.notifyOfCompletion();
313             log.info("Shut down complete");
314         } catch (Exception JavaDoc ex) {
315             System.out.println("Failed to run the Coadunation base [" +
316                     ex.getMessage() + "]");
317             ex.printStackTrace(System.out);
318             System.exit(-1);
319         }
320     }
321     
322 }
323
Popular Tags