001package org.apache.turbine.services;
002
003
004/*
005 * Licensed to the Apache Software Foundation (ASF) under one
006 * or more contributor license agreements.  See the NOTICE file
007 * distributed with this work for additional information
008 * regarding copyright ownership.  The ASF licenses this file
009 * to you under the Apache License, Version 2.0 (the
010 * "License"); you may not use this file except in compliance
011 * with the License.  You may obtain a copy of the License at
012 *
013 *   http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing,
016 * software distributed under the License is distributed on an
017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018 * KIND, either express or implied.  See the License for the
019 * specific language governing permissions and limitations
020 * under the License.
021 */
022
023
024import org.apache.turbine.pipeline.PipelineData;
025import org.apache.turbine.util.RunData;
026
027/**
028 * <p>This class provides a <code>Service</code> implementation that
029 * Services used in Turbine are required to extend.  The
030 * functionality provided in addition to <code>BaseService</code>
031 * functionality is recognizing objects used in early initialization
032 * of <code>Services</code> in Turbine, and passing them to
033 * appropriate convenience methods.  These methods should be overridden
034 * to provide desired initialization functionality.</p>
035 *
036 * <p><strong>Note!</strong><br>Remember to call
037 * <code>setInit(true)</code> after successful initialization.</p>
038 *
039 * <p><strong>Note!</strong><br>If you need to use another
040 * <code>Service</code> inside your early initialization, remember to
041 * request initialization of that <code>Service</code> before using
042 * it:</p>
043 *
044 * <pre>
045 * getServiceBroker().initClass("OtherService",data);
046 * OtherService service =
047 *         (OtherService)getServiceBroker().getService("OtherService");
048 * </pre>
049 *
050 * @author <a href="mailto:greg@shwoop.com">Greg Ritter</a>
051 * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
052 * @author <a href="mailto:burton@apache.org">Kevin Burton</a>
053 * @author <a href="mailto:krzewski@e-point.pl">Rafal Krzewski</a>
054 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
055 * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
056 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
057 * @version $Id$
058 */
059public abstract class TurbineBaseService
060        extends BaseService
061{
062    /**
063     * Performs early initialization.  Overrides init() method in
064     * BaseService to detect objects used in Turbine's Service
065     * initialization and pass them to appropriate init() methods.
066     *
067     * @param data An Object to use for initialization activities.
068     * @throws InitializationException if initialization of this
069     * class was not successful.
070     */
071    @Override
072    public void init(Object data)
073            throws InitializationException
074    {
075        if (data instanceof RunData)
076        {
077            init((RunData) data);
078        }
079        else if (data instanceof PipelineData)
080        {
081            init((PipelineData) data);
082        }
083    }
084
085    /**
086     * Performs early initialization.
087     *
088     * @param pipelineData A PipelineData to use for initialization activities.
089     * @throws InitializationException if initialization of this
090     * class was not successful.
091     */
092    public void init(PipelineData pipelineData) throws InitializationException
093    {
094        // empty
095    }
096
097    /**
098     * Performs late initialization.
099     *
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}