KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > persitence > HibernateGlobalCFGGenerate


1 package com.bull.eclipse.persitence;
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.actions.hibernate.GlobalHBMValue;
18 import com.bull.eclipse.jonas.utils.TemplateDirUtil;
19
20
21
22 /**
23  * This class is a “bean generator”. It uses Velocity and
24  * a set of template files to generate some files that can serve as a
25  * startup point for the developpement of a new bean.
26  */

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     /**
43      * Generates files for this new bean.
44      */

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 JavaDoc e) {
56                 fatalError("unable to initilise Velocity engine (" + e + ")");
57             }
58
59             // Creates a Velocity context and populates it by walking
60
// through the parameter set
61
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 JavaDoc(prj.getProject().getLocation().append("persistence").toFile(), "hibernate.cfg.xml"));
67         } catch (Exception JavaDoc e) {
68             error(e.toString());
69         }
70     
71     }
72
73
74
75     /**
76      * Generates a file from the specified template.
77      * @param templateFileName the name of the template file
78      * @param targetFileName the name of the generated file
79      */

80     private void generate(String JavaDoc templateFileName,
81                           File JavaDoc targetFileName) throws Exception JavaDoc, IOException JavaDoc, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
82         FileWriter JavaDoc fileWriter = null;
83         fileWriter = new FileWriter JavaDoc(targetFileName);
84         vEngine.mergeTemplate(templateFileName, vContext, fileWriter);
85         fileWriter.close();
86     }
87
88
89     /**
90      * Display the specified error message.
91      * @param errMsg the error message to display
92      */

93     static void error(String JavaDoc errMsg) {
94         System.err.println("NewBean error: " + errMsg);
95     }
96
97
98     /**
99      * Display the specified error message and exits with an
100      * EXIT_FAILURE status.
101      * @param errMsg the error message to display
102      */

103     static void fatalError(String JavaDoc errMsg) {
104         System.err.println("NewBean fatal error: " + errMsg);
105         System.exit(EXIT_FAILURE);
106     }
107
108 }
109
Popular Tags