View Javadoc

1   package org.apache.turbine.services;
2   
3   
4   /*
5    * Licensed to the Apache Software Foundation (ASF) under one
6    * or more contributor license agreements.  See the NOTICE file
7    * distributed with this work for additional information
8    * regarding copyright ownership.  The ASF licenses this file
9    * to you under the Apache License, Version 2.0 (the
10   * "License"); you may not use this file except in compliance
11   * with the License.  You may obtain a copy of the License at
12   *
13   *   http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing,
16   * software distributed under the License is distributed on an
17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   * KIND, either express or implied.  See the License for the
19   * specific language governing permissions and limitations
20   * under the License.
21   */
22  
23  
24  import java.rmi.RemoteException;
25  import java.rmi.server.UnicastRemoteObject;
26  import java.util.Properties;
27  import javax.servlet.ServletConfig;
28  
29  import org.apache.commons.configuration.Configuration;
30  import org.apache.commons.configuration.ConfigurationConverter;
31  
32  /**
33   * A base implementation of an {@link java.rmi.server.UnicastRemoteObject}
34   * as a Turbine {@link org.apache.turbine.services.Service}.
35   *
36   * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
37   */
38  public class BaseUnicastRemoteService extends UnicastRemoteObject
39          implements Service
40  {
41      /**
42       * Serial version.
43       */
44      private static final long serialVersionUID = -7775459623190960297L;
45      
46      protected Configuration configuration;
47      private boolean isInitialized;
48      private InitableBroker initableBroker;
49      private String name;
50      private ServiceBroker serviceBroker;
51  
52      public BaseUnicastRemoteService()
53              throws RemoteException
54      {
55          isInitialized = false;
56          initableBroker = null;
57          name = null;
58          serviceBroker = null;
59      }
60  
61      /**
62       * Returns the configuration of this service.
63       *
64       * @return The configuration of this service.
65       */
66      public Configuration getConfiguration()
67      {
68          if (name == null)
69          {
70              return null;
71          }
72          else
73          {
74              if (configuration == null)
75              {
76                  configuration = getServiceBroker().getConfiguration(name);
77              }
78              return configuration;
79          }
80      }
81  
82      public void init(ServletConfig config)
83              throws InitializationException
84      {
85          setInit(true);
86      }
87  
88      public void setInitableBroker(InitableBroker broker)
89      {
90          this.initableBroker = broker;
91      }
92  
93      public InitableBroker getInitableBroker()
94      {
95          return initableBroker;
96      }
97  
98      public void init(Object data)
99              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 }