KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > newbean > NewbeanRunner


1 /*
2  * Created on Sep 22, 2003
3  *
4  */

5  
6 package com.bull.eclipse.newbean;
7
8 import java.io.File JavaDoc;
9 import java.io.FileWriter JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.util.Vector JavaDoc;
12
13 import org.apache.velocity.VelocityContext;
14 import org.apache.velocity.app.VelocityEngine;
15 import org.apache.velocity.exception.MethodInvocationException;
16 import org.apache.velocity.exception.ParseErrorException;
17 import org.apache.velocity.exception.ResourceNotFoundException;
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IFolder;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.runtime.CoreException;
22
23 import com.bull.eclipse.jonas.JonasLauncherPlugin;
24 import com.bull.eclipse.jonas.JonasProject;
25 import com.bull.eclipse.jonas.utils.FileUtil;
26 import com.bull.eclipse.jonas.utils.TemplateDirUtil;
27 import com.bull.eclipse.jonas.wizard.CreateEJBWizardPage;
28
29
30 /**
31  * Interface with the JOnAS 'newbean' functionality.
32  * @author steve krupanszky
33  *
34  */

35 public class NewbeanRunner
36 {
37     private JonasProject jonasNature;
38     private IFolder pkgfolder; // the folder in which the java files will go
39
private JonasProject project;
40
41     private VelocityEngine vEngine = null;
42     private VelocityContext vContext = null;
43     
44     private static final int EXIT_SUCCESS = 0;
45     private static final int EXIT_FAILURE = 1;
46
47     private final Integer JavaDoc SESSION_BEAN = new Integer JavaDoc(0);
48     private final Integer JavaDoc ENTITY_BEAN = new Integer JavaDoc(1);
49     private final Integer JavaDoc MESSAGE_DRIVEN_BEAN = new Integer JavaDoc(2);
50     private final Integer JavaDoc BMP = new Integer JavaDoc(0);
51     private final Integer JavaDoc CMP1 = new Integer JavaDoc(1);
52     private final Integer JavaDoc CMP2 = new Integer JavaDoc(2);
53     private final Integer JavaDoc STATELESS = new Integer JavaDoc(0);
54     private final Integer JavaDoc STATEFUL = new Integer JavaDoc(1);
55     private final Boolean JavaDoc FALSE = Boolean.FALSE;
56     private final Boolean JavaDoc TRUE = Boolean.TRUE;
57
58
59
60     
61     
62     public NewbeanRunner( JonasProject proj )
63     {
64         project = proj;
65     }
66
67     /**
68      * Copy file into package, if it doesn't already exist there.
69      * Package must already exist.
70      */

71     private void copyFileToPackage( File JavaDoc f, String JavaDoc p )
72     {
73         IFile javafile = pkgfolder.getFile( f.getName() );
74         if( javafile.getLocation().toFile().exists() ) {
75             JonasLauncherPlugin.log(
76                 "JOPE: file " + p + "/" + f.getName()
77                 + " already exists in project " + project.getProject().getName() );
78         }
79         else {
80             try{ FileUtil.copy( f, javafile.getLocation().toFile() ); }
81             catch( IOException JavaDoc e ) {
82                 JonasLauncherPlugin.log(
83                     "JOPE: exception copying file " + p + "/" + f.getName()
84                     + " to project " + project.getProject().getName() + ": " + e );
85             }
86         }
87     }
88
89     private void copyFileToComponents(File JavaDoc f)
90     {
91         File JavaDoc javafile = project.getProject().getLocation().append(project.getSrcEJBDir()).append("build.xml").toFile();
92         try{ FileUtil.copy( f, javafile ); }
93         catch( IOException JavaDoc e ) {
94                 JonasLauncherPlugin.log(
95                     "JOPE: exception copying file " );
96         }
97     }
98
99     /**
100      * Copy file into project, if it doesn't already exist there.
101      */

102     private void copyFileToProject( File JavaDoc f )
103     {
104         IFile xmlfile = project.getProject().getFile( f.getName() );
105         if( xmlfile.getLocation().toFile().exists() ) {
106             JonasLauncherPlugin.log("JOPE: file " + f.getName()
107                 + " already exists in project " + project.getProject().getName() );
108         }
109         else {
110             try{ FileUtil.copy( f, xmlfile.getLocation().toFile() ); }
111             catch( IOException JavaDoc e ) {
112                 JonasLauncherPlugin.log(
113                     "JOPE: exception copying file " + f.getName()
114                     + " to project " + project.getProject().getName() + ": " + e );
115             }
116         }
117     }
118
119     private boolean myDelete( File JavaDoc f )
120     {
121         boolean b = f.delete();
122         int retryCount = 5;
123         while( !b && retryCount-->0 ) {
124             try{ java.lang.Thread.sleep(1000); } catch(Exception JavaDoc e) {}
125             b = f.delete();
126         }
127         return b;
128     }
129
130     public void RunNewbean( CreateEJBWizardPage EjbPage )
131     {
132         String JavaDoc jonasRoot = JonasLauncherPlugin.getDefault().getJonasDir();
133         vContext = new VelocityContext();
134         
135         String JavaDoc beanName = EjbPage.getEJBName();
136         vContext.put("beanName",beanName);
137         String JavaDoc pkgName = EjbPage.getPkgName();
138         vContext.put("pkgName",pkgName);
139         String JavaDoc jarName = EjbPage.getJarName();
140         Integer JavaDoc beanFlavor = null;
141         if( jarName.length()>0 )
142             vContext.put("jarName",jarName);
143         String JavaDoc s = EjbPage.getEJBType();
144         if( s.equals(CreateEJBWizardPage.STATEFUL) ) {
145             vContext.put("beanType","F");
146             vContext.put("sessionType",STATEFUL);
147             beanFlavor = SESSION_BEAN;
148             vContext.put("beanFlavor", beanFlavor);
149         } else
150         if( s.equals(CreateEJBWizardPage.STATELESS) ) {
151             vContext.put("beanType","L");
152             vContext.put("sessionType",STATELESS);
153             beanFlavor = SESSION_BEAN;
154             vContext.put("beanFlavor", beanFlavor);
155         } else
156         if( s.equals(CreateEJBWizardPage.ENTITYb) ) {
157             vContext.put("beanType","E");
158             vContext.put("sessionType","B");
159             beanFlavor = ENTITY_BEAN;
160             vContext.put("beanFlavor", beanFlavor);
161             vContext.put("persistenceManager", BMP);
162         } else
163         if( s.equals(CreateEJBWizardPage.ENTITYc1) ) {
164             vContext.put("beanType","E");
165             vContext.put("sessionType","C");
166             beanFlavor = ENTITY_BEAN;
167             vContext.put("beanFlavor", beanFlavor);
168             vContext.put("persistenceManager", CMP1);
169         } else
170         if( s.equals(CreateEJBWizardPage.ENTITYc2) ) {
171             vContext.put("beanType","E");
172             vContext.put("sessionType","C2");
173             beanFlavor = ENTITY_BEAN;
174             vContext.put("beanFlavor", beanFlavor);
175             vContext.put("persistenceManager", CMP2);
176         } else
177         if( s.equals(CreateEJBWizardPage.MDB) ) {
178             vContext.put("beanType","MD");
179             beanFlavor = MESSAGE_DRIVEN_BEAN;
180             vContext.put("beanFlavor", beanFlavor);
181         }
182         s = EjbPage.getPrimaryKey();
183         if( s.equals(CreateEJBWizardPage.STRING) ) {
184             vContext.put("primaryKey","S");
185             vContext.put("pkClass", "java.lang.String");
186         } else
187         if( s.equals(CreateEJBWizardPage.INTEGER) ) {
188             vContext.put("primaryKey","I");
189             vContext.put("pkClass", "java.lang.Integer");
190         } else
191         if( s.equals(CreateEJBWizardPage.OBJECT) ) {
192             vContext.put("primaryKey","O");
193             vContext.put("pkClass", "java.lang.Object");
194         }
195         s = EjbPage.getInterfaceName();
196         if( s.equals(CreateEJBWizardPage.LOCAL) ) {
197             vContext.put("location","L");
198             vContext.put("isLocal", TRUE);
199         } else
200         if( s.equals(CreateEJBWizardPage.REMOTE) ) {
201             vContext.put("location","R");
202             vContext.put("isLocal", FALSE);
203         }
204
205
206         vContext.put("SESSION_BEAN", SESSION_BEAN);
207         vContext.put("ENTITY_BEAN", ENTITY_BEAN);
208         vContext.put("MESSAGE_DRIVEN_BEAN", MESSAGE_DRIVEN_BEAN);
209         vContext.put("STATELESS", STATELESS);
210         vContext.put("STATEFUL", STATEFUL);
211         vContext.put("CMP1", CMP1);
212         vContext.put("CMP2", CMP2);
213         vContext.put("BMP", BMP);
214
215         
216         
217         vContext.put("jonasroot",jonasRoot);
218         vContext.put("jonasbase",JonasLauncherPlugin.getDefault().getBaseDir());
219         vContext.put("outputdirectory",project.getOutputFolder());
220         vContext.put("outputJAR",project.getProject().getLocation().toOSString());
221         vContext.put("projectName",project.getProject().getName());
222         
223         //On calcule le nombre de fragments du package
224
String JavaDoc fragment = "../..";
225         for (int i=0;i<pkgName.length();i++) {
226             if (pkgName.charAt(i) == ".".charAt(0)) {
227                 JonasLauncherPlugin.log("On passe dans if");
228                 fragment = fragment.concat("/..");
229             }
230         }
231         
232         vContext.put("relatifProjet",fragment);
233         vContext.put("pkgNameJava",pkgName);
234
235         pkgName = pkgName.replace(".".charAt(0),File.separatorChar);
236         
237         Vector JavaDoc pkgListe = project.getPackageProjectFolder();
238
239         // Generates webapps files
240
try {
241             vEngine = new VelocityEngine();
242             vEngine.setProperty(VelocityEngine.VM_LIBRARY, "");
243             vEngine.setProperty(VelocityEngine.RESOURCE_LOADER, "file");
244             vEngine.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH,
245                             jonasRoot.concat("/templates/newbean"));
246
247             try {
248                 vEngine.init();
249             } catch (Exception JavaDoc e) {
250                 fatalError("unable to initilise Velocity engine (" + e + ")");
251             }
252             
253             Integer JavaDoc MESSAGE_DRIVEN_BEAN =
254                 (Integer JavaDoc)vContext.get("MESSAGE_DRIVEN_BEAN");
255             Integer JavaDoc beanFlavorInVC = (Integer JavaDoc)vContext.get("beanFlavor");
256
257             boolean isClientToGenerate = true;
258             vContext.put("beanType","EJB");
259             File JavaDoc fileGeneration = new File JavaDoc(project.getProject().getLocation().append(project.getSrcEJBDir()).append(pkgName).toOSString());
260             generate("ejb-jar.vm", fileGeneration + "/" + jarName + ".xml");
261             generate("jonas-ejb-jar.vm", fileGeneration + "/" + "jonas-" + jarName + ".xml");
262             if (beanFlavorInVC != MESSAGE_DRIVEN_BEAN) {
263                 String JavaDoc local = "";
264                 Boolean JavaDoc isLocal = (Boolean JavaDoc)vContext.get("isLocal");
265                 if (isLocal.booleanValue()) {
266                     local = "Local";
267                     isClientToGenerate = false;
268                 }
269                 generate("remote.vm", fileGeneration + "/" + beanName + local + ".java");
270                 generate("home.vm", fileGeneration + "/" + beanName + local + "Home.java");
271             }
272             vEngine = new VelocityEngine();
273             vEngine.setProperty(VelocityEngine.VM_LIBRARY, "");
274             vEngine.setProperty(VelocityEngine.RESOURCE_LOADER, "file");
275             vEngine.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH,
276                     TemplateDirUtil.getTemplateDir().concat("/newbean"));
277             try {
278                 vEngine.init();
279             } catch (Exception JavaDoc e) {
280                 fatalError("unable to initilise Velocity engine (" + e + ")");
281             }
282
283             vContext.put("beanType","EJB");
284             generate("bean.vm", fileGeneration + "/" + beanName + "EJB" + ".java");
285             if (isClientToGenerate) {
286                 generate("client.vm", fileGeneration + "/" + beanName + "Client.java");
287             }
288             generate("build.vm", fileGeneration + "/" + "build.xml");
289             vContext.put("pkgListe", pkgListe);
290             generate("buildgene.vm", project.getProject().getLocation().append(project.getSrcEJBDir()).append("build.xml").toOSString());
291             System.out.println("Your bean files have been created. You can now customize them.");
292
293         } catch (Exception JavaDoc e) {
294             error(e.toString());
295         }
296
297         
298
299         // now to refresh project so it picks up the new files
300
try{
301             project.setPackageProjectFolder(pkgName);
302             project.setJarEjbName(EjbPage.getJarName());
303             //project.setSrcAsSourceFolder(EjbPage.getSrcDir());
304
project.getProject().refreshLocal( IResource.DEPTH_INFINITE, null );
305         }
306         catch( CoreException e ) {}
307
308     }
309
310     public void setNature( JonasProject nature )
311     {
312         jonasNature = nature;
313     }
314
315     public void setPkgFolder( IFolder f )
316     {
317         pkgfolder = f;
318     }
319
320     /**
321      * Create a new array : [ s1, s2, s3 ]
322      * (TODO move this method to StringUtil)
323      */

324     public static String JavaDoc[] concat( String JavaDoc[] s1, String JavaDoc s2, String JavaDoc s3 )
325     {
326         String JavaDoc[] full = new String JavaDoc[ s1.length + 2 ];
327         System.arraycopy( s1, 0, full, 0, s1.length );
328         full[ full.length-2 ] = s2;
329         full[ full.length-1 ] = s3;
330         return full;
331     }
332
333     /**
334      * Create a new array : [ s1, s2 ]
335      * (TODO move this method to StringUtil)
336      */

337     public static String JavaDoc[] concat( String JavaDoc[] s1, String JavaDoc s2 )
338     {
339         String JavaDoc[] full = new String JavaDoc[ s1.length + 1 ];
340         System.arraycopy( s1, 0, full, 0, s1.length );
341         full[ full.length-1 ] = s2;
342         return full;
343     }
344
345     /**
346      * Generates a file from the specified template.
347      * @param templateFileName the name of the template file
348      * @param targetFileName the name of the generated file
349      */

350     private void generate(String JavaDoc templateFileName,
351                           String JavaDoc targetFileName) throws Exception JavaDoc, IOException JavaDoc, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
352         FileWriter JavaDoc fileWriter = null;
353         fileWriter = new FileWriter JavaDoc(targetFileName);
354         vEngine.mergeTemplate(templateFileName, vContext, fileWriter);
355         fileWriter.close();
356     }
357
358     
359     /**
360      * Display the specified error message.
361      * @param errMsg the error message to display
362      */

363     static void error(String JavaDoc errMsg) {
364         System.err.println("NewBean error: " + errMsg);
365     }
366
367
368     /**
369      * Display the specified error message and exits with an
370      * EXIT_FAILURE status.
371      * @param errMsg the error message to display
372      */

373     static void fatalError(String JavaDoc errMsg) {
374         System.err.println("NewBean fatal error: " + errMsg);
375         System.exit(EXIT_FAILURE);
376     }
377
378 }
379
Popular Tags