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
36
37
38
39
40
41
42 public abstract class BaseServiceImpl
43 extends AbstractLogEnabled
44 implements BaseService
45 {
46
47 private String serviceName;
48
49
50 private Context serviceContext;
51
52
53 private ServiceManager serviceManager;
54
55
56 private Configuration serviceConfiguration;
57
58
59 private Parameters serviceParameters;
60
61
62 private File serviceApplicationDir;
63
64
65 private File serviceTempDir;
66
67
68 private String servicePartitionName;
69
70
71 private ClassLoader serviceClassLoader;
72
73
74
75
76
77
78
79
80 public BaseServiceImpl()
81 {
82
83 }
84
85
86
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
100
101 public void service(ServiceManager serviceManager) throws ServiceException
102 {
103 this.serviceManager = serviceManager;
104 }
105
106
107
108
109 public void configure(Configuration configuration) throws ConfigurationException
110 {
111 this.serviceConfiguration = configuration;
112 }
113
114
115
116
117 public void parameterize(Parameters parameters) throws ParameterException
118 {
119 this.serviceParameters = parameters;
120 }
121
122
123
124
125 public void reconfigure(Configuration configuration) throws ConfigurationException
126 {
127 this.serviceConfiguration = configuration;
128 }
129
130
131
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
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
207
208
209
210 protected boolean hasService(String key)
211 {
212 return this.getServiceManager().hasService(key);
213 }
214
215
216
217
218
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
236
237
238 protected void release(Object object)
239 {
240 if ( object != null )
241 {
242 object = null;
243 }
244 }
245
246
247
248
249
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
265
266
267
268 protected String createAbsolutePath( String fileName )
269 {
270 return this.createAbsoluteFile(fileName).getAbsolutePath();
271 }
272
273
274
275
276 protected File getServiceApplicationDir()
277 {
278 return serviceApplicationDir;
279 }
280
281
282
283
284 protected ClassLoader getServiceClassLoader()
285 {
286 return serviceClassLoader;
287 }
288
289
290
291
292 protected Configuration getServiceConfiguration()
293 {
294 return serviceConfiguration;
295 }
296
297
298
299
300 protected Context getServiceContext()
301 {
302 return serviceContext;
303 }
304
305
306
307
308 protected ServiceManager getServiceManager()
309 {
310 return serviceManager;
311 }
312
313
314
315
316 protected String getServiceName()
317 {
318 return serviceName;
319 }
320
321
322
323
324 protected Parameters getServiceParameters()
325 {
326 return serviceParameters;
327 }
328
329
330
331
332 protected String getServicePartitionName()
333 {
334 return servicePartitionName;
335 }
336
337
338
339
340 protected File getServiceTempDir()
341 {
342 return serviceTempDir;
343 }
344 }