1 6 package org.jfox.ioc; 7 8 import java.net.URL ; 9 import java.util.ArrayList ; 10 import java.util.Collections ; 11 import java.util.HashMap ; 12 import java.util.LinkedHashMap ; 13 import java.util.List ; 14 import java.util.Map ; 15 16 import org.jfox.ioc.classloader.ClassLoaderRepository; 17 import org.jfox.ioc.depend.Dependency; 18 import org.jfox.ioc.depend.DependencyPack; 19 import org.jfox.ioc.exception.ComponentException; 20 import org.jfox.ioc.ext.ActiveComponent; 21 import org.jfox.ioc.ext.InitializableComponent; 22 import org.jfox.ioc.ext.ServicableComponent; 23 import org.jfox.ioc.logger.Logger; 24 25 41 42 public class Registry { 43 44 47 private boolean started = false; 48 49 52 private Logger logger = Logger.getLogger(Registry.class.getName()); 53 54 57 private ClassLoaderRepository loaderRepo = new ClassLoaderRepository(new URL [0], Thread.currentThread().getContextClassLoader()); 58 59 62 private static final Registry me = new Registry(); 63 private final Repository repo = new Repository(); 64 65 68 public static Registry getInstance() { 69 return me; 70 } 71 72 private Registry() { 73 Thread.currentThread().setContextClassLoader(loaderRepo); 75 } 76 77 86 public synchronized ComponentMeta registerComponent(Component component) throws ComponentException { 87 return registerComponent(component,false); 88 } 89 90 99 public synchronized ComponentMeta registerComponent(Component component, boolean typeSingleton) throws ComponentException { 100 ComponentMeta meta = ComponentMetaFactory.getComponentMeta(component, typeSingleton); 101 return registerComponent(meta); 102 } 103 104 119 public synchronized ComponentMeta registerComponent(Class implementation) throws ComponentException { 120 return registerComponent(ComponentName.newInstance(implementation), implementation); 121 } 122 123 public synchronized ComponentMeta registerComponent(Class implementation, Dependency[] constructParams) throws ComponentException { 124 return registerComponent(implementation, constructParams, DependencyPack.EMPTY_PARAMETER_PACKS, true, false); 125 } 126 127 142 public synchronized ComponentMeta registerComponent(Class implementation, Dependency[] constructorParams, DependencyPack[] setterParams, boolean singleton, boolean typeSingleton) throws ComponentException { 143 ComponentMeta meta = ComponentMetaFactory.getComponentMeta(ComponentName.newInstance(implementation), implementation, constructorParams, setterParams, singleton, typeSingleton); 144 registerComponent(meta); 145 return meta; 146 147 } 148 149 public synchronized ComponentMeta registerComponent(ComponentName componentName,Class implementation) throws ComponentException { 150 return registerComponent(componentName, implementation,Dependency.EMPTY_PARAMETERS); 151 } 152 153 public synchronized ComponentMeta registerComponent(ComponentName componentName,Class implementation, Dependency[] constructParams) throws ComponentException { 154 return registerComponent(componentName, implementation,constructParams, DependencyPack.EMPTY_PARAMETER_PACKS, true, false); 155 } 156 157 161 162 public synchronized ComponentMeta registerComponent(ComponentName componentName,Class implementation, Dependency[] constructParams, DependencyPack[] setterParams, boolean singleton, boolean typeSingleton) throws ComponentException { 163 ComponentMeta componentMeta = ComponentMetaFactory.getComponentMeta(componentName,implementation,constructParams, setterParams, singleton, typeSingleton); 164 return registerComponent(componentMeta); 165 } 166 167 170 public synchronized ComponentMeta registerComponent(ComponentMeta componentMeta) throws ComponentException { 171 doRegister(componentMeta); 172 doActive(componentMeta); 173 return componentMeta; 174 } 175 176 182 public synchronized ComponentMeta[] registerComponentBatch(ComponentMeta[] componentMetas) { 183 List metas = new ArrayList (componentMetas.length); 184 if(componentMetas != null && componentMetas.length > 0) { 185 for(int i=0; i<componentMetas.length; i++){ 186 try { 187 ComponentMeta meta = componentMetas[i]; 188 doRegister(meta); 189 metas.add(meta); 190 } 191 catch(ComponentException e){ 192 logger.warn(e.getMessage(),e); 193 } 194 } 195 } 196 for(int i=0;i<metas.size(); i++){ 197 ComponentMeta meta = (ComponentMeta)metas.get(i); 198 try { 199 doActive(meta); 200 } 201 catch(ComponentException e){ 202 logger.warn(e.getMessage(),e); 203 } 204 } 205 return (ComponentMeta[])metas.toArray(new ComponentMeta[metas.size()]); 206 } 207 208 209 213 public synchronized ComponentMeta getComponentMeta(ComponentName name) throws ComponentException { 214 ComponentMeta meta = repo.getComponentMeta(name); 215 if(meta == null) { 216 throw new ComponentException("component " + name + " not found."); 217 } 218 return meta; 219 } 220 221 229 public Component getComponentInstance(Class implementaion) throws ComponentException { 230 ComponentName name = ComponentName.newInstance(implementaion); 231 return getComponentInstance(name); 232 } 233 234 241 public synchronized Component getComponentInstance(ComponentName name) throws ComponentException { 242 return getComponentInstance(name, DependencyPack.EMPTY_PARAMETER_PACKS); 243 } 244 245 254 public synchronized Component getComponentInstance(ComponentName name, DependencyPack[] paramPacks) throws ComponentException { 255 ComponentMeta meta = null; 256 if(!isRegistered(name)) { 257 throw new ComponentException("component " + name + " not registered."); 258 } 259 meta = getComponentMeta(name); 260 261 Component comp = meta.getComponentInstance(paramPacks); 262 return comp; 263 } 264 265 266 276 public synchronized ComponentMeta unregisterComponent(ComponentName name) throws ComponentException { 277 logger.info("unregister component " + name); 278 ComponentMeta meta = repo.unregisterComponent(name); 279 if(meta != null && meta.isSingleton()) { 281 Component comp = meta.getComponentInstance(DependencyPack.EMPTY_PARAMETER_PACKS); 282 stopComponent(meta, comp); 283 destroyComponent(meta, comp); 284 } 285 return meta; 286 } 287 288 291 public int countComponents() { 292 return repo.countComponents(); 293 } 294 295 298 public int countComponentTypes() { 299 return repo.countComponentTypes(); 300 } 301 302 306 public boolean isRegisteredImplemetation(Class implementation) { 307 return repo.isRegisteredImplemetation(implementation); 308 } 309 310 314 public boolean isRegistered(ComponentName name) { 315 return repo.isRegistered(name); 316 } 317 318 321 public synchronized List <ComponentName> getAllComponentNames() { 322 return repo.getAllComponentNames(); 323 } 324 325 public synchronized List <ComponentMeta> getAllComponentMetas() { 326 return repo.getAllComponentMetas(); 327 } 328 329 330 335 public List <ComponentName> getComponentNamesByImplemetation(Class type) { 336 return repo.getComponentNamesByImplemetation(type); 337 } 338 339 345 public synchronized ComponentName getFirstComponentNameByImplemetation(Class implementaion) { 346 return repo.getFirstComponentNameByType(implementaion); 347 } 348 349 353 public ComponentName getLastComponentNameByImplemetation(Class implementaion) { 354 return repo.getLastComponentNameByType(implementaion); 355 } 356 357 360 public synchronized void start() { 361 if(started) return; 362 logger.info("start Registry."); 363 List <ComponentMeta> compMetas = new ArrayList <ComponentMeta>(repo.getAllComponentMetas()); 364 Collections.sort(compMetas); 365 366 for(ComponentMeta meta : compMetas){ 367 try { 368 Component comp = meta.getComponentInstance(DependencyPack.EMPTY_PARAMETER_PACKS); 369 try { 370 initComponent(meta, comp); 371 } 372 catch(ComponentException e) { 373 logger.warn("init component error", e); 374 } 375 try { 376 startComponent(meta, comp); 377 } 378 catch(ComponentException e) { 379 logger.warn("start component error", e); 380 } 381 } 382 catch(Exception e) { 383 logger.warn("get component instance " + meta.getComponentName() + "failed.", e); 384 } 385 } 386 started = true; 387 } 388 389 392 public synchronized void stop() { 393 if(started == false) return; 394 logger.info("stop Registry."); 395 List <ComponentMeta> compMetas = new ArrayList <ComponentMeta>(repo.getAllComponentMetas()); 396 Collections.sort(compMetas); 397 Collections.reverse(compMetas); 398 399 for(ComponentMeta meta : compMetas){ 400 try { 401 if(meta.isSingleton()) { 403 Component comp = meta.getComponentInstance(DependencyPack.EMPTY_PARAMETER_PACKS); 404 if(ServicableComponent.class.isAssignableFrom(meta.getComponentImplementation())) { 405 ServicableComponent scomp = (ServicableComponent) comp; 406 if(scomp.isStarted()) { 407 scomp.stop(); 408 } 409 } 410 if(InitializableComponent.class.isAssignableFrom(meta.getComponentImplementation())) { 411 InitializableComponent dcomp = (InitializableComponent) comp; 412 dcomp.destroy(); 413 } 414 } 415 } 416 catch(Exception e) { 417 e.printStackTrace(); 418 } 419 } 420 started = false; 421 } 422 423 426 public synchronized boolean isStarted() { 427 return started; 428 } 429 430 435 public Class loadClass(String impl) throws ClassNotFoundException { 436 return loaderRepo.loadClass(impl); 437 } 438 439 443 public void addLibraryURL(URL url) { 444 loaderRepo.addURL(url); 445 } 446 447 451 public ClassLoaderRepository getClassLoaderRepository() { 452 return loaderRepo; 453 } 454 455 460 private synchronized void doRegister(ComponentMeta meta) throws ComponentException { 461 logger.info("register component " + meta.getComponentName()); 462 repo.registerComponent(meta); 463 meta.setRegistry(this); 465 } 468 469 private synchronized void doActive(ComponentMeta meta) throws ComponentException{ 470 if(ActiveComponent.class.isAssignableFrom(meta.getComponentImplementation())) { 472 getComponentInstance(meta.getComponentName()); 473 } 474 475 } 476 477 483 void initComponent(ComponentMeta meta, Component comp) throws ComponentException { 484 if(InitializableComponent.class.isAssignableFrom(comp.getClass())) { 485 InitializableComponent icomp = ((InitializableComponent) comp); 486 try { 487 logger.info("initializing component " + meta.getComponentName()); 488 icomp.init(); 489 logger.info("initialize component " + meta.getComponentName() + ", OK!"); 490 } 491 catch(Exception e) { 492 logger.warn("initialize component " + meta.getComponentName() + " failed.", e); 493 throw new ComponentException(e); 494 } 495 } 496 } 497 498 499 505 void startComponent(ComponentMeta meta, Component comp) throws ComponentException { 506 if(ServicableComponent.class.isAssignableFrom(meta.getComponentImplementation())) { 508 ServicableComponent scomp = (ServicableComponent) comp; 509 try { 510 if(!scomp.isStarted()) { 511 logger.info("starting component " + meta.getComponentName()); 512 scomp.start(); 513 logger.info("start component " + meta.getComponentName() + ", OK!"); 514 } 515 } 516 catch(Exception e) { 517 logger.info("start component " + meta.getComponentName() + " failed.", e); 518 throw new ComponentException(e); 519 } 520 } 521 } 522 523 529 void stopComponent(ComponentMeta meta, Component comp) throws ComponentException { 530 if(comp instanceof ServicableComponent) { 531 try { 532 ServicableComponent scomp = ((ServicableComponent) comp); 533 if(scomp.isStarted()) { 534 logger.info("stoping component " + meta.getComponentName()); 535 scomp.stop(); 536 logger.info("stop component " + meta.getComponentName() + ", OK!"); 537 } 538 } 539 catch(Exception e) { 540 logger.warn("stop component " + meta.getComponentName() + " failed.", e); 541 throw new ComponentException(e); 542 } 543 } 544 } 545 546 552 void destroyComponent(ComponentMeta meta, Component comp) throws ComponentException { 553 if(comp instanceof InitializableComponent) { 554 try { 555 InitializableComponent scomp = ((InitializableComponent) comp); 556 logger.info("destroying component " + meta.getComponentName()); 557 scomp.destroy(); 558 logger.info("destroy component " + meta.getComponentName() + ", OK!"); 559 } 560 catch(Exception e) { 561 logger.warn("destroy component " + meta.getComponentName() + " failed.", e); 562 throw new ComponentException(e); 563 } 564 } 565 } 566 567 class Repository { 568 571 private Map <ComponentName, ComponentMeta> components = new LinkedHashMap <ComponentName, ComponentMeta>(); 572 576 private Map <Class , List <ComponentName>> compCounts = new HashMap <Class , List <ComponentName>>(); 577 578 Repository() { 579 } 580 581 public synchronized void registerComponent(ComponentMeta meta) throws ComponentException { 582 ComponentName name = meta.getComponentName(); 583 if(isRegistered(name)) { 584 throw new ComponentException("Component " + name + " has already registered."); 585 } 586 else { if(meta.isTypeSingleton()) { 588 if(isRegisteredImplemetation(meta.getComponentImplementation())) { 590 throw new ComponentException(meta + " is type singleton, but this type has already been registered."); 591 } 592 } 593 else { 594 if(isRegisteredImplemetation(meta.getComponentImplementation())) { 596 List <ComponentName> names = getComponentNamesByImplemetation(meta.getComponentImplementation()); 597 ComponentName _name = names.get(names.size() - 1); 599 if(components.get(_name).isTypeSingleton()) { 600 throw new ComponentException("a component of this type has already been registered before " + _name.toString()); 601 } 602 } 603 } 604 } 605 606 List <ComponentName> metas; 607 if(isRegisteredImplemetation(meta.getComponentImplementation())) { 608 metas = (List <ComponentName>) (compCounts.get(meta.getComponentImplementation())); 609 } 610 else { 611 metas = new ArrayList <ComponentName>(); 612 compCounts.put(meta.getComponentImplementation(), metas); 613 } 614 metas.add(name); 615 616 components.put(name, meta); 617 } 618 619 public synchronized ComponentMeta unregisterComponent(ComponentName name) throws ComponentException { 620 if(isRegistered(name)) { 621 ComponentMeta meta = (ComponentMeta) components.remove(name); 622 623 List <ComponentName> names = ((List <ComponentName>) compCounts.get(meta.getComponentImplementation())); 624 names.remove(name); 625 626 if(names.isEmpty()) { 627 compCounts.remove(meta.getComponentImplementation()); 628 } 629 return meta; 630 } 631 return null; 632 } 633 634 639 public synchronized ComponentMeta getComponentMeta(ComponentName name) { 640 return (ComponentMeta) components.get(name); 641 } 642 643 public boolean isRegistered(ComponentName name) { 644 return components.containsKey(name); 645 } 646 647 public boolean isRegisteredImplemetation(Class clz) { 648 return compCounts.containsKey(clz); 649 } 650 651 656 public ComponentName getLastComponentNameByType(Class type) { 657 List names = new ArrayList (components.keySet()); 658 for(int i = names.size() - 1; i >= 0; i--) { 659 ComponentName name = (ComponentName) names.get(i); 660 if(name.getType().isAssignableFrom(type)) { 661 return name; 662 } 663 } 664 return null; 665 } 666 667 673 public synchronized ComponentName getFirstComponentNameByType(Class type) { 674 for(ComponentName name : components.keySet()) { 675 if(type.isAssignableFrom(name.getType())) { 676 return name; 677 } 678 } 679 return null; 680 } 681 682 685 public synchronized int countComponents() { 686 return components.size(); 687 } 688 689 692 public synchronized int countComponentTypes() { 693 return compCounts.size(); 694 } 695 696 699 public synchronized List <ComponentName> getAllComponentNames() { 700 return Collections.unmodifiableList(new ArrayList <ComponentName>(components.keySet())); 701 } 702 703 public synchronized List <ComponentMeta> getAllComponentMetas() { 704 return Collections.unmodifiableList(new ArrayList <ComponentMeta>(components.values())); 705 } 706 707 712 public List <ComponentName> getComponentNamesByImplemetation(Class type) { 713 if(isRegisteredImplemetation(type)) { 714 return compCounts.get(type); 715 } 716 else { 717 return new ArrayList <ComponentName>(); 718 } 719 } 720 721 } 722 723 } 724 725 | Popular Tags |