KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > core > ContainerSystem


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: ContainerSystem.java 1921 2005-06-19 22:40:34Z jlaskowski $
44  */

45 package org.openejb.core;
46
47 import java.util.HashMap JavaDoc;
48
49 import org.openejb.Container;
50 import org.openejb.DeploymentInfo;
51 import org.openejb.core.ivm.naming.IvmContext;
52 import org.openejb.core.ivm.naming.ObjectReference;
53 import org.openejb.core.ivm.naming.Reference;
54
55 public class ContainerSystem implements org.openejb.spi.ContainerSystem{
56
57     HashMap JavaDoc deployments = new HashMap JavaDoc();
58     HashMap JavaDoc containers = new HashMap JavaDoc();
59     IvmContext jndiRootContext = null;
60
61
62     public ContainerSystem( ){
63         // create OpenEJB JNDI Name Space
64
try {
65         // Create the root context.
66
jndiRootContext = IvmContext.createRootContext();
67         // Create a subcontext to house the EJBs.
68
jndiRootContext.createSubcontext("java:openejb/ejb");
69     }
70     catch( javax.naming.NamingException JavaDoc exception) {
71         throw new RuntimeException JavaDoc();
72     }
73     }
74
75     /**
76      * Gets the <code>DeploymentInfo</code> object for the bean with the specified deployment id.
77      *
78      * @param id the deployment id of the deployed bean.
79      * @return the DeploymentInfo object associated with the bean.
80      * @see DeploymentInfo
81      * @see org.openejb.Container#getDeploymentInfo(Object) Container.getDeploymentInfo
82      * @see DeploymentInfo#getDeploymentID()
83      */

84     public DeploymentInfo getDeploymentInfo(Object JavaDoc id){
85         return (DeploymentInfo)deployments.get(id);
86     }
87
88     /**
89      * Gets the <code>DeploymentInfo</code> objects for all the beans deployed in all the containers in this container system.
90      *
91      * @return an array of DeploymentInfo objects
92      * @see DeploymentInfo
93      * @see Container#deployments() Container.deployments()
94      */

95     public DeploymentInfo [] deployments( ){
96         return (DeploymentInfo [])deployments.values().toArray(new DeploymentInfo [deployments.size()]);
97     }
98
99     /**
100      * Returns the <code>Container</code> in this container system with the specified id.
101      *
102      * @param id the id of the Container
103      * @return the Container associated with the id
104      * @see Container
105      */

106     public Container getContainer(Object JavaDoc id){
107         return (Container)containers.get(id);
108     }
109
110     /**
111      * Gets all the <code>Container</code>s in this container system.
112      *
113      * @return an array of all the Containers
114      * @see Container
115      */

116     public Container [] containers( ){
117         return (Container [])containers.values().toArray(new Container [containers.size()]);
118     }
119
120     /**
121      * Adds a Container to the list of those that are managed by this container system.
122      * If a Container previously existed with the same id it will be replaced.
123      * @param id the id of the Container
124      * @param c Container to manage
125      * @see org.openejb.Container
126      */

127     public void addContainer(Object JavaDoc id, Container c){
128         containers.put(id,c);
129     }
130
131
132     /**
133      * Adds a DeploymentInfo object to the list of those that are registered
134      * by this container System.
135      *
136      * If a DeploymentInfo object previously existed with the same id it will
137      * be replaced.
138      *
139      * Also adds deployment to OpenEJB's global JNDI Name Space under the context
140      * java:openejb/ejb/<i>deployment-id</i>
141      *
142      * The global JNDI name space contains bindings for all enterprise bean
143      * EJBHome object deployed in the entire container system. EJBHome objects
144      * are bound using their deployment-id under the java:openejb/ejb/ namespace.
145      * For example, an enterprise bean with the deployment id = 55555 would be
146      * have its EJBHome bound to the name "java:openejb/ejb/55555"
147      *
148      * @param deployment
149      * @see org.openejb.DeploymentInfo
150      */

151     public void addDeployment(org.openejb.core.DeploymentInfo deployment){
152
153         // add deployment to registry
154
this.deployments.put(deployment.getDeploymentID(),deployment);
155
156         // add deployment to OpenEJB JNDI Name Space
157
if (deployment.getHomeInterface() != null){
158             bindProxy(deployment, deployment.getEJBHome(), false);
159         }
160         if (deployment.getLocalHomeInterface() != null){
161             bindProxy(deployment, deployment.getEJBLocalHome(), true);
162         }
163     }
164
165     private void bindProxy(org.openejb.core.DeploymentInfo deployment, Object JavaDoc proxy, boolean isLocal) {
166         Reference ref = new ObjectReference( proxy );
167         
168         if(deployment.getComponentType()== DeploymentInfo.STATEFUL){
169             ref = new org.openejb.core.stateful.EncReference( ref );
170         } else if(deployment.getComponentType()== DeploymentInfo.STATELESS){
171             ref = new org.openejb.core.stateless.EncReference( ref );
172         } else{
173             ref = new org.openejb.core.entity.EncReference( ref );
174         }
175         
176         try{
177
178             String JavaDoc bindName = deployment.getDeploymentID().toString();
179             
180             if(bindName.charAt(0)== '/') {
181                 bindName = bindName.substring(1);
182             }
183             
184             bindName = "openejb/ejb/"+bindName;
185             if (isLocal){
186                 bindName += "Local";
187             }
188             jndiRootContext.bind(bindName, ref);
189             
190         }catch(Exception JavaDoc e){ e.printStackTrace();throw new RuntimeException JavaDoc();}
191     }
192
193     /**
194     * Returns the global JNDI name space for the OpenEJB container system.
195     * The global JNDI name space contains bindings for all enterprise bean
196     * EJBHome object deployed in the entire container system. EJBHome objects
197     * are bound using their deployment-id under the java:openejb/ejb/ namespace.
198     * For example, an enterprise bean with the deployment id = 55555 would be
199     * have its EJBHome bound to the name "java:openejb/ejb/55555"
200     *
201     * @return the global JNDI context
202     */

203     public javax.naming.Context JavaDoc getJNDIContext(){
204         return jndiRootContext;
205     }
206 }
207
Popular Tags