FileDocCategorySizeDatePackage
ServiceGroup.javaAPI DocGlassfish v2 API12890Fri May 04 22:35:48 BST 2007com.sun.enterprise.server.ondemand

ServiceGroup

public abstract class ServiceGroup extends Object
Super class of all servicegroups. Service group is a collection of services in application server. Service group design follows a variant of composite design pattern. Thus each servicegroup is a composite of other servicegroup. This super class basically has the logic to handle the servicegroup state and a general logic to handle the children of the servicegroup.
see
ServiceGroupBuilder
see
MainServiceGroup
see
EjbServiceGroup
see
WebServiceGroup
see
ResourcesServiceGroup

Fields Summary
protected static final Logger
_logger
private final ArrayList
sgList
public static final int
NOTSTARTED
public static final int
STARTING
public static final int
STARTED
public static final int
STOPPING
public static final int
STOPPED
private int
state
private com.sun.appserv.server.ServerLifecycle[]
services
private static final ArrayList
listeners
Constructors Summary
Methods Summary
public abstract voidabort(EntryContext context)
Abort the servicegroup. Unused. For future!

public voidabortChildren(EntryContext context)

        Iterator it = serviceGroupIterator();
        while (it.hasNext()) {
            ServiceGroup sg = (ServiceGroup) it.next();
            if (sg.getState() != STOPPED) {
                sg.abort(context);
            }
        }
    
public voidaddServiceGroup(com.sun.enterprise.server.ondemand.ServiceGroup group)
Add a servicegroup as child of this servicegroup.

        sgList.add(group);
    
public static voidaddServiceGroupListener(ServiceGroupListener listener)

        listeners.add(listener);
    
public abstract booleananalyseEntryContext(EntryContext context)
Analyse the entrycontext and specifies whether this servicegroup can be started or not.

return
boolean If true is returned, this servicegroup can be started If false is returned, the entrycontext is not recognized by the servicegroup.

public intgetState()
Return the state of the servicegroup.

        return this.state;
    
public booleanisNotified(EntryContext context)
If atleast one of the servicegroup that need to be started is interested in this context, then the servicegroup is not notified.

        Iterator it = serviceGroupIterator();
        while (it.hasNext()) {
            final ServiceGroup sg = (ServiceGroup) it.next();
            if (sg.getState() != STARTED) {
                if (sg.analyseEntryContext(context)) {
                     return false;
                }
            }
        }
        return true;
    
private voidnotifyListener(int state, com.sun.enterprise.server.ondemand.ServiceGroup sg)


     
        listeners = new ArrayList();
        String extListener = 
        System.getProperty("com.sun.enterprise.server.ondemand.ExternalListener");
        if(extListener != null) {
            try {
                ServiceGroupListener servicegrouplistener = 
                (ServiceGroupListener)Class.forName(extListener).newInstance();
                listeners.add(servicegrouplistener);
            } catch(Throwable t) {
                t.printStackTrace();
            }
        }
    
        for (ServiceGroupListener l : listeners) {
            try {
                switch (state) {
                    case STARTING :
                        l.beforeStart(sg.getClass().getName());
                        break;
                    case STARTED :
                        l.afterStart(sg.getClass().getName());
                }
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    
private voidpostInvoke(com.sun.enterprise.ComponentInvocation dummy)
See the comment on preInvoke.

        InvocationManager im = Switch.getSwitch().getInvocationManager();
        im.postInvoke(dummy);
    
private com.sun.enterprise.ComponentInvocationpreInvoke()
The preInvoke is used to insert a dummy invocation context to the invocation chain for everything that happens within the service startup. This is detected by the InvocationManager and the threads created from within this context will not inherit the context of the parent. This is required since an application thread might end up in starting a container. Thread pools managed by that container should not inherit the properties by the application, since both are unrelated.

        InvocationManager im = Switch.getSwitch().getInvocationManager();
        ComponentInvocation dummy = 
        new ComponentInvocation(ComponentInvocation.SERVICE_STARTUP);
        im.preInvoke(dummy);
        return dummy;
    
public voidremoveServiceGroup(com.sun.enterprise.server.ondemand.ServiceGroup group)
Remove a servicegroup from its children.

        sgList.remove(group);
    
public static voidremoveServiceGroupListener(ServiceGroupListener listener)

        listeners.remove(listener);
    
public java.util.IteratorserviceGroupIterator()
Iterator of children.

        return sgList.iterator();
    
public voidsetState(int state)
Set the state of the servicegroup.

        this.state = state;
    
public abstract voidstart(EntryContext context)
Triggers the start of the servicegroup. The entry context that caused this startup is used by the servicegroup to obtain any startup information it require.

param
context EntryContext object.
see
EntryContext.

public voidstartChildren(EntryContext context)
Logic to start all children based on entrycontext, If any of the child recognises the entrycontext, then this will attempt to start that servicegroup.

        Iterator it = serviceGroupIterator();
        while (it.hasNext()) {
            final ServiceGroup sg = (ServiceGroup) it.next();

            if (_logger.isLoggable(Level.FINER)) {
                _logger.finer("Trying " + sg + " servicegroup");
            }

            if (sg.getState() != STARTED) {
                if (sg.analyseEntryContext(context)) {
                    synchronized (sg) {
                        if (sg.getState() == NOTSTARTED) {
                            sg.setState(STARTING);
                            if (_logger.isLoggable(Level.FINE)) {
                               _logger.fine("Starting " + sg + " servicegroup with context :" + context);
                            }
                            notifyListener(STARTING, sg);
                            ComponentInvocation dummy = preInvoke();
                            try {
                                AccessController.doPrivileged
                                    (new PrivilegedExceptionAction() {
                                    public Object run() throws Exception {
                                        sg.start(context);
                                        return null;
                                    }
                                });
                            } catch (PrivilegedActionException pae) {
                                throw new ServiceGroupException(
                                      pae.getException());
                            } finally {
                                postInvoke(dummy);
                            }
                            notifyListener(STARTED, sg);
                            sg.setState(STARTED);
                        }
                    }
                }
            }
        }
    
protected voidstartLifecycleServices(java.lang.String[][] s, com.sun.enterprise.server.ServerContext sc)

        services = new ServerLifecycle[s.length];
        for (int i =0; i < s.length; i ++) {
            try {
                String service = s[i][1];
                ServerLifecycle slc = (ServerLifecycle) 
                Class.forName(service).newInstance();
                services[i] = slc;
                slc.onInitialization(sc);
            } catch (Exception e) {
                _logger.log(Level.WARNING, e.getMessage(), e);
            }
        }

        for (ServerLifecycle slc : services) {
            try {
                slc.onStartup(sc);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        for (ServerLifecycle slc : services) {
            try {
                slc.onReady(sc);
            } catch (Exception e) {
                _logger.log(Level.WARNING, e.getMessage(), e);
            }
        }
    
public abstract voidstop(EntryContext context)
Stop the servicegroup.

public voidstopChildren(EntryContext context)

        Iterator it = serviceGroupIterator();
        while (it.hasNext()) {
            ServiceGroup sg = (ServiceGroup) it.next();
            if (sg.getState() != STOPPED && sg.getState() != NOTSTARTED) {
                sg.stop(context);
            }
        }
    
protected voidstopLifecycleServices()

        for (ServerLifecycle slc : services) {
            try {
                slc.onShutdown();
            } catch (Exception e) {
                _logger.log(Level.WARNING, e.getMessage(), e);
            }
        }

        for (ServerLifecycle slc : services) {
            try {
                slc.onTermination();
            } catch (Exception e) {
                _logger.log(Level.WARNING, e.getMessage(), e);
            }
        }