1 25 26 package org.objectweb.easybeans.component; 27 28 import java.util.ArrayList ; 29 import java.util.List ; 30 31 import org.objectweb.easybeans.component.api.EZBComponent; 32 import org.objectweb.easybeans.component.api.EZBComponentException; 33 import org.objectweb.easybeans.log.JLog; 34 import org.objectweb.easybeans.log.JLogFactory; 35 36 40 public class ComponentManager { 41 42 45 private static final String COMPONENT_STR = "Component"; 46 47 50 private JLog logger = JLogFactory.getLog(ComponentManager.class); 51 52 55 private List <String > componentNames = null; 56 57 60 private Components components = null; 61 62 63 66 private ComponentRegistry componentRegistry = null; 67 68 71 public ComponentManager() { 72 this.componentRegistry = new ComponentRegistry(); 73 this.componentNames = new ArrayList <String >(); 74 } 75 76 80 public ComponentManager(final Components components) { 81 this(); 82 setComponents(components); 83 } 84 85 86 90 public Components getComponents() { 91 return this.components; 92 } 93 94 98 public void setComponents(final Components components) { 99 this.components = components; 100 } 101 102 107 public void addComponent(final EZBComponent component) throws EZBComponentException { 108 addComponent(getComponentName(component), component); 110 } 111 112 117 public void removeComponent(final EZBComponent component) throws EZBComponentException { 118 String componentName = this.componentRegistry.getComponentName(component); 120 this.componentNames.remove(componentName); 121 this.componentRegistry.unregister(componentName); 122 } 123 124 129 private String getComponentName(final EZBComponent component) { 130 String componentName = component.getClass().getCanonicalName(); 132 133 int index = 2; 135 if (componentNames.contains(componentName)) { 136 while (componentNames.contains(componentName)) { 137 componentName = componentName + (index++); 138 } 139 } 140 return componentName; 141 } 142 143 149 private void addComponent(final String componentName, final EZBComponent component) throws EZBComponentException { 150 componentRegistry.register(componentName, component); 152 153 componentNames.add(componentName); 155 } 156 157 161 public void initComponents() throws EZBComponentException { 162 163 if (components == null) { 165 return; 166 } 167 168 List <EZBComponent> componentList = components.getEZBComponents(); 170 if (componentList != null) { 171 for (EZBComponent component : componentList) { 172 addComponent(component); 173 } 174 175 176 for (String componentName : componentNames) { 178 EZBComponent component = componentRegistry.getComponent(componentName); 179 component.init(); 180 } 181 182 } 183 } 184 185 189 public void startComponents() throws EZBComponentException { 190 191 StringBuilder sb = new StringBuilder (); 192 sb.append("[ Component(s) started : "); 193 194 for (String componentName : componentNames) { 196 EZBComponent component = componentRegistry.getComponent(componentName); 197 component.start(); 198 199 String name = component.getClass().getSimpleName(); 201 if (name.endsWith(COMPONENT_STR)) { 203 name = name.substring(0, name.lastIndexOf(COMPONENT_STR)); 204 } 205 sb.append(name); 206 sb.append(" "); 207 } 208 209 sb.append("]"); 210 logger.info(sb.toString()); 211 212 } 213 214 217 public void stopComponents() { 218 219 for (String componentName : componentNames) { 221 EZBComponent component = componentRegistry.getComponent(componentName); 222 try { 223 component.stop(); 224 } catch (EZBComponentException e) { 225 logger.error("Cannot stop component with name '" + componentName + "'.", e); 226 } 227 } 228 } 229 230 233 public ComponentRegistry getComponentRegistry() { 234 return componentRegistry; 235 } 236 } 237 | Popular Tags |