KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > alt > assembler > classic > Assembler


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: Assembler.java 2107 2005-08-26 21:06:05Z dblevins $
44  */

45 package org.openejb.alt.assembler.classic;
46
47 import java.util.ArrayList JavaDoc;
48 import java.util.Arrays JavaDoc;
49 import java.util.HashMap JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.Properties JavaDoc;
52
53 import javax.resource.spi.ConnectionManager JavaDoc;
54 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
55 import javax.transaction.TransactionManager JavaDoc;
56
57 import org.openejb.EnvProps;
58 import org.openejb.OpenEJBException;
59 import org.openejb.core.ConnectorReference;
60 import org.openejb.core.DeploymentInfo;
61 import org.openejb.spi.SecurityService;
62 import org.openejb.util.OpenEJBErrorHandler;
63 import org.openejb.util.SafeToolkit;
64 /**
65  * <pre>
66  * This method performs the following actions(in order):
67  *
68  * 1 Assembles Containers and Deployments
69  * 1.1 Assembles StatefulContainer(s)
70  * 1.1.1 Assembles Stateful SessionBean Deployments
71  * 1.2 Assembles StatelessContainer(s)
72  * 1.2.1 Assembles Stateless SessionBean Deployments
73  * 1.3 Assembles EntityContainer(s)
74  * 1.3.1 Assembles EntityBean Deployments
75  * 2 Assembles SecurityService
76  * 3 Assembles TransactionService
77  * 4 Assembles ConnectionManager(s)
78  * 5 Assembles Connector(s)
79  * 7 Applies MethodPermission(s)
80  * 8 Applies SecurityRole(s)
81  * 8 Applies TransactionAttribute(s)
82  * 10 Assembles ProxyFactory
83  * 11 Assembles bean JNDI namespaces
84  * </pre>
85  *
86  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
87  * @author <a HREF="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
88  * @see org.openejb.spi.Assembler
89  * @see OpenEjbConfigurationFactory
90  */

91 public class Assembler extends AssemblerTool implements org.openejb.spi.Assembler{
92     private org.openejb.core.ContainerSystem containerSystem;
93     private TransactionManager JavaDoc transactionManager;
94     private org.openejb.spi.SecurityService securityService;
95     private HashMap JavaDoc remoteJndiContexts = null;
96     
97     public org.openejb.spi.ContainerSystem getContainerSystem(){
98         return containerSystem;
99     }
100     public TransactionManager JavaDoc getTransactionManager(){
101         return transactionManager;
102     }
103     public SecurityService getSecurityService(){
104         return securityService;
105     }
106
107     protected SafeToolkit toolkit = SafeToolkit.getToolkit("Assembler");
108     protected OpenEjbConfiguration config;
109
110
111     //==================================
112
// Error messages
113

114     private String JavaDoc INVALID_CONNECTION_MANAGER_ERROR = "Invalid connection manager specified for connector identity = ";
115     
116     // Error messages
117
//==================================
118

119
120     public Assembler(){
121     }
122
123     public void init(Properties JavaDoc props) throws OpenEJBException{
124         this.props = props;
125
126         /* Get Configuration ////////////////////////////*/
127         String JavaDoc className = props.getProperty(EnvProps.CONFIGURATION_FACTORY);
128         if ( className == null ) className = props.getProperty("openejb.configurator","org.openejb.alt.config.ConfigurationFactory");
129         
130         OpenEjbConfigurationFactory configFactory = (OpenEjbConfigurationFactory)toolkit.newInstance(className);
131         configFactory.init(props);
132         config = configFactory.getOpenEjbConfiguration();
133         /*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
134
135         
136         /* Add IntraVM JNDI service /////////////////////*/
137         Properties JavaDoc systemProperties = System.getProperties();
138         synchronized(systemProperties){
139             String JavaDoc str = systemProperties.getProperty(javax.naming.Context.URL_PKG_PREFIXES);
140             String JavaDoc naming = "org.openejb.core.ivm.naming";
141             if (str == null) {
142                 str = naming;
143             } else if (str.indexOf(naming) == -1) {
144                 str = naming + ":" + str;
145             }
146             systemProperties.setProperty(javax.naming.Context.URL_PKG_PREFIXES, str);
147         }
148         /*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
149     }
150
151     public void build() throws OpenEJBException{
152         try{
153         containerSystem = buildContainerSystem(config);
154         }catch(OpenEJBException ae){
155             /* OpenEJBExceptions contain useful information and are debbugable.
156              * Let the exception pass through to the top and be logged.
157              */

158              throw ae;
159         }catch(Exception JavaDoc e){
160             /* General Exceptions at this level are too generic and difficult to debug.
161              * These exceptions are considered unknown bugs and are fatal.
162              * If you get an error at this level, please trap and handle the error
163              * where it is most relevant.
164              */

165             OpenEJBErrorHandler.handleUnknownError(e, "Assembler");
166             throw new OpenEJBException(e);
167         }
168     }
169     
170     /////////////////////////////////////////////////////////////////////
171
////
172
//// Public Methods Used for Assembly
173
////
174
/////////////////////////////////////////////////////////////////////
175

176     /**
177      * When given a complete OpenEjbConfiguration graph this method,
178      * will construct an entire container system and return a reference to that
179      * container system, as ContainerSystem instance.
180      *
181      * This method leverage the other assemble and apply methods which
182      * can be used independently.
183      *
184      * Assembles and returns the {@link org.openejb.core.ContainerSystem} using the
185      * information from the {@link OpenEjbConfiguration} object passed in.
186      * <pre>
187      * This method performs the following actions(in order):
188      *
189      * 1 Assembles ProxyFactory
190      * 2 Assembles Containers and Deployments
191      * 3 Assembles SecurityService
192      * 4 Apply method permissions, role refs, and tx attributes
193      * 5 Assembles TransactionService
194      * 6 Assembles ConnectionManager(s)
195      * 7 Assembles Connector(s)
196      * </pre>
197      *
198      * @param configInfo
199      * @return ContainerSystem
200      * @exception Exception if there was a problem constructing the ContainerSystem.
201      * @exception Exception
202      * @see OpenEjbConfiguration
203      */

204     public org.openejb.core.ContainerSystem buildContainerSystem(OpenEjbConfiguration configInfo)throws Exception JavaDoc{
205
206         /*[1] Assemble ProxyFactory //////////////////////////////////////////
207             
208             This operation must take place first because of interdependencies.
209             As DeploymentInfo objects are registered with the ContainerSystem using the
210             ContainerSystem.addDeploymentInfo() method, they are also added to the JNDI
211             Naming Service for OpenEJB. This requires that a proxy for the deployed bean's
212             EJBHome be created. The proxy requires that the default proxy factory is set.
213         */

214         
215         this.applyProxyFactory(configInfo.facilities.intraVmServer);
216         /*[1]\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
217         
218         
219         ContainerSystemInfo containerSystemInfo = configInfo.containerSystem;
220
221         
222         
223         org.openejb.core.ContainerSystem containerSystem = new org.openejb.core.ContainerSystem();
224
225         /*[2] Assemble Containers and Deployments ///////////////////////////////////*/
226         
227         assembleContainers(containerSystem,containerSystemInfo);
228         /*[2]\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
229
230         
231         /*[3] Assemble SecurityServices ////////////////////////////////////*/
232         securityService = assembleSecurityService(configInfo.facilities.securityService);
233         containerSystem.getJNDIContext().bind("java:openejb/SecurityService",securityService);
234         
235         /*[3]\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
236
237         /*[4] Apply method permissions, role refs, and tx attributes ////////////////////////////////////*/
238
239         // roleMapping used later in buildMethodPermissions
240
AssemblerTool.RoleMapping roleMapping = new AssemblerTool.RoleMapping(configInfo.facilities.securityService.roleMappings);
241         org.openejb.DeploymentInfo [] deployments = containerSystem.deployments();
242         for(int i = 0; i < deployments.length; i++){
243             applyMethodPermissions((org.openejb.core.DeploymentInfo)deployments[i], containerSystemInfo.methodPermissions, roleMapping);
244             applyTransactionAttributes((org.openejb.core.DeploymentInfo)deployments[i],containerSystemInfo.methodTransactions);
245         }
246         
247         ArrayList JavaDoc list = new ArrayList JavaDoc();
248         if(containerSystemInfo.entityContainers!=null)list.addAll(Arrays.asList(containerSystemInfo.entityContainers));
249         if(containerSystemInfo.statefulContainers!=null)list.addAll(Arrays.asList(containerSystemInfo.statefulContainers));
250         if(containerSystemInfo.statelessContainers!=null)list.addAll(Arrays.asList(containerSystemInfo.statelessContainers));
251         Iterator JavaDoc iterator = list.iterator();
252         while(iterator.hasNext()){
253             ContainerInfo container = (ContainerInfo)iterator.next();
254             for(int z = 0; z < container.ejbeans.length; z++){
255                 DeploymentInfo deployment= (org.openejb.core.DeploymentInfo)containerSystem.getDeploymentInfo(container.ejbeans[z].ejbDeploymentId);
256                 applySecurityRoleReference(deployment, container.ejbeans[z], roleMapping);
257             }
258         }
259         /*[4]\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
260         if(configInfo.facilities.remoteJndiContexts!=null){
261             for(int i = 0; i < configInfo.facilities.remoteJndiContexts.length; i++){
262                 javax.naming.InitialContext JavaDoc cntx = assembleRemoteJndiContext(configInfo.facilities.remoteJndiContexts[i]);
263                 containerSystem.getJNDIContext().bind("java:openejb/remote_jndi_contexts/"+configInfo.facilities.remoteJndiContexts[i].jndiContextId, cntx);
264             }
265             
266         }
267         
268
269         /*[5] Assemble TransactionManager /////////////////////////////////*/
270         transactionManager = assembleTransactionManager(configInfo.facilities.transactionService);
271         containerSystem.getJNDIContext().bind("java:openejb/TransactionManager",transactionManager);
272         
273         /*[5]\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
274         
275         /*[6] Assemble Connector(s) //////////////////////////////////////////*/
276         HashMap JavaDoc connectionManagerMap = new HashMap JavaDoc();
277         // connectors are optional in the openejb_config.dtd
278
if (configInfo.facilities.connectionManagers != null) {
279             for(int i = 0; i < configInfo.facilities.connectionManagers.length;i++){
280                 ConnectionManagerInfo cmInfo = configInfo.facilities.connectionManagers[i];
281                 ConnectionManager JavaDoc connectionManager = assembleConnectionManager(cmInfo);
282                 connectionManagerMap.put(cmInfo.connectionManagerId,connectionManager);
283             }
284         }
285         // connectors are optional in the openejb_config.dtd
286
if (configInfo.facilities.connectors != null) {
287             for(int i = 0; i < configInfo.facilities.connectors.length; i++){
288                 ConnectorInfo conInfo = configInfo.facilities.connectors[i];
289                             
290                 ConnectionManager JavaDoc connectionManager = (ConnectionManager JavaDoc)connectionManagerMap.get(conInfo.connectionManagerId);
291                 if(connectionManager == null)
292                     throw new RuntimeException JavaDoc(INVALID_CONNECTION_MANAGER_ERROR + conInfo.connectorId);
293                                 
294                 ManagedConnectionFactory JavaDoc managedConnectionFactory = assembleManagedConnectionFactory(conInfo.managedConnectionFactory);
295                 
296                 ConnectorReference reference = new ConnectorReference(connectionManager, managedConnectionFactory);
297                             
298                 containerSystem.getJNDIContext().bind("java:openejb/connector/"+conInfo.connectorId, reference);
299             }
300         }
301         /*[6]\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
302         return containerSystem;
303     }
304 }
305
Popular Tags