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