1 package com.bull.eclipse.persitence; 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.actions.hibernate.GlobalHBMValue; 18 import com.bull.eclipse.jonas.utils.TemplateDirUtil; 19 20 21 22 27 public class HibernateGlobalCFGGenerate { 28 29 private static final int EXIT_SUCCESS = 0; 30 private static final int EXIT_FAILURE = 1; 31 32 private VelocityEngine vEngine = null; 33 private VelocityContext vContext = null; 34 private JonasProject prj = null; 35 36 37 public HibernateGlobalCFGGenerate(JonasProject prj) { 38 this.prj = prj; 39 } 40 41 42 45 public void generateCFG(GlobalHBMValue ghv) { 46 47 try { 48 vEngine = new VelocityEngine(); 49 vEngine.setProperty(VelocityEngine.VM_LIBRARY, ""); 50 vEngine.setProperty(VelocityEngine.RESOURCE_LOADER, "file"); 51 vEngine.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, 52 TemplateDirUtil.getTemplateDir().concat("/hibernate")); 53 try { 54 vEngine.init(); 55 } catch (Exception e) { 56 fatalError("unable to initilise Velocity engine (" + e + ")"); 57 } 58 59 vContext = new VelocityContext(); 62 vContext.put("data_source_jndi_name", ghv.getDatasourceJNDI()); 63 vContext.put("dialect", ghv.getDialect()); 64 vContext.put("hbmList", ghv.getHbmList()); 65 66 generate("hibernateGlobalHBM.vm", new File (prj.getProject().getLocation().append("persistence").toFile(), "hibernate.cfg.xml")); 67 } catch (Exception e) { 68 error(e.toString()); 69 } 70 71 } 72 73 74 75 80 private void generate(String templateFileName, 81 File targetFileName) throws Exception , IOException , ResourceNotFoundException, ParseErrorException, MethodInvocationException { 82 FileWriter fileWriter = null; 83 fileWriter = new FileWriter (targetFileName); 84 vEngine.mergeTemplate(templateFileName, vContext, fileWriter); 85 fileWriter.close(); 86 } 87 88 89 93 static void error(String errMsg) { 94 System.err.println("NewBean error: " + errMsg); 95 } 96 97 98 103 static void fatalError(String errMsg) { 104 System.err.println("NewBean fatal error: " + errMsg); 105 System.exit(EXIT_FAILURE); 106 } 107 108 } 109 | Popular Tags |