You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by mm...@apache.org on 2007/05/10 11:45:00 UTC

svn commit: r536799 - in /incubator/cxf/trunk: codegen-plugin/src/main/java/org/apache/cxf/maven_plugin/eclipse/ tools/common/src/main/java/org/apache/cxf/tools/common/ tools/wsdlto/core/src/main/java/org/apache/cxf/tools/wsdlto/core/ tools/wsdlto/fron...

Author: mmao
Date: Thu May 10 02:44:59 2007
New Revision: 536799

URL: http://svn.apache.org/viewvc?view=rev&rev=536799
Log:
Code cleanup
* ReUse VelosityGenerator in couple places.
* Messages update


Modified:
    incubator/cxf/trunk/codegen-plugin/src/main/java/org/apache/cxf/maven_plugin/eclipse/EclipsePluginMojo.java
    incubator/cxf/trunk/tools/common/src/main/java/org/apache/cxf/tools/common/Messages.properties
    incubator/cxf/trunk/tools/wsdlto/core/src/main/java/org/apache/cxf/tools/wsdlto/core/AbstractGenerator.java
    incubator/cxf/trunk/tools/wsdlto/core/src/main/java/org/apache/cxf/tools/wsdlto/core/WSDLToProcessor.java
    incubator/cxf/trunk/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/generators/Messages.properties
    incubator/cxf/trunk/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/processor/internal/Messages.properties

Modified: incubator/cxf/trunk/codegen-plugin/src/main/java/org/apache/cxf/maven_plugin/eclipse/EclipsePluginMojo.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/codegen-plugin/src/main/java/org/apache/cxf/maven_plugin/eclipse/EclipsePluginMojo.java?view=diff&rev=536799&r1=536798&r2=536799
==============================================================================
--- incubator/cxf/trunk/codegen-plugin/src/main/java/org/apache/cxf/maven_plugin/eclipse/EclipsePluginMojo.java (original)
+++ incubator/cxf/trunk/codegen-plugin/src/main/java/org/apache/cxf/maven_plugin/eclipse/EclipsePluginMojo.java Thu May 10 02:44:59 2007
@@ -19,27 +19,21 @@
 
 package org.apache.cxf.maven_plugin.eclipse;
 
-import java.io.BufferedOutputStream;
 import java.io.File;
-import java.io.FileOutputStream;
-import java.io.OutputStreamWriter;
-import java.io.Writer;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
-import java.util.Properties;
+import java.util.Set;
 
 import org.apache.cxf.common.util.ReflectionUtil;
 import org.apache.cxf.common.util.StringUtils;
-import org.apache.cxf.tools.wsdlto.core.VelocityWriter;
+import org.apache.cxf.tools.common.VelocityGenerator;
+import org.apache.cxf.tools.util.FileWriterUtil;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.project.MavenProject;
-import org.apache.velocity.Template;
-import org.apache.velocity.VelocityContext;
-import org.apache.velocity.app.Velocity;
 
 /**
  * @goal eclipseplugin
@@ -59,7 +53,7 @@
      * @required
      * @readonly
      */
-    java.util.Set dependencies;
+    Set dependencies;
 
     /**
      * @parameter  expression="${project.build.directory}/plugin.xml";
@@ -99,26 +93,10 @@
     }
 
 
-    private String getVelocityLogFile(String log) {
-        return new File(targetFile.getParentFile(), log).toString();
-    }
-
     private String getVersion() {
         return StringUtils.formatVersionNumber(project.getVersion());
     }
 
-    // TODO: Reuse the velocity in the tools 
-    private void initVelocity() throws Exception {
-        Properties props = new Properties();
-        String clzName = "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader";
-        props.put("resource.loader", "class");
-        props.put("class.resource.loader.class", clzName);
-        props.put("runtime.log", getVelocityLogFile("velocity.log"));
-
-        Velocity.init(props);
-
-    }
-
     private List<String> getExportedPackages(List<File> jars) throws Exception {
         List<String> packages = new ArrayList<String>();
         for (File jarFile : jars) {
@@ -128,37 +106,23 @@
     }
 
     private void generatePluginXML(List<File> jars) throws Exception {
-        initVelocity();
-
-        Template tmpl = null;
+        VelocityGenerator velocity = new VelocityGenerator();
 
         String templateFile = getTemplateFile(eclipseVersion);
-        tmpl = Velocity.getTemplate(templateFile);
-
-        if (tmpl == null) {
-            throw new RuntimeException("Can not load template file: " + templateFile);
-        }
 
         File outputFile = targetFile;
-        
-        VelocityContext ctx = new VelocityContext();
-        ctx.put("ECLIPSE_VERSION", eclipseVersion);
-        ctx.put("PLUGIN_VERSION", getVersion());
-        ctx.put("GROUP_ID", project.getGroupId());
-        ctx.put("libPath", LIB_PATH);
-        ctx.put("jars", jars);
+
+        velocity.setAttributes("ECLIPSE_VERSION", eclipseVersion);
+        velocity.setAttributes("PLUGIN_VERSION", getVersion());
+        velocity.setAttributes("GROUP_ID", project.getGroupId());
+        velocity.setAttributes("libPath", LIB_PATH);
+        velocity.setAttributes("jars", jars);
         
         if ("3.1".equals(eclipseVersion.trim())) {
-            ctx.put("exportedPackages", getExportedPackages(jars));
+            velocity.setAttributes("exportedPackages", getExportedPackages(jars));
             outputFile = new File(targetFile.getParentFile(), "MANIFEST.MF");
         }
 
-        Writer outputs = null;
-
-        outputs = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(outputFile)), "UTF-8");
-        VelocityWriter writer = new VelocityWriter(outputs);
-
-        tmpl.merge(ctx, writer);
-        writer.close();
+        velocity.doWrite(templateFile, FileWriterUtil.getWriter(outputFile));
     }
 }

Modified: incubator/cxf/trunk/tools/common/src/main/java/org/apache/cxf/tools/common/Messages.properties
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/tools/common/src/main/java/org/apache/cxf/tools/common/Messages.properties?view=diff&rev=536799&r1=536798&r2=536799
==============================================================================
--- incubator/cxf/trunk/tools/common/src/main/java/org/apache/cxf/tools/common/Messages.properties (original)
+++ incubator/cxf/trunk/tools/common/src/main/java/org/apache/cxf/tools/common/Messages.properties Thu May 10 02:44:59 2007
@@ -25,3 +25,7 @@
 PARAMETER_MISSING = Required parameter is missing
 
 
+FAIL_TO_INITIALIZE_VELOCITY_ENGINE = Fail to initialize velocity engine
+TEMPLATE_MISSING = Can not find velocity tempalte file: {0}
+VELOCITY_ENGINE_WRITE_ERRORS = velocity engin write errors
+FAIL_TO_WRITE_FILE = Failed to write file : {0}
\ No newline at end of file

Modified: incubator/cxf/trunk/tools/wsdlto/core/src/main/java/org/apache/cxf/tools/wsdlto/core/AbstractGenerator.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/tools/wsdlto/core/src/main/java/org/apache/cxf/tools/wsdlto/core/AbstractGenerator.java?view=diff&rev=536799&r1=536798&r2=536799
==============================================================================
--- incubator/cxf/trunk/tools/wsdlto/core/src/main/java/org/apache/cxf/tools/wsdlto/core/AbstractGenerator.java (original)
+++ incubator/cxf/trunk/tools/wsdlto/core/src/main/java/org/apache/cxf/tools/wsdlto/core/AbstractGenerator.java Thu May 10 02:44:59 2007
@@ -22,9 +22,6 @@
 import java.io.IOException;
 import java.io.Writer;
 import java.util.Calendar;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
 import java.util.logging.Logger;
 
 import org.apache.cxf.common.i18n.Message;
@@ -33,47 +30,23 @@
 import org.apache.cxf.tools.common.ToolConstants;
 import org.apache.cxf.tools.common.ToolContext;
 import org.apache.cxf.tools.common.ToolException;
+import org.apache.cxf.tools.common.VelocityGenerator;
 import org.apache.cxf.tools.util.ClassCollector;
 import org.apache.cxf.tools.util.FileWriterUtil;
 import org.apache.cxf.version.Version;
-import org.apache.velocity.Template;
-import org.apache.velocity.VelocityContext;
-import org.apache.velocity.app.Velocity;
 
 public abstract class AbstractGenerator implements FrontEndGenerator {
 
     private static final Logger LOG = LogUtils.getL7dLogger(AbstractGenerator.class);
     protected ToolContext env;
-    protected Map<String, Object> attributes = new HashMap<String, Object>();
     protected String name;
+    protected final VelocityGenerator velocity = new VelocityGenerator();
 
     public AbstractGenerator() {
     }
 
     protected void doWrite(String templateName, Writer outputs) throws ToolException {
-        Template tmpl = null;
-        try {
-            tmpl = Velocity.getTemplate(templateName);
-        } catch (Exception e) {
-            Message msg = new Message("TEMPLATE_MISSING", LOG, templateName);
-            throw new ToolException(msg, e);
-        }
-
-        VelocityContext ctx = new VelocityContext();
-
-        for (Iterator iter = attributes.keySet().iterator(); iter.hasNext();) {
-            String key = (String)iter.next();
-            ctx.put(key, attributes.get(key));
-        }
-
-        VelocityWriter writer = new VelocityWriter(outputs);
-        try {
-            tmpl.merge(ctx, writer);
-            writer.close();
-        } catch (Exception e) {
-            Message msg = new Message("VELOCITY_ENGINE_WRITE_ERRORS", LOG);
-            throw new ToolException(msg, e);
-        }
+        velocity.doWrite(templateName, outputs);
     }
 
     protected boolean isCollision(String packageName, String filename) throws ToolException {        
@@ -130,16 +103,16 @@
     }
 
     protected void setAttributes(String n, Object value) {
-        attributes.put(n, value);
+        velocity.setAttributes(n, value);
     }
 
     protected void setCommonAttributes() {
-        attributes.put("currentdate", Calendar.getInstance().getTime());
-        attributes.put("version", Version.getCurrentVersion());
+        setAttributes("currentdate", Calendar.getInstance().getTime());
+        setAttributes("version", Version.getCurrentVersion());
     }
 
     protected void clearAttributes() {
-        attributes.clear();
+        velocity.clearAttributes();
     }
 
     public void setEnvironment(ToolContext penv) {

Modified: incubator/cxf/trunk/tools/wsdlto/core/src/main/java/org/apache/cxf/tools/wsdlto/core/WSDLToProcessor.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/tools/wsdlto/core/src/main/java/org/apache/cxf/tools/wsdlto/core/WSDLToProcessor.java?view=diff&rev=536799&r1=536798&r2=536799
==============================================================================
--- incubator/cxf/trunk/tools/wsdlto/core/src/main/java/org/apache/cxf/tools/wsdlto/core/WSDLToProcessor.java (original)
+++ incubator/cxf/trunk/tools/wsdlto/core/src/main/java/org/apache/cxf/tools/wsdlto/core/WSDLToProcessor.java Thu May 10 02:44:59 2007
@@ -19,20 +19,12 @@
 
 package org.apache.cxf.tools.wsdlto.core;
 
-import java.io.File;
-import java.io.IOException;
-import java.io.Writer;
-import java.util.Properties;
-import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.tools.common.Processor;
-import org.apache.cxf.tools.common.ToolConstants;
 import org.apache.cxf.tools.common.ToolContext;
 import org.apache.cxf.tools.common.ToolException;
-import org.apache.cxf.tools.util.FileWriterUtil;
-import org.apache.velocity.app.Velocity;
 
 public class WSDLToProcessor implements Processor {
     protected static final Logger LOG = LogUtils.getL7dLogger(WSDLToProcessor.class);
@@ -40,84 +32,8 @@
 
     protected ToolContext context;
 
-    protected Writer getOutputWriter(String newNameExt) throws ToolException {
-        Writer writer = null;
-        String newName = null;
-        String outputDir;
-
-        if (context.get(ToolConstants.CFG_OUTPUTFILE) != null) {
-            newName = (String)context.get(ToolConstants.CFG_OUTPUTFILE);
-        } else {
-            String oldName = (String)context.get(ToolConstants.CFG_WSDLURL);
-            int position = oldName.lastIndexOf("/");
-            if (position < 0) {
-                position = oldName.lastIndexOf("\\");
-            }
-            if (position >= 0) {
-                oldName = oldName.substring(position + 1, oldName.length());
-            }
-            if (oldName.toLowerCase().indexOf(WSDL_FILE_NAME_EXT) >= 0) {
-                newName = oldName.substring(0, oldName.length() - 5) + newNameExt + WSDL_FILE_NAME_EXT;
-            } else {
-                newName = oldName + newNameExt;
-            }
-        }
-        if (context.get(ToolConstants.CFG_OUTPUTDIR) != null) {
-            outputDir = (String)context.get(ToolConstants.CFG_OUTPUTDIR);
-            if (!("/".equals(outputDir.substring(outputDir.length() - 1)) || "\\".equals(outputDir
-                .substring(outputDir.length() - 1)))) {
-                outputDir = outputDir + "/";
-            }
-        } else {
-            outputDir = "./";
-        }
-        FileWriterUtil fw = new FileWriterUtil(outputDir);
-        try {
-            writer = fw.getWriter("", newName);
-        } catch (IOException ioe) {
-            org.apache.cxf.common.i18n.Message msg =
-                new org.apache.cxf.common.i18n.Message("FAIL_TO_WRITE_FILE",
-                                                       LOG,
-                                                       context.get(ToolConstants.CFG_OUTPUTDIR)
-                                                       + System.getProperty("file.seperator")
-                                                       + newName);
-            throw new ToolException(msg, ioe);
-        }
-        return writer;
-    }
-
-    private String getVelocityLogFile(String logfile) {
-        String logdir = System.getProperty("user.home");
-        if (logdir == null || logdir.length() == 0) {
-            logdir = System.getProperty("user.dir");
-        }
-        return logdir + File.separator + logfile;
-    }
-
-    private void initVelocity() throws ToolException {
-        try {
-            Properties props = new Properties();
-            String clzName = "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader";
-            props.put("resource.loader", "class");
-            props.put("class.resource.loader.class", clzName);
-            props.put("runtime.log", getVelocityLogFile("velocity.log"));
-
-            Velocity.init(props);
-        } catch (Exception e) {
-            org.apache.cxf.common.i18n.Message msg =
-                new org.apache.cxf.common.i18n.Message("FAIL_TO_INITIALIZE_VELOCITY_ENGINE",
-                                                             LOG);
-            LOG.log(Level.SEVERE, msg.toString());
-            throw new ToolException(msg, e);
-        }
-    }
-
-    private void init() throws ToolException {
-        initVelocity();    
-    }
-
     public void process() throws ToolException {
-        init();
+        // empty, suppose to extend
     }
 
     public void setEnvironment(ToolContext penv) {
@@ -127,6 +43,4 @@
     public ToolContext getEnvironment() {
         return this.context;
     }
-    
-   
 }

Modified: incubator/cxf/trunk/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/generators/Messages.properties
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/generators/Messages.properties?view=diff&rev=536799&r1=536798&r2=536799
==============================================================================
--- incubator/cxf/trunk/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/generators/Messages.properties (original)
+++ incubator/cxf/trunk/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/generators/Messages.properties Thu May 10 02:44:59 2007
@@ -18,8 +18,8 @@
 #    under the License.
 #
 #
-TEMPLATE_MISSING = Can not find velocity tempalte file: {0}
-VELOCITY_ENGINE_WRITE_ERRORS = velocity engin write errors
+
+
 FAIL_TO_WRITE_FILE = Failed to write file : {0}
 FAIL_TO_GET_WSDL = Fail to get wsdl url from : {0}
 CAN_NOT_GEN_ANT = WSDL2Java Warning : can not generate ant build.xml for the WSDL has no service : {0}

Modified: incubator/cxf/trunk/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/processor/internal/Messages.properties
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/processor/internal/Messages.properties?view=diff&rev=536799&r1=536798&r2=536799
==============================================================================
--- incubator/cxf/trunk/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/processor/internal/Messages.properties (original)
+++ incubator/cxf/trunk/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/processor/internal/Messages.properties Thu May 10 02:44:59 2007
@@ -31,7 +31,7 @@
 FAIL_TO_COPY_GENERATED_RESOURCE_FILE = Fail to copy generated resource file
 FAIL_TO_WRITE_FILE = Fail to write file {0}
 FAIL_TO_CREATE_WSDL_DEFINITION = Fail to create wsdl definition {0}
-FAIL_TO_INITIALIZE_VELOCITY_ENGINE = Fail to initialize velocity engine
+
 SERVICE_PORT_EXIST = Input service and port already exist in imported contract
 BINDING_NOT_EXIST = Input binding does not exist in imported contract
 FAIL_TO_WRITE_WSDL = Fail to write wsdl file