View Javadoc
1   package org.apache.fulcrum.yaafi.service.baseservice;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  
24  import org.apache.avalon.framework.configuration.Configuration;
25  import org.apache.avalon.framework.configuration.ConfigurationException;
26  import org.apache.avalon.framework.context.Context;
27  import org.apache.avalon.framework.context.ContextException;
28  import org.apache.avalon.framework.logger.AbstractLogEnabled;
29  import org.apache.avalon.framework.parameters.ParameterException;
30  import org.apache.avalon.framework.parameters.Parameters;
31  import org.apache.avalon.framework.service.ServiceException;
32  import org.apache.avalon.framework.service.ServiceManager;
33  
34  /**
35   * Base class for a service implementation capturing the Avalon
36   * serviceConfiguration artifacts. Take care that using this class
37   * introduces a dependency to the YAAFI library.
38   *
39   * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
40   */
41  
42  public abstract class BaseServiceImpl
43      extends AbstractLogEnabled
44      implements BaseService
45  {
46      /** The name of the service as defined in the role configuration file */
47      private String serviceName;
48  
49      /** The context supplied by the Avalon framework */
50      private Context serviceContext;
51  
52      /** The service manager supplied by the Avalon framework */
53      private ServiceManager serviceManager;
54  
55      /** The configuraton supplied by the Avalon framework */
56      private Configuration serviceConfiguration;
57  
58      /** The parameters supplied by the avalon framework */
59      private Parameters serviceParameters;
60  
61      /** the Avalon application directory */
62      private File serviceApplicationDir;
63  
64      /** the Avalon temp directory */
65      private File serviceTempDir;
66  
67      /** the Avalon partition name */
68      private String servicePartitionName;
69  
70      /** the class loader for this service */
71      private ClassLoader serviceClassLoader;
72  
73      /////////////////////////////////////////////////////////////////////////
74      // Avalon Lifecycle Implementation
75      /////////////////////////////////////////////////////////////////////////
76  
77      /**
78       * Constructor
79       */
80      public BaseServiceImpl()
81      {
82          // nothing to do
83      }
84  
85      /**
86       * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
87       */
88      public void contextualize(Context context) throws ContextException
89      {
90          this.serviceContext = context;
91          this.serviceName = (String) context.get("urn:avalon:name");
92          this.serviceApplicationDir = (File) context.get("urn:avalon:home");
93          this.serviceTempDir = (File) context.get("urn:avalon:temp");
94          this.servicePartitionName = (String) context.get("urn:avalon:partition");
95          this.serviceClassLoader = (ClassLoader) context.get("urn:avalon:classloader");
96      }
97  
98      /**
99       * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
100      */
101     public void service(ServiceManager serviceManager) throws ServiceException
102     {
103         this.serviceManager = serviceManager;
104     }
105 
106     /**
107      * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
108      */
109     public void configure(Configuration configuration) throws ConfigurationException
110     {
111         this.serviceConfiguration = configuration;
112     }
113 
114     /**
115      * @see org.apache.avalon.framework.parameters.Parameterizable#parameterize(org.apache.avalon.framework.parameters.Parameters)
116      */
117     public void parameterize(Parameters parameters) throws ParameterException
118     {
119         this.serviceParameters = parameters;
120     }
121 
122     /**
123      * @see org.apache.avalon.framework.configuration.Reconfigurable#reconfigure(org.apache.avalon.framework.configuration.Configuration)
124      */
125     public void reconfigure(Configuration configuration) throws ConfigurationException
126     {
127         this.serviceConfiguration = configuration;
128     }
129 
130     /**
131      * @see org.apache.avalon.framework.activity.Disposable#dispose()
132      */
133     public void dispose()
134     {
135         this.serviceApplicationDir = null;
136         this.serviceClassLoader = null;
137         this.serviceConfiguration = null;
138         this.serviceContext = null;
139         this.serviceManager = null;
140         this.serviceName = null;
141         this.serviceParameters = null;
142         this.servicePartitionName = null;
143         this.serviceTempDir = null;
144     }
145 
146     /////////////////////////////////////////////////////////////////////////
147     // Service Implementation
148     /////////////////////////////////////////////////////////////////////////
149 
150     /**
151      * @see java.lang.Object#toString()
152      */
153     public String toString()
154     {
155         StringBuilder result = new StringBuilder();
156 
157         result.append( getClass().getName() + "@" + Integer.toHexString(hashCode()));
158 
159         result.append("{");
160 
161         result.append("serviceName: ");
162         result.append(this.getServiceName());
163         result.append(";");
164 
165         result.append(" servicePartitionName: ");
166         result.append(this.getServicePartitionName());
167         result.append(";");
168 
169         result.append(" serviceApplicatonDir: ");
170         result.append(this.getServiceApplicationDir().getAbsolutePath());
171         result.append(";");
172 
173         result.append(" serviceTempDir: ");
174         result.append(this.getServiceTempDir().getAbsolutePath());
175         result.append(";");
176 
177         result.append(" serviceContext: ");
178         result.append(this.getServiceContext().toString());
179         result.append(";");
180 
181         result.append(" serviceConfiguration: ");
182         result.append(this.getServiceConfiguration().toString());
183         result.append(";");
184 
185         result.append(" serviceParameters: ");
186         result.append(Parameters.toProperties(this.getServiceParameters()));
187         result.append(";");
188 
189         result.append(" serviceClassLoader: ");
190         result.append(this.getServiceClassLoader());
191         result.append(";");
192 
193         result.append(" serviceLogger: ");
194         result.append(this.getLogger());
195         result.append(";");
196 
197         result.append(" serviceManager: ");
198         result.append(this.getServiceManager());
199 
200         result.append("}");
201 
202         return result.toString();
203     }
204 
205     /**
206      * @see org.apache.avalon.framework.service.ServiceManager#hasService(java.lang.String)
207      * @param key name of the service to test for
208      * @return boolean indicator if the service exists
209      */
210     protected boolean hasService(String key)
211     {
212         return this.getServiceManager().hasService(key);
213     }
214 
215     /**
216      * @see org.apache.avalon.framework.service.ServiceManager#lookup(java.lang.String)
217      * @param key name of service to lookup from the service manager
218      * @return reference to the service
219      */
220     protected Object lookup(String key)
221     {
222         try
223         {
224             return this.getServiceManager().lookup(key);
225         }
226         catch (ServiceException e)
227         {
228             String msg = "Unable to lookup the following service : " + key;
229             this.getLogger().error(msg,e);
230             throw new RuntimeException(msg);
231         }
232     }
233 
234     /**
235      * @param object service to release
236      * @see org.apache.avalon.framework.service.ServiceManager#release(java.lang.Object)
237      */
238     protected void release(Object object)
239     {
240     	if ( object != null )
241     	{
242     		object = null;
243     	}
244     }
245 
246     /**
247      * Determines the absolute file based on the application directory
248      * @param fileName the filename
249      * @return the absolute file
250      */
251     protected File createAbsoluteFile( String fileName )
252     {
253         File result = new File(fileName);
254 
255         if( result.isAbsolute() == false )
256         {
257             result = new File( this.getServiceApplicationDir(), fileName );
258         }
259 
260         return result;
261     }
262 
263     /**
264      * Determines the absolute path based on the application directory
265      * @param fileName the filename
266      * @return the absolute path
267      */
268     protected String createAbsolutePath( String fileName )
269     {
270         return this.createAbsoluteFile(fileName).getAbsolutePath();
271     }
272 
273     /**
274      * @return Returns the serviceApplicationDir.
275      */
276     protected File getServiceApplicationDir()
277     {
278         return serviceApplicationDir;
279     }
280 
281     /**
282      * @return Returns the serviceClassLoader.
283      */
284     protected ClassLoader getServiceClassLoader()
285     {
286         return serviceClassLoader;
287     }
288 
289     /**
290      * @return Returns the serviceConfiguration.
291      */
292     protected Configuration getServiceConfiguration()
293     {
294         return serviceConfiguration;
295     }
296 
297     /**
298      * @return Returns the serviceContext.
299      */
300     protected Context getServiceContext()
301     {
302         return serviceContext;
303     }
304 
305     /**
306      * @return Returns the serviceManager.
307      */
308     protected ServiceManager getServiceManager()
309     {
310         return serviceManager;
311     }
312 
313     /**
314      * @return Returns the serviceName.
315      */
316     protected String getServiceName()
317     {
318         return serviceName;
319     }
320 
321     /**
322      * @return Returns the serviceParameters.
323      */
324     protected Parameters getServiceParameters()
325     {
326         return serviceParameters;
327     }
328 
329     /**
330      * @return Returns the servicePartitionName.
331      */
332     protected String getServicePartitionName()
333     {
334         return servicePartitionName;
335     }
336 
337     /**
338      * @return Returns the serviceTempDir.
339      */
340     protected File getServiceTempDir()
341     {
342         return serviceTempDir;
343     }
344 }