KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URLClassLoader JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29 import java.io.StringWriter JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31 import java.util.logging.Level JavaDoc;
32 import java.util.Properties JavaDoc;
33 import java.util.ResourceBundle JavaDoc;
34
35 import java.text.MessageFormat JavaDoc;
36
37 import com.sun.logging.LogDomains;
38 import com.sun.enterprise.Switch;
39 import com.sun.enterprise.InvocationException;
40 import com.sun.enterprise.InvocationManager;
41 import com.sun.enterprise.ComponentInvocation;
42
43 import org.apache.catalina.Context;
44
45 import com.sun.enterprise.loader.ClassLoaderUtils;
46 import com.sun.enterprise.server.ServerContext;
47
48 /**
49  * @author Sridatta Viswanath
50  * @version $Id: ServerLifecycleModule.java,v 1.3 2005/12/25 04:13:01 tcfujii Exp $
51  */

52
53 public final class ServerLifecycleModule {
54     
55     private LifecycleListener slcl;
56     private String JavaDoc name;
57     private String JavaDoc className;
58     private String JavaDoc classpath;
59     private int loadOrder;
60     private boolean isFatal = true;
61     private String JavaDoc statusMsg = "OK";
62     
63     private ServerContext ctx;
64     private LifecycleEventContext leContext;
65     private ClassLoader JavaDoc urlClassLoader;
66     private Properties JavaDoc props = new Properties JavaDoc();
67
68     private static Logger JavaDoc _logger = null;
69     private static boolean _isTraceEnabled = false;
70     private static ResourceBundle JavaDoc _rb = null;
71
72     private ComponentInvocation lcmInvocation;
73
74     ServerLifecycleModule(ServerContext ctx, String JavaDoc name, String JavaDoc className) {
75         this.name = name;
76         this.className = className;
77         this.ctx = ctx;
78         this.leContext = new LifecycleEventContextImpl(ctx);
79
80         _logger = LogDomains.getLogger(LogDomains.ROOT_LOGGER);
81         _isTraceEnabled = _logger.isLoggable(Level.FINE);
82         _rb = _logger.getResourceBundle();
83     }
84     
85     void setClasspath(String JavaDoc classpath) {
86         this.classpath = classpath;
87     }
88     
89     void setProperty(String JavaDoc name, String JavaDoc value) {
90         props.put(name, value);
91     }
92     
93     Properties JavaDoc getProperties() {
94         return this.props;
95     }
96     
97     void setLoadOrder(int loadOrder) {
98         this.loadOrder = loadOrder;
99     }
100     
101     void setIsFatal(boolean isFatal) {
102         this.isFatal = isFatal;
103     }
104         
105     String JavaDoc getName() {
106         return this.name;
107     }
108     
109     String JavaDoc getClassName() {
110         return this.className;
111     }
112     String JavaDoc getclasspath() {
113         return this.classpath;
114     }
115     int getLoadOrder() {
116         return this.loadOrder;
117     }
118     
119     boolean isFatal() {
120         return isFatal;
121     }
122     
123     LifecycleListener loadServerLifecycle() throws ServerLifecycleException {
124         ClassLoader JavaDoc classLoader = ctx.getLifecycleParentClassLoader();
125
126         try {
127             if (this.classpath != null) {
128                 URL JavaDoc[] urls = getURLs();
129
130                 if (urls != null) {
131                     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(128);
132                     for(int i=0;i<urls.length;i++) {
133                         sb.append(urls[i].toString());
134                     }
135                     if (_isTraceEnabled)
136                         _logger.fine("Lifecycle module = " + getName() +
137                                         " has classpath URLs = " + sb.toString());
138                 }
139
140                 this.urlClassLoader = new URLClassLoader JavaDoc(urls, classLoader);
141                 classLoader = this.urlClassLoader;
142             }
143
144             Class JavaDoc cl = Class.forName(className, true, classLoader);
145             slcl = (LifecycleListener) cl.newInstance();
146         } catch (Exception JavaDoc ee) {
147             String JavaDoc msg = _rb.getString("lifecyclemodule.load_exception");
148             Object JavaDoc[] params = { this.name, ee.toString() };
149             msg = MessageFormat.format(msg, params);
150
151             _logger.log(Level.WARNING, msg);
152         }
153
154         return slcl;
155     }
156     
157     private URL JavaDoc[] getURLs() {
158         return ClassLoaderUtils.getUrlsFromClasspath(this.classpath);
159     }
160
161
162     private void preInvoke(InvocationManager invMgr)
163                             throws ServerLifecycleException {
164         try {
165             invMgr.preInvoke(lcmInvocation);
166         } catch (InvocationException ie) {
167             String JavaDoc msg = _rb.getString("lifecyclemodule.preInvoke_exception");
168             Object JavaDoc[] params = { this.name };
169             msg = MessageFormat.format(msg, params);
170
171             throw new ServerLifecycleException(msg, ie);
172         }
173     }
174
175     private void postInvoke(InvocationManager invMgr)
176                             throws ServerLifecycleException {
177         try {
178             invMgr.postInvoke(lcmInvocation);
179         } catch (InvocationException ie) {
180             String JavaDoc msg = _rb.getString("lifecyclemodule.postInvoke_exception");
181             Object JavaDoc[] params = { this.name };
182             msg = MessageFormat.format(msg, params);
183
184             throw new ServerLifecycleException(msg, ie);
185         }
186     }
187
188     private void postEvent(int eventType, Object JavaDoc data, InvocationManager invMgr)
189                                     throws ServerLifecycleException {
190         if (slcl == null) {
191             if (isFatal) {
192                 String JavaDoc msg = _rb.getString("lifecyclemodule.loadExceptionIsFatal");
193                 Object JavaDoc[] params = { this.name };
194                 msg = MessageFormat.format(msg, params);
195
196                 throw new ServerLifecycleException(msg);
197             }
198
199             return;
200         }
201
202         if (urlClassLoader != null)
203             setClassLoader();
204
205         if (invMgr != null)
206             preInvoke(invMgr);
207
208         LifecycleEvent slcEvent= new LifecycleEvent(this, eventType, data, this.leContext);
209         try {
210             slcl.handleEvent(slcEvent);
211         } catch (ServerLifecycleException sle) {
212
213             String JavaDoc msg = _rb.getString("lifecyclemodule.event_ServerLifecycleException");
214
215             Object JavaDoc[] params = { this.name };
216             msg = MessageFormat.format(msg, params);
217
218             _logger.log(Level.WARNING, msg, sle);
219
220             if (isFatal)
221                 throw sle;
222         } catch (Exception JavaDoc ee) {
223             String JavaDoc msg = _rb.getString("lifecyclemodule.event_Exception");
224
225             Object JavaDoc[] params = { this.name };
226             msg = MessageFormat.format(msg, params);
227
228             _logger.log(Level.WARNING, msg, ee);
229
230             if (isFatal) {
231                 throw new ServerLifecycleException(_rb.getString("lifecyclemodule.event_exceptionIsFatal"), ee);
232             }
233         } finally {
234             if (invMgr != null)
235                 postInvoke(invMgr);
236         }
237     }
238     
239     public void onInitialization(ServerContext context)
240                                 throws ServerLifecycleException {
241         postEvent(LifecycleEvent.INIT_EVENT, props, null);
242     }
243
244     public void onStartup(ServerContext context, Context invContext)
245                                     throws ServerLifecycleException {
246
247         /** create a ComponentInvocation for this module during startup;
248          * as otherwise during Initialization ServerContext is not fully
249          * established.
250          */

251         lcmInvocation = new ComponentInvocation(slcl, invContext);
252         postEvent(LifecycleEvent.STARTUP_EVENT, null, ctx.getInvocationManager());
253     }
254     
255     public void onReady(ServerContext context) throws ServerLifecycleException {
256         postEvent(LifecycleEvent.READY_EVENT, null, ctx.getInvocationManager());
257     }
258
259     public void onShutdown() throws ServerLifecycleException {
260         postEvent(LifecycleEvent.SHUTDOWN_EVENT, null, ctx.getInvocationManager());
261     }
262     
263     public void onTermination() throws ServerLifecycleException {
264         postEvent(LifecycleEvent.TERMINATION_EVENT, null, ctx.getInvocationManager());
265
266         // clear the ComponentInvocation for this module
267
lcmInvocation = null;
268     }
269     
270     private void setClassLoader() {
271          // set the common class loader as the thread context class loader
272
java.security.AccessController.doPrivileged(
273             new java.security.PrivilegedAction JavaDoc() {
274                 public Object JavaDoc run() {
275                     Thread.currentThread().setContextClassLoader(urlClassLoader);
276                     return null;
277                 }
278             }
279         );
280     }
281
282     /**
283      * return status of this lifecycle module as a string
284      */

285     public String JavaDoc getStatus() {
286         return statusMsg;
287     }
288
289     public String JavaDoc toString() {
290         return "Server LifecycleListener support";
291     }
292 }
293
Popular Tags