JVelocity.java

00001 
00025 package org.objectweb.jonas_ws.wsgen.generator.axis;
00026 
00027 import java.io.File;
00028 import java.io.FileWriter;
00029 import java.io.IOException;
00030 
00031 import org.apache.velocity.Template;
00032 import org.apache.velocity.VelocityContext;
00033 import org.apache.velocity.app.VelocityEngine;
00034 import org.apache.velocity.runtime.RuntimeConstants;
00035 
00036 import org.objectweb.jonas_lib.I18n;
00037 
00038 import org.objectweb.jonas_ws.wsgen.WsGenException;
00039 
00040 import org.objectweb.jonas.common.Log;
00041 
00042 import org.objectweb.util.monolog.api.BasicLevel;
00043 import org.objectweb.util.monolog.api.Logger;
00044 
00050 public class JVelocity {
00051 
00053     private static I18n i18n = I18n.getInstance(JVelocity.class);
00054 
00058     private static Logger logger =  Log.getLogger(Log.JONAS_WSGEN_PREFIX);
00059 
00061     private VelocityEngine vEngine;
00062 
00064     private Template template;
00065 
00074     public JVelocity(String tmplName) throws WsGenException {
00075         // Prepare Velocity Engine
00076         String jonasRoot = System.getProperty("install.root");
00077 
00078         if (jonasRoot == null) {
00079             String err = i18n.getMessage("JVelocity.constr.notset");
00080             throw new WsGenException(err);
00081         }
00082 
00083         String path2Tmpl = new String(jonasRoot + File.separatorChar + "templates" + File.separatorChar + "wsgen"
00084                 + File.separatorChar + "generator" + File.separatorChar + "axis");
00085 
00086         // instanciate the engine
00087         vEngine = new VelocityEngine();
00088         vEngine.setProperty(RuntimeConstants.VM_LIBRARY, "");
00089         vEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "file");
00090         vEngine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, path2Tmpl);
00091 
00092         try {
00093             vEngine.init();
00094         } catch (Exception e) {
00095             String err = i18n.getMessage("JVelocity.constr.initFailure");
00096             throw new WsGenException(err, e);
00097         }
00098 
00099         // Create the Template
00100         try {
00101             template = vEngine.getTemplate(tmplName);
00102         } catch (Exception e) {
00103             String err = i18n.getMessage("JVelocity.constr.tmplError", tmplName);
00104             throw new WsGenException(err, e);
00105         }
00106     }
00107 
00116     public void generate(File fs, VelocityContext context) throws WsGenException {
00117         FileWriter fwriter = null;
00118 
00119         try {
00120             // Create before the parent directory
00121             File fdir = fs.getParentFile();
00122 
00123             if (fdir != null) {
00124                 if (!fdir.exists()) {
00125                     if (!fdir.mkdirs()) {
00126                         String err = i18n.getMessage("JVelocity.generate.directories", fdir.getPath());
00127                         throw new WsGenException(err);
00128                     }
00129                 }
00130             }
00131 
00132             fwriter = new FileWriter(fs);
00133         } catch (IOException e) {
00134             String err = i18n.getMessage("JVelocity.generate.file", fs);
00135             throw new WsGenException(err, e);
00136         }
00137 
00138         try {
00139             template.merge(context, fwriter);
00140         } catch (Exception e) {
00141             String err = i18n.getMessage("JVelocity.generate.cannot", fs);
00142             throw new WsGenException(err, e);
00143         }
00144 
00145         try {
00146             fwriter.flush();
00147             fwriter.close();
00148         } catch (IOException e) {
00149             // do nothing, just a warn
00150             String err = i18n.getMessage("JVelocity.generate.close", fs);
00151             logger.log(BasicLevel.WARN, err);
00152         }
00153     }
00154 }

Generated on Tue Feb 15 15:06:00 2005 for JOnAS by  doxygen 1.3.9.1