KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > server > LifecycleModuleService


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 package com.sun.appserv.server;
25
26 import java.util.HashSet JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import com.sun.enterprise.deployment.WebBundleDescriptor;
32 import com.sun.enterprise.deployment.Application;
33 import com.sun.web.security.RealmAdapter;
34
35 import org.apache.catalina.Context;
36 import org.apache.catalina.core.StandardContext;
37
38 import com.sun.enterprise.config.ConfigContext;
39 import com.sun.enterprise.config.ConfigException;
40
41 import com.sun.enterprise.config.serverbeans.ServerBeansFactory;
42 import com.sun.enterprise.config.serverbeans.Server;
43 import com.sun.enterprise.config.serverbeans.Applications;
44 import com.sun.enterprise.config.serverbeans.ApplicationRef;
45 import com.sun.enterprise.config.serverbeans.LifecycleModule;
46 import com.sun.enterprise.config.serverbeans.ElementProperty;
47
48 import com.sun.enterprise.server.ServerContext;
49
50 /**
51  * Support class to assist in firing LifecycleEvent notifications to
52  * registered LifecycleListeners.
53  *
54  * @author Sridatta Viswanath
55  * @version $Id: LifecycleModuleService.java,v 1.6 2006/03/18 14:30:34 hzhang_jn Exp $
56  */

57
58 public class LifecycleModuleService implements ServerLifecycle {
59
60     static final String JavaDoc DELAY_LIFECYCLE_INIT = "com.sun.enterprise.server.DelayLifecycleInit";
61     static String JavaDoc lifeCycleVersion = System.getProperty(DELAY_LIFECYCLE_INIT, "false");
62     protected static boolean delayLifeCycleInit = new Boolean JavaDoc(lifeCycleVersion);
63
64     /**
65      * The set of registered LifecycleListeners for event notifications.
66      */

67     private ArrayList JavaDoc listeners = new ArrayList JavaDoc();
68     
69     protected boolean lifeCycleVersionMatches() {
70         return delayLifeCycleInit == false;
71     }
72
73     public synchronized void onInitialization(ServerContext context)
74                                         throws ServerLifecycleException {
75
76         if (!lifeCycleVersionMatches()) {
77             return;
78         }
79
80         try {
81             //ROB: config changes
82
//Applications apps =
83
//ServerBeansFactory.getServerBean(context.getConfigContext()).getApplications();
84
Applications apps = ServerBeansFactory.getApplicationsBean(context.getConfigContext());
85             if (apps == null) return;
86
87             LifecycleModule[] lcms = apps.getLifecycleModule();
88             if(lcms == null) return;
89
90             HashSet JavaDoc listenerSet = new HashSet JavaDoc();
91             for(int i=0;i<lcms.length;i++) {
92                 LifecycleModule next = lcms[i];
93                     
94                 if ( isEnabled(next, context.getConfigContext()) ) {
95                     int order = Integer.MAX_VALUE;
96                     String JavaDoc strOrder = next.getLoadOrder();
97                     if (strOrder != null) {
98                         try {
99                             order = Integer.parseInt(strOrder);
100                         } catch(NumberFormatException JavaDoc nfe) {
101                             nfe.printStackTrace();
102                         }
103                     }
104                     ServerLifecycleModule slcm =
105                         new ServerLifecycleModule(context,
106                                     next.getName(), next.getClassName());
107                     slcm.setLoadOrder(order);
108                     slcm.setClasspath(next.getClasspath());
109                     slcm.setIsFatal(next.isIsFailureFatal());
110                         
111                     ElementProperty[] s = next.getElementProperty();
112                     if(s != null) {
113                         for(int j=0;j< s.length;j++) {
114                             ElementProperty next1 = s[j];
115                             slcm.setProperty(next1.getName(), next1.getValue());
116                         }
117                     }
118
119                     LifecycleListener listener = slcm.loadServerLifecycle();
120                     listenerSet.add(slcm);
121                 }
122             }
123             sortModules(listenerSet);
124         } catch(Exception JavaDoc ce1) {
125             // FIXME eat it?
126
ce1.printStackTrace();
127         }
128
129         initialize(context);
130     }
131
132     /**
133      * Returns true if life cycle module is enabled in the application
134      * level and in the application ref level.
135      *
136      * @param lcm life cycle module
137      * @param config config context
138      *
139      * @return true if life cycle module is enabled
140      */

141     private boolean isEnabled(LifecycleModule lcm, ConfigContext config) {
142
143         try {
144             // return false if arguments are null
145
if (lcm == null || config == null) {
146                 return false;
147             }
148
149             // find the ref to the life cycle module
150
Server server = ServerBeansFactory.getServerBean(config);
151             ApplicationRef appRef=server.getApplicationRefByRef(lcm.getName());
152
153             // true if enabled in both lifecyle module and in the ref
154
return ((lcm.isEnabled()) &&
155                         (appRef != null && appRef.isEnabled()));
156
157         } catch (ConfigException e) {
158             return false;
159         }
160     }
161
162     private void resetClassLoader(final ClassLoader JavaDoc c) {
163          // set the common class loader as the thread context class loader
164
java.security.AccessController.doPrivileged(
165             new java.security.PrivilegedAction JavaDoc() {
166                 public Object JavaDoc run() {
167                     Thread.currentThread().setContextClassLoader(c);
168                     return null;
169                 }
170             }
171         );
172     }
173     
174     private void sortModules(HashSet JavaDoc listenerSet) {
175         // FIXME: use a better sorting algo
176
for(Iterator JavaDoc iter = listenerSet.iterator(); iter.hasNext();) {
177             ServerLifecycleModule next = (ServerLifecycleModule) iter.next();
178             int order = next.getLoadOrder();
179             int i=0;
180             for(;i<this.listeners.size();i++) {
181                 if(((ServerLifecycleModule)listeners.get(i)).getLoadOrder() > order) {
182                     break;
183                 }
184             }
185             this.listeners.add(i,next);
186         }
187     }
188     
189     private void initialize(ServerContext context)
190                             throws ServerLifecycleException {
191
192         if (listeners.isEmpty())
193             return;
194
195         final ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
196         for(Iterator JavaDoc iter = listeners.iterator(); iter.hasNext();) {
197             ServerLifecycleModule next = (ServerLifecycleModule) iter.next();
198             next.onInitialization(context);
199         }
200         // set it back
201
resetClassLoader(cl);
202     }
203     
204     public void onStartup(ServerContext context) throws ServerLifecycleException {
205
206         if (!lifeCycleVersionMatches()) {
207             return;
208         }
209
210         if (listeners.isEmpty())
211             return;
212
213         final ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
214         for(Iterator JavaDoc iter = listeners.iterator(); iter.hasNext();) {
215             ServerLifecycleModule next = (ServerLifecycleModule) iter.next();
216
217             // the SERVLET invocation context so J2EE invocations from
218
// lifecycle modules get a legal ComponentInvocation.
219

220             // create an invocation context that is of the type
221
// SERVLET_INVOCATION
222
Context invocationContext = new StandardContext();
223
224             WebBundleDescriptor wbd = new WebBundleDescriptor();
225             Application.createApplication(next.getName(),
226                 wbd.getModuleDescriptor());
227             invocationContext.setRealm(new RealmAdapter(wbd, false));
228             next.onStartup(context, invocationContext);
229         }
230         // set it back
231
resetClassLoader(cl);
232     }
233     
234     public void onReady(ServerContext context) throws ServerLifecycleException {
235
236         if (!lifeCycleVersionMatches()) {
237             return;
238         }
239
240         if (listeners.isEmpty())
241             return;
242
243         final ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
244         for(Iterator JavaDoc iter = listeners.iterator(); iter.hasNext();) {
245             ServerLifecycleModule next = (ServerLifecycleModule) iter.next();
246             next.onReady(context);
247         }
248         // set it back
249
resetClassLoader(cl);
250     }
251
252     public void onShutdown() throws ServerLifecycleException {
253
254         if (!lifeCycleVersionMatches()) {
255             return;
256         }
257
258         if (listeners.isEmpty())
259             return;
260
261         final ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
262         for(Iterator JavaDoc iter = listeners.iterator(); iter.hasNext();) {
263             ServerLifecycleModule next = (ServerLifecycleModule) iter.next();
264             next.onShutdown();
265         }
266         // set it back
267
resetClassLoader(cl);
268     }
269     
270     public void onTermination() throws ServerLifecycleException {
271
272         if (!lifeCycleVersionMatches()) {
273             return;
274         }
275
276         if (listeners.isEmpty())
277             return;
278
279         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
280         for(Iterator JavaDoc iter = listeners.iterator(); iter.hasNext();) {
281             ServerLifecycleModule next = (ServerLifecycleModule) iter.next();
282             next.onTermination();
283         }
284         // set it back
285
resetClassLoader(cl);
286     }
287 }
288
Popular Tags