KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lateralnz > panther > deploy > SessionBeanGenerator


1 /* ====================================================================
2  * The LateralNZ Software License, Version 1.0
3  *
4  * Copyright (c) 2003 LateralNZ. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by
21  * LateralNZ (http://www.lateralnz.org/) and other third parties."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "LateralNZ" must not be used to endorse or promote
26  * products derived from this software without prior written
27  * permission. For written permission, please
28  * contact oss@lateralnz.org.
29  *
30  * 5. Products derived from this software may not be called "Panther",
31  * or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
32  * "LATERALNZ" appear in their name, without prior written
33  * permission of LateralNZ.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of LateralNZ. For more
51  * information on Lateral, please see http://www.lateralnz.com/ or
52  * http://www.lateralnz.org
53  *
54  */

55 package org.lateralnz.panther.deploy;
56
57 import java.io.File JavaDoc;
58 import java.io.FileWriter JavaDoc;
59 import java.io.IOException JavaDoc;
60 import java.io.Serializable JavaDoc;
61 import java.lang.reflect.Method JavaDoc;
62 import java.util.ArrayList JavaDoc;
63 import java.util.Iterator JavaDoc;
64 import java.util.List JavaDoc;
65 import java.util.StringTokenizer JavaDoc;
66
67 import org.apache.velocity.app.VelocityEngine;
68 import org.apache.velocity.Template;
69 import org.apache.velocity.VelocityContext;
70
71 import org.w3c.dom.Document JavaDoc;
72
73 import org.lateralnz.common.util.Constants;
74 import org.lateralnz.common.util.ObjectUtils;
75 import org.lateralnz.common.util.StringUtils;
76 import org.lateralnz.common.util.XMLUtils;
77
78 import org.lateralnz.panther.util.EJBConstants;
79
80 /**
81  * @author J R Briggs
82  */

83 public class SessionBeanGenerator implements Constants, Serializable JavaDoc {
84   private String JavaDoc ejbjarXML;
85   private String JavaDoc tempdir;
86   private String JavaDoc classesdir;
87   private String JavaDoc wrapperTemplate;
88   private String JavaDoc homeWrapperTemplate;
89   private String JavaDoc wrapperTemplateDir;
90   private String JavaDoc logfile;
91
92  /**
93   * set the EJBJarXML property
94   */

95   public void setEJBJarXML(String JavaDoc ejbjarXML) {
96     this.ejbjarXML = ejbjarXML;
97   }
98   
99  /**
100   * set the temporary/working directory
101   */

102   public void setTempDir(String JavaDoc tempdir) {
103     this.tempdir = StringUtils.toDirectory(tempdir);
104   }
105   
106  /**
107   * set the directory where classes can be found
108   */

109   public void setClassesDir(String JavaDoc classesdir) {
110     this.classesdir = StringUtils.toDirectory(classesdir);
111   }
112
113  /**
114   * set the name of the sessionbean wrapper template
115   */

116   public void setWrapperTemplate(String JavaDoc wrapperTemplate) {
117     this.wrapperTemplate = wrapperTemplate;
118   }
119   
120   public void setHomeWrapperTemplate(String JavaDoc homeWrapperTemplate) {
121     this.homeWrapperTemplate = homeWrapperTemplate;
122   }
123   
124  /**
125   * set the directory where we can find the template
126   */

127   public void setWrapperTemplateDir(String JavaDoc wrapperTemplateDir) {
128     this.wrapperTemplateDir = wrapperTemplateDir;
129   }
130   
131   public void setLogFile(String JavaDoc logfile) {
132     this.logfile = logfile;
133   }
134
135  /**
136   * execute this task
137   */

138   public void execute() throws Exception JavaDoc {
139     VelocityEngine ve = new VelocityEngine();
140     ve.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, wrapperTemplateDir);
141     if (!StringUtils.isEmpty(logfile)) {
142       ve.setProperty(VelocityEngine.RUNTIME_LOG, logfile);
143     }
144     ve.init();
145
146     Template ejbWrapper = ve.getTemplate(wrapperTemplate);
147     Template homeWrapper = ve.getTemplate(homeWrapperTemplate);
148
149     File JavaDoc f = new File JavaDoc(ejbjarXML);
150     String JavaDoc xml = StringUtils.readFromFile(ejbjarXML);
151     Document JavaDoc doc = XMLUtils.parse(xml);
152
153     File JavaDoc fworkingDir = new File JavaDoc(tempdir);
154
155     // various lists needed during the parse
156
List JavaDoc ejbwrappers = new ArrayList JavaDoc();
157
158     // work through the session descriptors
159
List JavaDoc l = EJBJarUtils.getSessionDescriptors(doc);
160     Iterator JavaDoc iter = l.iterator();
161     while (iter.hasNext()) {
162       SessionDescriptor sd = (SessionDescriptor)iter.next();
163       if (sd.getSessionType().equalsIgnoreCase(EJBConstants.STATEFUL)) {
164         iter.remove();
165         
166         System.out.println("note: stateful bean " + sd.getEJBName() + " is not supported");
167         continue;
168       }
169
170       // get the remote, ejb and home classes
171
Class JavaDoc remote = Class.forName(sd.getEJBRemoteClass());
172       Class JavaDoc home = Class.forName(sd.getEJBHomeClass());
173       Class JavaDoc ejb = Class.forName(sd.getEJBClass());
174
175       String JavaDoc packageName = sd.getEJBRemoteClass().substring(0, sd.getEJBRemoteClass().lastIndexOf('.'));
176       String JavaDoc packageDir = StringUtils.replace(packageName, DOT, FILE_SEPARATOR);
177       checkDirStructure(tempdir, packageDir);
178       String JavaDoc[] tmp = StringUtils.toArray(remote.getName(), DOT);
179
180       // this is what we'll call our ejb wrapper
181
String JavaDoc wrapperName = tmp[tmp.length - 1] + "Impl";
182       String JavaDoc homeName = tmp[tmp.length - 1] + "HomeImpl";
183       String JavaDoc wrapperFullName = StringUtils.replace(sd.getEJBName(), FILE_SEPARATOR, UNDERSCORE);
184
185       // this is the classname and class signature file
186
String JavaDoc className = classesdir + packageDir + FILE_SEPARATOR + wrapperName + ".class";
187       String JavaDoc signatureFile = tempdir + packageDir + FILE_SEPARATOR + wrapperName + ".sig";
188
189       // get signatures for comparison (if they're not the same, we'll need to regenerate)
190
String JavaDoc oldsig = StringUtils.readFromFile(signatureFile);
191       String JavaDoc newsig = ObjectUtils.getMD5Signature(remote);
192
193       // check if we have already generated the class or if the signatures are different
194
File JavaDoc tst = new File JavaDoc(className);
195
196       String JavaDoc logtmp = EMPTY + (tst.exists() ? ONE : ZERO) + (tst.canRead() ? ONE : ZERO) + (newsig.equals(oldsig) ? ONE : ZERO);
197       if (!tst.exists() || !tst.canRead() || !newsig.equals(oldsig)) {
198         // get all remote methods and add to a list
199
Method JavaDoc[] methods = remote.getDeclaredMethods();
200         ArrayList JavaDoc methodList = new ArrayList JavaDoc();
201         for (int i = 0; i < methods.length; i++) {
202           if (!ObjectUtils.containsObject(methods[i].getExceptionTypes(), java.rmi.RemoteException JavaDoc.class)) {
203             continue;
204           }
205           boolean trans = false;
206           String JavaDoc tt = sd.getTransEntry(methods[i].getName());
207           if (tt.equalsIgnoreCase(REQUIRED)
208            || tt.equalsIgnoreCase(REQUIRES_NEW)) {
209             trans = true;
210           }
211           methodList.add(new MethodDescriptor(methods[i], trans, tt));
212         }
213
214         // velocity generation of java code
215
VelocityContext ctx = new VelocityContext();
216         ctx.put("ejbpackage", packageName);
217         ctx.put("ejbname", wrapperFullName);
218         ctx.put("ejbwrapper", wrapperName);
219         ctx.put("ejbhomewrapper", homeName);
220         ctx.put("ejb", ejb.getName());
221         ctx.put("ejbremote", remote.getName());
222         ctx.put("ejbhome", home.getName());
223         ctx.put("methods", methodList);
224         ctx.put("generator", this);
225
226         System.out.println("note: generating source for " + remote.getName() + " [" + logtmp + "]");
227
228         // write to file
229
FileWriter JavaDoc fw = new FileWriter JavaDoc(tempdir + packageDir + FILE_SEPARATOR + wrapperName + ".java");
230         ejbWrapper.merge(ctx, fw);
231         fw.flush();
232         fw.close();
233
234         fw = new FileWriter JavaDoc(tempdir + packageDir + FILE_SEPARATOR + homeName + ".java");
235         homeWrapper.merge(ctx, fw);
236         fw.flush();
237         fw.close();
238
239         // write the new signature
240
tst = new File JavaDoc(signatureFile);
241         tst.delete();
242         fw = new FileWriter JavaDoc(signatureFile);
243         fw.write(newsig);
244         fw.flush();
245         fw.close();
246
247       }
248     }
249   }
250   
251  /**
252   * check the directory structure for a package in a working directory, and if it
253   * doesn't exist, then create it. For example, the call: <br />
254   * checkDirStructure("test", "org/lateralnz/whatever");<br/>
255   * will create:<br/>
256   * test/org/lateralnz/whatever<br/>
257   * if it does not already exist
258   */

259   protected void checkDirStructure(String JavaDoc workingDir, String JavaDoc packageDir) throws IOException JavaDoc {
260     File JavaDoc f = new File JavaDoc(workingDir + packageDir);
261     if (f.exists()) {
262       return;
263     }
264     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(workingDir);
265     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(packageDir, FILE_SEPARATOR);
266     while (st.hasMoreTokens()) {
267       String JavaDoc next = st.nextToken();
268       sb.append(next).append(FILE_SEPARATOR);
269       f = new File JavaDoc(sb.toString());
270       if (!f.exists()) {
271         if (!f.mkdir()) {
272           throw new IOException JavaDoc("error creating directory : " + sb.toString());
273         }
274       }
275     }
276   }
277   
278  /**
279   * returns the length of an object if it is a List or Array. <br/>
280   * NOTE: needs to be public otherwise the velocity engine can't get access
281   * to it
282   * @param an object that is a list or an array
283   * @return the length of the list or array
284   */

285   public int getLength(Object JavaDoc obj) {
286     try {
287       if (obj instanceof List JavaDoc) {
288         return ((List JavaDoc)obj).size();
289       }
290       else {
291         return java.lang.reflect.Array.getLength(obj);
292       }
293     }
294     catch (Exception JavaDoc e) {
295       e.printStackTrace();
296       return 0;
297     }
298   }
299   
300  /**
301   * so this task can be called standalone
302   */

303   public static final void main(String JavaDoc[] args) throws Exception JavaDoc {
304     SessionBeanGenerator sbg = new SessionBeanGenerator();
305     sbg.setEJBJarXML(args[0]);
306     sbg.setTempDir(args[1]);
307     sbg.setClassesDir(args[2]);
308     sbg.setWrapperTemplate(args[3]);
309     sbg.setHomeWrapperTemplate(args[4]);
310     sbg.setWrapperTemplateDir(args[5]);
311     sbg.setLogFile(args[6]);
312     
313     sbg.execute();
314   }
315 }
Popular Tags