| 1 17 18 package org.sape.carbon.core.bootstrap; 19 20 import org.sape.carbon.core.component.ComponentKeeper; 21 import org.sape.carbon.core.component.startup.StartupService; 22 import org.sape.carbon.core.config.ConfigurationService; 23 import org.sape.carbon.core.exception.ExceptionUtility; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 28 import java.util.Properties ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 import java.util.Set ; 32 33 54 public final class BootStrapper { 55 56 57 58 59 private Log log = LogFactory.getLog(this.getClass()); 60 61 62 private ConfigurationService configurationService = null; 63 64 65 private ComponentKeeper componentKeeper = null; 66 67 70 private BootStrapperStateEnum state = BootStrapperStateEnum.NOT_LOADED; 71 72 76 private final Object stateLock = new Object (); 77 78 81 private final DeploymentProperties deploymentProperties; 82 83 84 88 private BootStrapper() { 89 Package pkg = BootStrapper.class.getPackage(); 91 92 if (pkg != null) { 93 if (log.isInfoEnabled()) { 94 log.info("Loading the Carbon Core, version: " 95 + pkg.getSpecificationTitle() + " " 96 + pkg.getSpecificationVersion() + ", Copyright " 97 + pkg.getSpecificationVendor()); 98 } 99 } else { 100 if (log.isInfoEnabled()) { 101 log.info("Loading the Carbon Core, no package " 102 + "information available"); 103 } 104 } 105 106 this.deploymentProperties = new DeploymentProperties(); 107 } 108 109 115 public synchronized ConfigurationService fetchConfigurationService() { 116 load(); 117 return this.configurationService; 118 } 119 120 126 public synchronized ComponentKeeper fetchComponentKeeper() { 127 load(); 128 return this.componentKeeper; 129 } 130 131 142 public synchronized void load() { 143 144 if (this.configurationService == null 148 || this.componentKeeper == null) { 149 150 try { 155 javax.xml.parsers.SAXParserFactory saxParserFactory = 156 javax.xml.parsers.SAXParserFactory.newInstance(); 157 158 javax.xml.parsers.SAXParser saxParser = 159 saxParserFactory.newSAXParser(); 160 161 if (log.isInfoEnabled()) { 162 log.info("System default SAXParser is: " 163 + saxParser.getClass().getName()); 164 } 165 166 } catch (Exception e) { 167 } 169 170 171 Loader loader = fetchLoader(); 172 try { 174 this.configurationService = 175 loader.fetchConfigurationService(); 176 } catch (BootStrapException bse) { 177 System.out.println( 178 "The carbon services framework was unable to boot."); 179 bse.printStackTrace(); 180 throw bse; 181 } 182 183 this.componentKeeper = 185 loader.fetchComponentKeeper(configurationService); 186 try { 188 189 if (log.isDebugEnabled()) { 190 log.debug("Starting up components configured in [" 191 + STARTUP_COMPONENT_NAME + "]"); 192 } 193 StartupService startupService = (StartupService) 194 componentKeeper.fetchComponent(STARTUP_COMPONENT_NAME); 195 startupService.startComponents(); 196 } catch (Exception e) { 197 if (log.isWarnEnabled()) { 198 log.warn("Could not start startup components: " 199 + ExceptionUtility.printStackTracesToString(e)); 200 } 201 } 202 203 synchronized (this.stateLock) { 208 this.state = BootStrapperStateEnum.LOADED; 209 } 210 } 211 } 212 213 224 public BootStrapperStateEnum getState() { 225 synchronized (this.stateLock) { 228 return this.state; 229 } 230 } 231 232 255 public String getDeploymentProperty(String key) { 256 return this.deploymentProperties.get(key); 257 } 258 259 277 public Object setDeploymentProperty(String key, String value) { 278 return this.deploymentProperties.set(key, value); 279 } 280 281 292 private Loader fetchLoader() { 293 294 295 String loaderClassName = this.getDeploymentProperty( 296 BOOT_LOADER_PROPERTY); 297 298 if (loaderClassName == null || "".equals(loaderClassName)) { 299 loaderClassName = DEFAULT_LOADER_CLASS_NAME; 300 } 301 302 try { 303 304 if (log.isDebugEnabled()) { 305 log.debug("Using boot loader [" 306 + loaderClassName + "] to bootstrap the system"); 307 } 308 return (Loader) Class.forName(loaderClassName).newInstance(); 309 310 } catch (ClassCastException cce) { 311 throw new BootStrapException(this.getClass(), 312 "Specified loader [" + loaderClassName 313 + "] was not of the correct type: [" 314 + Loader.class.getName() + "]", cce); 315 } catch (ClassNotFoundException cnfe) { 316 throw new BootStrapException(this.getClass(), 317 "Could not instantiate Loader: [" 318 + loaderClassName + "]", cnfe); 319 } catch (InstantiationException ie) { 320 throw new BootStrapException(this.getClass(), 321 "Could not instantiate Loader: [" 322 + loaderClassName + "]", ie); 323 } catch (IllegalAccessException iae) { 324 throw new BootStrapException(this.getClass(), 325 "Could not instantiate Loader: [" 326 + loaderClassName + "]", iae); 327 } 328 } 329 330 333 public static final String BOOT_LOADER_PROPERTY = 334 "carbon.bootstrap.Loader"; 335 338 private static final String DEFAULT_LOADER_CLASS_NAME = 339 "org.sape.carbon.core.bootstrap.DefaultLoader"; 340 343 private static final String STARTUP_COMPONENT_NAME = 344 "/core/StartupService"; 345 348 private static final BootStrapper INSTANCE = new BootStrapper(); 349 350 351 352 353 361 public static BootStrapper getInstance() { 362 return BootStrapper.INSTANCE; 363 } 364 } 365 | Popular Tags |