00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 package org.objectweb.jonas.newbean;
00031
00032
00033 import java.io.FileWriter;
00034 import java.io.IOException;
00035 import java.util.HashMap;
00036
00037 import org.apache.velocity.VelocityContext;
00038 import org.apache.velocity.app.VelocityEngine;
00039 import org.apache.velocity.exception.MethodInvocationException;
00040 import org.apache.velocity.exception.ParseErrorException;
00041 import org.apache.velocity.exception.ResourceNotFoundException;
00042
00043
00049 public class NewBean {
00050
00051 private static final int EXIT_SUCCESS = 0;
00052 private static final int EXIT_FAILURE = 1;
00053
00054 private VelocityEngine vEngine = null;
00055 private VelocityContext vContext = null;
00056
00057
00058 private NewBean() {
00059
00060
00061 vEngine = new VelocityEngine();
00062 vEngine.setProperty(VelocityEngine.VM_LIBRARY, "");
00063 vEngine.setProperty(VelocityEngine.RESOURCE_LOADER, "file");
00064 vEngine.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH,
00065 System.getProperty("install.root")
00066 + "/" + "templates" + "/" + "newbean");
00067 try {
00068 vEngine.init();
00069 } catch (Exception e) {
00070 fatalError("unable to initilise Velocity engine (" + e + ")");
00071 }
00072
00073
00074
00075 vContext = new VelocityContext();
00076 ParameterSet parameterSet = new ParameterSet(vContext);
00077 parameterSet.walkThrough();
00078
00079
00080 generate();
00081
00082 }
00083
00084
00088 private void generate() {
00089
00090
00091 final Integer MESSAGE_DRIVEN_BEAN =
00092 (Integer)vContext.get("MESSAGE_DRIVEN_BEAN");
00093 String beanName = (String)vContext.get("beanName");
00094 String beanType = (String)vContext.get("beanType");
00095 String pkgName = (String)vContext.get("pkgName");
00096 String jarName = (String)vContext.get("jarName");
00097 Integer beanFlavor = (Integer)vContext.get("beanFlavor");
00098
00099
00100 System.out.println("Creating bean "
00101 + beanName
00102 + " (type "
00103 + beanType
00104 + ") in package "
00105 + pkgName);
00106
00107
00108 try {
00109 boolean isClientToGenerate = true;
00110 generate("ejb-jar.vm", jarName + ".xml");
00111 generate("jonas-ejb-jar.vm", "jonas-" + jarName + ".xml");
00112 if (beanFlavor != MESSAGE_DRIVEN_BEAN) {
00113 String local = "";
00114 Boolean isLocal = (Boolean)vContext.get("isLocal");
00115 if (isLocal.booleanValue()) {
00116 local = "Local";
00117 isClientToGenerate = false;
00118 }
00119 generate("remote.vm", beanName + local + ".java");
00120 generate("home.vm", beanName + local + "Home.java");
00121 }
00122 generate("bean.vm", beanName + beanType + ".java");
00123 if (isClientToGenerate) {
00124 generate("client.vm", beanName + "Client.java");
00125 }
00126 generate("build.vm", "build.xml");
00127 System.out.println("Your bean files have been created. You can now customize them.");
00128 } catch (Exception e) {
00129 error(e.toString());
00130 }
00131
00132 }
00133
00134
00140 private void generate(String templateFileName,
00141 String targetFileName) throws Exception, IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
00142 FileWriter fileWriter = null;
00143 fileWriter = new FileWriter(targetFileName);
00144 vEngine.mergeTemplate(templateFileName, vContext, fileWriter);
00145 fileWriter.close();
00146 }
00147
00148 public static HashMap commandLine = new HashMap();
00149
00150 private static void putCmdArgsInHashmap( String[] args )
00151 {
00152 for( int i=0; i<args.length; i++ ) {
00153 String key = args[i];
00154 if( key.startsWith("-") ) {
00155 String val = null;
00156 if( i+1<args.length
00157 && !args[i+1].startsWith("-") ) {
00158 val = args[i+1];
00159 i++;
00160 }
00161 commandLine.put( key, val );
00162 }
00163 }
00164 }
00165
00166 public static void main( String[] args ) {
00167
00168 if( args.length > 0 ) {
00169
00170
00171 putCmdArgsInHashmap(args);
00172 }
00173
00174 new NewBean();
00175 }
00176
00177
00182 static void error(String errMsg) {
00183 System.err.println("NewBean error: " + errMsg);
00184 }
00185
00186
00192 static void fatalError(String errMsg) {
00193 System.err.println("NewBean fatal error: " + errMsg);
00194 System.exit(EXIT_FAILURE);
00195 }
00196
00197 }