KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > OpenEJB


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 1999 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: OpenEJB.java 2174 2005-09-21 00:59:18Z jcscoobyrs $
44  */

45
46
47 package org.openejb;
48
49 import java.net.URL JavaDoc;
50 import java.util.Date JavaDoc;
51 import java.util.Properties JavaDoc;
52
53 import javax.transaction.TransactionManager JavaDoc;
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 /**
67  * OpenEJB is the main factory for bootstrapping and obtaining a references to
68  * the ContainerSystem. The properties used in the static init( )
69  * method this class determines the characteristics of the ContainerSystem
70  * assembled at run time.
71  * <p>
72  * An OpenEJB container system is assembled at runtime by the application server
73  * according to the properties passed into the static init( ) method.
74  * Properties determine the Assembler used to assemble the ContainerSystem as
75  * well as the source configuration data used by the Assembler. A set of
76  * standard environment property names are defined in the org.openejb.EnvProps
77  * class.
78  * <p>
79  * Below is an example of how an application server would initialize and use this
80  * class to assemble and obtain an ContainerSystem.
81  * <p><blockquote><tt>
82  * Properties initProps = new Properites();
83  * initProps.setProperty(EnvProps.ASSEMBLER,"org.openejb.core.conf.Assembler");
84  * initProps.setProperty(EnvProps.CONFIGURATION, "/openejb/bin/config/openejb.xml");
85  * OpenEJB myEJB = OpenEJB.init(initProps);
86  * </tt></blockquote>
87  * <p>
88  * When assembling a ContainerSystem OpenEJB will use the Assembler implementation
89  * specified in the EnvProps.ASSEMBLER property. By default the
90  * org.openejb.core.conf.Assembler is used. In addition to specifying the Assembler,
91  * developers can also specify the location configuration of the configuration file
92  * that the Assembler will use. In the case of the default Assembler the configuration
93  * property must a URI to an XML document that adheres to the OpenEJB DTD.
94  * <p>
95  * Custom Assembler can be created that assemble containers from different a
96  * different configuration source using a different algorithm and implementation
97  * classes. See the org.openejb.spi.Assembler interfce for more details.
98  * <p>
99  * Initialization properties can also be declared in the System properties.
100  * The System properties are combined with properties explicitly passed in the
101  * OpenEJB.init( )method. Properties passed into the init( ) method override
102  * System properties.
103  * <p>
104  * OpenEJB provides a singleton interface for the OpenEJB container system. Only one OpenEJB
105  * instance can be constructed in the lifetime of a VM process.
106  * <p>
107  * @author Richard Monson-Haefel
108  * @author David Blevins
109  * @version $Rev: 2174 $
110  * @since JDK 1.2
111  * @see org.openejb.EnvProps
112  * @see org.openejb.alt.assembler.classic.Assembler
113  * @see org.openejb.spi.Assembler
114  */

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 JavaDoc transactionManager;
122     private static Properties JavaDoc 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 JavaDoc props)
128     throws OpenEJBException{
129         init(props,null);
130     }
131
132     /**
133      *
134      * @param initProps Specifies the Assembler and other properties used to build the ContainerSystem
135      * @param appServer application server
136      * @exception org.openejb.OpenEJBException Thrown if a problem occurs building the ContainerSystem
137      * @since JDK 1.2
138      */

139     public static void init(Properties JavaDoc initProps, ApplicationServer appServer) throws OpenEJBException {
140         try {
141             SystemInstance.init(initProps);
142         } catch (Exception JavaDoc e) {
143             throw new OpenEJBException(e);
144         }
145         if ( initialized ) {
146         String JavaDoc msg = messages.message( "startup.alreadyInitialzied" );
147             logger.i18n.error( msg );
148             throw new OpenEJBException( msg );
149         } else {
150             // Setting the handler system property should be the first thing
151
// OpenEJB does.
152
JarUtils.setHandlerSystemProperty();
153
154             Logger.initialize( initProps );
155             
156             logger = Logger.getInstance( "OpenEJB.startup", "org.openejb.util.resources" );
157         }
158
159     /*
160      * Output startup message
161      */

162     Properties JavaDoc versionInfo = new Properties JavaDoc();
163
164     try {
165         versionInfo.load( new URL JavaDoc( "resource:/openejb-version.properties" ).openConnection().getInputStream() );
166     } catch (java.io.IOException JavaDoc 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 JavaDoc(), 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         /* DMB: This is causing bug 725781.
181          * I can't even remember why we decided to add a default security manager.
182          * the testsuite runs fine without it, so out it goes for now.
183         SecurityManager sm = System.getSecurityManager();
184         if (sm == null) {
185             try{
186                 logger.i18n.debug( "startup.noSecurityManagerInstalled" );
187                 System.setSecurityManager(new SecurityManager(){
188                     public void checkPermission(Permission perm) {}
189                     public void checkPermission(Permission perm, Object context) {}
190
191                 });
192             } catch (Exception e){
193                 logger.i18n.warning( "startup.couldNotInstalllDefaultSecurityManager", e.getClass().getName(), e.getMessage() );
194             }
195         }
196         */

197
198         props = new Properties JavaDoc(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         /* Uses the EnvProps.ASSEMBLER property to obtain the Assembler impl.
213            Default is org.openejb.alt.assembler.classic.Assembler */

214         String JavaDoc 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 JavaDoc t ){
230         String JavaDoc 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 JavaDoc t ){
241         String JavaDoc 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 JavaDoc t ){
252         String JavaDoc 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 JavaDoc 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 JavaDoc(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 JavaDoc 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 JavaDoc(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 JavaDoc 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       //logger.i18n.debug("There are "+containerSystem.containers().length+" containers.");
301
//logger.i18n.debug("There are "+containerSystem.deployments().length+" ejb deployments.");
302

303         securityService = assembler.getSecurityService();
304         if (securityService == null) {
305         String JavaDoc 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 JavaDoc 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         // TODO: Figure out how to print out OpenEJB startup final message to Tomcat log
326
String JavaDoc 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     /**
333      * Gets the <code>TransactionManager</code> that this container manager exposes to the <code>Container</code>s it manages.
334      *
335      * @return the TransactionManager to be used by this container manager's containers when servicing beans
336      * @see "javax.transaction.TransactionManager"
337      * @see org.openejb.spi.TransactionService#getTransactionManager() TransactionService.getTransactionManager()
338      */

339     public static TransactionManager JavaDoc getTransactionManager( ){
340         return transactionManager;
341     }
342
343     /**
344      * Gets the <code>SecurityService</code> that this container manager exposes to the <code>Container</code>s it manages.
345      *
346      * @return the SecurityService to be used by this container manager's containers when servicing beans
347      * @see org.openejb.spi.SecurityService
348      */

349     public static SecurityService getSecurityService( ){
350         return securityService;
351     }
352
353     public static ApplicationServer getApplicationServer(){
354         return applicationServer;
355     }
356
357     /**
358      * Gets the <code>DeploymentInfo</code> object for the bean with the specified deployment id.
359      *
360      * @param id the deployment id of the deployed bean.
361      * @return the DeploymentInfo object associated with the bean.
362      * @see DeploymentInfo
363      * @see Container#getDeploymentInfo(Object) Container.getDeploymentInfo
364      * @see DeploymentInfo#getDeploymentID()
365      */

366     public static DeploymentInfo getDeploymentInfo(Object JavaDoc id){
367         return containerSystem.getDeploymentInfo(id);
368     }
369
370     /**
371      * Gets the <code>DeploymentInfo</code> objects for all the beans deployed in all the containers in this container system.
372      *
373      * @return an array of DeploymentInfo objects
374      * @see DeploymentInfo
375      * @see Container#deployments() Container.deployments()
376      */

377     public static DeploymentInfo [] deployments( ){
378         return containerSystem.deployments();
379     }
380
381     /**
382      * Returns the <code>Container</code> in this container system with the specified id.
383      *
384      * @param id the id of the Container
385      * @return the Container associated with the id
386      * @see Container
387      * @see Container#getContainerID() Container.getContainerID()
388      */

389     public static Container getContainer(Object JavaDoc id){
390         return containerSystem.getContainer(id);
391     }
392
393     /**
394      * Gets all the <code>Container</code>s in this container system.
395      *
396      * @return an array of all the Containers
397      * @see Container
398      */

399     public static Container [] containers() {
400         if ( containerSystem == null ) {// Something went wrong in the configuration.
401
logger.i18n.warning( "startup.noContainersConfigured" );
402             return null;
403         } else {
404             return containerSystem.containers();
405     }
406     }
407
408     /**
409     * Returns the global JNDI name space for the OpenEJB container system.
410     * The global JNDI name space contains bindings for all enterprise bean
411     * EJBHome object deployed in the entire container system. EJBHome objects
412     * are bound using their deployment-id under the java:openejb/ejb/ namespace.
413     * For example, an enterprise bean with the deployment id = 55555 would be
414     * have its EJBHome bound to the name "java:openejb/ejb/55555"
415     *
416     * @return the global JNDI context
417     */

418     public static javax.naming.Context JavaDoc getJNDIContext(){
419         return containerSystem.getJNDIContext();
420     }
421
422     /**
423     * This method returns a clone of the original properties used to initialize the OpenEJB
424     * class. Modifications to the clone will not affect the operations of the OpenEJB
425     * container system.
426     */

427     public static Properties JavaDoc getInitProps( ){
428         return (Properties JavaDoc)props.clone();
429     }
430
431     public static boolean isInitialized(){
432         return initialized;
433     }
434
435     /**
436      * @param property property name the value is asked for
437      * @return property value
438      */

439     public static String JavaDoc getProperty(String JavaDoc property)
440     {
441         return props.getProperty(property);
442     }
443
444     public static ClassLoader JavaDoc getContextClassLoader() {
445         return (ClassLoader JavaDoc) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction JavaDoc() {
446             public Object JavaDoc run() {
447                 return Thread.currentThread().getContextClassLoader();
448             }
449         });
450     }
451 }
452
Popular Tags