FileDocCategorySizeDatePackage
StandardService.javaAPI DocGlassfish v2 API20162Fri May 04 22:31:54 BST 2007org.apache.catalina.core

StandardService

public class StandardService extends Object implements org.apache.catalina.Lifecycle, org.apache.catalina.Service, MBeanRegistration
Standard implementation of the Service interface. The associated Container is generally an instance of Engine, but this is not required.
author
Craig R. McClanahan

Fields Summary
private static com.sun.org.apache.commons.logging.Log
log
private static final String
info
Descriptive information about this component implementation.
private String
name
The name of this service.
private org.apache.catalina.util.LifecycleSupport
lifecycle
The lifecycle event support for this component.
private static final org.apache.catalina.util.StringManager
sm
The string manager for this package.
private org.apache.catalina.Server
server
The Server that owns this Service, if any.
private boolean
started
Has this component been started?
protected int
debug
The debugging detail level for this component.
protected PropertyChangeSupport
support
The property change support for this component.
protected org.apache.catalina.Connector[]
connectors
The set of Connectors associated with this Service.
protected org.apache.catalina.Container
container
The Container associated with this Service. (In the case of the org.apache.catalina.startup.Embedded subclass, this holds the most recently added Engine.)
protected boolean
initialized
Has this component been initialized?
protected String
type
protected String
domain
protected String
suffix
protected ObjectName
oname
protected ObjectName
controller
protected MBeanServer
mserver
Constructors Summary
Methods Summary
public voidaddConnector(org.apache.catalina.Connector connector)
Add a new Connector to the set of defined Connectors, and associate it with this Service's Container.

param
connector The Connector to be added


        synchronized (connectors) {
            connector.setContainer(this.container);
            connector.setService(this);
            Connector results[] = new Connector[connectors.length + 1];
            System.arraycopy(connectors, 0, results, 0, connectors.length);
            results[connectors.length] = connector;
            connectors = results;

            if (initialized) {
                try {
                    connector.initialize();
                } catch (LifecycleException e) {
                    log.error("Connector.initialize", e);
                }
            }

            if (started && (connector instanceof Lifecycle)) {
                try {
                    ((Lifecycle) connector).start();
                } catch (LifecycleException e) {
                    log.error("Connector.start", e);
                }
            }

            // Report this property change to interested listeners
            support.firePropertyChange("connector", null, connector);
        }

    
public voidaddLifecycleListener(org.apache.catalina.LifecycleListener listener)
Add a LifecycleEvent listener to this component.

param
listener The listener to add


        lifecycle.addLifecycleListener(listener);

    
public voidaddPropertyChangeListener(java.beans.PropertyChangeListener listener)
Add a property change listener to this component.

param
listener The listener to add


        support.addPropertyChangeListener(listener);

    
public voiddestroy()

        if( started ) stop();
        // unregister should be here probably
        // START CR 6368091
        if (initialized) {
            if( oname==controller ) {
                // we registered ourself on init().
                // That should be the typical case - this object is just for
                // backward compat, nobody should bother to load it explicitely
                Registry.getRegistry().unregisterComponent(oname);
            }
            initialized = false;
        }
        // END CR 6368091
    
public org.apache.catalina.Connector[]findConnectors()
Find and return the set of Connectors associated with this Service.


        return (connectors);

    
public org.apache.catalina.LifecycleListener[]findLifecycleListeners()
Get the lifecycle listeners associated with this lifecycle. If this Lifecycle has no listeners registered, a zero-length array is returned.


        return lifecycle.findLifecycleListeners();

    
public javax.management.ObjectName[]getConnectorNames()

        ObjectName results[] = new ObjectName[connectors.length];
        for( int i=0; i<results.length; i++ ) {
            // if it's a coyote connector
            //if( connectors[i] instanceof CoyoteConnector ) {
            //    results[i]=((CoyoteConnector)connectors[i]).getJmxName();
            //}
        }
        return results;
    
public org.apache.catalina.ContainergetContainer()
Return the Container that handles requests for all Connectors associated with this Service.



    // ------------------------------------------------------------- Properties


                      
       

        return (this.container);

    
public javax.management.ObjectNamegetContainerName()

        if( container instanceof ContainerBase ) {
            return ((ContainerBase)container).getJmxName();
        }
        return null;
    
public intgetDebug()
Return the debugging detail level of this component.


        return (this.debug);

    
public java.lang.StringgetDomain()

        return domain;
    
public java.lang.StringgetInfo()
Return descriptive information about this Service implementation and the corresponding version number, in the format <description>/<version>.


        return (this.info);

    
public java.lang.StringgetName()
Return the name of this Service.


        return (this.name);

    
public javax.management.ObjectNamegetObjectName()

        return oname;
    
public org.apache.catalina.ServergetServer()
Return the Server with which we are associated (if any).


        return (this.server);

    
public voidinit()

        try {
            initialize();
        } catch( Throwable t ) {
            log.error(sm.getString("standardService.initialize.failed",
                                   domain),t);
        }
    
public voidinitialize()
Invoke a pre-startup initialization. This is used to allow connectors to bind to restricted ports under Unix operating environments.

        // Service shouldn't be used with embeded, so it doesn't matter
        if (initialized) {
            log.info(sm.getString("standardService.initialize.initialized"));
            return;
        }
        initialized = true;

        if( oname==null ) {
            try {
                // Hack - Server should be deprecated...
                Container engine=this.getContainer();
                domain=engine.getName();
                oname=new ObjectName(domain + ":type=Service,serviceName="+name);
                this.controller=oname;
                Registry.getRegistry().registerComponent(this, oname, null);
            } catch (Exception e) {
                log.error(sm.getString("standardService.register.failed",
                                       domain),e);
            }
            
            
        }
        if( server==null ) {
            // Register with the server 
            // HACK: ServerFactory should be removed...
            
            ServerFactory.getServer().addService(this);
        }
               

        // Initialize our defined Connectors
        synchronized (connectors) {
                for (int i = 0; i < connectors.length; i++) {
                    connectors[i].initialize();
                }
        }
    
public voidpostDeregister()

    
public voidpostRegister(java.lang.Boolean registrationDone)

    
public voidpreDeregister()

    
public javax.management.ObjectNamepreRegister(javax.management.MBeanServer server, javax.management.ObjectName name)

        oname=name;
        mserver=server;
        domain=name.getDomain();
        return name;
    
public voidremoveConnector(org.apache.catalina.Connector connector)
Remove the specified Connector from the set associated from this Service. The removed Connector will also be disassociated from our Container.

param
connector The Connector to be removed

    // END SJSAS 6231069

        synchronized (connectors) {
            int j = -1;
            for (int i = 0; i < connectors.length; i++) {
                if (connector == connectors[i]) {
                    j = i;
                    break;
                }
            }
           
            if (j < 0)
                return;

            // START SJSAS 6231069
            /*if (started && (connectors[j] instanceof Lifecycle)) {
                try {
                   ((Lifecycle) connectors[j]).stop();
               } catch (LifecycleException e) {
                   log.error("Connector.stop", e);
               }
            }*/
            ((Lifecycle) connectors[j]).stop();
            // END SJSAS 6231069

            // START SJSAS 6231069
            /*connectors[j].setContainer(null);
            connector.setService(null);*/
            // END SJSAS 6231069           
            int k = 0;
            Connector results[] = new Connector[connectors.length - 1];
            for (int i = 0; i < connectors.length; i++) {
                if (i != j)
                    results[k++] = connectors[i];
            }
            connectors = results;

            // Report this property change to interested listeners
            support.firePropertyChange("connector", connector, null);
        }

    
public voidremoveLifecycleListener(org.apache.catalina.LifecycleListener listener)
Remove a LifecycleEvent listener from this component.

param
listener The listener to remove


        lifecycle.removeLifecycleListener(listener);

    
public voidremovePropertyChangeListener(java.beans.PropertyChangeListener listener)
Remove a property change listener from this component.

param
listener The listener to remove


        support.removePropertyChangeListener(listener);

    
public voidsetContainer(org.apache.catalina.Container container)
Set the Container that handles requests for all Connectors associated with this Service.

param
container The new Container


        Container oldContainer = this.container;
        if ((oldContainer != null) && (oldContainer instanceof Engine))
            ((Engine) oldContainer).setService(null);
        this.container = container;
        if ((this.container != null) && (this.container instanceof Engine))
            ((Engine) this.container).setService(this);
        if (started && (this.container != null) &&
            (this.container instanceof Lifecycle)) {
            try {
                ((Lifecycle) this.container).start();
            } catch (LifecycleException e) {
                ;
            }
        }
        synchronized (connectors) {
            for (int i = 0; i < connectors.length; i++)
                connectors[i].setContainer(this.container);
        }
        if (started && (oldContainer != null) &&
            (oldContainer instanceof Lifecycle)) {
            try {
                ((Lifecycle) oldContainer).stop();
            } catch (LifecycleException e) {
                ;
            }
        }

        // Report this property change to interested listeners
        support.firePropertyChange("container", oldContainer, this.container);

    
public voidsetDebug(int debug)
Set the debugging detail level of this component.

param
debug The new debugging detail level


        int oldDebug = this.debug;
        this.debug = debug;
        support.firePropertyChange("debug", Integer.valueOf(oldDebug),
                                   Integer.valueOf(this.debug));
    
public voidsetName(java.lang.String name)
Set the name of this Service.

param
name The new service name


        this.name = name;

    
public voidsetServer(org.apache.catalina.Server server)
Set the Server with which we are associated (if any).

param
server The server that owns this Service


        this.server = server;

    
public voidstart()
Prepare for the beginning of active use of the public methods of this component. This method should be called before any of the public methods of this component are utilized. It should also send a LifecycleEvent of type START_EVENT to any registered listeners.

exception
LifecycleException if this component detects a fatal error that prevents this component from being used


        // Validate and update our current component state
        if (started) {
            log.info(sm.getString("standardService.start.started"));
        }
        
        if( ! initialized )
            init(); 

        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);

        log.info(sm.getString("standardService.start.name", this.name));
        lifecycle.fireLifecycleEvent(START_EVENT, null);
        started = true;

        // Start our defined Container first
        if (container != null) {
            synchronized (container) {
                if (container instanceof Lifecycle) {
                    ((Lifecycle) container).start();
                }
            }
        }

        // Start our defined Connectors second
        synchronized (connectors) {
            for (int i = 0; i < connectors.length; i++) {
                if (connectors[i] instanceof Lifecycle)
                    ((Lifecycle) connectors[i]).start();
            }
        }

        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null);

    
public voidstop()
Gracefully terminate the active use of the public methods of this component. This method should be the last one called on a given instance of this component. It should also send a LifecycleEvent of type STOP_EVENT to any registered listeners.

exception
LifecycleException if this component detects a fatal error that needs to be reported


        // Validate and update our current component state
        if (!started) {
            return;
        }

        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(BEFORE_STOP_EVENT, null);

        lifecycle.fireLifecycleEvent(STOP_EVENT, null);

        log.info
            (sm.getString("standardService.stop.name", this.name));
        started = false;

        // Stop our defined Connectors first
        synchronized (connectors) {
            for (int i = 0; i < connectors.length; i++) {
                if (connectors[i] instanceof Lifecycle)
                    ((Lifecycle) connectors[i]).stop();
            }
        }

        // Stop our defined Container second
        if (container != null) {
            synchronized (container) {
                if (container instanceof Lifecycle) {
                    ((Lifecycle) container).stop();
                }
            }
        }

        /* CR 6368091
        if( oname==controller ) {
            // we registered ourself on init().
            // That should be the typical case - this object is just for
            // backward compat, nobody should bother to load it explicitely
            Registry.getRegistry().unregisterComponent(oname);
        }        
        */

        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(AFTER_STOP_EVENT, null);

    
public java.lang.StringtoString()
Return a String representation of this component.


        StringBuffer sb = new StringBuffer("StandardService[");
        sb.append(getName());
        sb.append("]");
        return (sb.toString());