001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer(s): Nicolas Christin
022: * Contributor(s): ______________________________________.
023: *
024: * --------------------------------------------------------------------------
025: * $Id: NewBean.java 4408 2004-03-19 14:31:54Z sauthieg $
026: * --------------------------------------------------------------------------
027: */
028:
029: package org.objectweb.jonas.newbean;
030:
031: import java.io.FileWriter;
032: import java.io.IOException;
033: import java.util.HashMap;
034:
035: import org.apache.velocity.VelocityContext;
036: import org.apache.velocity.app.VelocityEngine;
037: import org.apache.velocity.exception.MethodInvocationException;
038: import org.apache.velocity.exception.ParseErrorException;
039: import org.apache.velocity.exception.ResourceNotFoundException;
040:
041: /**
042: * This class is a “bean generator”. It uses Velocity and
043: * a set of template files to generate some files that can serve as a
044: * startup point for the developpement of a new bean.
045: */
046: public class NewBean {
047:
048: private static final int EXIT_SUCCESS = 0;
049: private static final int EXIT_FAILURE = 1;
050:
051: private VelocityEngine vEngine = null;
052: private VelocityContext vContext = null;
053:
054: private NewBean() {
055:
056: // Creates and initializes a Velocity engine
057: vEngine = new VelocityEngine();
058: vEngine.setProperty(VelocityEngine.VM_LIBRARY, "");
059: vEngine.setProperty(VelocityEngine.RESOURCE_LOADER, "file");
060: vEngine.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH,
061: System.getProperty("install.root") + "/" + "templates"
062: + "/" + "newbean");
063: try {
064: vEngine.init();
065: } catch (Exception e) {
066: fatalError("unable to initilise Velocity engine (" + e
067: + ")");
068: }
069:
070: // Creates a Velocity context and populates it by walking
071: // through the parameter set
072: vContext = new VelocityContext();
073: ParameterSet parameterSet = new ParameterSet(vContext);
074: parameterSet.walkThrough();
075:
076: // Generates files
077: generate();
078:
079: }
080:
081: /**
082: * Generates files for this new bean.
083: */
084: private void generate() {
085:
086: // Gets some useful information from Velocity's context
087: final Integer MESSAGE_DRIVEN_BEAN = (Integer) vContext
088: .get("MESSAGE_DRIVEN_BEAN");
089: String beanName = (String) vContext.get("beanName");
090: String beanType = (String) vContext.get("beanType");
091: String pkgName = (String) vContext.get("pkgName");
092: String jarName = (String) vContext.get("jarName");
093: Integer beanFlavor = (Integer) vContext.get("beanFlavor");
094:
095: System.out.println("Creating bean " + beanName + " (type "
096: + beanType + ") in package " + pkgName);
097:
098: // Generates bean files
099: try {
100: boolean isClientToGenerate = true;
101: generate("ejb-jar.vm", jarName + ".xml");
102: generate("jonas-ejb-jar.vm", "jonas-" + jarName + ".xml");
103: if (beanFlavor != MESSAGE_DRIVEN_BEAN) {
104: String local = "";
105: Boolean isLocal = (Boolean) vContext.get("isLocal");
106: if (isLocal.booleanValue()) {
107: local = "Local";
108: isClientToGenerate = false;
109: }
110: generate("remote.vm", beanName + local + ".java");
111: generate("home.vm", beanName + local + "Home.java");
112: }
113: generate("bean.vm", beanName + beanType + ".java");
114: if (isClientToGenerate) {
115: generate("client.vm", beanName + "Client.java");
116: }
117: generate("build.vm", "build.xml");
118: System.out
119: .println("Your bean files have been created. You can now customize them.");
120: } catch (Exception e) {
121: error(e.toString());
122: }
123:
124: }
125:
126: /**
127: * Generates a file from the specified template.
128: * @param templateFileName the name of the template file
129: * @param targetFileName the name of the generated file
130: */
131: private void generate(String templateFileName, String targetFileName)
132: throws Exception, IOException, ResourceNotFoundException,
133: ParseErrorException, MethodInvocationException {
134: FileWriter fileWriter = null;
135: fileWriter = new FileWriter(targetFileName);
136: vEngine.mergeTemplate(templateFileName, vContext, fileWriter);
137: fileWriter.close();
138: }
139:
140: public static HashMap commandLine = new HashMap();
141:
142: private static void putCmdArgsInHashmap(String[] args) {
143: for (int i = 0; i < args.length; i++) {
144: String key = args[i];
145: if (key.startsWith("-")) {
146: String val = null;
147: if (i + 1 < args.length && !args[i + 1].startsWith("-")) {
148: val = args[i + 1];
149: i++;
150: }
151: commandLine.put(key, val);
152: }
153: }
154: }
155:
156: public static void main(String[] args) {
157:
158: if (args.length > 0) {
159: // save the command line arguments so that we don't
160: // have to be interactive when called from JOPE
161: putCmdArgsInHashmap(args);
162: }
163:
164: new NewBean();
165: }
166:
167: /**
168: * Display the specified error message.
169: * @param errMsg the error message to display
170: */
171: static void error(String errMsg) {
172: System.err.println("NewBean error: " + errMsg);
173: }
174:
175: /**
176: * Display the specified error message and exits with an
177: * EXIT_FAILURE status.
178: * @param errMsg the error message to display
179: */
180: static void fatalError(String errMsg) {
181: System.err.println("NewBean fatal error: " + errMsg);
182: System.exit(EXIT_FAILURE);
183: }
184:
185: }
|