1 package org.apache.fulcrum.yaafi.service.baseservice;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
75
76
77 /**
78 * Constructor
79 */
80 public BaseServiceImpl()
81 {
82
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
148
149
150 /**
151 * @see java.lang.Object#toString()
152 */
153 public String toString()
154 {
155 StringBuffer result = new StringBuffer();
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 */
208 protected boolean hasService(String key)
209 {
210 return this.getServiceManager().hasService(key);
211 }
212
213 /**
214 * @see org.apache.avalon.framework.service.ServiceManager#lookup(java.lang.String)
215 */
216 protected Object lookup(String key)
217 {
218 try
219 {
220 return this.getServiceManager().lookup(key);
221 }
222 catch (ServiceException e)
223 {
224 String msg = "Unable to lookup the following service : " + key;
225 this.getLogger().error(msg,e);
226 throw new RuntimeException(msg);
227 }
228 }
229
230 /**
231 * @see org.apache.avalon.framework.service.ServiceManager#release(java.lang.Object)
232 */
233 protected void release(Object object)
234 {
235 this.release(object);
236 }
237
238 /**
239 * Determines the absolute file based on the application directory
240 * @param fileName the filename
241 * @return the absolute file
242 */
243 protected File createAbsoluteFile( String fileName )
244 {
245 File result = new File(fileName);
246
247 if( result.isAbsolute() == false )
248 {
249 result = new File( this.getServiceApplicationDir(), fileName );
250 }
251
252 return result;
253 }
254
255 /**
256 * Determines the absolute path based on the application directory
257 * @param fileName the filename
258 * @return the absolute path
259 */
260 protected String createAbsolutePath( String fileName )
261 {
262 return this.createAbsoluteFile(fileName).getAbsolutePath();
263 }
264
265 /**
266 * @return Returns the serviceApplicationDir.
267 */
268 protected File getServiceApplicationDir()
269 {
270 return serviceApplicationDir;
271 }
272
273 /**
274 * @return Returns the serviceClassLoader.
275 */
276 protected ClassLoader getServiceClassLoader()
277 {
278 return serviceClassLoader;
279 }
280
281 /**
282 * @return Returns the serviceConfiguration.
283 */
284 protected Configuration getServiceConfiguration()
285 {
286 return serviceConfiguration;
287 }
288
289 /**
290 * @return Returns the serviceContext.
291 */
292 protected Context getServiceContext()
293 {
294 return serviceContext;
295 }
296
297 /**
298 * @return Returns the serviceManager.
299 */
300 protected ServiceManager getServiceManager()
301 {
302 return serviceManager;
303 }
304
305 /**
306 * @return Returns the serviceName.
307 */
308 protected String getServiceName()
309 {
310 return serviceName;
311 }
312
313 /**
314 * @return Returns the serviceParameters.
315 */
316 protected Parameters getServiceParameters()
317 {
318 return serviceParameters;
319 }
320
321 /**
322 * @return Returns the servicePartitionName.
323 */
324 protected String getServicePartitionName()
325 {
326 return servicePartitionName;
327 }
328
329 /**
330 * @return Returns the serviceTempDir.
331 */
332 protected File getServiceTempDir()
333 {
334 return serviceTempDir;
335 }
336 }