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.rmi.RemoteException;
025import java.rmi.server.UnicastRemoteObject;
026import java.util.Properties;
027
028import org.apache.commons.configuration2.Configuration;
029import org.apache.commons.configuration2.ConfigurationConverter;
030
031/**
032 * A base implementation of an {@link java.rmi.server.UnicastRemoteObject}
033 * as a Turbine {@link org.apache.turbine.services.Service}.
034 *
035 * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
036 */
037public class BaseUnicastRemoteService extends UnicastRemoteObject
038        implements Service
039{
040    /**
041     * Serial version.
042     */
043    private static final long serialVersionUID = -7775459623190960297L;
044
045    protected Configuration configuration;
046    private boolean isInitialized;
047    private InitableBroker initableBroker;
048    private String name;
049    private ServiceBroker serviceBroker;
050
051    /**
052     * Default constructor
053     * @throws RemoteException if the remote object cannot be created
054     */
055    public BaseUnicastRemoteService()
056            throws RemoteException
057    {
058        isInitialized = false;
059        initableBroker = null;
060        name = null;
061        serviceBroker = null;
062    }
063
064    /**
065     * Returns the configuration of this service.
066     *
067     * @return The configuration of this service.
068     */
069    @Override
070    public Configuration getConfiguration()
071    {
072        if (name == null)
073        {
074            return null;
075        }
076        else
077        {
078            if (configuration == null)
079            {
080                configuration = getServiceBroker().getConfiguration(name);
081            }
082            return configuration;
083        }
084    }
085
086    @Override
087    public void setInitableBroker(InitableBroker broker)
088    {
089        this.initableBroker = broker;
090    }
091
092    /**
093     * Get the {@link InitableBroker} instance
094     * @return the broker instance
095     */
096    public InitableBroker getInitableBroker()
097    {
098        return initableBroker;
099    }
100
101    @Override
102    public void init(Object data)
103            throws InitializationException
104    {
105        init();
106    }
107
108    @Override
109    public void init() throws InitializationException
110    {
111        setInit(true);
112    }
113
114    protected void setInit(boolean value)
115    {
116        isInitialized = value;
117    }
118
119    @Override
120    public boolean getInit()
121    {
122        return isInitialized;
123    }
124
125    /**
126     * Shuts down this service.
127     */
128    @Override
129    public void shutdown()
130    {
131        setInit(false);
132    }
133
134    @Override
135    public Properties getProperties()
136    {
137        return ConfigurationConverter.getProperties(getConfiguration());
138    }
139
140    @Override
141    public void setName(String name)
142    {
143        this.name = name;
144    }
145
146    @Override
147    public String getName()
148    {
149        return name;
150    }
151
152    /**
153     * Get the {@link ServiceBroker} instance
154     * @return the broker instance
155     */
156    public ServiceBroker getServiceBroker()
157    {
158        return serviceBroker;
159    }
160
161    @Override
162    public void setServiceBroker(ServiceBroker broker)
163    {
164        this.serviceBroker = broker;
165    }
166}