KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > webadmin > ejbgen > EJBTemplate


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

45 package org.openejb.webadmin.ejbgen;
46
47 /** EJBTemplate file housing methods to build all EJB Skeletons
48  * @author <a HREF="mailto:jcscoobyrs@developerkb.com">Jeremy Whitlock</a>
49  */

50 import java.io.File JavaDoc;
51 import java.io.FileInputStream JavaDoc;
52 import java.io.FileOutputStream JavaDoc;
53 import java.io.FileWriter JavaDoc;
54 import java.io.IOException JavaDoc;
55 import java.util.StringTokenizer JavaDoc;
56 import java.util.zip.ZipEntry JavaDoc;
57 import java.util.zip.ZipOutputStream JavaDoc;
58
59 public abstract class EJBTemplate
60 {
61     private String JavaDoc name;
62     private String JavaDoc desc;
63     private String JavaDoc auth;
64     private String JavaDoc pack;
65     private String JavaDoc sloc;
66     private String JavaDoc styp;
67     private File JavaDoc cdir;
68     private File JavaDoc ddir;
69     private File JavaDoc backup;
70     private File JavaDoc packup;
71     private String JavaDoc psep = System.getProperty("file.separator");
72     
73     public abstract void createEJB();
74     public abstract void createEJBXML();
75 // public abstract void createOPEXML();
76
public abstract void createObjCode();
77     public abstract void createHmeCode();
78     public abstract void createBenCode();
79     
80     /** The setVars method will initialize all String
81      * variables needed for the methods in this class.
82      * @param ejbname the ejb's name
83      * @param ejbdesc the ejb's description
84      * @param ejbauth the ejb's author
85      * @param ejbpack the ejb's package
86      * @param ejbsloc the ejb's save location
87     **/

88     public void setVars(String JavaDoc ejbname, String JavaDoc ejbdesc, String JavaDoc ejbauth, String JavaDoc ejbpack, String JavaDoc ejbsloc, String JavaDoc ejbstyp)
89     {
90         name = ejbname;
91         desc = ejbdesc;
92         auth = ejbauth;
93         pack = ejbpack;
94         styp = ejbstyp;
95         sloc = ejbsloc + psep + ejbname;
96     }
97     
98     
99     /** This method backs up all structure and classes for the
100      * EJB if there are already classes in the save location
101      * with the same name.
102     **/

103     public void createBackup()
104     {
105         File JavaDoc metaback = new File JavaDoc(backup.getPath() + psep + "META-INF");
106         File JavaDoc cbdir = new File JavaDoc(backup.getPath());
107         //String[] dirs = pack.split("\\.");
108
StringTokenizer JavaDoc dirs = new StringTokenizer JavaDoc(pack,"\\.");
109         
110         backup.mkdir();
111         metaback.mkdir();
112     
113         //for(int i = 0; i < dirs.length; i++)
114
//{
115
// File ndir = new File(cbdir.getPath() + psep + dirs[i]);
116
// ndir.mkdir();
117
// cbdir = new File(ndir.getPath());
118
//}
119

120         while (dirs.hasMoreTokens())
121         {
122             File JavaDoc ndir = new File JavaDoc(cbdir.getPath() + psep + dirs.nextToken());
123             ndir.mkdir();
124             cbdir = new File JavaDoc(ndir.getPath());
125         }
126         
127         packup = new File JavaDoc(cbdir.getPath());
128     }
129     
130     /** This method creates the directory structure to fit
131      * the same structure as the package input by the user.
132     **/

133     public void createPackage()
134     {
135         cdir = new File JavaDoc(sloc);
136         ddir = new File JavaDoc(sloc);
137         backup = new File JavaDoc(ddir.getPath() + psep + "backup");
138         //String[] dirs = pack.split("\\.");
139
StringTokenizer JavaDoc dirs = new StringTokenizer JavaDoc(pack,"\\.");
140         
141         ddir.mkdir();
142     
143         //for(int i = 0; i < dirs.length; i++)
144
//{
145
// File ndir = new File(cdir.getPath() + psep + dirs[i]);
146
// ndir.mkdir();
147
// cdir = new File(ndir.getPath());
148
//}
149

150         while (dirs.hasMoreTokens())
151         {
152             File JavaDoc ndir = new File JavaDoc(cdir.getPath() + psep + dirs.nextToken());
153             ndir.mkdir();
154             cdir = new File JavaDoc(ndir.getPath());
155         }
156         
157         //cdir = new File(odir);
158
}
159     
160     /** This method actually creates the file for the proper class.
161      * It's results depend on what is passed to it. It concats
162      * the two parameters together to form a single file name.
163      * @param ejbname the name of the class
164      * @param ejbobj the type of the class
165     **/

166     public void createClass(String JavaDoc ejbname, String JavaDoc ejbobj)
167     {
168         File JavaDoc ejbObj = new File JavaDoc(cdir.getPath() + psep + ejbname + ejbobj);
169     
170         if(ejbObj.exists())
171         {
172             createBackup();
173             
174             File JavaDoc fbackup = new File JavaDoc(packup.getPath() + psep + ejbname + ejbobj);
175             
176             ejbObj.renameTo(fbackup);
177             ejbObj.delete();
178         }
179     
180         try
181         {
182             ejbObj.createNewFile();
183         }
184         catch(IOException JavaDoc e)
185         {
186             System.out.println("Couldn\'t create file!");
187         }
188     }
189     
190     /** This method does the writing to the class created
191      * by the createClass() method. It will create a File
192      * object of the filename passed and write code to it
193      * as passed by code.
194      * @param filename the filename in String datatype
195      * @param code the code to be written to the filename
196     **/

197     public void writeClass(String JavaDoc filename, String JavaDoc code)
198     {
199         File JavaDoc wrtFile = new File JavaDoc(cdir.getPath()+ psep + filename);
200         try
201         {
202             FileWriter JavaDoc wrtWriter = new FileWriter JavaDoc(wrtFile);
203             wrtWriter.write(code);
204             wrtWriter.close();
205         }
206         catch (IOException JavaDoc e)
207         {
208             System.out.println("File Not Found. - " + e);
209         }
210     }
211     
212     /** Same as writeClass() method but it creates the structure
213      * and files that are XML Files.
214      * @param filename the filename in String datatype
215      * @param code the code to be written to the filename
216      */

217     public void createXML(String JavaDoc filename, String JavaDoc code)
218     {
219         String JavaDoc location = sloc;
220         File JavaDoc metainf = new File JavaDoc(location + psep + "META-INF");
221         File JavaDoc xmlfile = new File JavaDoc(location + psep + "META-INF" + psep + filename);
222         
223         if(metainf.exists())
224         {
225             if(xmlfile.exists())
226             {
227                 File JavaDoc nfile = new File JavaDoc(backup.getPath() + psep + "META-INF" + psep + filename);
228                 xmlfile.renameTo(nfile);
229                 xmlfile.delete();
230             }
231         }
232         else
233         {
234             metainf.mkdir();
235         }
236         
237         try
238         {
239             xmlfile.createNewFile();
240             
241             try
242             {
243                 FileWriter JavaDoc ejbWriter = new FileWriter JavaDoc(xmlfile);
244                 
245                 ejbWriter.write(code);
246                 ejbWriter.close();
247             }
248             catch(IOException JavaDoc e1)
249             {
250                 System.out.println("I/O Exception: " + e1);
251             }
252         }
253         catch(IOException JavaDoc e)
254         {
255             System.out.println("I/O Exception: " + e);
256         }
257     }
258     
259     /**This will build a zip file for all the files built
260      * by the EJB Generator
261      */

262     public void buildZipFile()
263     {
264         File JavaDoc bdir = new File JavaDoc(getBeanDir());
265         File JavaDoc mdir = new File JavaDoc(sloc + psep + "META-INF");
266         File JavaDoc zdir = new File JavaDoc(sloc);
267         String JavaDoc zname = name + ".zip";
268         File JavaDoc myZipFile = new File JavaDoc(zdir,zname);
269         byte[] buf = new byte[1024];
270     
271         //System.out.println(bdir);
272
//System.out.println(mdir);
273
//System.out.println(zdir);
274
//System.out.println(zname);
275

276         try
277         {
278             myZipFile.createNewFile();
279         
280             ZipOutputStream JavaDoc out = new ZipOutputStream JavaDoc(new FileOutputStream JavaDoc(myZipFile));
281             File JavaDoc[] mfiles = mdir.listFiles();
282             File JavaDoc[] bfiles = bdir.listFiles();
283             //String[] dirs = pack.split("\\.");
284
StringTokenizer JavaDoc dirs = new StringTokenizer JavaDoc(pack,"\\.");
285             String JavaDoc dpath = "";
286         
287             //META-INF
288
out.putNextEntry(new ZipEntry JavaDoc("META-INF/"));
289         
290             for(int i = 0; i < mfiles.length; i++)
291             {
292                 File JavaDoc cfile = new File JavaDoc(sloc + psep + "META-INF" +
293                 psep + mfiles[i].getName());
294                 FileInputStream JavaDoc in = new FileInputStream JavaDoc(cfile);
295
296                 // Add ZIP entry to output stream.
297
out.putNextEntry(new ZipEntry JavaDoc("META-INF/" + mfiles[i].getName()));
298
299                 // Transfer bytes from the file to the ZIP file
300
int len;
301                 while ((len = in.read(buf)) > 0) {
302                     out.write(buf, 0, len);
303                 }
304
305                 // Complete the entry
306
out.closeEntry();
307                 in.close();
308             }
309         
310             //System.out.println("META-INF zipped!");
311

312             //for(int i = 0; i < dirs.length; i ++)
313
//{
314
// dpath = dpath + dirs[i] + "/";
315
// out.putNextEntry(new ZipEntry(dpath));
316
//System.out.println(dpath);
317
//}
318

319             while(dirs.hasMoreTokens())
320             {
321                 dpath = dpath + dirs.nextToken() + "/";
322                 out.putNextEntry(new ZipEntry JavaDoc(dpath));
323                 //System.out.println(dpath);
324
}
325         
326             //System.out.println("Directories zipped!");
327

328             for(int i = 0; i < bfiles.length; i++)
329             {
330                 File JavaDoc cfile = new File JavaDoc(getBeanDir() + psep + bfiles[i].getName());
331                 FileInputStream JavaDoc in = new FileInputStream JavaDoc(cfile);
332
333                 // Add ZIP entry to output stream.
334
out.putNextEntry(new ZipEntry JavaDoc(dpath + bfiles[i].getName()));
335
336                 // Transfer bytes from the file to the ZIP file
337
int len;
338                 while ((len = in.read(buf)) > 0) {
339                     out.write(buf, 0, len);
340                 }
341
342                 // Complete the entry
343
out.closeEntry();
344                 in.close();
345             }
346         
347             //System.out.println("Zip Completed!");
348

349             out.close();
350         }
351         catch(IOException JavaDoc e)
352         {
353             System.out.println("I/O Exception: " + e);
354         }
355     }
356     
357     /**This method is used to get the path to where the EJB
358      * Template's source is stored locally on the computer.
359      *
360      * @return Returns the full path to the local directory
361      */

362     public String JavaDoc getBeanDir()
363     {
364         //String[] dirs = request.getFormParameter("ejbpack").split("\\.");
365
StringTokenizer JavaDoc dirs = new StringTokenizer JavaDoc(pack,"\\.");
366         String JavaDoc beandir = sloc;
367         
368         //for(int i = 0; i < dirs.length; i++)
369
//{
370
// beandir = beandir + psep + dirs[i];
371
//}
372

373         while (dirs.hasMoreTokens())
374         {
375             beandir = beandir + psep + dirs.nextToken();
376         }
377
378         
379         //System.out.println(beandir);
380
return beandir;
381     }
382 }
383
Popular Tags