View Javadoc

    1   package org.apache.turbine.services;
    2
    3
    4   /*
    5    * Licensed to the Apache Software Foundation (ASF) under one
    6    * or more contributor license agreements.  See the NOTICE file
    7    * distributed with this work for additional information
    8    * regarding copyright ownership.  The ASF licenses this file
    9    * to you under the Apache License, Version 2.0 (the
    10   * "License"); you may not use this file except in compliance
    11   * with the License.  You may obtain a copy of the License at
    12   *
    13   *   http://www.apache.org/licenses/LICENSE-2.0
    14   *
    15   * Unless required by applicable law or agreed to in writing,
    16   * software distributed under the License is distributed on an
    17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    18   * KIND, either express or implied.  See the License for the
    19   * specific language governing permissions and limitations
    20   * under the License.
    21   */
    22
    23
    24  import org.apache.turbine.pipeline.PipelineData;
    25  import org.apache.turbine.util.RunData;
    26
    27  /**
    28   * <p>This class provides a <code>Service</code> implementation that
    29   * Services used in Turbine are required to extend.  The
    30   * functionality provided in addition to <code>BaseService</code>
    31   * functionality is recognizing objects used in early initialization
    32   * of <code>Services</code> in Turbine, and passing them to
    33   * appropriate convenience methods.  These methods should be overridden
    34   * to provide desired initialization functionality.</p>
    35   *
    36   * <p><strong>Note!</strong><br>Remember to call
    37   * <code>setInit(true)</code> after successful initialization.</p>
    38   *
    39   * <p><strong>Note!</strong><br>If you need to use another
    40   * <code>Service</code> inside your early initialization, remember to
    41   * request initialization of that <code>Service</code> before using
    42   * it:</p>
    43   *
    44   * <pre>
    45   * getServiceBroker().initClass("OtherService",data);
    46   * OtherService service =
    47   *         (OtherService)getServiceBroker().getService("OtherService");
    48   * </pre>
    49   *
    50   * @author <a href="mailto:greg@shwoop.com">Greg Ritter</a>
    51   * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
    52   * @author <a href="mailto:burton@apache.org">Kevin Burton</a>
    53   * @author <a href="mailto:krzewski@e-point.pl">Rafal Krzewski</a>
    54   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
    55   * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
    56   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
    57   * @version $Id: TurbineBaseService.java 1773378 2016-12-09 13:19:59Z tv $
    58   */
    59  public abstract class TurbineBaseService
    60          extends BaseService
    61  {
    62      /**
    63       * Performs early initialization.  Overrides init() method in
    64       * BaseService to detect objects used in Turbine's Service
    65       * initialization and pass them to appropriate init() methods.
    66       *
    67       * @param data An Object to use for initialization activities.
    68       * @throws InitializationException if initialization of this
    69       * class was not successful.
    70       */
    71      @Override
    72      public void init(Object data)
    73              throws InitializationException
    74      {
    75          if (data instanceof RunData)
    76          {
    77              init((RunData) data);
    78          }
    79          else if (data instanceof PipelineData)
    80          {
    81              init((PipelineData) data);
    82          }
    83      }
    84
    85      /**
    86       * Performs early initialization.
    87       *
    88       * @param pipelineData A PipelineData to use for initialization activities.
    89       * @throws InitializationException if initialization of this
    90       * class was not successful.
    91       */
    92      public void init(PipelineData pipelineData) throws InitializationException
    93      {
    94          // empty
    95      }
    96
    97      /**
    98       * Performs late initialization.
    99       *
    100      * If your class relies on early initialization, and the object it
    101      * expects was not received, you can use late initialization to
    102      * throw an exception and complain.
    103      *
    104      * @throws InitializationException if initialization of this
    105      * class was not successful.
    106      */
    107     @Override
    108     public void init() throws InitializationException
    109     {
    110         setInit(true);
    111     }
    112
    113     /**
    114      * Returns to uninitialized state.
    115      *
    116      * You can use this method to release resources that your Service
    117      * allocated when Turbine shuts down.
    118      */
    119     @Override
    120     public void shutdown()
    121     {
    122         setInit(false);
    123     }
    124 }