1 23 24 package com.sun.appserv.server; 25 26 import java.net.URLClassLoader ; 27 import java.net.URL ; 28 import java.io.PrintWriter ; 29 import java.io.StringWriter ; 30 import java.util.logging.Logger ; 31 import java.util.logging.Level ; 32 import java.util.Properties ; 33 import java.util.ResourceBundle ; 34 35 import java.text.MessageFormat ; 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 52 53 public final class ServerLifecycleModule { 54 55 private LifecycleListener slcl; 56 private String name; 57 private String className; 58 private String classpath; 59 private int loadOrder; 60 private boolean isFatal = true; 61 private String statusMsg = "OK"; 62 63 private ServerContext ctx; 64 private LifecycleEventContext leContext; 65 private ClassLoader urlClassLoader; 66 private Properties props = new Properties (); 67 68 private static Logger _logger = null; 69 private static boolean _isTraceEnabled = false; 70 private static ResourceBundle _rb = null; 71 72 private ComponentInvocation lcmInvocation; 73 74 ServerLifecycleModule(ServerContext ctx, String name, String 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 classpath) { 86 this.classpath = classpath; 87 } 88 89 void setProperty(String name, String value) { 90 props.put(name, value); 91 } 92 93 Properties 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 getName() { 106 return this.name; 107 } 108 109 String getClassName() { 110 return this.className; 111 } 112 String 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 classLoader = ctx.getLifecycleParentClassLoader(); 125 126 try { 127 if (this.classpath != null) { 128 URL [] urls = getURLs(); 129 130 if (urls != null) { 131 StringBuffer sb = new StringBuffer (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 (urls, classLoader); 141 classLoader = this.urlClassLoader; 142 } 143 144 Class cl = Class.forName(className, true, classLoader); 145 slcl = (LifecycleListener) cl.newInstance(); 146 } catch (Exception ee) { 147 String msg = _rb.getString("lifecyclemodule.load_exception"); 148 Object [] 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 [] 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 msg = _rb.getString("lifecyclemodule.preInvoke_exception"); 168 Object [] 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 msg = _rb.getString("lifecyclemodule.postInvoke_exception"); 181 Object [] 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 data, InvocationManager invMgr) 189 throws ServerLifecycleException { 190 if (slcl == null) { 191 if (isFatal) { 192 String msg = _rb.getString("lifecyclemodule.loadExceptionIsFatal"); 193 Object [] 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 msg = _rb.getString("lifecyclemodule.event_ServerLifecycleException"); 214 215 Object [] 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 ee) { 223 String msg = _rb.getString("lifecyclemodule.event_Exception"); 224 225 Object [] 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 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 lcmInvocation = null; 268 } 269 270 private void setClassLoader() { 271 java.security.AccessController.doPrivileged( 273 new java.security.PrivilegedAction () { 274 public Object run() { 275 Thread.currentThread().setContextClassLoader(urlClassLoader); 276 return null; 277 } 278 } 279 ); 280 } 281 282 285 public String getStatus() { 286 return statusMsg; 287 } 288 289 public String toString() { 290 return "Server LifecycleListener support"; 291 } 292 } 293 | Popular Tags |