1 package org.apache.turbine.services; 2 3 18 19 import java.util.ArrayList ; 20 import java.util.Hashtable ; 21 import java.util.Iterator ; 22 23 import org.apache.commons.configuration.BaseConfiguration; 24 import org.apache.commons.configuration.Configuration; 25 import org.apache.commons.lang.StringUtils; 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 48 public abstract class BaseServiceBroker implements ServiceBroker 49 { 50 53 protected Configuration mapping = new BaseConfiguration(); 54 55 58 protected Hashtable services = new Hashtable (); 59 60 65 protected Configuration configuration; 66 67 71 public static final String SERVICE_PREFIX = "services."; 72 73 77 public static final String CLASSNAME_SUFFIX = ".classname"; 78 79 90 protected Hashtable serviceObjects = new Hashtable (); 91 92 93 private static Log log = LogFactory.getLog(BaseServiceBroker.class); 94 95 99 protected String applicationRoot; 100 101 106 protected BaseServiceBroker() 107 { 108 } 109 110 118 public void setConfiguration(Configuration configuration) 119 { 120 this.configuration = configuration; 121 } 122 123 128 public Configuration getConfiguration() 129 { 130 return configuration; 131 } 132 133 136 public void init() throws InitializationException 137 { 138 143 146 initMapping(); 149 150 initServices(false); 153 } 154 155 163 public void setServiceObject(String name, Object value) 164 { 165 serviceObjects.put(name, value); 166 } 167 168 173 public Object getServiceObject(String name) 174 { 175 return serviceObjects.get(name); 176 } 177 178 194 protected void initMapping() 195 { 196 208 for (Iterator keys = configuration.getKeys(); keys.hasNext();) 209 { 210 String key = (String ) keys.next(); 211 String [] keyParts = StringUtils.split(key, "."); 212 213 if ((keyParts.length == 3) 214 && (keyParts[0] + ".").equals(SERVICE_PREFIX) 215 && ("." + keyParts[2]).equals(CLASSNAME_SUFFIX)) 216 { 217 String serviceKey = keyParts[1]; 218 log.info("Added Mapping for Service: " + serviceKey); 219 220 if (!mapping.containsKey(serviceKey)) 221 { 222 mapping.setProperty(serviceKey, 223 configuration.getString(key)); 224 } 225 } 226 } 227 } 228 229 236 public boolean isRegistered(String serviceName) 237 { 238 return (services.get(serviceName) != null); 239 } 240 241 246 public Iterator getServiceNames() 247 { 248 return mapping.getKeys(); 249 } 250 251 258 public Iterator getServiceNames(String prefix) 259 { 260 return mapping.getKeys(prefix); 261 } 262 263 272 public synchronized void initService(String name) 273 throws InitializationException 274 { 275 Service instance = getServiceInstance(name); 279 280 if (!instance.getInit()) 281 { 282 instance.init(); 284 } 285 } 286 287 293 public void initServices() 294 { 295 try 296 { 297 initServices(false); 298 } 299 catch (InstantiationException notThrown) 300 { 301 log.debug("Caught non fatal exception", notThrown); 302 } 303 catch (InitializationException notThrown) 304 { 305 log.debug("Caught non fatal exception", notThrown); 306 } 307 } 308 309 317 public void initServices(boolean report) 318 throws InstantiationException , InitializationException 319 { 320 if (report) 321 { 322 for (Iterator names = getServiceNames(); names.hasNext();) 324 { 325 doInitService((String ) names.next()); 326 } 327 } 328 else 329 { 330 for (Iterator names = getServiceNames(); names.hasNext();) 332 { 333 try 334 { 335 doInitService((String ) names.next()); 336 } 337 catch (InstantiationException e) 340 { 341 log.error(e); 342 } 343 catch (InitializationException e) 344 { 345 log.error(e); 346 } 347 } 348 } 349 log.info("Finished initializing all services!"); 350 } 351 352 356 private void doInitService(String name) 357 throws InstantiationException , InitializationException 358 { 359 if (getConfiguration(name).getBoolean("earlyInit", false)) 361 { 362 log.info("Start Initializing service (early): " + name); 363 initService(name); 364 log.info("Finish Initializing service (early): " + name); 365 } 366 } 367 368 376 public synchronized void shutdownService(String name) 377 { 378 try 379 { 380 Service service = getServiceInstance(name); 381 if (service != null && service.getInit()) 382 { 383 service.shutdown(); 384 if (service.getInit() && service instanceof BaseService) 385 { 386 ((BaseService) service).setInit(false); 389 } 390 } 391 } 392 catch (InstantiationException e) 393 { 394 log.error("Shutdown of a nonexistent Service '" 396 + name + "' was requested", e); 397 } 398 } 399 400 404 public void shutdownServices() 405 { 406 log.info("Shutting down all services!"); 407 408 String serviceName = null; 409 410 416 417 ArrayList reverseServicesList = new ArrayList (); 418 419 for (Iterator serviceNames = getServiceNames(); serviceNames.hasNext();) 420 { 421 serviceName = (String ) serviceNames.next(); 422 reverseServicesList.add(0, serviceName); 423 } 424 425 for (Iterator serviceNames = reverseServicesList.iterator(); serviceNames.hasNext();) 426 { 427 serviceName = (String ) serviceNames.next(); 428 log.info("Shutting down service: " + serviceName); 429 shutdownService(serviceName); 430 } 431 } 432 433 441 public Service getService(String name) throws InstantiationException 442 { 443 Service service; 444 try 445 { 446 service = getServiceInstance(name); 447 if (!service.getInit()) 448 { 449 synchronized (service.getClass()) 450 { 451 if (!service.getInit()) 452 { 453 log.info("Start Initializing service (late): " + name); 454 service.init(); 455 log.info("Finish Initializing service (late): " + name); 456 } 457 } 458 } 459 if (!service.getInit()) 460 { 461 throw new InitializationException( 466 "init() failed to initialize service " + name); 467 } 468 return service; 469 } 470 catch (InitializationException e) 471 { 472 throw new InstantiationException ("Service " + name + 473 " failed to initialize", e); 474 } 475 } 476 477 494 protected Service getServiceInstance(String name) 495 throws InstantiationException 496 { 497 Service service = (Service) services.get(name); 498 499 if (service == null) 500 { 501 String className = mapping.getString(name); 502 if (StringUtils.isEmpty(className)) 503 { 504 throw new InstantiationException ( 505 "ServiceBroker: unknown service " + name 506 + " requested"); 507 } 508 try 509 { 510 service = (Service) services.get(className); 511 512 if (service == null) 513 { 514 try 515 { 516 service = (Service) 517 Class.forName(className).newInstance(); 518 } 519 catch (ThreadDeath t) 521 { 522 throw t; 523 } 524 catch (OutOfMemoryError t) 525 { 526 throw t; 527 } 528 catch (Throwable t) 529 { 530 String msg = null; 532 533 if (t instanceof NoClassDefFoundError ) 534 { 535 msg = "A class referenced by " + className + 536 " is unavailable. Check your jars and classes."; 537 } 538 else if (t instanceof ClassNotFoundException ) 539 { 540 msg = "Class " + className + 541 " is unavailable. Check your jars and classes."; 542 } 543 else if (t instanceof ClassCastException ) 544 { 545 msg = "Class " + className + 546 " doesn't implement the Service interface"; 547 } 548 else 549 { 550 msg = "Failed to instantiate " + className; 551 } 552 553 throw new InstantiationException (msg, t); 554 } 555 } 556 } 557 catch (ClassCastException e) 558 { 559 throw new InstantiationException ("ServiceBroker: Class " 560 + className 561 + " does not implement Service interface.", e); 562 } 563 catch (InstantiationException e) 564 { 565 throw new InstantiationException ( 566 "Failed to instantiate service " + name, e); 567 } 568 service.setServiceBroker(this); 569 service.setName(name); 570 services.put(name, service); 571 } 572 573 return service; 574 } 575 576 582 public Configuration getConfiguration(String name) 583 { 584 return configuration.subset(SERVICE_PREFIX + name); 585 } 586 587 592 public void setApplicationRoot(String applicationRoot) 593 { 594 this.applicationRoot = applicationRoot; 595 } 596 597 603 public String getApplicationRoot() 604 { 605 return applicationRoot; 606 } 607 } 608 | Popular Tags |