1 28 29 30 package org.objectweb.jonas.newbean; 31 32 33 import java.io.FileWriter ; 34 import java.io.IOException ; 35 import java.util.HashMap ; 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 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 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 e) { 70 fatalError("unable to initilise Velocity engine (" + e + ")"); 71 } 72 73 vContext = new VelocityContext(); 76 ParameterSet parameterSet = new ParameterSet(vContext); 77 parameterSet.walkThrough(); 78 79 generate(); 81 82 } 83 84 85 88 private void generate() { 89 90 final Integer MESSAGE_DRIVEN_BEAN = 92 (Integer )vContext.get("MESSAGE_DRIVEN_BEAN"); 93 String beanName = (String )vContext.get("beanName"); 94 String beanType = (String )vContext.get("beanType"); 95 String pkgName = (String )vContext.get("pkgName"); 96 String jarName = (String )vContext.get("jarName"); 97 Integer beanFlavor = (Integer )vContext.get("beanFlavor"); 98 99 100 System.out.println("Creating bean " 101 + beanName 102 + " (type " 103 + beanType 104 + ") in package " 105 + pkgName); 106 107 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 local = ""; 114 Boolean isLocal = (Boolean )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 e) { 129 error(e.toString()); 130 } 131 132 } 133 134 135 140 private void generate(String templateFileName, 141 String targetFileName) throws Exception , IOException , ResourceNotFoundException, ParseErrorException, MethodInvocationException { 142 FileWriter fileWriter = null; 143 fileWriter = new FileWriter (targetFileName); 144 vEngine.mergeTemplate(templateFileName, vContext, fileWriter); 145 fileWriter.close(); 146 } 147 148 public static HashMap commandLine = new HashMap (); 149 150 private static void putCmdArgsInHashmap( String [] args ) 151 { 152 for( int i=0; i<args.length; i++ ) { 153 String key = args[i]; 154 if( key.startsWith("-") ) { 155 String 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 [] args ) { 167 168 if( args.length > 0 ) { 169 putCmdArgsInHashmap(args); 172 } 173 174 new NewBean(); 175 } 176 177 178 182 static void error(String errMsg) { 183 System.err.println("NewBean error: " + errMsg); 184 } 185 186 187 192 static void fatalError(String errMsg) { 193 System.err.println("NewBean fatal error: " + errMsg); 194 System.exit(EXIT_FAILURE); 195 } 196 197 } 198 | Popular Tags |