1 7 package com.bull.eclipse.jonas.actions.middlegen; 8 9 import java.io.File ; 10 import java.io.FileInputStream ; 11 import java.io.FileOutputStream ; 12 import java.io.FileWriter ; 13 import java.io.IOException ; 14 import java.util.Properties ; 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 42 public abstract class MiddlegenActionAbstract extends JonasProjectAbstractActionDelegate { 43 JonasProject prj = null; 44 File middleGenDir = null; 45 String tempMiddlegen = null; 46 String packageEntityEJB = null; 47 String pathPackageBusiness = null; 48 String pathPackageEntity = null; 49 String ejbName = null; 50 static private String persistenceDir = "persistence"; 51 52 public void copyConfigurationFile(String origin, String destination) throws IOException { 53 IPath prjPath = prj.getProject().getLocation(); 54 middleGenDir = new File (prjPath.append(destination).toOSString()); 55 tempMiddlegen = TemplateDirUtil.getTemplateDir().concat(File.separator).concat("middlegen"); 56 if (!middleGenDir.exists()) 57 middleGenDir.mkdir(); 58 } 62 63 public void createPropertiesFile(String persistence) throws IOException , CoreException { 64 File buildProp = new File (middleGenDir, "build.properties"); 65 if (!buildProp.exists()) { 66 FileUtil.copy(new File (tempMiddlegen,"build.properties"),buildProp); 67 FileUtil.copy(new File (tempMiddlegen,"build.xml"),new File (middleGenDir,"build.xml")); 68 } 69 FileInputStream fis = new FileInputStream (buildProp); 70 Properties props = new Properties (); 71 props.load(fis); 72 fis.close(); 73 String 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 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 fos = new FileOutputStream (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 target) { 113 String 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 { 118 VelocityContext vContext = new VelocityContext(); 120 VelocityEngine vEngine = null; 121 122 String 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 jonasRoot = JonasLauncherPlugin.getDefault().getJonasDir(); 135 136 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 e) { 146 throw new Exception ("unable to initilise Velocity engine (" + e + ")"); 147 } 148 File duDir = new File (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 e) { 154 throw new Exception (e.toString()); 155 } 156 157 prj.getProject().refreshLocal( IResource.DEPTH_INFINITE, null ); 158 159 } 160 161 162 public String getEjbPackage(String 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 templateFileName, String targetFileName, 175 VelocityEngine vEngine, VelocityContext vContext) throws Exception , 176 IOException , ResourceNotFoundException, ParseErrorException, 177 MethodInvocationException { 178 FileWriter fileWriter = null; 179 fileWriter = new FileWriter (targetFileName); 180 vEngine.mergeTemplate(templateFileName, vContext, fileWriter); 181 fileWriter.close(); 182 } 183 184 185 } 186 | Popular Tags |