1 25 26 package org.objectweb.easybeans.component; 27 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 import java.util.Set ; 32 33 import org.objectweb.easybeans.component.api.EZBComponent; 34 import org.objectweb.easybeans.component.api.EZBComponentException; 35 import org.objectweb.easybeans.log.JLog; 36 import org.objectweb.easybeans.log.JLogFactory; 37 38 42 public class ComponentRegistry { 43 44 47 private JLog logger = JLogFactory.getLog(ComponentRegistry.class); 48 49 52 private Map <String , EZBComponent> components = null; 53 54 57 public ComponentRegistry() { 58 components = new HashMap <String , EZBComponent>(); 60 } 61 62 68 public void register(final String componentName, final EZBComponent component) throws EZBComponentException { 69 if (components.containsKey(componentName)) { 71 throw new EZBComponentException("Cannot register the component with the name '" + componentName 72 + "'. There is an existing component with this name."); 73 } 74 75 logger.debug("Registering component with name {0}.", componentName); 76 components.put(componentName, component); 77 } 78 79 84 public void unregister(final String componentName) throws EZBComponentException { 85 if (!components.containsKey(componentName)) { 87 throw new EZBComponentException("No component with the name '" + componentName 88 + "' found. Component not unregistered"); 89 } 90 91 logger.info("Unregistering component with name {0}.", componentName); 92 components.remove(componentName); 93 } 94 95 100 public void unregister(final EZBComponent component) throws EZBComponentException { 101 String name = null; 102 103 Set <String > keys = components.keySet(); 105 for (String key : keys) { 106 EZBComponent foundComponent = components.get(key); 107 if (foundComponent.equals(component)) { 108 name = key; 110 break; 111 } 112 } 113 if (name != null) { 115 unregister(name); 116 } 117 throw new EZBComponentException("No component found in the registry with the given component '" + component + "'."); 118 119 } 120 121 126 public EZBComponent getComponent(final String componentName) { 127 return components.get(componentName); 128 } 129 130 134 public String getComponentName(final EZBComponent component) { 135 136 String match = null; 138 for(Iterator <String > i = this.components.keySet().iterator(); 139 i.hasNext() && (match == null);) { 140 String key = i.next(); 141 EZBComponent candidate = this.components.get(key); 142 if (component == candidate) { 143 match = key; 144 } 145 } 146 if (match == null) { 147 throw new IllegalStateException ("Each component should be registered in the registry."); 148 } 149 return match; 150 } 151 } 152 | Popular Tags |