1 45 46 47 package org.openejb; 48 49 import java.net.URL ; 50 import java.util.Date ; 51 import java.util.Properties ; 52 53 import javax.transaction.TransactionManager ; 54 55 import org.openejb.spi.ApplicationServer; 56 import org.openejb.spi.Assembler; 57 import org.openejb.spi.ContainerSystem; 58 import org.openejb.spi.SecurityService; 59 import org.openejb.util.JarUtils; 60 import org.openejb.util.Logger; 61 import org.openejb.util.Messages; 62 import org.openejb.util.SafeToolkit; 63 import org.openejb.loader.SystemInstance; 64 65 66 115 116 public final class OpenEJB { 117 118 private static ContainerSystem containerSystem; 119 private static SecurityService securityService; 120 private static ApplicationServer applicationServer; 121 private static TransactionManager transactionManager; 122 private static Properties props; 123 private static boolean initialized; 124 private static Logger logger; 125 private static Messages messages = new Messages( "org.openejb.util.resources" ); 126 127 public static void init(Properties props) 128 throws OpenEJBException{ 129 init(props,null); 130 } 131 132 139 public static void init(Properties initProps, ApplicationServer appServer) throws OpenEJBException { 140 try { 141 SystemInstance.init(initProps); 142 } catch (Exception e) { 143 throw new OpenEJBException(e); 144 } 145 if ( initialized ) { 146 String msg = messages.message( "startup.alreadyInitialzied" ); 147 logger.i18n.error( msg ); 148 throw new OpenEJBException( msg ); 149 } else { 150 JarUtils.setHandlerSystemProperty(); 153 154 Logger.initialize( initProps ); 155 156 logger = Logger.getInstance( "OpenEJB.startup", "org.openejb.util.resources" ); 157 } 158 159 162 Properties versionInfo = new Properties (); 163 164 try { 165 versionInfo.load( new URL ( "resource:/openejb-version.properties" ).openConnection().getInputStream() ); 166 } catch (java.io.IOException e) { 167 } 168 if( initProps.getProperty( "openejb.nobanner" ) == null ) { 169 System.out.println( "OpenEJB " + versionInfo.get( "version" ) +" build: "+versionInfo.get( "date" )+"-"+versionInfo.get( "time" )); 170 System.out.println( "" + versionInfo.get( "url" ) ); 171 } 172 173 logger.i18n.info( "startup.banner", versionInfo.get( "url" ), new Date (), versionInfo.get( "copyright" ), 174 versionInfo.get( "version" ), versionInfo.get( "date" ), versionInfo.get( "time" ) ); 175 176 logger.info("openejb.home = "+SystemInstance.get().getHome().getDirectory().getAbsolutePath()); 177 logger.info("openejb.base = "+SystemInstance.get().getBase().getDirectory().getAbsolutePath()); 178 179 180 197 198 props = new Properties (System.getProperties()); 199 200 if ( initProps == null ) { 201 logger.i18n.debug( "startup.noInitializationProperties" ); 202 } else { 203 props.putAll( initProps ); 204 } 205 206 if ( appServer == null ) logger.i18n.warning( "startup.noApplicationServerSpecified" ); 207 applicationServer = appServer; 208 209 210 SafeToolkit toolkit = SafeToolkit.getToolkit("OpenEJB"); 211 212 214 String className = props.getProperty( EnvProps.ASSEMBLER ); 215 if ( className == null ) { 216 className = props.getProperty( "openejb.assembler", "org.openejb.alt.assembler.classic.Assembler" ); 217 } else { 218 logger.i18n.warning( "startup.deprecatedPropertyName", EnvProps.ASSEMBLER ); 219 } 220 221 logger.i18n.debug( "startup.instantiatingAssemberClass", className ); 222 Assembler assembler = null; 223 224 try { 225 assembler = (Assembler)toolkit.newInstance(className); 226 } catch ( OpenEJBException oe ){ 227 logger.i18n.fatal( "startup.assemblerCannotBeInstanitated", oe ); 228 throw oe; 229 } catch ( Throwable t ){ 230 String msg = messages.message( "startup.openEjbEncounterUnexpectedError" ); 231 logger.i18n.fatal( msg, t ); 232 throw new OpenEJBException( msg, t ); 233 } 234 235 try { 236 assembler.init(props); 237 } catch ( OpenEJBException oe ){ 238 logger.i18n.fatal( "startup.assemblerFailedToInitialize", oe ); 239 throw oe; 240 } catch ( Throwable t ){ 241 String msg = messages.message( "startup.assemblerEncounterUnexpectedError" ); 242 logger.i18n.fatal( msg, t ); 243 throw new OpenEJBException( msg, t ); 244 } 245 246 try { 247 assembler.build(); 248 } catch ( OpenEJBException oe ){ 249 logger.i18n.fatal( "startup.assemblerFailedToBuild", oe ); 250 throw oe; 251 } catch ( Throwable t ){ 252 String msg = messages.message( "startup.assemblerEncounterUnexpectedBuildError" ); 253 logger.i18n.fatal( msg, t ); 254 throw new OpenEJBException( msg, t ); 255 } 256 257 containerSystem = assembler.getContainerSystem(); 258 if (containerSystem == null) { 259 String msg = messages.message( "startup.assemblerReturnedNullContainer" ); 260 logger.i18n.fatal( msg ); 261 throw new OpenEJBException( msg ); 262 } 263 264 if (logger.isDebugEnabled()){ 265 logger.i18n.debug( "startup.debugContainers", new Integer (containerSystem.containers().length) ); 266 267 if (containerSystem.containers().length > 0) { 268 Container[] c = containerSystem.containers(); 269 logger.i18n.debug( "startup.debugContainersType" ); 270 for (int i=0; i < c.length; i++){ 271 String entry = " "; 272 switch ( c[i].getContainerType() ) { 273 case Container.ENTITY: entry += "ENTITY "; break; 274 case Container.STATEFUL: entry += "STATEFUL "; break; 275 case Container.STATELESS: entry += "STATELESS "; break; 276 } 277 entry += c[i].getContainerID(); 278 logger.i18n.debug( "startup.debugEntry", entry) ; 279 } 280 } 281 282 logger.i18n.debug( "startup.debugDeployments", new Integer (containerSystem.deployments().length) ); 283 if (containerSystem.deployments().length > 0) { 284 logger.i18n.debug( "startup.debugDeploymentsType" ); 285 DeploymentInfo[] d = containerSystem.deployments(); 286 for (int i=0; i < d.length; i++){ 287 String entry = " "; 288 switch ( d[i].getComponentType() ) { 289 case DeploymentInfo.BMP_ENTITY: entry += "BMP_ENTITY "; break; 290 case DeploymentInfo.CMP_ENTITY: entry += "CMP_ENTITY "; break; 291 case DeploymentInfo.STATEFUL: entry += "STATEFUL "; break; 292 case DeploymentInfo.STATELESS: entry += "STATELESS "; break; 293 } 294 entry += d[i].getDeploymentID(); 295 logger.i18n.debug( "startup.debugEntry", entry ); 296 } 297 } 298 } 299 300 303 securityService = assembler.getSecurityService(); 304 if (securityService == null) { 305 String msg = messages.message( "startup.assemblerReturnedNullSecurityService" ); 306 logger.i18n.fatal( msg ); 307 throw new OpenEJBException( msg ); 308 } else { 309 logger.i18n.debug( "startup.securityService", securityService.getClass().getName() ); 310 } 311 312 transactionManager = assembler.getTransactionManager(); 313 if (transactionManager == null) { 314 String msg = messages.message( "startup.assemblerReturnedNullTransactionManager" ); 315 logger.i18n.fatal( msg ); 316 throw new OpenEJBException( msg ); 317 } else { 318 logger.i18n.debug( "startup.transactionManager", transactionManager.getClass().getName() ); 319 } 320 321 initialized = true; 322 323 logger.i18n.info( "startup.ready" ); 324 325 String loader = initProps.getProperty("openejb.loader"), nobanner = initProps.getProperty("openejb.nobanner"); 327 if (nobanner == null && (loader == null || (loader != null && loader.startsWith("tomcat")))) { 328 System.out.println(messages.message("startup.ready")); 329 } 330 } 331 332 339 public static TransactionManager getTransactionManager( ){ 340 return transactionManager; 341 } 342 343 349 public static SecurityService getSecurityService( ){ 350 return securityService; 351 } 352 353 public static ApplicationServer getApplicationServer(){ 354 return applicationServer; 355 } 356 357 366 public static DeploymentInfo getDeploymentInfo(Object id){ 367 return containerSystem.getDeploymentInfo(id); 368 } 369 370 377 public static DeploymentInfo [] deployments( ){ 378 return containerSystem.deployments(); 379 } 380 381 389 public static Container getContainer(Object id){ 390 return containerSystem.getContainer(id); 391 } 392 393 399 public static Container [] containers() { 400 if ( containerSystem == null ) { logger.i18n.warning( "startup.noContainersConfigured" ); 402 return null; 403 } else { 404 return containerSystem.containers(); 405 } 406 } 407 408 418 public static javax.naming.Context getJNDIContext(){ 419 return containerSystem.getJNDIContext(); 420 } 421 422 427 public static Properties getInitProps( ){ 428 return (Properties )props.clone(); 429 } 430 431 public static boolean isInitialized(){ 432 return initialized; 433 } 434 435 439 public static String getProperty(String property) 440 { 441 return props.getProperty(property); 442 } 443 444 public static ClassLoader getContextClassLoader() { 445 return (ClassLoader ) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction () { 446 public Object run() { 447 return Thread.currentThread().getContextClassLoader(); 448 } 449 }); 450 } 451 } 452 | Popular Tags |