1 23 24 package com.sun.appserv.server; 25 26 import java.util.HashSet ; 27 import java.util.Set ; 28 import java.util.ArrayList ; 29 import java.util.Iterator ; 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 57 58 public class LifecycleModuleService implements ServerLifecycle { 59 60 static final String DELAY_LIFECYCLE_INIT = "com.sun.enterprise.server.DelayLifecycleInit"; 61 static String lifeCycleVersion = System.getProperty(DELAY_LIFECYCLE_INIT, "false"); 62 protected static boolean delayLifeCycleInit = new Boolean (lifeCycleVersion); 63 64 67 private ArrayList listeners = new ArrayList (); 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 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 listenerSet = new HashSet (); 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 strOrder = next.getLoadOrder(); 97 if (strOrder != null) { 98 try { 99 order = Integer.parseInt(strOrder); 100 } catch(NumberFormatException 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 ce1) { 125 ce1.printStackTrace(); 127 } 128 129 initialize(context); 130 } 131 132 141 private boolean isEnabled(LifecycleModule lcm, ConfigContext config) { 142 143 try { 144 if (lcm == null || config == null) { 146 return false; 147 } 148 149 Server server = ServerBeansFactory.getServerBean(config); 151 ApplicationRef appRef=server.getApplicationRefByRef(lcm.getName()); 152 153 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 c) { 163 java.security.AccessController.doPrivileged( 165 new java.security.PrivilegedAction () { 166 public Object run() { 167 Thread.currentThread().setContextClassLoader(c); 168 return null; 169 } 170 } 171 ); 172 } 173 174 private void sortModules(HashSet listenerSet) { 175 for(Iterator 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 cl = Thread.currentThread().getContextClassLoader(); 196 for(Iterator iter = listeners.iterator(); iter.hasNext();) { 197 ServerLifecycleModule next = (ServerLifecycleModule) iter.next(); 198 next.onInitialization(context); 199 } 200 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 cl = Thread.currentThread().getContextClassLoader(); 214 for(Iterator iter = listeners.iterator(); iter.hasNext();) { 215 ServerLifecycleModule next = (ServerLifecycleModule) iter.next(); 216 217 220 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 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 cl = Thread.currentThread().getContextClassLoader(); 244 for(Iterator iter = listeners.iterator(); iter.hasNext();) { 245 ServerLifecycleModule next = (ServerLifecycleModule) iter.next(); 246 next.onReady(context); 247 } 248 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 cl = Thread.currentThread().getContextClassLoader(); 262 for(Iterator iter = listeners.iterator(); iter.hasNext();) { 263 ServerLifecycleModule next = (ServerLifecycleModule) iter.next(); 264 next.onShutdown(); 265 } 266 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 cl = Thread.currentThread().getContextClassLoader(); 280 for(Iterator iter = listeners.iterator(); iter.hasNext();) { 281 ServerLifecycleModule next = (ServerLifecycleModule) iter.next(); 282 next.onTermination(); 283 } 284 resetClassLoader(cl); 286 } 287 } 288 | Popular Tags |