Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 
 package org.apache.catalina.core;
 
 
 import static org.jboss.web.CatalinaMessages.MESSAGES;
 
 
 
Standard implementation of the Service interface. The associated Container is generally an instance of Engine, but this is not required.

Author(s):
Craig R. McClanahan
 
 
 public class StandardService
         implements LifecycleServiceMBeanRegistration 
  {

    
Alternate flag to enable delaying startup of connectors in embedded mode.
 
     public static final boolean DELAY_CONNECTOR_STARTUP =
         Boolean.valueOf(System.getProperty("org.apache.catalina.core.StandardService.DELAY_CONNECTOR_STARTUP""true")).booleanValue();
 
     // ----------------------------------------------------- Instance Variables
 

    
Descriptive information about this component implementation.
 
     private static final String info =
         "org.apache.catalina.core.StandardService/1.0";


    
The name of this service.
 
     private String name = null;


    
The lifecycle event support for this component.
 
     private LifecycleSupport lifecycle = new LifecycleSupport(this);

    
The Server that owns this Service, if any.
 
     private Server server = null;

    
Has this component been started?
 
     private boolean started = false;


    
The property change support for this component.
    protected PropertyChangeSupport support = new PropertyChangeSupport(this);


    
The set of Connectors associated with this Service.
    protected Connector connectors[] = new Connector[0];
    
    
    protected ArrayList<Executorexecutors = new ArrayList<Executor>();

    
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 Container container = null;

    
    
Mapper.
    protected Mapper mapper = new Mapper();


    
The associated mapper.
    protected ServiceMapperListener mapperListener = new ServiceMapperListener();
    

    
Has this component been initialized?
    protected boolean initialized = false;


    
A String initialization parameter used to increase the entropy of the initialization of our random number generator.
    protected String entropy = null;

    
    
The random associated with this service.
    protected SecureRandom random = null;
    // ------------------------------------------------------------- Properties


    
Return the Container that handles requests for all Connectors associated with this Service.
    public Container getContainer() {
        return (this.);
    }


    
Set the Container that handles requests for all Connectors associated with this Service.

Parameters:
container The new Container
    public void setContainer(Container container) {
        Container oldContainer = this.;
        if ((oldContainer != null) && (oldContainer instanceof Engine))
            ((EngineoldContainer).setService(null);
        this. = container;
        if ((this. != null) && (this. instanceof Engine))
            ((Enginethis.).setService(this);
        if ( && (this. != null) &&
            (this. instanceof Lifecycle)) {
            try {
                ((Lifecyclethis.).start();
            } catch (LifecycleException e) {
                ;
            }
        }
        synchronized () {
            for (int i = 0; i < .i++)
                [i].setContainer(this.);
        }
        if ( && (oldContainer != null) &&
            (oldContainer instanceof Lifecycle)) {
            try {
                ((LifecycleoldContainer).stop();
            } catch (LifecycleException e) {
                ;
            }
        }
        // Report this property change to interested listeners
        .firePropertyChange("container"oldContainerthis.);
    }
    public Mapper getMapper() {
        return ;
    }
    public ObjectName getContainerName() {
        if instanceof ContainerBase ) {
            return ((ContainerBase)).getJmxName();
        }
        return null;
    }


    
Return descriptive information about this Service implementation and the corresponding version number, in the format <description>/<version>.
    public String getInfo() {
        return ();
    }


    
Return the name of this Service.
    public String getName() {
        return (this.);
    }


    
Set the name of this Service.

Parameters:
name The new service name
    public void setName(String name) {
        this. = name;
    }


    
Return the Server with which we are associated (if any).
    public Server getServer() {
        return (this.);
    }


    
Set the Server with which we are associated (if any).

Parameters:
server The server that owns this Service
    public void setServer(Server server) {
        this. = server;
    }
    @Override
    public String getEntropy() {
        // Calculate a semi-useful value if this has not been set
        if (this. == null) {
            // Use APR to get a crypto secure entropy value
            byte[] result = new byte[32];
            boolean apr = false;
            try {
                String methodName = "random";
                Class paramTypes[] = new Class[2];
                paramTypes[0] = result.getClass();
                paramTypes[1] = int.class;
                Object paramValues[] = new Object[2];
                paramValues[0] = result;
                paramValues[1] = new Integer(32);
                Method method = Class.forName("org.apache.tomcat.jni.OS")
                    .getMethod(methodNameparamTypes);
                method.invoke(nullparamValues);
                apr = true;
            } catch (Throwable t) {
                // Ignore
            }
            if (apr) {
                setEntropy(new String(Base64.encode(result)));
            }
        }
        return (this.);
    }
    @Override
    public void setEntropy(String entropy) {
        this. = entropy;
    }
    // --------------------------------------------------------- Public Methods
    @Override
    public SecureRandom getRandom() {
        return ;
    }


    
Add a new Connector to the set of defined Connectors, and associate it with this Service's Container.

Parameters:
connector The Connector to be added
    public void addConnector(Connector connector) {
        synchronized () {
            connector.setContainer(this.);
            connector.setService(this);
            Connector results[] = new Connector[. + 1];
            System.arraycopy(, 0, results, 0, .);
            results[.] = connector;
             = results;
            if (!) {
                if () {
                    try {
                        connector.init();
                    } catch (LifecycleException e) {
                        ..errorInitializingConnector(e);
                    }
                }
                if ( && (connector instanceof Lifecycle)) {
                    try {
                        ((Lifecycleconnector).start();
                    } catch (LifecycleException e) {
                        ..errorStartingConnector(e);
                    }
                }
            }
            // Report this property change to interested listeners
            .firePropertyChange("connector"nullconnector);
        }
    }
    public ObjectName[] getConnectorNames() {
        ObjectName results[] = new ObjectName[.];
        for (int i=0; i<results.lengthi++) {
            results[i] = [i].getObjectName();
        }
        return results;
    }


    
Add a property change listener to this component.

Parameters:
listener The listener to add
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        .addPropertyChangeListener(listener);
    }


    
Find and return the set of Connectors associated with this Service.
    public Connector[] findConnectors() {
        return ();
    }


    
Remove the specified Connector from the set associated from this Service. The removed Connector will also be disassociated from our Container.

Parameters:
connector The Connector to be removed
    public void removeConnector(Connector connector) {
        synchronized () {
            int j = -1;
            for (int i = 0; i < .i++) {
                if (connector == [i]) {
                    j = i;
                    break;
                }
            }
            if (j < 0)
                return;
            if (!) {
                if ( && ([jinstanceof Lifecycle)) {
                    try {
                        ((Lifecycle[j]).stop();
                    } catch (LifecycleException e) {
                        ..errorStoppingConnector(e);
                    }
                }
            }
            [j].setContainer(null);
            connector.setService(null);
            int k = 0;
            Connector results[] = new Connector[. - 1];
            for (int i = 0; i < .i++) {
                if (i != j)
                    results[k++] = [i];
            }
             = results;
            // Report this property change to interested listeners
            .firePropertyChange("connector"connectornull);
        }
    }


    
Remove a property change listener from this component.

Parameters:
listener The listener to remove
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        .removePropertyChangeListener(listener);
    }


    
Return a String representation of this component.
    public String toString() {
        StringBuilder sb = new StringBuilder("StandardService[");
        sb.append(getName());
        sb.append("]");
        return (sb.toString());
    }
    // ------------------------------------------------------ Lifecycle Methods


    
Add a LifecycleEvent listener to this component.

Parameters:
listener The listener to add
    public void addLifecycleListener(LifecycleListener listener) {
        .addLifecycleListener(listener);
    }


    
Get the lifecycle listeners associated with this lifecycle. If this Lifecycle has no listeners registered, a zero-length array is returned.
        return .findLifecycleListeners();
    }


    
Remove a LifecycleEvent listener from this component.

Parameters:
listener The listener to remove
    public void removeLifecycleListener(LifecycleListener listener) {
        .removeLifecycleListener(listener);
    }
    
    
Adds a named executor to the service

Parameters:
ex Executor
    public void addExecutor(Executor ex) {
        synchronized () {
            if (!.contains(ex)) {
                .add(ex);
                if ()
                    try {
                        ex.start();
                    } catch (LifecycleException x) {
                        ..errorStartingExecutor(x);
                    }
            }
        }
    }

    
Retrieves all executors

Returns:
Executor[]
    public Executor[] findExecutors() {
        synchronized () {
            Executor[] arr = new Executor[.size()];
            .toArray(arr);
            return arr;
        }
    }

    
Retrieves executor by name, null if not found

Parameters:
name String
Returns:
Executor
    public Executor getExecutor(String name) {
        synchronized () {
            for (int i = 0; i < .size(); i++) {
                if (name.equals(.get(i).getName()))
                    return .get(i);
            }
        }
        return null;
    }

    
Removes an executor from the service

Parameters:
ex Executor
    public void removeExecutor(Executor ex) {
        synchronized () {
            if ( .remove(ex) &&  ) {
                try {
                    ex.stop();
                } catch (LifecycleException e) {
                    ..errorStoppingExecutor(e);
                }
            }
        }
    }



    
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.

Throws:
org.apache.catalina.LifecycleException if this component detects a fatal error that prevents this component from being used
    public void start() throws LifecycleException {
        // Validate and update our current component state
        if () {
            return;
        }
        
        if( !  )
            init(); 
        // Notify our interested LifecycleListeners
         = true;
        // Start our defined Container first
        if ( != null) {
            synchronized () {
                if ( instanceof Lifecycle) {
                    ((Lifecycle).start();
                }
            }
        }
        synchronized () {
            for ( int i=0; i<.size(); i++ ) {
                .get(i).start();
            }
        }
        // Start our defined Connectors second
        if (!) {
        	synchronized () {
        		for (int i = 0; i < .i++) {
        			if ([iinstanceof Lifecycle)
        				((Lifecycle[i]).start();
        		}
        	}
        }
        
        // Notify our interested LifecycleListeners
    }


    
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.

Throws:
org.apache.catalina.LifecycleException if this component detects a fatal error that needs to be reported
    public void stop() throws LifecycleException {
        // Validate and update our current component state
        if (!) {
            return;
        }
        // Notify our interested LifecycleListeners
        // Stop our defined Connectors first
        if (!) {
            synchronized () {
                for (int i = 0; i < .i++) {
                    [i].pause();
                }
            }
        }
        .fireLifecycleEvent(null);
         = false;
        // Stop our defined Container second
        if ( != null) {
            synchronized () {
                if ( instanceof Lifecycle) {
                    ((Lifecycle).stop();
                }
            }
        }
        // Stop our defined Connectors first
        if (!) {
            synchronized () {
                for (int i = 0; i < .i++) {
                    if ([iinstanceof Lifecycle)
                        ((Lifecycle[i]).stop();
                }
            }
        }
        synchronized () {
            for ( int i=0; i<.size(); i++ ) {
                .get(i).stop();
            }
        }
        if (.....) {
            if== ) {
                // 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(nullnull).unregisterComponent();
                Executor[] executors = findExecutors();
                for (int i = 0; i < executors.lengthi++) {
                    try {
                        ObjectName executorObjectName = 
                            new ObjectName( + ":type=Executor,name=" + executors[i].getName());
                        Registry.getRegistry(nullnull).unregisterComponent(executorObjectName);
                    } catch (Exception e) {
                        // Ignore (invalid ON, which cannot happen)
                    }
                }
            }
        }        
        // Notify our interested LifecycleListeners
    }


    
Invoke a pre-startup initialization. This is used to allow connectors to bind to restricted ports under Unix operating environments.
    public void initialize()
            throws LifecycleException
    {
        // Service shouldn't be used with embeded, so it doesn't matter
        if () {
            return;
        }
         = true;
        if (.....) {
            if==null ) {
                try {
                    // Hack - Server should be deprecated...
                    Container engine=this.getContainer();
                    =engine.getName();
                    =new ObjectName( + ":type=Service,serviceName="+);
                    this.=;
                    Registry.getRegistry(nullnull)
                    .registerComponent(thisnull);
                    Executor[] executors = findExecutors();
                    for (int i = 0; i < executors.lengthi++) {
                        ObjectName executorObjectName = 
                            new ObjectName( + ":type=Executor,name=" + executors[i].getName());
                        Registry.getRegistry(nullnull)
                        .registerComponent(executors[i], executorObjectNamenull);
                    }
                } catch (Exception e) {
                    ..failedServiceJmxRegistration(e);
                }
            }
        }
        if==null ) {
            // If no server was defined - create one
             = new StandardServer();
            .addService(this);
        }
               
        
        // Initialize our defined Connectors
        if (!) {
            synchronized () {
                for (int i = 0; i < .i++) {
                    [i].init();
                }
            }
        }
        // Construct and seed a new random number generator
        String entropy = getEntropy();
        if (entropy != null) {
             = new SecureRandom(getEntropy().getBytes());
        } else {
             = new SecureRandom();
        }
    }
    
    public void destroy() throws LifecycleException {
        if ) stop();
        // FIXME unregister should be here probably -- stop doing that ?
    }
    public void init() {
        try {
            initialize();
        } catchThrowable t ) {
        }
    }
    protected String type;
    protected String domain;
    protected String suffix;
    protected ObjectName oname;
    protected ObjectName controller;
    protected MBeanServer mserver;
    public ObjectName getObjectName() {
        return ;
    }
    public String getDomain() {
        return ;
    }
    public ObjectName preRegister(MBeanServer server,
                                  ObjectName namethrows Exception {
        =name;
        =server;
        =name.getDomain();
        return name;
    }
    public void postRegister(Boolean registrationDone) {
    }
    public void preDeregister() throws Exception {
    }
    public void postDeregister() {
    }
New to GrepCode? Check out our FAQ X