KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > newbean > NewBean


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): Nicolas Christin
22  * Contributor(s): ______________________________________.
23  *
24  * --------------------------------------------------------------------------
25  * $Id: NewBean.java,v 1.8 2004/03/19 14:31:52 sauthieg Exp $
26  * --------------------------------------------------------------------------
27  */

28
29
30 package org.objectweb.jonas.newbean;
31
32
33 import java.io.FileWriter JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.util.HashMap JavaDoc;
36
37 import org.apache.velocity.VelocityContext;
38 import org.apache.velocity.app.VelocityEngine;
39 import org.apache.velocity.exception.MethodInvocationException;
40 import org.apache.velocity.exception.ParseErrorException;
41 import org.apache.velocity.exception.ResourceNotFoundException;
42
43
44 /**
45  * This class is a “bean generator”. It uses Velocity and
46  * a set of template files to generate some files that can serve as a
47  * startup point for the developpement of a new bean.
48  */

49 public class NewBean {
50
51     private static final int EXIT_SUCCESS = 0;
52     private static final int EXIT_FAILURE = 1;
53
54     private VelocityEngine vEngine = null;
55     private VelocityContext vContext = null;
56
57
58     private NewBean() {
59
60         // Creates and initializes a Velocity engine
61
vEngine = new VelocityEngine();
62         vEngine.setProperty(VelocityEngine.VM_LIBRARY, "");
63         vEngine.setProperty(VelocityEngine.RESOURCE_LOADER, "file");
64         vEngine.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH,
65                             System.getProperty("install.root")
66                             + "/" + "templates" + "/" + "newbean");
67         try {
68             vEngine.init();
69         } catch (Exception JavaDoc e) {
70             fatalError("unable to initilise Velocity engine (" + e + ")");
71         }
72
73         // Creates a Velocity context and populates it by walking
74
// through the parameter set
75
vContext = new VelocityContext();
76         ParameterSet parameterSet = new ParameterSet(vContext);
77         parameterSet.walkThrough();
78
79         // Generates files
80
generate();
81
82     }
83
84
85     /**
86      * Generates files for this new bean.
87      */

88     private void generate() {
89
90         // Gets some useful information from Velocity's context
91
final Integer JavaDoc MESSAGE_DRIVEN_BEAN =
92             (Integer JavaDoc)vContext.get("MESSAGE_DRIVEN_BEAN");
93         String JavaDoc beanName = (String JavaDoc)vContext.get("beanName");
94         String JavaDoc beanType = (String JavaDoc)vContext.get("beanType");
95         String JavaDoc pkgName = (String JavaDoc)vContext.get("pkgName");
96         String JavaDoc jarName = (String JavaDoc)vContext.get("jarName");
97         Integer JavaDoc beanFlavor = (Integer JavaDoc)vContext.get("beanFlavor");
98
99
100         System.out.println("Creating bean "
101                            + beanName
102                            + " (type "
103                            + beanType
104                            + ") in package "
105                            + pkgName);
106     
107         // Generates bean files
108
try {
109             boolean isClientToGenerate = true;
110             generate("ejb-jar.vm", jarName + ".xml");
111             generate("jonas-ejb-jar.vm", "jonas-" + jarName + ".xml");
112             if (beanFlavor != MESSAGE_DRIVEN_BEAN) {
113                 String JavaDoc local = "";
114                 Boolean JavaDoc isLocal = (Boolean JavaDoc)vContext.get("isLocal");
115                 if (isLocal.booleanValue()) {
116                     local = "Local";
117                     isClientToGenerate = false;
118                 }
119                 generate("remote.vm", beanName + local + ".java");
120                 generate("home.vm", beanName + local + "Home.java");
121             }
122             generate("bean.vm", beanName + beanType + ".java");
123             if (isClientToGenerate) {
124                 generate("client.vm", beanName + "Client.java");
125             }
126             generate("build.vm", "build.xml");
127             System.out.println("Your bean files have been created. You can now customize them.");
128         } catch (Exception JavaDoc e) {
129             error(e.toString());
130         }
131     
132     }
133
134
135     /**
136      * Generates a file from the specified template.
137      * @param templateFileName the name of the template file
138      * @param targetFileName the name of the generated file
139      */

140     private void generate(String JavaDoc templateFileName,
141                           String JavaDoc targetFileName) throws Exception JavaDoc, IOException JavaDoc, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
142         FileWriter JavaDoc fileWriter = null;
143         fileWriter = new FileWriter JavaDoc(targetFileName);
144         vEngine.mergeTemplate(templateFileName, vContext, fileWriter);
145         fileWriter.close();
146     }
147
148     public static HashMap JavaDoc commandLine = new HashMap JavaDoc();
149
150     private static void putCmdArgsInHashmap( String JavaDoc[] args )
151     {
152         for( int i=0; i<args.length; i++ ) {
153             String JavaDoc key = args[i];
154             if( key.startsWith("-") ) {
155                 String JavaDoc val = null;
156                 if( i+1<args.length
157                 && !args[i+1].startsWith("-") ) {
158                     val = args[i+1];
159                     i++;
160                 }
161                 commandLine.put( key, val );
162             }
163         }
164     }
165
166     public static void main( String JavaDoc[] args ) {
167
168         if( args.length > 0 ) {
169             // save the command line arguments so that we don't
170
// have to be interactive when called from JOPE
171
putCmdArgsInHashmap(args);
172         }
173
174         new NewBean();
175     }
176
177
178     /**
179      * Display the specified error message.
180      * @param errMsg the error message to display
181      */

182     static void error(String JavaDoc errMsg) {
183         System.err.println("NewBean error: " + errMsg);
184     }
185
186
187     /**
188      * Display the specified error message and exits with an
189      * EXIT_FAILURE status.
190      * @param errMsg the error message to display
191      */

192     static void fatalError(String JavaDoc errMsg) {
193         System.err.println("NewBean fatal error: " + errMsg);
194         System.exit(EXIT_FAILURE);
195     }
196
197 }
198
Popular Tags