KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > jonas > actions > middlegen > MiddlegenActionAbstract


1 /*
2  * Created on 9 févr. 2005
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */

7 package com.bull.eclipse.jonas.actions.middlegen;
8
9 import java.io.File JavaDoc;
10 import java.io.FileInputStream JavaDoc;
11 import java.io.FileOutputStream JavaDoc;
12 import java.io.FileWriter JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.util.Properties JavaDoc;
15
16 import org.apache.velocity.VelocityContext;
17 import org.apache.velocity.app.VelocityEngine;
18 import org.apache.velocity.exception.MethodInvocationException;
19 import org.apache.velocity.exception.ParseErrorException;
20 import org.apache.velocity.exception.ResourceNotFoundException;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IPath;
24 import org.eclipse.jdt.internal.ui.JavaPlugin;
25 import org.eclipse.jface.dialogs.InputDialog;
26 import org.eclipse.jface.preference.IPreferenceStore;
27
28 import com.bull.eclipse.jonas.JonasLauncherPlugin;
29 import com.bull.eclipse.jonas.JonasPluginResources;
30 import com.bull.eclipse.jonas.JonasProject;
31 import com.bull.eclipse.jonas.actions.JonasProjectAbstractActionDelegate;
32 import com.bull.eclipse.jonas.utils.AntUtils;
33 import com.bull.eclipse.jonas.utils.FileUtil;
34 import com.bull.eclipse.jonas.utils.TemplateDirUtil;
35
36 /**
37  * @author riase
38  *
39  * TODO To change the template for this generated type comment go to
40  * Window - Preferences - Java - Code Style - Code Templates
41  */

42 public abstract class MiddlegenActionAbstract extends JonasProjectAbstractActionDelegate {
43     JonasProject prj = null;
44     File JavaDoc middleGenDir = null;
45     String JavaDoc tempMiddlegen = null;
46     String JavaDoc packageEntityEJB = null;
47     String JavaDoc pathPackageBusiness = null;
48     String JavaDoc pathPackageEntity = null;
49     String JavaDoc ejbName = null;
50     static private String JavaDoc persistenceDir = "persistence";
51     
52     public void copyConfigurationFile(String JavaDoc origin, String JavaDoc destination) throws IOException JavaDoc {
53         IPath prjPath = prj.getProject().getLocation();
54         middleGenDir = new File JavaDoc(prjPath.append(destination).toOSString());
55         tempMiddlegen = TemplateDirUtil.getTemplateDir().concat(File.separator).concat("middlegen");
56         if (!middleGenDir.exists())
57             middleGenDir.mkdir();
58 // File middleGenEJBDir = new File(prjPath.append(origin).append("src").append(prj.getProject().getName().toLowerCase() + "-prefs.properties").toOSString());
59
// if (middleGenEJBDir.exists())
60
// FileUtil.copy(middleGenEJBDir, new File(middleGenDir,"src" + File.separator + prj.getProject().getName().toLowerCase() + "-prefs.properties"));
61
}
62
63     public void createPropertiesFile(String JavaDoc persistence) throws IOException JavaDoc, CoreException {
64         File JavaDoc buildProp = new File JavaDoc(middleGenDir, "build.properties");
65         if (!buildProp.exists()) {
66             FileUtil.copy(new File JavaDoc(tempMiddlegen,"build.properties"),buildProp);
67             FileUtil.copy(new File JavaDoc(tempMiddlegen,"build.xml"),new File JavaDoc(middleGenDir,"build.xml"));
68         }
69         FileInputStream JavaDoc fis = new FileInputStream JavaDoc(buildProp);
70         Properties JavaDoc props = new Properties JavaDoc();
71         props.load(fis);
72         fis.close();
73         String JavaDoc defaultValue = props.getProperty("package.business");
74         props.setProperty("jonas.root", JonasLauncherPlugin.getDefault().getJonasDir());
75         props.setProperty("middlegen.lib", TemplateDirUtil.getTemplateDir().concat("/middlegen").concat("/lib"));
76         props.setProperty("velocity.lib", TemplateDirUtil.getTemplateDir().concat("/.."));
77         props.setProperty("name", prj.getProject().getName().toLowerCase());
78         props.setProperty("persistence", persistence);
79         String JavaDoc packageEntity = getEjbPackage(defaultValue);
80         pathPackageBusiness = packageEntity.replace('.',File.separatorChar);
81         props.setProperty("package.business",packageEntity);
82         packageEntityEJB = packageEntity;
83         pathPackageEntity = packageEntityEJB.replace('.',File.separatorChar);
84         ejbName = packageEntityEJB.replace('.','_');
85
86         props.setProperty("package.entity",packageEntityEJB);
87         props.setProperty("path.package.entity",pathPackageEntity);
88         IPath srcPersistenceDir = prj.getProject().getLocation().append(prj.getSrcPersistenceDir());
89         if (!srcPersistenceDir.toFile().exists()) {
90             srcPersistenceDir.toFile().mkdir();
91             prj.setSrcAsSourceFolder(prj.getSrcPersistenceDir());
92         }
93         props.setProperty("gen.src.dir", srcPersistenceDir.toOSString());
94         IPreferenceStore pref = JonasLauncherPlugin.getDefault().getPreferenceStore();
95         props.setProperty("database.url", pref.getString("url"));
96         props.setProperty("datasource.jndi.name", pref.getString("datasource"));
97         props.setProperty("database.driver", pref.getString("driver"));
98         props.setProperty("database.userid", pref.getString("user"));
99         props.setProperty("database.password", pref.getString("password"));
100         props.setProperty("database.driver.classpath", pref.getString("jdbcDriver"));
101         props.setProperty("database.schema", pref.getString("schema"));
102         props.setProperty("database.catalog", pref.getString("catalog"));
103         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(buildProp);
104         props.store(fos,null);
105         fos.close();
106         prj.getProject().refreshLocal( IResource.DEPTH_INFINITE, null );
107         prj.setPackageProjectFolder(packageEntityEJB);
108         prj.setJarEjbName(ejbName);
109
110     }
111     
112     public void startIHM(String JavaDoc target) {
113         String JavaDoc path = prj.getProject().getName().concat(File.separator).concat(middleGenDir.getName()).concat(File.separator).concat("build.xml");
114         AntUtils.runAnt(path, target);
115     }
116
117     public void generateBuildWithVelocity() throws Exception JavaDoc {
118         //Build file creation
119
VelocityContext vContext = new VelocityContext();
120         VelocityEngine vEngine = null;
121         
122         //On calcule le nombre de fragments du package
123
String JavaDoc fragment = "../..";
124         for (int i=0;i<packageEntityEJB.length();i++) {
125             if (packageEntityEJB.charAt(i) == ".".charAt(0)) {
126                 fragment = fragment.concat("/..");
127             }
128         }
129         vContext.put("relatifProjet",fragment);
130         vContext.put("pkgNameJava",pathPackageEntity);
131         vContext.put("pkgNameJavaBusiness",pathPackageBusiness);
132         vContext.put("jarName",ejbName);
133         
134         String JavaDoc jonasRoot = JonasLauncherPlugin.getDefault().getJonasDir();
135         
136         // Generates webapps files
137
try {
138             vEngine = new VelocityEngine();
139             vEngine.setProperty(VelocityEngine.VM_LIBRARY, "");
140             vEngine.setProperty(VelocityEngine.RESOURCE_LOADER, "file");
141             vEngine.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH,
142                             TemplateDirUtil.getTemplateDir().concat("/middlegen"));
143             try {
144                 vEngine.init();
145             } catch (Exception JavaDoc e) {
146                 throw new Exception JavaDoc("unable to initilise Velocity engine (" + e + ")");
147             }
148             File JavaDoc duDir = new File JavaDoc(prj.getProject().getLocation().append(persistenceDir).append(pathPackageEntity).toOSString());
149             if (!duDir.exists())
150                 duDir.mkdirs();
151             if (!prj.getProject().getLocation().append(persistenceDir).append(pathPackageEntity).append("build.xml").toFile().exists())
152                 generate("build.vm", prj.getProject().getLocation().append(persistenceDir).append(pathPackageEntity).append("build.xml").toOSString(),vEngine,vContext);
153         } catch (Exception JavaDoc e) {
154             throw new Exception JavaDoc(e.toString());
155         }
156         
157         prj.getProject().refreshLocal( IResource.DEPTH_INFINITE, null );
158
159     }
160     
161     
162     public String JavaDoc getEjbPackage(String JavaDoc defaultValue) {
163         InputDialog id = new InputDialog(JavaPlugin.getActiveWorkbenchShell(),
164                 JonasPluginResources.EJB_PACKAGE_TITLE_LABEL,
165                 JonasPluginResources.EJB_PACKAGE_MESSAGE_LABEL, defaultValue, null);
166
167         if (id.open() == org.eclipse.jface.window.Window.OK)
168             return id.getValue();
169         else
170             return null;
171
172     }
173     
174      private void generate(String JavaDoc templateFileName, String JavaDoc targetFileName,
175             VelocityEngine vEngine, VelocityContext vContext) throws Exception JavaDoc,
176             IOException JavaDoc, ResourceNotFoundException, ParseErrorException,
177             MethodInvocationException {
178         FileWriter JavaDoc fileWriter = null;
179         fileWriter = new FileWriter JavaDoc(targetFileName);
180         vEngine.mergeTemplate(templateFileName, vContext, fileWriter);
181         fileWriter.close();
182     }
183
184
185 }
186
Popular Tags