1 package org.apache.fulcrum.yaafi.framework.container; 2 3 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 46 47 public class ServiceComponentImpl 48 implements ServiceComponent 49 { 50 51 private String name; 52 53 54 private String clazzName; 55 56 57 private Class clazz; 58 59 60 private Object instance; 61 62 63 private String shorthand; 64 65 66 private Logger logger; 67 68 69 private boolean isEarlyInit; 70 71 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 104 109 public Class loadClass() 110 throws ClassNotFoundException 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 123 public Object create() 124 throws InstantiationException , IllegalAccessException 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 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 t) 145 { 146 String msg = "LogEnable the following service failed : " + this.getName(); 147 this.getLogger().error(msg,t); 148 throw new RuntimeException (msg,t); 149 } 150 } 151 } 152 153 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 msg = "Contextualizing the following service failed : " + this.getShorthand(); 170 this.getLogger().error(msg,e); 171 throw e; 172 } 173 catch (Throwable t) 174 { 175 String 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 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 msg = "Servicing the following service failed : " + this.getShorthand(); 199 this.getLogger().error(msg,e); 200 throw e; 201 } 202 catch (Throwable t) 203 { 204 String msg = "Servicing the following service failed : " + this.getShorthand(); 205 this.getLogger().error(msg,t); 206 throw new RuntimeException (msg,t); 207 } 208 } 209 } 210 211 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 msg = "Configuring the following service failed : " + this.getShorthand(); 229 this.getLogger().error(msg,e); 230 throw e; 231 } 232 catch (Throwable t) 233 { 234 String 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 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 msg = "Parameterizing the following service failed : " + this.getShorthand(); 258 this.getLogger().error(msg,e); 259 throw e; 260 } 261 catch (Throwable t) 262 { 263 String 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 273 public void initialize() throws Exception 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 e) 283 { 284 String msg = "Initializing the following service failed : " + this.getShorthand(); 285 this.getLogger().error(msg,e); 286 throw e; 287 } 288 catch (Throwable t) 289 { 290 String 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 300 public void execute() throws Exception 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 e) 310 { 311 String msg = "Executing the following service failed : " + this.getShorthand(); 312 this.getLogger().error(msg,e); 313 throw e; 314 } 315 catch (Throwable t) 316 { 317 String 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 327 public void start() throws Exception 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 e) 337 { 338 String msg = "Starting the following service failed : " + this.getShorthand(); 339 this.getLogger().error(msg,e); 340 throw e; 341 } 342 catch (Throwable t) 343 { 344 String msg = "Starting the following service failed : " + this.getShorthand(); 345 this.getLogger().error(msg,t); 346 throw new RuntimeException (msg,t); 347 } 348 } 349 } 350 351 354 public void stop() throws Exception 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 e) 364 { 365 String msg = "Stopping the following service failed : " + this.getShorthand(); 366 this.getLogger().error(msg,e); 367 throw e; 368 } 369 } 370 } 371 372 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 e) 385 { 386 String msg = "Resuming the following service failed : " + this.getShorthand(); 387 this.getLogger().error(msg,e); 388 } 389 } 390 } 391 392 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 e) 405 { 406 String msg = "Suspending the following service failed : " + this.getShorthand(); 407 this.getLogger().error(msg,e); 408 } 409 } 410 } 411 412 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 shorthand = this.getShorthand(); 425 Configuration componentConfiguraton = configuration.getChild(shorthand); 426 ((Reconfigurable )this.getInstance()).reconfigure(componentConfiguraton); 427 } 428 catch (Exception e) 429 { 430 String msg = "Reconfiguring the following service failed : " + this.getShorthand(); 431 this.getLogger().error(msg,e); 432 } 433 } 434 } 435 436 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 e) 450 { 451 String msg = "Disposing the following service failed : " + this.getShorthand(); 452 this.getLogger().error(msg,e); 453 } 454 } 455 } 456 457 458 461 public boolean isInstantiated() 462 { 463 return ( this.instance != null ? true : false ); 464 } 465 466 469 public boolean isEarlyInit() 470 { 471 return isEarlyInit; 472 } 473 474 478 public Object getInstance() 479 throws InstantiationException , IllegalAccessException 480 { 481 if( this.isInstantiated() ) 482 { 483 return this.instance; 484 } 485 else 486 { 487 return this.create(); 488 } 489 } 490 491 495 498 public Class getClazz() 499 { 500 return clazz; 501 } 502 505 public void setClazz(Class clazz) 506 { 507 this.clazz = clazz; 508 } 509 512 public String getClazzName() 513 { 514 return clazzName; 515 } 516 519 public void setClazzName(String clazzName) 520 { 521 this.clazzName = clazzName; 522 } 523 526 public void setInstance(Object instance) 527 { 528 this.instance = instance; 529 } 530 533 public Logger getLogger() 534 { 535 return logger; 536 } 537 540 public void setLogger(Logger logger) 541 { 542 this.logger = logger; 543 } 544 547 public String getName() 548 { 549 return name; 550 } 551 554 public void setName(String name) 555 { 556 this.name = name; 557 } 558 561 public String getShorthand() 562 { 563 return shorthand; 564 } 565 568 public void setShorthand(String shorthand) 569 { 570 this.shorthand = shorthand; 571 } 572 573 577 private void notNull( Object object, String name ) 578 { 579 if( object == null ) 580 { 581 throw new NullPointerException ( name ); 582 } 583 } 584 } 585 | Popular Tags |