KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.bull.eclipse.newbean;
2
3
4 import java.io.File JavaDoc;
5 import java.io.FileWriter JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.util.Vector JavaDoc;
8
9 import org.apache.velocity.VelocityContext;
10 import org.apache.velocity.app.VelocityEngine;
11 import org.apache.velocity.exception.MethodInvocationException;
12 import org.apache.velocity.exception.ParseErrorException;
13 import org.apache.velocity.exception.ResourceNotFoundException;
14 import org.eclipse.jdt.ui.wizards.NewTypeWizardPage;
15
16 import com.bull.eclipse.jonas.JonasProject;
17 import com.bull.eclipse.jonas.utils.TemplateDirUtil;
18
19
20
21 /**
22  * This class is a “bean generator”. It uses Velocity and
23  * a set of template files to generate some files that can serve as a
24  * startup point for the developpement of a new bean.
25  */

26 public class FacadeEJBGenerate {
27
28     private static final int EXIT_SUCCESS = 0;
29     private static final int EXIT_FAILURE = 1;
30
31     private VelocityEngine vEngine = null;
32     private VelocityContext vContext = null;
33     private JonasProject prj = null;
34
35
36     public FacadeEJBGenerate(JonasProject prj) {
37         this.prj = prj;
38     }
39
40
41     /**
42      * Generates files for this new bean.
43      */

44     public void runNewFacade(NewFacadeValue nfv) {
45         // Package folder creation.
46
// Substitute entity by session
47

48         //String sessionPackage = nfv.getPackageName().replaceAll("entity","session");
49
String JavaDoc sessionPackage = nfv.getPackageName();
50         String JavaDoc sessionFolder = sessionPackage.replace('.', File.separatorChar);
51                 
52         File JavaDoc sessionFile = new File JavaDoc(prj.getProject().getLocation().append(prj.getSrcPersistenceDir()).toOSString(), sessionFolder);
53         File JavaDoc tempFile = new File JavaDoc(prj.getProject().getLocation().append("temp").toOSString());
54         if (!sessionFile.exists())
55             sessionFile.mkdirs();
56         if (!tempFile.exists())
57             tempFile.mkdirs();
58         try {
59             vEngine = new VelocityEngine();
60             vEngine.setProperty(VelocityEngine.VM_LIBRARY, "");
61             vEngine.setProperty(VelocityEngine.RESOURCE_LOADER, "file");
62             vEngine.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH,
63                     TemplateDirUtil.getTemplateDir().concat("/newbean"));
64             try {
65                 vEngine.init();
66             } catch (Exception JavaDoc e) {
67                 fatalError("unable to initilise Velocity engine (" + e + ")");
68             }
69
70             // Creates a Velocity context and populates it by walking
71
// through the parameter set
72
vContext = new VelocityContext();
73             vContext.put("beanName", nfv.getBeanName());
74             vContext.put("package", sessionPackage);
75             vContext.put("valueNamePackage", nfv.getVecValue());
76             vContext.put("facadeMethods", nfv.getFacadeMethods());
77             generate("facadeNode.vm", new File JavaDoc(tempFile, "facadeNode.xml"));
78             generate("JOnASFacadeNode.vm", new File JavaDoc(tempFile, "JOnASFacadeNode.xml"));
79             
80             generate("FacadeUtil.vm", new File JavaDoc(sessionFile, nfv.getBeanName() + "Util.java"));
81             generate("FacadeHome.vm", new File JavaDoc(sessionFile, nfv.getBeanName() + "Home.java"));
82             generate("Facade.vm", new File JavaDoc(sessionFile, nfv.getBeanName() + ".java"));
83             generate("FacadeBean.vm", new File JavaDoc(sessionFile, nfv.getBeanName() + "Bean.java"));
84             File JavaDoc ejbFile = new File JavaDoc(sessionFile, nfv.getBeanName() + "EJB.java");
85             if (!ejbFile.exists())
86                 generate("FacadeEJB.vm", ejbFile);
87             File JavaDoc clientFile = new File JavaDoc(sessionFile, nfv.getBeanName() + "Client.java");
88             if (!clientFile.exists())
89                 generate("facadeclient.vm", clientFile);
90
91             System.out.println("Your bean files have been created. You can now customize them.");
92         } catch (Exception JavaDoc e) {
93             error(e.toString());
94         }
95     
96     }
97
98
99
100     /**
101      * Generates a file from the specified template.
102      * @param templateFileName the name of the template file
103      * @param targetFileName the name of the generated file
104      */

105     private void generate(String JavaDoc templateFileName,
106                           File JavaDoc targetFileName) throws Exception JavaDoc, IOException JavaDoc, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
107         FileWriter JavaDoc fileWriter = null;
108         fileWriter = new FileWriter JavaDoc(targetFileName);
109         vEngine.mergeTemplate(templateFileName, vContext, fileWriter);
110         fileWriter.close();
111     }
112
113
114     /**
115      * Display the specified error message.
116      * @param errMsg the error message to display
117      */

118     static void error(String JavaDoc errMsg) {
119         System.err.println("NewBean error: " + errMsg);
120     }
121
122
123     /**
124      * Display the specified error message and exits with an
125      * EXIT_FAILURE status.
126      * @param errMsg the error message to display
127      */

128     static void fatalError(String JavaDoc errMsg) {
129         System.err.println("NewBean fatal error: " + errMsg);
130         System.exit(EXIT_FAILURE);
131     }
132
133 }
134
Popular Tags