KickJava   Java API By Example, From Geeks To Geeks.

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


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  * @(#) SystemAppLifecycle.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.io.File JavaDoc;
40
41 import com.sun.enterprise.Switch;
42
43 import com.sun.enterprise.config.serverbeans.Domain;
44 import com.sun.enterprise.config.serverbeans.Servers;
45 import com.sun.enterprise.config.serverbeans.Server;
46 import com.sun.enterprise.config.serverbeans.ServerHelper;
47 import com.sun.enterprise.config.ConfigContext;
48
49 import com.sun.enterprise.deployment.autodeploy.AutoDeployer;
50 import com.sun.enterprise.deployment.autodeploy.AutoDeploymentException;
51
52 import java.util.logging.Level JavaDoc;
53 import java.util.logging.Logger JavaDoc;
54 import com.sun.logging.LogDomains;
55
56 import com.sun.enterprise.server.ondemand.OnDemandServer;
57 import com.sun.enterprise.server.ServerContext;
58 import com.sun.appserv.server.ServerLifecycleException;
59 import com.sun.enterprise.instance.ServerManager;
60
61 /**
62  * This class implements the lifecycle methods used by the System Apps.
63  *
64  * @author Sandhya E
65  */

66 public final class SystemAppLifecycle extends ApplicationLifecycle {
67
68     /**
69      * Right now this lifecycle is not registered as a lifecycle.
70      * This method will be explicitly called from J2EE Server.
71      * This method will call two lifecycle methods: onInit and onStartup.
72      * OnShutdown and onTermination need not be called as the required
73      * task will be done ALC for now. This is a mockup startup method
74      * which will take care of lifecycle methods till onStartup
75      */

76     public void startup(ServerContext sc) throws ServerLifecycleException {
77         onInitialization(sc);
78         onStartup(sc);
79     }
80
81     /**
82      * Server is starting up applications
83      *
84      * @param sc ServerContext the server runtime context.
85      *
86      * @exception ServerLifecycleException if this subsystem detects a fatal
87      * error that prevents this subsystem from being used
88      */

89     public void onStartup(ServerContext sc) throws ServerLifecycleException {
90
91         try {
92             //deploys any new system apps
93
deploySystemApps();
94
95             //loads all deployed system apps
96
//Loading system apps will happen as per ondemand startup.
97
if (OnDemandServer.isOnDemandOff()) {
98                 loadSystemApps();
99             }
100         } catch (Throwable JavaDoc th) {
101             _logger.log(Level.SEVERE,
102                 "core.unexpected_error_occured_while_app_loading", th);
103         }
104     }
105
106     /**
107      * Server has complted loading the system applications
108      *
109      * @param sc ServerContext the server runtime context.
110      *
111      * @exception ServerLifecycleException if this subsystem detects a fatal
112      * error that prevents this subsystem from being used
113      */

114     public void onReady(ServerContext sc) throws ServerLifecycleException {
115         // not required to notify listeners(ejb containers) as this will be done by ALC
116
}
117
118     /**
119      * Server is shutting down applications
120      *
121      * @exception ServerLifecycleException if this subsystem detects a fatal
122      * error that prevents this subsystem from being used
123      */

124     public void onShutdown() throws ServerLifecycleException {
125         //not required
126
}
127
128     /**
129      * Server is terminating the subsystems and the runtime environment.
130      * Gracefully terminate the active use of the public methods of this
131      * subsystem. This method should be the last one called on a given
132      * instance of this subsystem.
133      *
134      * @exception ServerLifecycleException if this subsystem detects a fatal
135      * error that prevents this subsystem from being used
136      */

137     public void onTermination() throws ServerLifecycleException {
138         //not required
139
}
140
141     /**
142      * Loads all the deployed system applications.
143      */

144     private void loadSystemApps() {
145         _logger.log(Level.FINE, "core.loading_system_apps");
146         // loads all deployed connector modules
147
this._connMgr.loadSystem();
148
149         // loads all deployed stand alone ejb modules
150
this._ejbMgr.loadSystem();
151
152         // loads all deployed j2ee applications
153
this._applicationMgr.loadSystem();
154     }
155     private String JavaDoc getSystemAppDirPath(){
156         String JavaDoc sysAppDirPath = System.getProperty(Constants.INSTALL_ROOT) + File.separator
157             + Constants.LIB + File.separator + Constants.LIB_INSTALL + File.separator + Constants.LIB_INSTALL_APPLICATIONS;
158         return sysAppDirPath;
159     }
160     
161     /**
162      * Deploys all system apps under system directory to all
163      * server instances under <Servers> in domain.xml. There
164      * are three categories in system apps, applicable to admin
165      * instance only, applicable to instances only, applicable to all.
166      *
167      */

168     private void deploySystemApps(){
169         //FIXME: deploying system apps shoudl be done using J2EEC when it is available.
170
try{
171             String JavaDoc[] targets = getTargets();
172             if(targets == null) return;
173             int size = targets.length;
174             for(int i = 0; i < size; i++) {
175                 deployToTarget(targets[i]);
176             }
177         }catch(Exception JavaDoc ex){
178             _logger.log(Level.SEVERE,
179                 "core.exception_while_deploying_system_apps", ex);
180         }
181     }
182
183     /**
184      * Deploys the system apps under system_app_dir to a specific target.
185      * Target here is a single server
186      * @param target targetserver to which application should be deployed
187      */

188     private void deployToTarget(String JavaDoc target) {
189         String JavaDoc sysAppDirPath = getSystemAppDirPath();
190         _logger.log(Level.FINE,"core.deploying_system_apps", new Object JavaDoc[]{target, sysAppDirPath});
191         com.sun.enterprise.deployment.autodeploy.AutoDeployer deployer =
192            new com.sun.enterprise.deployment.autodeploy.AutoDeployer();
193         deployer.setTarget(target);
194         deployer.setDirectoryScanner(new SystemAppScanner(getTargetType(target)));
195         deployer.disableRenameOnSuccess();
196         
197         File JavaDoc sysAppDir = new File JavaDoc(sysAppDirPath);
198         try{
199             if(sysAppDir.exists() && sysAppDir.canRead()) {
200                 deployer.deployAll(sysAppDir);
201                 _logger.log(Level.FINE,"core.deployed_system_apps",target);
202             } else {
203                 _logger.log(Level.WARNING, "core.system_app_dir_not_found", new Object JavaDoc[] {sysAppDirPath});
204             }
205         }catch(AutoDeploymentException ade) {
206             _logger.log(Level.SEVERE,
207                 "core.exception_while_deploying_system_apps", ade);
208         }
209     }
210
211     private String JavaDoc[] getTargets(){
212         try{
213             ConfigContext confContext = _context.getConfigContext();
214             Domain domain = (Domain)confContext.getRootConfigBean();
215             Servers svrs = domain.getServers();
216             Server[] svrArr = svrs.getServer();
217             int size = svrArr.length;
218             String JavaDoc[] targetNames = new String JavaDoc[size];
219             for(int i = 0 ; i< size; i++) {
220                targetNames[i] = svrArr[i].getName();
221             }
222             return targetNames;
223         }catch(Exception JavaDoc ex){
224             _logger.log(Level.SEVERE,
225                 "core.exception_while_getting_targets", ex);
226             return null;
227         }
228     }
229
230     private String JavaDoc getTargetType(String JavaDoc targetName){
231
232     /*
233         if(targetName.equalsIgnoreCase(ServerManager.ADMINSERVER_ID))
234             return Constants.TARGET_TYPE_ADMIN;
235         else
236             return Constants.TARGET_TYPE_INSTANCE;
237     */

238
239     /**
240      * commented the above code since it doesn't return the
241      * target type properly. In case of DAS since the instance name is
242      * server it returns the type as instance
243      *
244      * Following code uses the isDAS of ServerHelper to determine
245      * if the target type is admin or not
246      */

247
248         try{
249         if (ServerHelper.isDAS(_context.getConfigContext(), targetName)) {
250                 return Constants.TARGET_TYPE_ADMIN;
251         } else {
252                 return Constants.TARGET_TYPE_INSTANCE;
253         }
254         }catch(Exception JavaDoc ex){
255             _logger.log(Level.SEVERE, "core.exception_while_getting_targetType", ex);
256         }
257
258     return null;
259     }
260
261 }
262
Popular Tags