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 java.util.Properties;
025
026import org.apache.commons.configuration.Configuration;
027import org.apache.commons.configuration.ConfigurationConverter;
028
029/**
030 * This class is a generic implementation of <code>Service</code>.
031 *
032 * @author <a href="mailto:burton@apache.org">Kevin Burton</a>
033 * @author <a href="mailto:krzewski@e-point.pl">Rafal Krzewski</a>
034 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
035 * @version $Id: BaseService.java 615328 2008-01-25 20:25:05Z tv $
036 */
037public class BaseService
038        extends BaseInitable
039        implements Service
040{
041    /** A reference to the ServiceBroker that instantiated this object. */
042    protected ServiceBroker serviceBroker;
043
044    /** The configuration for this service */
045    protected Configuration configuration;
046
047    /** The name of this Service. */
048    protected String name;
049
050    /**
051     * Saves a reference to the ServiceBroker that instantiated this
052     * object, so that it can ask for its properties and access other
053     * Services.
054     *
055     * @param broker The ServiceBroker that instantiated this object.
056     */
057    public void setServiceBroker(ServiceBroker broker)
058    {
059        this.serviceBroker = broker;
060    }
061
062    /**
063     * ServiceBroker uses this method to pass a Service its name.
064     *
065     * @param name The name of this Service.
066     */
067    public void setName(String name)
068    {
069        this.name = name;
070    }
071
072    /**
073     * Returns the name of this service.
074     *
075     * @return The name of this Service.
076     */
077    public String getName()
078    {
079        return name;
080    }
081
082    /**
083     * Returns a ServiceBroker reference.
084     *
085     * @return The ServiceBroker that instantiated this object.
086     */
087    public ServiceBroker getServiceBroker()
088    {
089        return serviceBroker;
090    }
091
092    /**
093     * Returns the properties of this Service.
094     *
095     * @return The Properties of this Service.
096     */
097    public Properties getProperties()
098    {
099        return ConfigurationConverter.getProperties(getConfiguration());
100    }
101
102    /**
103     * Returns the configuration of this Service.
104     *
105     * @return The Configuration of this Service.
106     */
107    public Configuration getConfiguration()
108    {
109        if (name == null)
110        {
111            return null;
112        }
113
114        if (configuration == null)
115        {
116            configuration = getServiceBroker().getConfiguration(name);
117        }
118        return configuration;
119    }
120}