1 package com.bull.eclipse.newbean; 2 3 4 import java.io.File ; 5 import java.io.FileWriter ; 6 import java.io.IOException ; 7 import java.util.Vector ; 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 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 44 public void runNewFacade(NewFacadeValue nfv) { 45 48 String sessionPackage = nfv.getPackageName(); 50 String sessionFolder = sessionPackage.replace('.', File.separatorChar); 51 52 File sessionFile = new File (prj.getProject().getLocation().append(prj.getSrcPersistenceDir()).toOSString(), sessionFolder); 53 File tempFile = new File (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 e) { 67 fatalError("unable to initilise Velocity engine (" + e + ")"); 68 } 69 70 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 (tempFile, "facadeNode.xml")); 78 generate("JOnASFacadeNode.vm", new File (tempFile, "JOnASFacadeNode.xml")); 79 80 generate("FacadeUtil.vm", new File (sessionFile, nfv.getBeanName() + "Util.java")); 81 generate("FacadeHome.vm", new File (sessionFile, nfv.getBeanName() + "Home.java")); 82 generate("Facade.vm", new File (sessionFile, nfv.getBeanName() + ".java")); 83 generate("FacadeBean.vm", new File (sessionFile, nfv.getBeanName() + "Bean.java")); 84 File ejbFile = new File (sessionFile, nfv.getBeanName() + "EJB.java"); 85 if (!ejbFile.exists()) 86 generate("FacadeEJB.vm", ejbFile); 87 File clientFile = new File (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 e) { 93 error(e.toString()); 94 } 95 96 } 97 98 99 100 105 private void generate(String templateFileName, 106 File targetFileName) throws Exception , IOException , ResourceNotFoundException, ParseErrorException, MethodInvocationException { 107 FileWriter fileWriter = null; 108 fileWriter = new FileWriter (targetFileName); 109 vEngine.mergeTemplate(templateFileName, vContext, fileWriter); 110 fileWriter.close(); 111 } 112 113 114 118 static void error(String errMsg) { 119 System.err.println("NewBean error: " + errMsg); 120 } 121 122 123 128 static void fatalError(String errMsg) { 129 System.err.println("NewBean fatal error: " + errMsg); 130 System.exit(EXIT_FAILURE); 131 } 132 133 } 134 | Popular Tags |