1 64 65 package com.jcorporate.expresso.kernel.management; 66 67 import com.jcorporate.expresso.kernel.ComponentContainer; 68 import com.jcorporate.expresso.kernel.ComponentLifecycle; 69 import com.jcorporate.expresso.kernel.Containable; 70 import com.jcorporate.expresso.kernel.DefaultContainerImpl; 71 import com.jcorporate.expresso.kernel.Describable; 72 import com.jcorporate.expresso.kernel.ExpressoComponent; 73 import com.jcorporate.expresso.kernel.RootContainer; 74 import com.jcorporate.expresso.kernel.RootContainerInterface; 75 import com.jcorporate.expresso.kernel.Startable; 76 import com.jcorporate.expresso.kernel.digester.ComponentConfig; 77 import com.jcorporate.expresso.kernel.digester.ComponentMetadataConfig; 78 import com.jcorporate.expresso.kernel.exception.ConfigurationException; 79 import com.jcorporate.expresso.kernel.internal.DefaultConfigBean; 80 import com.jcorporate.expresso.kernel.metadata.ComponentMetadata; 81 import com.jcorporate.expresso.kernel.metadata.IndexedProperty; 82 import com.jcorporate.expresso.kernel.metadata.MappedProperty; 83 import com.jcorporate.expresso.kernel.metadata.Property; 84 import com.jcorporate.expresso.kernel.metadata.SimpleProperty; 85 import com.jcorporate.expresso.kernel.util.ClassLocator; 86 import org.apache.log4j.Logger; 87 88 import java.net.URL ; 89 import java.util.Iterator ; 90 import java.util.Map ; 91 92 99 100 public class ComponentFactory { 101 102 105 static final protected ComponentFactory instance = new ComponentFactory(); 106 107 110 static Logger log = Logger.getLogger(ComponentFactory.class); 111 112 115 protected ComponentFactory() { 116 } 117 118 124 public static final ComponentFactory getInstance() { 125 return instance; 126 } 127 128 133 public void startComponent(ExpressoComponent ec) { 134 if (ec instanceof Startable) { 135 if (log.isDebugEnabled()) { 136 log.debug("Starting component: " + ec.getClass().getName()); 137 } 138 139 ((Startable) ec).start(); 140 141 if (log.isInfoEnabled()) { 142 log.info("Started component: " + ec.getClass().getName()); 143 } 144 } else { 145 if (log.isDebugEnabled()) { 146 log.debug("Object " + ec.getClass().getName() + 147 " does not implement Startable. Skipping 'start' method"); 148 } 149 } 150 151 } 152 153 163 public ComponentConfig constructDefaultConfig(ExpressoComponent component) 164 throws ConfigurationException { 165 ComponentConfig returnValue = new ComponentConfig(); 166 167 ComponentMetadata metadata = component.getMetaData(); 168 Map properties = metadata.getProperties(); 169 170 171 returnValue.setName(metadata.getName()); 172 173 for (Iterator j = properties.values().iterator(); j.hasNext();) { 174 Property p = (Property) j.next(); 175 if (p instanceof SimpleProperty) { 176 SimpleProperty prop = (SimpleProperty) p; 177 returnValue.addProperty(p.getName(), prop.getValue()); 178 } else if (p instanceof MappedProperty) { 179 MappedProperty prop = (MappedProperty) p; 180 for (Iterator i = prop.getValues().keySet().iterator(); 181 i.hasNext();) { 182 String key = (String ) i.next(); 183 String oneValue = (String ) prop.getMappedValue(key); 184 returnValue.addMappedProperty(p.getName(), key, oneValue); 185 } 186 } else if (p instanceof IndexedProperty) { 187 IndexedProperty prop = (IndexedProperty) p; 188 for (Iterator i = prop.getValues().keySet().iterator(); 189 i.hasNext();) { 190 Integer key = (Integer ) i.next(); 191 String oneValue = (String ) prop.getIndexedValue(key.intValue()); 192 returnValue.addIndexedProperty(prop.getName(), key.intValue(), oneValue); 193 } 194 195 } else { 196 throw new ConfigurationException("Unknown class instance for property: " 197 + p.getClass().getName()); 198 } 199 } 200 201 return returnValue; 202 } 203 204 211 public ComponentContainer constructComponentContainer() { 212 return new DefaultContainerImpl(); 213 } 214 215 222 public RootContainerInterface constructRootContainer() throws ConfigurationException { 223 RootContainerInterface rci = new RootContainer(); 224 ComponentContainer componentContainer = constructComponentContainer(); 225 componentContainer.setContainerComponent(rci); 226 rci.setContainerImplementation(componentContainer); 227 228 ComponentMetadataConfig metaConfig = new ComponentMetadataConfig(); 232 233 if (rci instanceof Describable) { 234 try { 235 Describable desc = (Describable) rci; 236 metaConfig.loadComponentMetadata(desc.getMetadataLocation()); 237 desc.setMetaData(metaConfig.getMetadata()); 238 } catch (IllegalArgumentException ex) { 239 log.error("Error loading Root container metadata", ex); 240 } 241 } 242 243 if (rci instanceof ComponentLifecycle) { 244 ((ComponentLifecycle) rci).initialize(); 245 } 246 247 return rci; 248 } 249 250 261 public ExpressoComponent constructComponent(ComponentContainer parent, 262 ComponentConfig config) throws ConfigurationException { 263 try { 264 Class clazz = ClassLocator.loadClass(config.getClassName()); 265 Object oneObj = clazz.newInstance(); 266 267 268 if (!(oneObj instanceof ExpressoComponent)) { 269 throw new ConfigurationException(config.getClassName() 270 + " does not implement the ExpressoComponent interface"); 271 } 272 273 ExpressoComponent ec = (ExpressoComponent) oneObj; 274 this.initComponent((ExpressoComponent) oneObj); 275 276 ec.setParent(parent.getContainerComponent()); 277 278 if (ec instanceof Describable) { 282 try { 283 Describable desc = (Describable) ec; 284 URL metadataLocation = desc.getMetadataLocation(); 285 if (metadataLocation == null) { 286 throw new ConfigurationException(desc.getClass().getName() + 287 ".getMetadataLocation() returned null"); 288 } 289 290 ComponentMetadataConfig digester = new ComponentMetadataConfig(); 291 digester.loadComponentMetadata(metadataLocation); 292 ComponentMetadata metadata = digester.getMetadata(); 293 if (metadata == null) { 294 throw new ConfigurationException("Parsed Metadata for component " + 295 desc.getClass().getName() + " was null"); 296 } 297 desc.setMetaData(metadata); 298 } catch (Exception e) { 299 log.error("Error loading component " + ec.toString(), e); 300 throw new ConfigurationException("Error loading Expresso Container", e); 301 } 302 } 303 304 String name = config.getName(); 305 ec.getMetaData().setName(name); 306 parent.addComponent(ec); 307 308 if (oneObj instanceof Containable) { 309 ComponentContainer newContainer = this.constructComponentContainer(); 310 newContainer.setContainerComponent((Containable) oneObj); 311 ((Containable) oneObj).setContainerImplementation(newContainer); 312 } 313 314 315 return ec; 316 } catch (IllegalAccessException ex) { 317 throw new ConfigurationException("Error creating component: " + config.getClassName(), ex); 318 } catch (ClassNotFoundException ex) { 319 throw new ConfigurationException("Error creating component: " + config.getClassName(), ex); 320 } catch (InstantiationException ex) { 321 throw new ConfigurationException("Error creating component: " + config.getClassName(), ex); 322 } 323 } 324 325 331 public void initComponent(ExpressoComponent ec) { 332 if (ec instanceof ComponentLifecycle) { 333 ((ComponentLifecycle) ec).initialize(); 334 } 335 } 336 337 344 public void configureComponent(ComponentConfig config, ExpressoComponent ec) 345 throws ConfigurationException { 346 if (ec instanceof ComponentLifecycle) { 347 ComponentMetadata metadata = ec.getMetaData(); 348 Map properties = metadata.getProperties(); 349 DefaultConfigBean targetConfig = new DefaultConfigBean(); 350 351 for (Iterator j = properties.values().iterator(); j.hasNext();) { 352 Property p = (Property) j.next(); 353 p.createConfigBean(targetConfig, config, metadata); 354 } 355 356 357 ((ComponentLifecycle) ec).configure(targetConfig); 358 } 359 360 } 361 362 363 } | Popular Tags |