1 64 package com.jcorporate.expresso.kernel; 65 66 import com.jcorporate.expresso.kernel.digester.ComponentConfig; 67 import com.jcorporate.expresso.kernel.digester.ExpressoServicesConfig; 68 import com.jcorporate.expresso.kernel.exception.ConfigurationException; 69 import com.jcorporate.expresso.kernel.management.ComponentFactory; 70 import com.jcorporate.expresso.kernel.management.ExpressoRuntimeMap; 71 import org.apache.log4j.Logger; 72 73 import java.net.MalformedURLException ; 74 import java.net.URL ; 75 import java.util.ArrayList ; 76 import java.util.Iterator ; 77 import java.util.List ; 78 79 80 98 public class SystemFactory { 99 102 public static final String DEFAULT_EXPRESSO_SERVICES = "expresso-services.xml"; 103 104 107 private List componentOrder = new ArrayList (10); 108 109 113 private List configOrder = new ArrayList (10); 114 115 118 protected SystemFactory() { 119 } 120 121 126 public static SystemFactory getInstance() { 127 return new SystemFactory(); 128 } 129 130 143 public static RootContainerInterface buildExpressoComponentSystem(String servicesFile, 144 String loggingConfiguration, 145 String logDir) 146 throws ConfigurationException { 147 return SystemFactory.getInstance().initialize(servicesFile, 148 loggingConfiguration, logDir); 149 } 150 151 161 public static RootContainerInterface buildExpressoComponentSystem(URL servicesFile, 162 URL loggingConfiguration, 163 String logDir) 164 throws ConfigurationException { 165 if (servicesFile == null) { 166 throw new IllegalArgumentException ("Must have a non-null servicesFile parameter"); 167 } 168 169 if (loggingConfiguration == null) { 170 throw new IllegalArgumentException ("Must have a non-null loggingConfiguration parameter"); 171 } 172 173 return SystemFactory.getInstance().initialize(servicesFile, 174 loggingConfiguration, logDir); 175 } 176 177 184 public void buildAndInitializeComponents(RootContainerInterface root, 185 ExpressoServicesConfig esc) throws ConfigurationException { 186 long startTime = System.currentTimeMillis(); 187 188 Logger initLog = Logger.getLogger(RootContainer.class); 189 190 if (initLog.isInfoEnabled()) { 191 initLog.info("Logging Initialized. Loading Expresso Services Config"); 192 } 193 194 esc.loadExpressoServices(); 195 196 ExpressoRuntimeMap.registerRuntime(esc.getName(), root); 201 202 try { 203 ComponentConfig configuration = esc.getRootConfig(); 204 205 if (initLog.isDebugEnabled()) { 206 initLog.debug("Successfully parsed expresso-services.xml"); 207 } 208 209 componentOrder.add(root); 210 configOrder.add(esc.getRootConfig()); 211 initializeOneContainer(root.getContainerImplementation(), 212 configuration, initLog); 213 214 if (initLog.isInfoEnabled()) { 215 initLog.info("Successfully loaded all component classes"); 216 } 217 218 configureComponents(initLog); 219 startComponents(initLog); 220 root.setExpressoServicesConfig(esc); 221 222 long stopTime = System.currentTimeMillis(); 223 224 if (initLog.isInfoEnabled()) { 225 initLog.info("Total time for this runtime initialization: " + 226 (stopTime - startTime) + " ms"); 227 } 228 } catch (ConfigurationException ex) { 229 initLog.error("Error initializing Configuration System", ex); 230 ExpressoRuntimeMap.unregisterRuntime(esc.getName()); 231 throw new ConfigurationException(ex); 232 } 233 } 234 235 242 public ComponentContainer constructComponentContainer() { 243 return new DefaultContainerImpl(); 244 } 245 246 259 protected synchronized RootContainerInterface initialize(URL servicesFile, 260 URL loggingConfiguration, String logDir) throws ConfigurationException { 261 LogManager lm = new LogManager(loggingConfiguration, logDir); 262 Logger initLog = Logger.getLogger(SystemFactory.class); 263 264 if (initLog.isInfoEnabled()) { 265 initLog.info("Logging Initialized. Loading Expresso Services Config"); 266 } 267 268 ComponentFactory cf = ComponentFactory.getInstance(); 269 270 RootContainerInterface gc = cf.constructRootContainer(); 271 gc.setLogManager(lm); 272 273 ExpressoServicesConfig esc = new ExpressoServicesConfig(); 274 esc.setExpressoServicesFile(servicesFile); 275 gc.setServicesFileLocation(servicesFile); 276 277 buildAndInitializeComponents(gc, esc); 278 279 return gc; 280 } 281 282 296 protected synchronized RootContainerInterface initialize(String servicesFile, 297 String loggingConfiguration, 298 String logDir) 299 throws ConfigurationException { 300 synchronized (SystemFactory.class) { 304 LogManager lm = new LogManager(loggingConfiguration, logDir); 308 309 Logger initLog = Logger.getLogger(SystemFactory.class); 310 311 if (initLog.isInfoEnabled()) { 312 initLog.info("Logging Initialized. Loading Expresso Services Config"); 313 } 314 315 ComponentFactory cf = ComponentFactory.getInstance(); 316 317 RootContainerInterface rootContainer = cf.constructRootContainer(); 318 rootContainer.setLogManager(lm); 319 320 ExpressoServicesConfig esc = new ExpressoServicesConfig(); 321 322 if (servicesFile == null) { 323 if (initLog.isDebugEnabled()) { 324 initLog.debug("Attempting to load expresso services from class loader"); 325 } 326 327 URL url = Thread.currentThread().getContextClassLoader() 328 .getResource(DEFAULT_EXPRESSO_SERVICES); 329 esc.setExpressoServicesFile(url); 330 rootContainer.setServicesFileLocation(url); 331 } else { 332 if (initLog.isDebugEnabled()) { 333 initLog.debug("Attempting to load expresso services from file name: " + 334 servicesFile); 335 } 336 337 esc.setExpressoServicesFile(servicesFile); 338 339 java.io.File f = new java.io.File (servicesFile); 340 341 if (f == null) { 342 throw new ConfigurationException("Unable to get services file location as a URL."); 343 } 344 345 try { 346 rootContainer.setServicesFileLocation(f.toURL()); 347 } catch (MalformedURLException ex) { 348 throw new ConfigurationException("Error getting services file location as a URL.", 349 ex); 350 } 351 } 352 353 buildAndInitializeComponents(rootContainer, esc); 354 355 return rootContainer; 356 } 357 } 358 359 365 private void configureComponents(Logger log) { 366 ComponentFactory cf = ComponentFactory.getInstance(); 367 368 for (Iterator i = componentOrder.iterator(), eachConfig = configOrder.iterator(); 369 i.hasNext();) { 370 Object o = i.next(); 371 ComponentConfig oneConfig = (ComponentConfig) eachConfig.next(); 372 373 try { 374 if (o instanceof ComponentLifecycle) { 375 if (log.isDebugEnabled()) { 376 log.debug("Configuring component: " + 377 o.getClass().getName()); 378 } 379 380 ExpressoComponent ec = (ExpressoComponent) o; 381 cf.configureComponent(oneConfig, ec); 382 } else { 383 if (log.isDebugEnabled()) { 384 log.debug("Object " + o.getClass().getName() + 385 " does not implement ComponentLifecycle. Skipping 'configure' method"); 386 } 387 } 388 } catch (ConfigurationException ex) { 389 log.error("Error configuring component: " + 390 o.getClass().getName(), ex); 391 } 392 } 393 } 394 395 404 private void initializeOneContainer(ComponentContainer currentContainer, 405 ComponentConfig containerComponentConfig, Logger log) 406 throws ConfigurationException { 407 List children = containerComponentConfig.getChildComponents(); 408 409 try { 410 ComponentFactory cf = ComponentFactory.getInstance(); 411 412 for (Iterator i = children.iterator(); i.hasNext();) { 413 ComponentConfig childComponentConfig = (ComponentConfig) i.next(); 414 ExpressoComponent oneObj = cf.constructComponent(currentContainer, 415 childComponentConfig); 416 417 this.componentOrder.add(oneObj); 418 this.configOrder.add(childComponentConfig); 419 420 if (log.isInfoEnabled()) { 421 log.info("Loading component: " + 422 childComponentConfig.getClassName() + " as " + 423 childComponentConfig.getName()); 424 } 425 426 ExpressoComponent ec = (ExpressoComponent) oneObj; 427 428 if (oneObj instanceof Containable) { 429 ComponentContainer newContainer = ((Containable) ec).getContainerImplementation(); 430 431 if (childComponentConfig.getChildComponents().size() > 0) { 432 initializeOneContainer(newContainer, 433 childComponentConfig, log); 434 } 435 } 436 } 437 } catch (Exception e) { 438 log.error("Error configuring component.", e); 439 } 440 } 441 442 448 private void startComponents(Logger log) { 449 ComponentFactory cf = ComponentFactory.getInstance(); 450 451 for (Iterator i = componentOrder.iterator(); i.hasNext();) { 452 Object o = i.next(); 453 cf.startComponent((ExpressoComponent) o); 454 } 455 } 456 } 457 | Popular Tags |