KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fulcrum > yaafi > framework > container > ServiceComponentImpl


1 package org.apache.fulcrum.yaafi.framework.container;
2
3 /*
4  * Copyright 2004 Apache Software Foundation
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 import org.apache.avalon.framework.activity.Disposable;
21 import org.apache.avalon.framework.activity.Executable;
22 import org.apache.avalon.framework.activity.Initializable;
23 import org.apache.avalon.framework.activity.Startable;
24 import org.apache.avalon.framework.activity.Suspendable;
25 import org.apache.avalon.framework.configuration.Configurable;
26 import org.apache.avalon.framework.configuration.Configuration;
27 import org.apache.avalon.framework.configuration.ConfigurationException;
28 import org.apache.avalon.framework.configuration.Reconfigurable;
29 import org.apache.avalon.framework.context.Context;
30 import org.apache.avalon.framework.context.ContextException;
31 import org.apache.avalon.framework.context.Contextualizable;
32 import org.apache.avalon.framework.logger.LogEnabled;
33 import org.apache.avalon.framework.logger.Logger;
34 import org.apache.avalon.framework.parameters.ParameterException;
35 import org.apache.avalon.framework.parameters.Parameterizable;
36 import org.apache.avalon.framework.parameters.Parameters;
37 import org.apache.avalon.framework.service.ServiceException;
38 import org.apache.avalon.framework.service.ServiceManager;
39 import org.apache.avalon.framework.service.Serviceable;
40
41 /**
42  * Holder of the metadata of a service component.
43  *
44  * @author <a HREF="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
45  */

46
47 public class ServiceComponentImpl
48     implements ServiceComponent
49 {
50     /** The name of the service */
51     private String JavaDoc name;
52
53     /** The name of the implementation class of the service */
54     private String JavaDoc clazzName;
55     
56     /** The actual implementation class of the service */
57     private Class JavaDoc clazz;
58
59     /** The instance of the implementation class of the service */
60     private Object JavaDoc instance;
61
62     /** The short name of this service */
63     private String JavaDoc shorthand;
64     
65     /** The logger to be used */
66     private Logger logger;
67      
68     /** Do we incarnate this instance during start-up */
69     private boolean isEarlyInit;
70     
71     /**
72      * Constructor
73      * @param configuration The configuration to obtain the meta informations
74      * @param clazzName The logger of the service container
75      */

76     public ServiceComponentImpl( Configuration configuration, Logger logger )
77         throws ConfigurationException
78     {
79         this.notNull( configuration, "configuration" );
80         this.notNull( logger, "logger" );
81         
82         if( configuration.getName().equals("role") )
83         {
84             this.clazzName = configuration.getAttribute("default-class");
85             this.name = configuration.getAttribute("name",this.clazzName);
86             this.shorthand = configuration.getAttribute("shorthand",this.name);
87             this.logger = logger;
88             this.isEarlyInit = configuration.getAttributeAsBoolean("early-init",true);
89         }
90         else
91         {
92             this.clazzName = configuration.getAttribute("class");
93             this.name = configuration.getAttribute("name",this.clazzName);
94             this.shorthand = configuration.getAttribute("shorthand",this.name);
95             this.logger = logger;
96             this.isEarlyInit = configuration.getAttributeAsBoolean("early-init",true);
97         }
98     }
99     
100     /////////////////////////////////////////////////////////////////////////
101
// Service Lifecycle Implementation
102
/////////////////////////////////////////////////////////////////////////
103

104     /**
105      * Create an instance of the service class
106      *
107      * @throws ClassNotFoundException
108      */

109     public Class JavaDoc loadClass()
110         throws ClassNotFoundException JavaDoc
111     {
112         this.getLogger().debug( "Loading the implementation class for " + this.getShorthand() );
113         this.clazz = this.getClass().getClassLoader().loadClass(this.clazzName);
114         return this.clazz;
115     }
116     
117     /**
118      * Create an instance of the service class
119      *
120      * @throws InstantiationException
121      * @throws IllegalAccessException
122      */

123     public Object JavaDoc create()
124         throws InstantiationException JavaDoc, IllegalAccessException JavaDoc
125     {
126         this.getLogger().debug( "Instantiating the implementation class for " + this.getShorthand() );
127         this.instance = this.clazz.newInstance();
128         return this.instance;
129     }
130
131     /**
132      * @see org.apache.avalon.framework.logger.LogEnabled#enableLogging(org.apache.avalon.framework.logger.Logger)
133      */

134     public void enableLogging(Logger logger)
135     {
136         if( this.instance instanceof LogEnabled )
137         {
138             try
139             {
140                 this.getLogger().debug( "LogEnabled.enableLogging() for " + this.getShorthand() );
141                 Logger avalonLogger = logger.getChildLogger( this.getClazzName() );
142                 ((LogEnabled )this.getInstance()).enableLogging(avalonLogger);
143             }
144             catch (Throwable JavaDoc t)
145             {
146                 String JavaDoc msg = "LogEnable the following service failed : " + this.getName();
147                 this.getLogger().error(msg,t);
148                 throw new RuntimeException JavaDoc(msg,t);
149             }
150         }
151     }
152
153     /**
154      * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
155      */

156     public void contextualize(Context context) throws ContextException
157     {
158         this.notNull( context, "context" );
159         
160         if( this.instance instanceof Contextualizable )
161         {
162             try
163             {
164                 this.getLogger().debug( "Contextualizable.contextualize() for " + this.getShorthand() );
165                 ((Contextualizable )this.getInstance()).contextualize(context);
166             }
167             catch (ContextException e)
168             {
169                 String JavaDoc msg = "Contextualizing the following service failed : " + this.getShorthand();
170                 this.getLogger().error(msg,e);
171                 throw e;
172             }
173             catch (Throwable JavaDoc t)
174             {
175                 String JavaDoc msg = "Contextualizing the following service failed : " + this.getShorthand();
176                 this.getLogger().error(msg,t);
177                 throw new ContextException(msg,t);
178             }
179          }
180     }
181
182     /**
183      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceContainer)
184      */

185     public void service(ServiceManager serviceManager) throws ServiceException
186     {
187         this.notNull( serviceManager, "serviceManager" );
188         
189         if( this.instance instanceof Serviceable )
190         {
191             try
192             {
193                 this.getLogger().debug( "Serviceable.service() for " + this.getShorthand() );
194                 ((Serviceable )this.getInstance()).service(serviceManager);
195             }
196             catch (ServiceException e)
197             {
198                 String JavaDoc msg = "Servicing the following service failed : " + this.getShorthand();
199                 this.getLogger().error(msg,e);
200                 throw e;
201             }
202             catch (Throwable JavaDoc t)
203             {
204                 String JavaDoc msg = "Servicing the following service failed : " + this.getShorthand();
205                 this.getLogger().error(msg,t);
206                 throw new RuntimeException JavaDoc(msg,t);
207             }
208         }
209     }
210
211     /**
212      * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
213      */

214     public void configure(Configuration configuration) throws ConfigurationException
215     {
216         this.notNull( configuration, "configuration" );
217         
218         if( this.instance instanceof Configurable )
219         {
220             try
221             {
222                 this.getLogger().debug( "Configurable.configure() for " + this.getShorthand() );
223                 Configuration componentConfiguraton = configuration.getChild(this.getShorthand());
224                 ((Configurable )this.getInstance()).configure(componentConfiguraton);
225             }
226             catch (ConfigurationException e)
227             {
228                 String JavaDoc msg = "Configuring the following service failed : " + this.getShorthand();
229                 this.getLogger().error(msg,e);
230                 throw e;
231             }
232             catch (Throwable JavaDoc t)
233             {
234                 String JavaDoc msg = "Configuring the following service failed : " + this.getShorthand();
235                 this.getLogger().error(msg,t);
236                 throw new ConfigurationException(msg,t);
237             }
238         }
239     }
240
241     /**
242      * @see org.apache.avalon.framework.parameters.Parameterizable#parameterize(org.apache.avalon.framework.parameters.Parameters)
243      */

244     public void parameterize(Parameters parameters) throws ParameterException
245     {
246         this.notNull( parameters, "parameters" );
247         
248         if( this.instance instanceof Parameterizable )
249         {
250             try
251             {
252                 this.getLogger().debug( "Parameterizable.parametrize() for " + this.getShorthand() );
253                 ((Parameterizable )this.getInstance()).parameterize(parameters);
254             }
255             catch (ParameterException e)
256             {
257                 String JavaDoc msg = "Parameterizing the following service failed : " + this.getShorthand();
258                 this.getLogger().error(msg,e);
259                 throw e;
260             }
261             catch (Throwable JavaDoc t)
262             {
263                 String JavaDoc msg = "Parameterizing the following service failed : " + this.getShorthand();
264                 this.getLogger().error(msg,t);
265                 throw new ParameterException(msg,t);
266             }
267         }
268     }
269
270     /**
271      * @see org.apache.avalon.framework.activity.Initializable#initialize()
272      */

273     public void initialize() throws Exception JavaDoc
274     {
275         if( this.instance instanceof Initializable )
276         {
277             try
278             {
279                 this.getLogger().debug( "Initializable.initialize() for " + this.getShorthand() );
280                 ((Initializable )this.getInstance()).initialize();
281             }
282             catch (Exception JavaDoc e)
283             {
284                 String JavaDoc msg = "Initializing the following service failed : " + this.getShorthand();
285                 this.getLogger().error(msg,e);
286                 throw e;
287             }
288             catch (Throwable JavaDoc t)
289             {
290                 String JavaDoc msg = "Initializing the following service failed : " + this.getShorthand();
291                 this.getLogger().error(msg,t);
292                 throw new ConfigurationException(msg,t);
293             }
294         }
295     }
296
297     /**
298      * @see org.apache.avalon.framework.activity.Executable#execute()
299      */

300     public void execute() throws Exception JavaDoc
301     {
302         if( this.instance instanceof Executable )
303         {
304             try
305             {
306                 this.getLogger().debug( "Executable.execute() for " + this.getShorthand() );
307                 ((Executable )this.getInstance()).execute();
308             }
309             catch (Exception JavaDoc e)
310             {
311                 String JavaDoc msg = "Executing the following service failed : " + this.getShorthand();
312                 this.getLogger().error(msg,e);
313                 throw e;
314             }
315             catch (Throwable JavaDoc t)
316             {
317                 String JavaDoc msg = "Executing the following service failed : " + this.getShorthand();
318                 this.getLogger().error(msg,t);
319                 throw new ConfigurationException(msg,t);
320             }
321         }
322     }
323
324     /**
325      * @see org.apache.avalon.framework.activity.Startable#start()
326      */

327     public void start() throws Exception JavaDoc
328     {
329         if( this.instance instanceof Startable )
330         {
331             try
332             {
333                 this.getLogger().debug( "Startable.start() for " + this.getShorthand() );
334                 ((Startable )this.getInstance()).start();
335             }
336             catch (Exception JavaDoc e)
337             {
338                 String JavaDoc msg = "Starting the following service failed : " + this.getShorthand();
339                 this.getLogger().error(msg,e);
340                 throw e;
341             }
342             catch (Throwable JavaDoc t)
343             {
344                 String JavaDoc msg = "Starting the following service failed : " + this.getShorthand();
345                 this.getLogger().error(msg,t);
346                 throw new RuntimeException JavaDoc(msg,t);
347             }
348         }
349     }
350
351     /**
352      * @see org.apache.avalon.framework.activity.Startable#stop()
353      */

354     public void stop() throws Exception JavaDoc
355     {
356         if( this.instance instanceof Startable )
357         {
358             try
359             {
360                 this.getLogger().debug( "Startable.stop() for " + this.getShorthand() );
361                 ((Startable )this.getInstance()).stop();
362             }
363             catch (Exception JavaDoc e)
364             {
365                 String JavaDoc msg = "Stopping the following service failed : " + this.getShorthand();
366                 this.getLogger().error(msg,e);
367                 throw e;
368             }
369         }
370     }
371
372     /**
373      * @see org.apache.avalon.framework.activity.Suspendable#resume()
374      */

375     public void resume()
376     {
377         if( this.instance instanceof Suspendable )
378         {
379             try
380             {
381                 this.getLogger().debug( "Suspendable.resume() for " + this.getShorthand() );
382                 ((Suspendable )this.getInstance()).resume();
383             }
384             catch (Exception JavaDoc e)
385             {
386                 String JavaDoc msg = "Resuming the following service failed : " + this.getShorthand();
387                 this.getLogger().error(msg,e);
388             }
389         }
390     }
391
392     /**
393      * @see org.apache.avalon.framework.activity.Suspendable#suspend()
394      */

395     public void suspend()
396     {
397         if( this.instance instanceof Suspendable )
398         {
399             try
400             {
401                 this.getLogger().debug( "Suspendable.suspend() for " + this.getShorthand() );
402                 ((Suspendable )this.getInstance()).suspend();
403             }
404             catch (Exception JavaDoc e)
405             {
406                 String JavaDoc msg = "Suspending the following service failed : " + this.getShorthand();
407                 this.getLogger().error(msg,e);
408             }
409         }
410     }
411
412     /**
413      * @see org.apache.avalon.framework.configuration.Reconfigurable#reconfigure(org.apache.avalon.framework.configuration.Configuration)
414      */

415     public void reconfigure(Configuration configuration) throws ConfigurationException
416     {
417         this.notNull( configuration, "configuration" );
418         
419         if( this.instance instanceof Reconfigurable )
420         {
421             try
422             {
423                 this.getLogger().debug( "Reconfigurable.reconfigure() for " + this.getShorthand() );
424                 String JavaDoc shorthand = this.getShorthand();
425                 Configuration componentConfiguraton = configuration.getChild(shorthand);
426                 ((Reconfigurable )this.getInstance()).reconfigure(componentConfiguraton);
427             }
428             catch (Exception JavaDoc e)
429             {
430                 String JavaDoc msg = "Reconfiguring the following service failed : " + this.getShorthand();
431                 this.getLogger().error(msg,e);
432             }
433         }
434     }
435
436     /**
437      * @see org.apache.avalon.framework.activity.Disposable#dispose()
438      */

439     public void dispose()
440     {
441         if( this.instance instanceof Disposable )
442         {
443             try
444             {
445                 this.getLogger().debug( "Disposable.dispose() for " + this.getShorthand() );
446                 ((Disposable )this.getInstance()).dispose();
447                 this.instance = null;
448             }
449             catch (Exception JavaDoc e)
450             {
451                 String JavaDoc msg = "Disposing the following service failed : " + this.getShorthand();
452                 this.getLogger().error(msg,e);
453             }
454         }
455     }
456     
457     
458     /**
459      * @return Returns the if the service instance was already instantiated.
460      */

461     public boolean isInstantiated()
462     {
463         return ( this.instance != null ? true : false );
464     }
465
466     /**
467      * @return Return true if the service is created on startup
468      */

469     public boolean isEarlyInit()
470     {
471         return isEarlyInit;
472     }
473     
474     /**
475      * @return Returns the instance. If it is not instantiated yet then
476      * do it
477      */

478     public Object JavaDoc getInstance()
479         throws InstantiationException JavaDoc, IllegalAccessException JavaDoc
480     {
481         if( this.isInstantiated() )
482         {
483             return this.instance;
484         }
485         else
486         {
487             return this.create();
488         }
489     }
490
491     /////////////////////////////////////////////////////////////////////////
492
// Generated getters and setters
493
/////////////////////////////////////////////////////////////////////////
494

495     /**
496      * @return Returns the clazz.
497      */

498     public Class JavaDoc getClazz()
499     {
500         return clazz;
501     }
502     /**
503      * @param clazz The clazz to set.
504      */

505     public void setClazz(Class JavaDoc clazz)
506     {
507         this.clazz = clazz;
508     }
509     /**
510      * @return Returns the clazzName.
511      */

512     public String JavaDoc getClazzName()
513     {
514         return clazzName;
515     }
516     /**
517      * @param clazzName The clazzName to set.
518      */

519     public void setClazzName(String JavaDoc clazzName)
520     {
521         this.clazzName = clazzName;
522     }
523     /**
524      * @param instance The instance to set.
525      */

526     public void setInstance(Object JavaDoc instance)
527     {
528         this.instance = instance;
529     }
530     /**
531      * @return Returns the logger.
532      */

533     public Logger getLogger()
534     {
535         return logger;
536     }
537     /**
538      * @param logger The logger to set.
539      */

540     public void setLogger(Logger logger)
541     {
542         this.logger = logger;
543     }
544     /**
545      * @return Returns the name.
546      */

547     public String JavaDoc getName()
548     {
549         return name;
550     }
551     /**
552      * @param name The name to set.
553      */

554     public void setName(String JavaDoc name)
555     {
556         this.name = name;
557     }
558     /**
559      * @return Returns the shorthand.
560      */

561     public String JavaDoc getShorthand()
562     {
563         return shorthand;
564     }
565     /**
566      * @param shorthand The shorthand to set.
567      */

568     public void setShorthand(String JavaDoc shorthand)
569     {
570         this.shorthand = shorthand;
571     }
572     
573     /////////////////////////////////////////////////////////////////////////
574
// MISC
575
/////////////////////////////////////////////////////////////////////////
576

577     private void notNull( Object JavaDoc object, String JavaDoc name )
578     {
579         if( object == null )
580         {
581             throw new NullPointerException JavaDoc( name );
582         }
583     }
584 }
585
Popular Tags