KickJava   Java API By Example, From Geeks To Geeks.

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


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 "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact info@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP 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  * THE OPENEJB GROUP 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 2005 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: ContainerBuilder.java 2129 2005-08-30 04:46:33Z dblevins $
44  */

45
46 package org.openejb.alt.assembler.classic;
47
48 import org.openejb.Container;
49 import org.openejb.OpenEJBException;
50 import org.openejb.RpcContainer;
51 import org.openejb.loader.SystemInstance;
52 import org.openejb.core.DeploymentInfo;
53 import org.openejb.util.Logger;
54 import org.openejb.util.SafeToolkit;
55
56 import java.io.File JavaDoc;
57 import java.lang.reflect.Constructor JavaDoc;
58 import java.lang.reflect.InvocationTargetException JavaDoc;
59 import java.net.MalformedURLException JavaDoc;
60 import java.net.URL JavaDoc;
61 import java.net.URLClassLoader JavaDoc;
62 import java.util.ArrayList JavaDoc;
63 import java.util.HashMap JavaDoc;
64 import java.util.List JavaDoc;
65 import java.util.Properties JavaDoc;
66
67 /**
68  * @version $Revision: 2129 $ $Date: 2005-08-29 21:46:33 -0700 (Mon, 29 Aug 2005) $
69  */

70 public class ContainerBuilder {
71
72     private static final Logger logger = Logger.getInstance("OpenEJB", "org.openejb.util.resources");
73
74     private final Properties JavaDoc props;
75     private final EjbJarInfo[] ejbJars;
76     private final ContainerInfo[] containerInfos;
77     private final String JavaDoc[] decorators;
78
79
80     public ContainerBuilder(ContainerSystemInfo containerSystemInfo, Properties JavaDoc props) {
81         this.props = props;
82         this.ejbJars = containerSystemInfo.ejbJars;
83         this.containerInfos = containerSystemInfo.containers;
84         String JavaDoc decorators = props.getProperty("openejb.container.decorators");
85         this.decorators = (decorators == null)? new String JavaDoc[]{}: decorators.split(":");
86     }
87
88     public Object JavaDoc build() throws OpenEJBException {
89         HashMap JavaDoc deployments = new HashMap JavaDoc();
90         URL JavaDoc[] jars = new URL JavaDoc[this.ejbJars.length];
91         for (int i = 0; i < this.ejbJars.length; i++) {
92             try {
93                 jars[i] = new File JavaDoc(this.ejbJars[i].jarPath).toURL();
94             } catch (MalformedURLException JavaDoc e) {
95                 throw new OpenEJBException(AssemblerTool.messages.format("cl0001", ejbJars[i].jarPath, e.getMessage()));
96             }
97         }
98
99         ClassLoader JavaDoc classLoader = new URLClassLoader JavaDoc(jars, org.openejb.OpenEJB.class.getClassLoader());
100
101         for (int i = 0; i < this.ejbJars.length; i++) {
102             EjbJarInfo ejbJar = this.ejbJars[i];
103
104             EnterpriseBeanInfo[] ejbs = ejbJar.enterpriseBeans;
105             for (int j = 0; j < ejbs.length; j++) {
106                 EnterpriseBeanInfo ejbInfo = ejbs[j];
107                 EnterpriseBeanBuilder deploymentBuilder = new EnterpriseBeanBuilder(classLoader, ejbInfo);
108                 DeploymentInfo deployment = (DeploymentInfo) deploymentBuilder.build();
109                 deployments.put(ejbInfo.ejbDeploymentId, deployment);
110             }
111         }
112
113         List JavaDoc containers = new ArrayList JavaDoc();
114         for (int i = 0; i < containerInfos.length; i++) {
115             ContainerInfo containerInfo = containerInfos[i];
116
117             HashMap JavaDoc deploymentsList = new HashMap JavaDoc();
118             for (int z = 0; z < containerInfo.ejbeans.length; z++) {
119                 String JavaDoc ejbDeploymentId = containerInfo.ejbeans[z].ejbDeploymentId;
120                 DeploymentInfo deployment = (DeploymentInfo) deployments.get(ejbDeploymentId);
121                 deploymentsList.put(ejbDeploymentId, deployment);
122             }
123
124             containers.add(buildContainer(containerInfo, deploymentsList));
125         }
126         return containers;
127     }
128
129     private Container buildContainer(ContainerInfo containerInfo, HashMap JavaDoc deploymentsList) throws OpenEJBException {
130         String JavaDoc className = containerInfo.className;
131         String JavaDoc codebase = containerInfo.codebase;
132         String JavaDoc containerName = containerInfo.containerName;
133
134         try {
135             Class JavaDoc factory = SafeToolkit.loadClass(className, codebase);
136             if (!Container.class.isAssignableFrom(factory)) {
137                 throw new OpenEJBException(AssemblerTool.messages.format("init.0100", "Container", containerName, factory.getName(), Container.class.getName()));
138             }
139
140             Properties JavaDoc clonedProps = (Properties JavaDoc) (props.clone());
141             clonedProps.putAll(containerInfo.properties);
142
143             Container container = (Container) factory.newInstance();
144
145             ClassLoader JavaDoc contextClassLoader = Thread.currentThread().getContextClassLoader();
146             for (int i = 0; i < decorators.length && container instanceof RpcContainer; i++) {
147                 try {
148                     String JavaDoc decoratorName = decorators[i];
149                     Class JavaDoc decorator = contextClassLoader.loadClass(decoratorName);
150                     Constructor JavaDoc constructor = decorator.getConstructor(new Class JavaDoc[]{RpcContainer.class});
151                     container = (Container) constructor.newInstance(new Object JavaDoc[]{container});
152                 } catch (NoSuchMethodException JavaDoc e) {
153                     String JavaDoc name = decorators[i].replaceAll(".*\\.", "");
154                     logger.error("Container wrapper " + decorators[i] + " does not have the required constructor 'public " + name + "(RpcContainer container)'");
155                 } catch (InvocationTargetException JavaDoc e) {
156                     logger.error("Container wrapper " + decorators[i] + " could not be constructed and will be skipped. Received message: " + e.getCause().getMessage(), e.getCause());
157                 } catch (ClassNotFoundException JavaDoc e) {
158                     logger.error("Container wrapper class " + decorators[i] + " could not be loaded and will be skipped.");
159                 }
160             }
161
162             Properties JavaDoc systemProperties = System.getProperties();
163             synchronized(systemProperties) {
164                 String JavaDoc userDir = systemProperties.getProperty("user.dir");
165                 try{
166                     File JavaDoc base = SystemInstance.get().getBase().getDirectory();
167                     systemProperties.setProperty("user.dir", base.getAbsolutePath());
168                     container.init(containerName, deploymentsList, clonedProps);
169                 } finally {
170                     systemProperties.setProperty("user.dir",userDir);
171                 }
172             }
173
174             return container;
175         } catch (OpenEJBException e) {
176             throw new OpenEJBException(AssemblerTool.messages.format("as0002", containerName, e.getMessage()));
177         } catch (InstantiationException JavaDoc e) {
178             throw new OpenEJBException(AssemblerTool.messages.format("as0003", containerName, e.getMessage()));
179         } catch (IllegalAccessException JavaDoc e) {
180             throw new OpenEJBException(AssemblerTool.messages.format("as0003", containerName, e.getMessage()));
181         }
182     }
183 }
184
Popular Tags