1 23 24 package com.sun.enterprise.server.ondemand; 25 26 import java.util.*; 27 import java.util.logging.Level ; 28 import java.util.logging.Logger ; 29 import java.security.AccessController ; 30 import java.security.PrivilegedExceptionAction ; 31 import java.security.PrivilegedActionException ; 32 import com.sun.logging.LogDomains; 33 import com.sun.appserv.server.ServerLifecycle; 34 import com.sun.appserv.server.ServerLifecycleException; 35 import com.sun.enterprise.server.PEMain; 36 import com.sun.enterprise.server.ondemand.entry.*; 37 import com.sun.enterprise.config.ConfigException; 38 import com.sun.enterprise.server.ServerContext; 39 import com.sun.enterprise.InvocationManager; 40 import com.sun.enterprise.ComponentInvocation; 41 import com.sun.enterprise.Switch; 42 43 58 public abstract class ServiceGroup { 59 60 protected static Logger _logger = LogDomains.getLogger(LogDomains.CORE_LOGGER); 61 private ArrayList sgList = new ArrayList(); 62 63 public static final int NOTSTARTED = 0; 64 public static final int STARTING = 1; 65 public static final int STARTED = 2; 66 public static final int STOPPING = 8; 67 public static final int STOPPED = 9; 68 69 private int state = NOTSTARTED; 70 71 private ServerLifecycle[] services = {}; 72 private static ArrayList<ServiceGroupListener> listeners; 73 74 static { 75 listeners = new ArrayList(); 76 String extListener = 77 System.getProperty("com.sun.enterprise.server.ondemand.ExternalListener"); 78 if(extListener != null) { 79 try { 80 ServiceGroupListener servicegrouplistener = 81 (ServiceGroupListener)Class.forName(extListener).newInstance(); 82 listeners.add(servicegrouplistener); 83 } catch(Throwable t) { 84 t.printStackTrace(); 85 } 86 } 87 } 88 89 private void notifyListener (int state, ServiceGroup sg) { 90 for (ServiceGroupListener l : listeners) { 91 try { 92 switch (state) { 93 case STARTING : 94 l.beforeStart(sg.getClass().getName()); 95 break; 96 case STARTED : 97 l.afterStart(sg.getClass().getName()); 98 } 99 } catch (Throwable t) { 100 t.printStackTrace(); 101 } 102 } 103 } 104 105 public static void addServiceGroupListener(ServiceGroupListener listener) { 106 listeners.add(listener); 107 } 108 109 public static void removeServiceGroupListener(ServiceGroupListener listener) { 110 listeners.remove(listener); 111 } 112 113 116 public void addServiceGroup(ServiceGroup group) { 117 sgList.add(group); 118 } 119 120 123 public void removeServiceGroup(ServiceGroup group) { 124 sgList.remove(group); 125 } 126 127 130 public Iterator serviceGroupIterator() { 131 return sgList.iterator(); 132 } 133 134 139 public boolean isNotified(EntryContext context) { 140 Iterator it = serviceGroupIterator(); 141 while (it.hasNext()) { 142 final ServiceGroup sg = (ServiceGroup) it.next(); 143 if (sg.getState() != STARTED) { 144 if (sg.analyseEntryContext(context)) { 145 return false; 146 } 147 } 148 } 149 return true; 150 } 151 152 157 public void startChildren(final EntryContext context) throws ServiceGroupException { 158 Iterator it = serviceGroupIterator(); 159 while (it.hasNext()) { 160 final ServiceGroup sg = (ServiceGroup) it.next(); 161 162 if (_logger.isLoggable(Level.FINER)) { 163 _logger.finer("Trying " + sg + " servicegroup"); 164 } 165 166 if (sg.getState() != STARTED) { 167 if (sg.analyseEntryContext(context)) { 168 synchronized (sg) { 169 if (sg.getState() == NOTSTARTED) { 170 sg.setState(STARTING); 171 if (_logger.isLoggable(Level.FINE)) { 172 _logger.fine("Starting " + sg + " servicegroup with context :" + context); 173 } 174 notifyListener(STARTING, sg); 175 ComponentInvocation dummy = preInvoke(); 176 try { 177 AccessController.doPrivileged 178 (new PrivilegedExceptionAction () { 179 public Object run() throws Exception { 180 sg.start(context); 181 return null; 182 } 183 }); 184 } catch (PrivilegedActionException pae) { 185 throw new ServiceGroupException( 186 pae.getException()); 187 } finally { 188 postInvoke(dummy); 189 } 190 notifyListener(STARTED, sg); 191 sg.setState(STARTED); 192 } 193 } 194 } 195 } 196 } 197 } 198 199 210 private ComponentInvocation preInvoke() { 211 InvocationManager im = Switch.getSwitch().getInvocationManager(); 212 ComponentInvocation dummy = 213 new ComponentInvocation(ComponentInvocation.SERVICE_STARTUP); 214 im.preInvoke(dummy); 215 return dummy; 216 } 217 218 221 private void postInvoke(ComponentInvocation dummy) { 222 InvocationManager im = Switch.getSwitch().getInvocationManager(); 223 im.postInvoke(dummy); 224 } 225 226 public void stopChildren(EntryContext context) throws ServiceGroupException { 228 Iterator it = serviceGroupIterator(); 229 while (it.hasNext()) { 230 ServiceGroup sg = (ServiceGroup) it.next(); 231 if (sg.getState() != STOPPED && sg.getState() != NOTSTARTED) { 232 sg.stop(context); 233 } 234 } 235 } 236 237 public void abortChildren(EntryContext context) { 239 Iterator it = serviceGroupIterator(); 240 while (it.hasNext()) { 241 ServiceGroup sg = (ServiceGroup) it.next(); 242 if (sg.getState() != STOPPED) { 243 sg.abort(context); 244 } 245 } 246 } 247 248 protected void startLifecycleServices(String [][] s, ServerContext sc) { 251 services = new ServerLifecycle[s.length]; 252 for (int i =0; i < s.length; i ++) { 253 try { 254 String service = s[i][1]; 255 ServerLifecycle slc = (ServerLifecycle) 256 Class.forName(service).newInstance(); 257 services[i] = slc; 258 slc.onInitialization(sc); 259 } catch (Exception e) { 260 _logger.log(Level.WARNING, e.getMessage(), e); 261 } 262 } 263 264 for (ServerLifecycle slc : services) { 265 try { 266 slc.onStartup(sc); 267 } catch (Exception e) { 268 e.printStackTrace(); 269 } 270 } 271 272 for (ServerLifecycle slc : services) { 273 try { 274 slc.onReady(sc); 275 } catch (Exception e) { 276 _logger.log(Level.WARNING, e.getMessage(), e); 277 } 278 } 279 } 280 281 protected void stopLifecycleServices() { 284 for (ServerLifecycle slc : services) { 285 try { 286 slc.onShutdown(); 287 } catch (Exception e) { 288 _logger.log(Level.WARNING, e.getMessage(), e); 289 } 290 } 291 292 for (ServerLifecycle slc : services) { 293 try { 294 slc.onTermination(); 295 } catch (Exception e) { 296 _logger.log(Level.WARNING, e.getMessage(), e); 297 } 298 } 299 } 300 301 309 public abstract void start(EntryContext context) throws ServiceGroupException ; 310 311 319 public abstract boolean analyseEntryContext(EntryContext context); 320 321 324 public abstract void stop(EntryContext context) throws ServiceGroupException; 325 326 329 public abstract void abort(EntryContext context); 330 331 334 public void setState(int state) { 335 this.state = state; 336 } 337 338 341 public int getState() { 342 return this.state; 343 } 344 } 345 | Popular Tags |