KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > server > ApplicationLifecycle


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * @(#) ApplicationLifecycle.java
26  *
27  * Copyright 2000-2001 by iPlanet/Sun Microsystems, Inc.,
28  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
29  * All rights reserved.
30  *
31  * This software is the confidential and proprietary information
32  * of iPlanet/Sun Microsystems, Inc. ("Confidential Information").
33  * You shall not disclose such Confidential Information and shall
34  * use it only in accordance with the terms of the license
35  * agreement you entered into with iPlanet/Sun Microsystems.
36  */

37 package com.sun.enterprise.server;
38
39 import java.util.Collection JavaDoc;
40 import java.util.Iterator JavaDoc;
41
42 import com.sun.ejb.Container;
43 import com.sun.ejb.ContainerFactory;
44 import com.sun.enterprise.Switch;
45 import com.sun.enterprise.log.Log;
46
47 import com.sun.enterprise.config.serverbeans.Applications;
48 import com.sun.enterprise.config.serverbeans.DasConfig;
49 import com.sun.enterprise.config.serverbeans.ServerBeansFactory;
50 import com.sun.enterprise.config.serverbeans.ServerXPathHelper;
51 import com.sun.enterprise.config.ConfigBeansFactory;
52 import com.sun.enterprise.config.ConfigContext;
53 import com.sun.enterprise.config.ConfigException;
54
55 import com.sun.enterprise.instance.InstanceFactory;
56 import com.sun.enterprise.instance.InstanceEnvironment;
57 import com.sun.enterprise.instance.AppsManager;
58 import com.sun.enterprise.instance.EjbModulesManager;
59 import com.sun.enterprise.instance.ConnectorModulesManager;
60
61 import java.util.logging.Level JavaDoc;
62 import java.util.logging.Logger JavaDoc;
63 import com.sun.logging.LogDomains;
64
65 import com.sun.enterprise.server.ServerContext;
66 import com.sun.appserv.server.ServerLifecycle;
67 import com.sun.appserv.server.ServerLifecycleException;
68 import com.sun.appserv.server.util.PreprocessorUtil;
69
70 import com.sun.enterprise.resource.ResourceInstaller;
71 /**
72  * This class implements the lifecycle methods used by the J2EE applications.
73  * It is responsible for loading and shutting down the applications.
74  *
75  * @author Nazrul Islam
76  * @since JDK 1.4
77  */

78 public class ApplicationLifecycle implements ServerLifecycle {
79
80     /**
81      * Server is initializing subsystems and setting up the runtime environment.
82      * Prepare for the beginning of active use of the public methods of this
83      * subsystem. This method is called before any of the public methods of
84      * this subsystem are utilized.
85      *
86      * @param sc ServerContext the server runtime context.
87      *
88      * @exception IllegalStateException if this subsystem has already been
89      * started
90      * @exception ServerLifecycleException if this subsystem detects a fatal
91      * error that prevents this subsystem from being used
92      */

93     public void onInitialization(ServerContext sc)
94                         throws ServerLifecycleException {
95
96         // set system environment variables here
97

98         this._context = sc;
99
100         try {
101             // ---- J2EE APPLICATIONS ----------------------------------
102

103             // instance environment of this server instance
104
/*
105             InstanceEnvironment iEnv = sc.getInstanceEnvironment();
106
107             // shared class loader is used as the parent for the applications
108             ClassLoader sharedCL = sc.getSharedClassLoader();
109
110             AppsManager appsManager =
111                 InstanceFactory.createAppsManager(iEnv, false);
112             */

113             // manager for the j2ee applications
114
//this._applicationMgr = new ApplicationManager(appsManager,sharedCL);
115
this._applicationMgr = ManagerFactory.getApplicationManager();
116
117             // ---- STAND ALONE EJB MODULES --------------------------------
118
/*
119             // config manager for stand alone ejb modules
120             EjbModulesManager ejbModuleManager =
121                 InstanceFactory.createEjbModuleManager(iEnv, false);
122
123             // manager for stand alone ejb modules
124             this._ejbMgr =
125                 new StandAloneEJBModulesManager(ejbModuleManager, sharedCL);
126             */

127             this._ejbMgr = ManagerFactory.getSAEJBModulesManager();
128             // ---- STAND ALONE Web MODULES --------------------------------
129

130             this._webMgr = ManagerFactory.getSAWebModulesManager();
131
132             // ---- STAND ALONE RAR MODULES --------------------------------
133
/*
134             // config manager for stand alone connector modules
135             ConnectorModulesManager connModuleManager =
136                 InstanceFactory.createConnectorModuleManager(iEnv, false);
137             
138             // manager for stand alone connector modules
139             this._connMgr = new StandAloneConnectorModulesManager(
140                                                 connModuleManager, sharedCL);
141             */

142             this._connMgr = ManagerFactory.getSAConnectorModulesManager();
143
144             this._connectorResLoader = new ConnectorResourcesLoader(sc);
145
146         // ---- STAND ALONE APPLICATION CLIENT MODULES ----------------------
147
// manager for stand alone application client modules
148

149         this._acMgr = ManagerFactory.getSAACModulesManager();
150
151         } catch (ConfigException confEx) {
152             _logger.log(Level.SEVERE,
153                 "core.config_exception_while_app_loading", confEx);
154         } catch (Throwable JavaDoc th) {
155             _logger.log(Level.SEVERE,
156                 "core.unexpected_error_occured_while_app_loading", th);
157         }
158     }
159
160     /**
161      * Server is starting up applications
162      *
163      * @param sc ServerContext the server runtime context.
164      *
165      * @exception ServerLifecycleException if this subsystem detects a fatal
166      * error that prevents this subsystem from being used
167      */

168     public void onStartup(ServerContext sc) throws ServerLifecycleException {
169
170         try {
171
172             // Loads the ResourceAdapter Configurations
173

174             this._connectorResLoader.loadRAConfigs();
175
176             // loads all deployed stand alone connector modules
177
// stand alone connectors are loaded first since
178
// their class paths gets added to the the shared
179
// class loader. Any application acceessing these
180
// rar modules otherwise will not be able them.
181
this._connMgr.load();
182
183             boolean standAloneRarsResources=true;
184
185             // Loads connector resources belonging to standalone rars.
186
// Skip the resources belonging to embedded rars
187
this._connectorResLoader.load(standAloneRarsResources);
188
189             // Now do recovery
190
ResourceInstaller installer =
191                     Switch.getSwitch().getResourceInstaller();
192             installer.recoverXAResources();
193
194             // loads all deployed stand alone application client modules
195
this._acMgr.load();
196
197             // loads all deployed stand alone ejb modules
198
this._ejbMgr.load();
199
200             // loads all deployed stand alone web modules
201
this._webMgr.load();
202
203             // loads all deployed j2ee applications
204
this._applicationMgr.load();
205
206             // Loads connector resources belonging to embedded rars.
207
// Resources belonging to stand alone rars are already loaded
208
//this._connectorResLoader.load(!standAloneRarsResources);
209

210
211         } catch (Throwable JavaDoc th) {
212             _logger.log(Level.SEVERE,
213                 "core.unexpected_error_occured_while_app_loading", th);
214         }
215     }
216
217     /**
218      * Server has complted loading the applications and is ready to serve
219      * requests.
220      *
221      * @param sc ServerContext the server runtime context.
222      *
223      * @exception ServerLifecycleException if this subsystem detects a fatal
224      * error that prevents this subsystem from being used
225      */

226     public void onReady(ServerContext sc) throws ServerLifecycleException {
227         try {
228             ApplicationRegistry registry = ApplicationRegistry.getInstance();
229             Collection JavaDoc containers = null;
230
231             if (registry != null) {
232                 containers = registry.getAllEjbContainers();
233             }
234
235             // call onReady on all the ejb bean containers available
236
// in this server instance
237
if (containers != null) {
238                 Iterator JavaDoc iter = containers.iterator();
239
240                 while (iter.hasNext()) {
241                     Container container = (Container) iter.next();
242
243                     if (container != null) {
244                         container.onReady();
245                     }
246                 }
247             }
248
249             _logger.log(Level.FINE, "core.application_onReady_complete");
250
251         } catch (Throwable JavaDoc th) {
252             _logger.log(Level.SEVERE,
253                 "core.unexpected_error_occured_while_app_onready", th);
254         }
255
256         // Starts the dynamic reload monitor thread. This thread monitors the
257
// $APP_ROOT/.reload file of every applicatons and stand alone ejb
258
// module. If the time stamp of these files are updated, this thread
259
// sends a callback to the listeners for a dynamic reload.
260
//
261
// <p> Also starts the auto deploy monitor thread. This monitors the
262
// $INSTANCE/autodeploy directory for new archives to be deployed.
263

264         // server configuration context
265
ConfigContext configCtx = sc.getConfigContext();
266
267         // applicatons node from server configuration
268
Applications applicationsBean = null;
269
270         DasConfig dasConfig = null;
271
272         // flag used to turn on/off the dynamic monitor thread
273
boolean monitor = false;
274
275         try {
276             //ROB: config changes
277
//applicationsBean =
278
//(Applications)ConfigBeansFactory.getConfigBeanByXPath(configCtx,
279
// ServerXPathHelper.XPATH_APPLICATIONS);
280

281             dasConfig = ServerBeansFactory.getDasConfigBean(configCtx);
282
283             //monitor = applicationsBean.isDynamicReloadEnabled();
284
monitor = dasConfig.isDynamicReloadEnabled();
285
286         } catch (ConfigException ce) {
287             _logger.log(Level.SEVERE,
288                 "core.config_exception_while_dynamic_reloading", ce);
289             monitor = false;
290         }
291
292         // starts the dynamic reload monitor thread only if it is on
293
if (monitor) {
294             // reload monitor is initialized in application loader
295
ReloadMonitor reloadMonitor = ReloadMonitor.getInstance(2000l);
296             reloadMonitor.start();
297         }
298
299         /* Nazrul: Auto Deploy will be included in a subsequent release
300
301         // FIXME: get it from the applicationsBean
302         //boolean autoDeploy = false;
303         boolean autoDeploy = true;
304
305         // start the auto deploy monitor is turned ON in server configuration
306         if (autoDeploy) {
307             // FIXME: get it from the applicationsBean
308             long monitorIntv = 2000l;
309
310             // monitors the auto deploy directory
311             AutoDeployMonitor adm = AutoDeployMonitor.getInstance(monitorIntv);
312
313             // instance environment for this server
314             InstanceEnvironment env = this.context.getInstanceEnvironment();
315
316             // auto deploy directory
317             File autoDeployDir = new File(env.getAutoDeployDirPath());
318
319             // handles auto deploy callbacks
320             AutoDeployer autoDeployer = new AutoDeployer();
321
322             // monitor entry for the auto deploy dir
323             MonitorableEntry entry =
324                 new MonitorableEntry(autoDeployDir, autoDeployer);
325
326             adm.addMonitorableEntry(entry);
327
328             // starts auto deploy monitor
329             adm.start();
330         }
331         */

332         
333         ContainerFactory cf = Switch.getSwitch().getContainerFactory();
334         try {
335             cf.restoreEJBTimers();
336         } catch (Exception JavaDoc ex) {
337             _logger.log(Level.SEVERE,
338                 "ApplicationLifeCycle.onReady():: exception when calling " +
339                     "restoreEJBTimers()", ex);
340         }
341     }
342
343     /**
344      * Server is shutting down applications
345      *
346      * @exception ServerLifecycleException if this subsystem detects a fatal
347      * error that prevents this subsystem from being used
348      */

349     public void onShutdown() throws ServerLifecycleException {
350
351         if (this._applicationMgr == null) {
352             // not initialized
353
return;
354         }
355         try {
356             _logger.log(Level.INFO, "core.shutting_down_applications");
357
358             // first shuts down the dynamic reload monitor if running
359
this._applicationMgr.shutdown();
360
361             // application registry for this server instance
362
ApplicationRegistry registry = ApplicationRegistry.getInstance();
363             Collection JavaDoc containers = null;
364
365             if (registry != null) {
366                 containers = registry.getAllEjbContainers();
367             }
368
369             // shuts down all the ejb bean containers available
370
// in this server instance
371
if (containers != null) {
372                 Iterator JavaDoc iter = containers.iterator();
373
374                 while (iter.hasNext()) {
375                     Container container = (Container) iter.next();
376
377                     if (container != null) {
378                         container.onShutdown();
379                     }
380                 }
381             }
382             
383             // After the applicationmanager shutdown, call stop method
384
// of all J2EE Connector 1.5 specification compliant, inbound and
385
// outbound resource adapters.
386
// This call to stop() of all deployed candidate connector modules
387
// is implemented in a concurrent fashion and this call times-out
388
// based on the shutdown-timeout-in-seconds attribute in domain.xml
389
_logger.log(Level.INFO, "core.shutting_down_resource_adapters");
390             this._connectorResLoader.stopActiveResourceAdapters();
391             _logger.log(Level.INFO, "core.ra_shutdown_complete");
392
393             _logger.log(Level.INFO, "core.application_shutdown_complete");
394
395         } catch (Throwable JavaDoc th) {
396             _logger.log(Level.SEVERE,
397                 "core.unexpected_error_occured_while_app_shutdown", th);
398         }
399     }
400
401     /**
402      * Server is terminating the subsystems and the runtime environment.
403      * Gracefully terminate the active use of the public methods of this
404      * subsystem. This method should be the last one called on a given
405      * instance of this subsystem.
406      *
407      * @exception ServerLifecycleException if this subsystem detects a fatal
408      * error that prevents this subsystem from being used
409      */

410     public void onTermination() throws ServerLifecycleException {
411
412         try {
413             // application registry for this server instance
414
ApplicationRegistry registry = ApplicationRegistry.getInstance();
415             Collection JavaDoc containers = null;
416
417             if (registry != null) {
418                 containers = registry.getAllEjbContainers();
419             }
420
421             // final shut down call to all ejb containers
422
if (containers != null) {
423                 Iterator JavaDoc iter = containers.iterator();
424
425                 while (iter.hasNext()) {
426                     Container container = (Container) iter.next();
427
428                     if (container != null) {
429                         container.onTermination();
430                     }
431                 }
432             }
433         } catch (Throwable JavaDoc th) {
434             _logger.log(Level.SEVERE,
435                 "core.unexpected_error_occured_while_app_terminate", th);
436         }
437     }
438
439     // ---- VARIABLE(S) - PRIVATE ------------------------------------------
440
protected ServerContext _context = null;
441     protected ApplicationManager _applicationMgr = null;
442     protected StandAloneEJBModulesManager _ejbMgr = null;
443     protected StandAloneConnectorModulesManager _connMgr = null;
444     protected ConnectorResourcesLoader _connectorResLoader= null;
445     protected StandAloneAppClientModulesManager _acMgr = null;
446     protected DummyWebModuleManager _webMgr = null;
447
448     protected static Logger JavaDoc _logger=LogDomains.getLogger(LogDomains.CORE_LOGGER);
449 }
450
Popular Tags