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.configuration2.Configuration;
027import org.apache.commons.configuration2.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$
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    @Override
058    public void setServiceBroker(ServiceBroker broker)
059    {
060        this.serviceBroker = broker;
061    }
062
063    /**
064     * ServiceBroker uses this method to pass a Service its name.
065     *
066     * @param name The name of this Service.
067     */
068    @Override
069    public void setName(String name)
070    {
071        this.name = name;
072    }
073
074    /**
075     * Returns the name of this service.
076     *
077     * @return The name of this Service.
078     */
079    @Override
080    public String getName()
081    {
082        return name;
083    }
084
085    /**
086     * Returns a ServiceBroker reference.
087     *
088     * @return The ServiceBroker that instantiated this object.
089     */
090    public ServiceBroker getServiceBroker()
091    {
092        return serviceBroker;
093    }
094
095    /**
096     * Returns the properties of this Service.
097     *
098     * @return The Properties of this Service.
099     */
100    @Override
101    public Properties getProperties()
102    {
103        return ConfigurationConverter.getProperties(getConfiguration());
104    }
105
106    /**
107     * Returns the configuration of this Service.
108     *
109     * @return The Configuration of this Service.
110     */
111    @Override
112    public Configuration getConfiguration()
113    {
114        if (name == null)
115        {
116            return null;
117        }
118
119        if (configuration == null)
120        {
121            configuration = getServiceBroker().getConfiguration(name);
122        }
123        return configuration;
124    }
125}