1 package org.apache.fulcrum.yaafi.framework.container; 2 3 19 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.InputStream ; 23 import java.util.ArrayList ; 24 import java.util.Arrays ; 25 import java.util.Hashtable ; 26 import java.util.List ; 27 import java.util.Properties ; 28 29 import org.apache.avalon.framework.configuration.Configuration; 30 import org.apache.avalon.framework.configuration.ConfigurationException; 31 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 32 import org.apache.avalon.framework.context.Context; 33 import org.apache.avalon.framework.context.ContextException; 34 import org.apache.avalon.framework.context.DefaultContext; 35 import org.apache.avalon.framework.logger.Logger; 36 import org.apache.avalon.framework.parameters.ParameterException; 37 import org.apache.avalon.framework.parameters.Parameters; 38 import org.apache.avalon.framework.service.ServiceException; 39 40 45 46 public class ServiceContainerImpl 47 implements ServiceContainer 48 { 49 50 private String componentRolesLocation = COMPONENT_ROLE_VALUE; 51 52 53 private String componentConfigurationLocation = COMPONENT_CONFIG_VALUE; 54 55 56 private String parametersLocation = COMPONENT_PARAMETERS_VALUE; 57 58 59 private File applicationRootDir; 60 61 62 private File tempRootDir; 63 64 65 private Logger logger; 66 67 68 private List serviceList; 69 70 71 private Hashtable serviceMap; 72 73 74 private Configuration roleConfiguration; 75 76 77 private Configuration serviceConfiguration; 78 79 80 private Context context; 81 82 83 private Parameters parameters; 84 85 86 private boolean isDisposed; 87 88 92 95 public ServiceContainerImpl() 96 { 97 super(); 98 99 this.componentRolesLocation = COMPONENT_ROLE_VALUE; 100 this.componentConfigurationLocation = COMPONENT_CONFIG_VALUE; 101 this.parametersLocation = COMPONENT_PARAMETERS_VALUE; 102 103 this.isDisposed = false; 104 this.applicationRootDir = new File ( new File ("").getAbsolutePath() ); 105 this.tempRootDir = new File ( System.getProperty("java.io.tmpdir",".")); 106 this.serviceList = new ArrayList (); 107 this.serviceMap = new Hashtable (); 108 } 109 110 111 114 public void enableLogging(Logger logger) 115 { 116 this.logger = logger; 117 } 118 119 122 public void configure(Configuration configuration) throws ConfigurationException 123 { 124 this.setComponentRolesLocation( 125 configuration.getChild(this.COMPONENT_ROLE_KEYS).getValue( 126 this.COMPONENT_ROLE_VALUE ) 127 ); 128 129 this.setComponentConfigurationLocation( 130 configuration.getChild(this.COMPONENT_CONFIG_KEY).getValue( 131 this.COMPONENT_CONFIG_VALUE ) 132 ); 133 134 this.setParametersLocation( 135 configuration.getChild( this.COMPONENT_PARAMETERS_KEY).getValue( 136 this.COMPONENT_PARAMETERS_VALUE ) 137 ); 138 } 139 140 144 145 public void initialize() throws Exception 146 { 147 this.getLogger().debug( "Service Framework is starting up"); 148 149 151 this.getLogger().debug( 152 "Using the following applicationRootDir : " + this.applicationRootDir.getAbsolutePath() 153 ); 154 155 this.getLogger().debug( 156 "Using the following tempRootDir : " + this.tempRootDir.getAbsolutePath() 157 ); 158 159 161 this.serviceConfiguration = loadConfiguration(this.componentConfigurationLocation); 162 this.roleConfiguration = loadConfiguration(this.componentRolesLocation); 163 164 if( roleConfiguration == null ) 165 { 166 String msg = "Unable to locate the role configuration : " + this.componentRolesLocation; 167 this.getLogger().error( msg ); 168 throw new ConfigurationException( msg ); 169 } 170 171 173 if( this.context == null ) 174 { 175 this.getLogger().debug("Creating a DefaultContext"); 176 this.context = this.createDefaultContext(); 177 } 178 179 181 if( this.parameters == null ) 182 { 183 this.parameters = this.loadParameters( this.parametersLocation ); 184 } 185 186 188 List currServiceList = this.createServiceComponents(this.roleConfiguration,this.logger); 189 this.setServiceList( currServiceList ); 190 191 193 for( int i=0; i<this.serviceList.size(); i++ ) 194 { 195 ServiceComponent serviceComponent = (ServiceComponent) this.serviceList.get(i); 196 this.serviceMap.put( serviceComponent.getName(), serviceComponent ); 197 } 198 199 201 this.incarnate( this.serviceList ); 202 203 205 this.isDisposed = false; 206 this.getLogger().debug( "Service Framework is up and running"); 207 } 208 209 212 public void contextualize(Context context) throws ContextException 213 { 214 Object entry = null; 215 File currApplicationRootDir = null; 216 File currTempRootDir = null; 217 218 this.context = context; 219 220 223 if( this.isInContext( context, this.URN_AVALON_HOME ) ) 224 { 225 entry = context.get( this.URN_AVALON_HOME ); 226 227 if( entry instanceof String ) 228 { 229 String dirName = (String ) entry; 230 231 if( dirName.length() == 0 ) 232 { 233 currApplicationRootDir = new File ( new File (dirName).getAbsolutePath() ); 234 } 235 else 236 { 237 currApplicationRootDir = new File ( dirName ); 238 } 239 } 240 241 if( entry instanceof File ) 242 { 243 currApplicationRootDir = (File ) context.get( this.URN_AVALON_HOME ); 244 } 245 } 246 247 if( currApplicationRootDir != null ) 248 { 249 this.setApplicationRootDir( currApplicationRootDir ); 250 } 251 252 254 if( this.isInContext( context, this.URN_AVALON_TEMP ) ) 255 { 256 entry = context.get( this.URN_AVALON_TEMP ); 257 258 if( entry instanceof String ) 259 { 260 currTempRootDir = new File ( (String ) entry ); 261 } 262 263 if( entry instanceof File ) 264 { 265 currTempRootDir = (File ) context.get( this.URN_AVALON_TEMP ); 266 } 267 } 268 269 if( currTempRootDir != null ) 270 { 271 this.setTempRootDir( currTempRootDir ); 272 } 273 } 274 275 279 public synchronized void dispose() 280 { 281 if( this.isDisposed ) return; 282 283 this.getLogger().info("Disposing all services"); 284 285 287 this.decommision( this.getServiceList() ); 288 289 291 this.serviceList.clear(); 292 this.serviceMap.clear(); 293 294 this.componentRolesLocation = null; 295 this.componentConfigurationLocation = null; 296 this.context = null; 297 this.parametersLocation = null; 298 this.roleConfiguration = null; 299 this.serviceConfiguration = null; 300 this.parameters = null; 301 302 this.isDisposed = true; 303 this.getLogger().debug( "All services are disposed" ); 304 } 305 306 312 public synchronized void reconfigure(Configuration configuration) throws ConfigurationException 313 { 314 ServiceComponent serviceComponent = null; 315 316 this.getLogger().info("Reconfiguring all services"); 317 318 320 for( int i=this.getServiceList().size()-1; i>=0; i-- ) 321 { 322 serviceComponent = (ServiceComponent) this.getServiceList().get(i); 323 serviceComponent.suspend(); 324 } 325 326 328 for( int i=0; i<this.getServiceList().size(); i++ ) 329 { 330 serviceComponent = (ServiceComponent) this.getServiceList().get(i); 331 serviceComponent.reconfigure(this.getServiceConfiguration()); 332 } 333 334 336 for( int i=0; i<this.getServiceList().size(); i++ ) 337 { 338 serviceComponent = (ServiceComponent) this.getServiceList().get(i); 339 serviceComponent.resume(); 340 } 341 } 342 343 347 350 public synchronized boolean hasService(String name) 351 { 352 ServiceComponent serviceComponent = 353 (ServiceComponent) this. getServiceMap().get(name); 354 355 return ( serviceComponent != null ? true : false ); 356 } 357 358 361 public synchronized Object lookup(String name) throws ServiceException 362 { 363 Object result = null; 364 ServiceComponent serviceComponent = null; 365 366 serviceComponent = (ServiceComponent) this. getServiceMap().get(name); 367 368 if( serviceComponent != null ) 369 { 370 try 371 { 372 if( serviceComponent.isInstantiated() ) 373 { 374 result = serviceComponent.getInstance(); 376 } 377 else 378 { 379 result = serviceComponent.create(); 381 this.incarnate( serviceComponent ); 382 } 383 } 384 catch (Exception e) 385 { 386 String msg = "Failed to lookup the service " + serviceComponent.getShorthand(); 387 this.getLogger().error( msg, e ); 388 throw new ServiceException( serviceComponent.getShorthand(), msg, e ); 389 } 390 } 391 392 if( result == null ) 393 { 394 String msg = "Service not found : " + name; 395 this.getLogger().error(msg); 396 throw new ServiceException(name, msg ); 397 } 398 else 399 { 400 return result; 401 } 402 } 403 404 407 public synchronized void release(Object arg0) 408 { 409 } 410 411 415 418 protected void setComponentConfigurationLocation(String string) 419 { 420 this.componentConfigurationLocation = string; 421 } 422 423 426 protected void setComponentRolesLocation(String string) 427 { 428 this.componentRolesLocation = string; 429 } 430 431 434 protected void setParametersLocation(String string) 435 { 436 this.parametersLocation = string; 437 } 438 439 442 protected Logger getLogger() 443 { 444 return logger; 445 } 446 447 450 protected Hashtable getServiceMap() 451 { 452 return serviceMap; 453 } 454 455 462 protected void incarnate( ServiceComponent serviceComponent ) 463 throws ContextException, ServiceException, ConfigurationException, ParameterException, Exception 464 { 465 this.getLogger().debug( "Incarnating the service " + serviceComponent.getShorthand() ); 466 467 serviceComponent.enableLogging( this.getLogger() ); 468 serviceComponent.contextualize( context ); 469 serviceComponent.service( this ); 470 serviceComponent.configure( this.getServiceConfiguration() ); 471 serviceComponent.parameterize( parameters ); 472 serviceComponent.initialize(); 473 serviceComponent.execute(); 474 serviceComponent.start(); 475 } 476 477 485 486 protected void decommision( ServiceComponent serviceComponent ) 487 { 488 this.getLogger().debug( "Decommisioning the service " + serviceComponent.getShorthand() ); 489 490 try 491 { 492 serviceComponent.stop(); 493 } 494 catch (Throwable e) 495 { 496 String msg = "Stopping the following service failed : " + serviceComponent.getName(); 497 this.getLogger().error( msg, e ); 498 } 499 500 try 501 { 502 serviceComponent.dispose(); 503 } 504 catch (Throwable e) 505 { 506 String msg = "Disposing the following service failed : " + serviceComponent.getName(); 507 this.getLogger().error( msg, e ); 508 } 509 } 510 511 514 private void incarnate(List serviceList) 515 throws ContextException, ServiceException, ConfigurationException, ParameterException, Exception 516 { 517 ServiceComponent serviceComponent = null; 518 519 for( int i=0; i<serviceList.size(); i++ ) 520 { 521 serviceComponent = (ServiceComponent) this.serviceList.get(i); 522 this.incarnate( serviceComponent ); 523 } 524 } 525 526 529 private void decommision(List serviceList) 530 { 531 ServiceComponent serviceComponent = null; 532 533 for( int i=serviceList.size()-1; i>=0; i-- ) 534 { 535 serviceComponent = (ServiceComponent) this.getServiceList().get(i); 536 537 } 538 } 539 540 543 private List getServiceList() 544 { 545 return this.serviceList; 546 } 547 548 551 private void setServiceList(List list) 552 { 553 this.serviceList = list; 554 } 555 556 559 private Configuration getServiceConfiguration() 560 { 561 return this.serviceConfiguration; 562 } 563 564 569 570 private List createServiceComponents(Configuration roleConfiguration, Logger logger ) 571 throws ConfigurationException 572 { 573 if( roleConfiguration == null ) 574 { 575 String msg = "The roleConfiguration is <null>"; 576 throw new ConfigurationException(msg); 577 } 578 579 ArrayList result = new ArrayList (); 580 Configuration[] roleListEntries = getRoleConfigurationList(roleConfiguration); 581 582 ServiceComponent serviceComponent = null; 583 584 for ( int i=0; i<roleListEntries.length; i++ ) 585 { 586 try 587 { 588 serviceComponent = new ServiceComponentImpl(roleListEntries[i],logger); 589 590 593 if( serviceComponent.isEarlyInit() ) 594 { 595 serviceComponent.loadClass(); 596 serviceComponent.create(); 597 } 598 else 599 { 600 serviceComponent.loadClass(); 601 } 602 603 result.add( serviceComponent ); 604 } 605 catch( Throwable t ) 606 { 607 String msg = "Failed to load the service " + serviceComponent.getName(); 608 this.getLogger().error( msg, t ); 609 throw new ConfigurationException( msg, t ); 610 } 611 } 612 613 return result; 614 } 615 616 622 private Configuration loadConfiguration( String location ) throws Exception 623 { 624 Configuration result = null; 625 InputStream is = this.getInputStream( location ); 626 DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(); 627 628 if( is != null ) 629 { 630 result = builder.build( is ); 631 } 632 633 return result; 634 } 635 636 642 private Parameters loadParameters( String location ) throws Exception 643 { 644 InputStream is = this.getInputStream( location ); 645 Configuration conf = null; 646 Parameters result = new Parameters(); 647 648 if( is != null ) 649 { 650 if( location.endsWith(".xml") ) 651 { 652 result = Parameters.fromConfiguration( conf ); 653 } 654 else 655 { 656 Properties props = new Properties (); 657 props.load( is ); 658 result = Parameters.fromProperties( props ); 659 } 660 } 661 662 return result; 663 } 664 665 666 669 private InputStream getInputStream( String location ) throws Exception 670 { 671 if( ( location == null ) || ( location.length() == 0 ) ) 672 { 673 return null; 674 } 675 676 File file = null; 677 InputStream is = null; 678 679 682 if( ( is == null ) && ( location.startsWith( "/" ) == false ) ) 683 { 684 file = new File ( this.applicationRootDir, location ); 685 686 this.getLogger().debug("Looking for " + location + " in the application directory"); 687 688 if( file.exists() ) 689 { 690 is = new FileInputStream ( file ); 691 } 692 } 693 694 697 if( is == null ) 698 { 699 file = new File ( location ); 700 701 this.getLogger().debug("Looking for " + location + " as absolute file location"); 702 703 if( file.isAbsolute() && file.exists() ) 704 { 705 is = new FileInputStream ( file ); 706 } 707 } 708 709 712 if( ( is == null ) && ( location.startsWith( "/" ) == true ) ) 713 { 714 this.getLogger().debug("Looking for " + location + " using the class loader"); 715 is = getClass().getResourceAsStream( location ); 716 } 717 718 if( is == null ) 719 { 720 this.getLogger().warn("Unable to locate " + location); 721 } 722 else 723 { 724 this.getLogger().debug("Successfully located " + location); 725 } 726 727 return is; 728 } 729 730 735 private Configuration[] getRoleConfigurationList( Configuration roleConfiguration ) 736 { 737 Configuration[] result = null; 738 ArrayList roleListEntries = new ArrayList (); 739 String rootElementName = roleConfiguration.getName(); 740 741 if( rootElementName.equals("role-list") ) 742 { 743 745 roleListEntries.addAll( Arrays.asList( roleConfiguration.getChildren() ) ); 746 } 747 else if( rootElementName.equals("container") ) 748 { 749 751 Configuration[] temp = roleConfiguration.getChildren(); 752 for( int i=0; i<temp.length; i++ ) 753 { 754 if( temp[i].getName().equals("component") ) 755 { 756 roleListEntries.add(temp[i]); 757 } 758 } 759 } 760 else 761 { 762 throw new RuntimeException ( 763 "Don't know how to parse the roleConfiguration" 764 ); 765 } 766 767 result = (Configuration[]) roleListEntries.toArray( 768 new Configuration[roleListEntries.size()] 769 ); 770 771 return result; 772 } 773 774 private DefaultContext createDefaultContext() 775 { 776 DefaultContext result = new DefaultContext(); 777 778 result.put( ServiceConstants.URN_AVALON_HOME, this.applicationRootDir ); 779 result.put( ServiceConstants.URN_AVALON_TEMP, this.tempRootDir ); 780 result.put( ServiceConstants.COMPONENT_APP_ROOT, this.applicationRootDir.getAbsolutePath() ); 781 782 return result; 783 } 784 785 791 792 private boolean isInContext( Context context, String name ) 793 { 794 if( context == null ) 795 { 796 return false; 797 } 798 799 try 800 { 801 if( context.get(name) != null ) 802 { 803 return true; 804 } 805 else 806 { 807 return false; 808 } 809 } 810 catch (ContextException e) 811 { 812 return false; 813 } 814 } 815 816 819 private void setApplicationRootDir(File dir) 820 { 821 if( dir == null ) 822 { 823 String msg = "The applicationRootDir is <null>"; 824 throw new IllegalArgumentException ( msg ); 825 } 826 827 if( dir.exists() == false ) 828 { 829 String msg = "The applicatonRootDir " + dir.getAbsolutePath() + " does not exist"; 830 throw new IllegalArgumentException ( msg ); 831 } 832 833 this.getLogger().debug( "Setting applicationRootDir to " + dir.getAbsolutePath() ); 834 835 this.applicationRootDir = dir; 836 } 837 838 841 public void setTempRootDir(File dir) 842 { 843 if( dir == null ) 844 { 845 String msg = "The tempRootDir is <null>"; 846 throw new IllegalArgumentException ( msg ); 847 } 848 849 if( dir.exists() == false ) 850 { 851 String msg = "The tempRootDir " + dir.getAbsolutePath() + " does not exist"; 852 throw new IllegalArgumentException ( msg ); 853 } 854 855 this.getLogger().debug( "Setting tempRootDir to " + dir.getAbsolutePath() ); 856 857 this.tempRootDir = dir; 858 } 859 } 860 | Popular Tags |