KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > alt > config > AutoDeployer


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 dev@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://www.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 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: AutoDeployer.java 2082 2005-08-16 04:18:56Z dblevins $
44  */

45 package org.openejb.alt.config;
46
47 import java.lang.reflect.Method JavaDoc;
48
49 import org.openejb.OpenEJBException;
50 import org.openejb.alt.config.ejb11.EjbDeployment;
51 import org.openejb.alt.config.ejb11.EjbJar;
52 import org.openejb.alt.config.ejb11.OpenejbJar;
53 import org.openejb.alt.config.ejb11.ResourceLink;
54 import org.openejb.alt.config.ejb11.ResourceRef;
55 import org.openejb.alt.config.sys.Connector;
56 import org.openejb.alt.config.sys.Container;
57 import org.openejb.alt.config.sys.Openejb;
58 import org.openejb.util.SafeToolkit;
59
60 /**
61  * This class represents a command line tool for deploying beans.
62  *
63  * At the moment it contains multiple println statements
64  * and statements that read input from the user.
65  *
66  * These statements are really in chunks in specific times throughout
67  * the class. These chunks could be refactored into methods. Then
68  * the implementation of those methods could actually be delegated
69  * to another class that implements a specific interface we create.
70  *
71  * The command line statements could be moved into an implementation
72  * of this new interface. We could then create another implementation
73  * that gathers information from a GUI.
74  *
75  * This would give us a Deploy API rather than just a command line
76  * tool. Then beans could be deployed programmatically by another
77  * application, by a GUI screen, or by command line.
78  *
79  * Note: The command line version should be finished first!!! We
80  * don't want to start on a crusade of abstracting code that doesn't
81  * yet exist. Functionality first, neat flexible stuff later.
82  *
83  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
84  */

85 public class AutoDeployer {
86
87     private Openejb config;
88     private String JavaDoc configFile;
89     private Container[] containers;
90     private Connector[] resources;
91     private ClassLoader JavaDoc classLoader;
92     private String JavaDoc jarLocation;
93
94     public AutoDeployer(Openejb config) {
95         this.config = config;
96
97         /* Load container list */
98         this.containers = config.getContainer();
99         
100         /* Load resource list */
101         this.resources = config.getConnector();
102         System.out.println("resources "+resources.length);
103     }
104     
105     public void init() throws OpenEJBException {
106     }
107
108     public OpenejbJar deploy(EjbJarUtils ejbJarUtils, String JavaDoc jarLocation, ClassLoader JavaDoc classLoader) throws OpenEJBException {
109         this.jarLocation = jarLocation;
110         this.classLoader = classLoader;
111         OpenejbJar openejbJar = new OpenejbJar();
112
113         Bean[] beans = ejbJarUtils.getBeans();
114
115         for (int i = 0; i < beans.length; i++) {
116             openejbJar.addEjbDeployment(deployBean(beans[i], jarLocation));
117         }
118         return openejbJar;
119     }
120
121     private EjbDeployment deployBean(Bean bean, String JavaDoc jarLocation) throws OpenEJBException {
122         EjbDeployment deployment = new EjbDeployment();
123
124         deployment.setEjbName(bean.getEjbName());
125
126         deployment.setDeploymentId(autoAssignDeploymentId(bean));
127
128         deployment.setContainerId(autoAssignContainerId(bean));
129
130         ResourceRef[] refs = bean.getResourceRef();
131
132         if (refs.length > 1){
133             throw new OpenEJBException("Beans with more that one resource-ref cannot be autodeployed; there is no accurate way to determine how the references should be mapped.");
134         }
135         
136         for (int i = 0; i < refs.length; i++) {
137             deployment.addResourceLink(autoAssingResourceRef(refs[i]));
138         }
139
140         if (bean.getType().equals("CMP_ENTITY")){
141             if (bean.getHome() != null){
142                 Class JavaDoc tempBean = loadClass(bean.getHome());
143                 if (hasFinderMethods(tempBean)){
144                     throw new OpenEJBException("CMP 1.1 Beans with finder methods cannot be autodeployed; finder methods require OQL Select statements which cannot be generated accurately.");
145                 }
146             }
147             if (bean.getLocalHome() != null){
148                 Class JavaDoc tempBean = loadClass(bean.getLocalHome());
149                 if (hasFinderMethods(tempBean)){
150                     throw new OpenEJBException("CMP 1.1 Beans with finder methods cannot be autodeployed; finder methods require OQL Select statements which cannot be generated accurately.");
151                 }
152             }
153         }
154
155         return deployment;
156     }
157
158     private Class JavaDoc loadClass(String JavaDoc className) throws OpenEJBException {
159         try {
160             return classLoader.loadClass(className);
161         } catch (ClassNotFoundException JavaDoc cnfe) {
162             throw new OpenEJBException(SafeToolkit.messages.format("cl0007", className, this.jarLocation));
163         }
164     }
165
166     private boolean hasFinderMethods(Class JavaDoc bean)
167     throws OpenEJBException {
168
169         Method JavaDoc[] methods = bean.getMethods();
170
171         for (int i = 0; i < methods.length; i++) {
172             if (methods[i].getName().startsWith("find")
173                 && !methods[i].getName().equals("findByPrimaryKey")) {
174                 return true;
175             }
176         }
177         return false;
178     }
179
180     private String JavaDoc autoAssignDeploymentId(Bean bean) throws OpenEJBException {
181         return bean.getEjbName();
182     }
183
184     private String JavaDoc autoAssignContainerId(Bean bean) throws OpenEJBException {
185         String JavaDoc answer = null;
186         boolean replied = false;
187
188         Container[] cs = getUsableContainers(bean);
189
190         if (cs.length == 0) {
191             throw new OpenEJBException("A container of type "+bean.getType()+" must be declared in the configuration file.");
192         }
193         return cs[0].getId();
194     }
195
196     private ResourceLink autoAssingResourceRef(ResourceRef ref) throws OpenEJBException {
197         if (resources.length == 0) {
198             throw new OpenEJBException("A Connector must be declared in the configuration file to satisfy the resource-ref "+ref.getResRefName());
199         }
200
201         ResourceLink link = new ResourceLink();
202         link.setResRefName(ref.getResRefName());
203         link.setResId(resources[0].getId());
204         return link;
205     }
206
207     private Container[] getUsableContainers(Bean bean) {
208         return EjbJarUtils.getUsableContainers(containers, bean);
209     }
210 }
211
Popular Tags