NewBean.java

00001 /*
00002  * JOnAS: Java(TM) Open Application Server
00003  * Copyright (C) 1999 Bull S.A.
00004  * Contact: jonas-team@objectweb.org
00005  * 
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or any later version.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00019  * USA
00020  *
00021  * Initial developer(s): Nicolas Christin
00022  * Contributor(s): ______________________________________.
00023  *
00024  * --------------------------------------------------------------------------
00025  * $Id: NewBean.java,v 1.8 2004/03/19 14:31:52 sauthieg Exp $
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         // Creates and initializes a Velocity engine
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         // Creates a Velocity context and populates it by walking
00074         // through the parameter set
00075         vContext = new VelocityContext();
00076         ParameterSet parameterSet = new ParameterSet(vContext);
00077         parameterSet.walkThrough();
00078 
00079         // Generates files
00080         generate();
00081 
00082     }
00083 
00084 
00088     private void generate() {
00089 
00090         // Gets some useful information from Velocity's context
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         // Generates bean files
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                         // save the command line arguments so that we don't
00170                         // have to be interactive when called from JOPE
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 }

Generated on Tue Feb 15 15:05:19 2005 for JOnAS by  doxygen 1.3.9.1