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  
28  import org.apache.commons.configuration.Configuration;
29  import org.apache.commons.configuration.ConfigurationConverter;
30  
31  /**
32   * A base implementation of an {@link java.rmi.server.UnicastRemoteObject}
33   * as a Turbine {@link org.apache.turbine.services.Service}.
34   *
35   * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
36   */
37  public class BaseUnicastRemoteService extends UnicastRemoteObject
38          implements Service
39  {
40      /**
41       * Serial version.
42       */
43      private static final long serialVersionUID = -7775459623190960297L;
44  
45      protected Configuration configuration;
46      private boolean isInitialized;
47      private InitableBroker initableBroker;
48      private String name;
49      private ServiceBroker serviceBroker;
50  
51      /**
52       * Default constructor
53       * @throws RemoteException if the remote object cannot be created
54       */
55      public BaseUnicastRemoteService()
56              throws RemoteException
57      {
58          isInitialized = false;
59          initableBroker = null;
60          name = null;
61          serviceBroker = null;
62      }
63  
64      /**
65       * Returns the configuration of this service.
66       *
67       * @return The configuration of this service.
68       */
69      @Override
70      public Configuration getConfiguration()
71      {
72          if (name == null)
73          {
74              return null;
75          }
76          else
77          {
78              if (configuration == null)
79              {
80                  configuration = getServiceBroker().getConfiguration(name);
81              }
82              return configuration;
83          }
84      }
85  
86      @Override
87      public void setInitableBroker(InitableBroker broker)
88      {
89          this.initableBroker = broker;
90      }
91  
92      /**
93       * Get the {@link InitableBroker} instance
94       * @return the broker instance
95       */
96      public InitableBroker getInitableBroker()
97      {
98          return initableBroker;
99      }
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 }