[Buix-commits] r748 - trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx
Author: tchemit Date: 2008-07-21 13:21:26 +0000 (Mon, 21 Jul 2008) New Revision: 748 Added: trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/AbstractActionGeneratorGoal.java trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/ActionProviderGeneratorGoal.java Modified: trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/ActionGeneratorGoal.java Log: generate action provider + refactor actions-*** goals Added: trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/AbstractActionGeneratorGoal.java =================================================================== --- trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/AbstractActionGeneratorGoal.java (rev 0) +++ trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/AbstractActionGeneratorGoal.java 2008-07-21 13:21:26 UTC (rev 748) @@ -0,0 +1,68 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. This program is + * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * # #% + */ +package org.codelutin.jaxx; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.project.MavenProject; + +import java.io.File; + +/** @author chemit */ +public abstract class AbstractActionGeneratorGoal extends AbstractMojo { + + /** + * @description D�pendance du projet. + * @parameter default-value="${project}" + * @readonly + */ + protected MavenProject project; + + /** + * @description Nom du fichier destination � g�n�rer. + * @parameter expression="${jaxx.baseResource}" default-value="${basedir}/src/main/resources" + */ + protected File baseResource; + + /** + * @description Nom du fichier destination � g�n�rer. + * @parameter expression="${jaxx.actionsFile}" default-value="/jaxx/${project.artifactId}-actions.properties" + */ + protected String actionsFile; + + /** + * @description Nom du fichier destination � g�n�rer. + * @parameter expression="${jaxx.cp}" default-value="${basedir}/target/classes" + */ + protected File cp; + + /** + * @description R�pertoire sources des fichiers java � traiter. + * @parameter expression="${jaxx.src}" default-value="${basedir}/src/main/java" + */ + protected File src; + + /** + * @description verbose + * @parameter expression="${jaxx.verbose}" default-value="false" + */ + protected boolean verbose; + + /** + * @description flag to copy generated file to cp + * @parameter expression="${jaxx.copyToCP}" default-value="false" + */ + protected boolean copyToCP; + +} Modified: trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/ActionGeneratorGoal.java =================================================================== --- trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/ActionGeneratorGoal.java 2008-07-21 10:53:01 UTC (rev 747) +++ trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/ActionGeneratorGoal.java 2008-07-21 13:21:26 UTC (rev 748) @@ -20,7 +20,6 @@ package org.codelutin.jaxx; 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.plugin.logging.Log; @@ -46,44 +45,12 @@ * Classe permettant de generer le fichier de propri�t�s des actions d�tect�es. * * @author chemit - * @goal generate-actions + * @goal generate-actions-properties * @phase process-classes */ -public class ActionGeneratorGoal extends AbstractMojo { +public class ActionGeneratorGoal extends AbstractActionGeneratorGoal { /** - * @description D�pendance du projet. - * @parameter default-value="${project}" - * @readonly - */ - protected MavenProject project; - - /** - * @description R�pertoire sources des fichiers java � traiter. - * @parameter expression="${jaxx.src}" default-value="${basedir}/src/main/java" - */ - protected File src; - - /** - * @description Nom du fichier destination � g�n�rer. - * @parameter expression="${jaxx.baseResource}" default-value="${basedir}/src/main/resources" - */ - protected File baseResource; - - /** - * @description Nom du fichier destination � g�n�rer. - * @parameter expression="${jaxx.out}" default-value="${basedir}/src/main/resources/jaxx/${project.artifactId}-actions.properties" - */ - protected File out; - - /** - * @description Nom du fichier destination � g�n�rer. - * @parameter expression="${jaxx.cp}" default-value="${basedir}/target/classes" - */ - protected File cp; - - - /** * @description pour filter * @parameter expression="${jaxx.includes}" */ @@ -95,19 +62,6 @@ */ protected String[] excludes; - /** - * @description verbose - * @parameter expression="${jaxx.verbose}" default-value="false" - */ - protected boolean verbose; - - /** - * @description flag to copy generated file to cp - * @parameter expression="${jaxx.copyToCP}" default-value="false" - */ - protected boolean copyToCP; - - protected String[] files; private static final String[] INCLUDES = {"**\\/*.java"}; @@ -117,6 +71,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { init(); + if (verbose) { printInit(); } @@ -204,12 +159,13 @@ protected void saveProperties(Properties result) throws IOException { OutputStream writer = null; - File parent = out.getParentFile(); + File generatedFile = new File(baseResource, actionsFile); + File parent = generatedFile.getParentFile(); if (!parent.exists()) { parent.mkdirs(); } try { - writer = new org.codelutin.util.PropertiesDateRemoveFilterStream(new java.io.FileOutputStream(out)); + writer = new org.codelutin.util.PropertiesDateRemoveFilterStream(new java.io.FileOutputStream(generatedFile)); result.store(writer, null); @@ -225,8 +181,7 @@ if (copyToCP) { // save it also in classes - String path = out.getAbsolutePath().substring(baseResource.getAbsolutePath().length() + 1); - File compiledFile = new File(cp, path); + File compiledFile = new File(cp, actionsFile); if (getLog().isDebugEnabled()) { getLog().debug("copy to classapth generated file " + compiledFile); } @@ -234,46 +189,15 @@ if (!parent.exists()) { parent.mkdirs(); } - FileUtil.copy(out, compiledFile); + FileUtil.copy(generatedFile, compiledFile); } } - public File getSrc() { - return src; - } - public File getOut() { - return out; - } - - public String[] getIncludes() { - return includes; - } - - public void setOut(File out) { - this.out = out; - } - - public void setSrc(File src) { - this.src = src; - } - - public void setVerbose(boolean verbose) { - this.verbose = verbose; - } - - public void setIncludes(String[] includes) { - this.includes = includes; - } - - public String[] getFiles() { - return files; - } - protected void init() { DirectoryScanner ds; ds = new DirectoryScanner(); - ds.setBasedir(getSrc()); + ds.setBasedir(src); boolean noIncludes = includes == null || includes.length == 0; ds.setIncludes(noIncludes ? INCLUDES : includes); boolean noExcludes = excludes == null || excludes.length == 0; @@ -283,7 +207,7 @@ ds.scan(); files = ds.getIncludedFiles(); - getLog().info("jaxx - found " + files.length + " file(s) to generate. "); + getLog().info("jaxx-actions - found " + files.length + " file(s) to treate. "); loader = initClassLoader(project, getLog()); } @@ -346,9 +270,9 @@ result = new URLClassLoader(url, getClass().getClassLoader()); } } catch (MalformedURLException eee) { - throw new RuntimeException(_("Can't create ClassLoader for script, bad directory: {0} for reason {1}", getOut(), eee.getMessage()), eee); + throw new RuntimeException(_("Can't create ClassLoader for script, bad directory: {0} for reason {1}", cp, eee.getMessage()), eee); } catch (IOException e) { - throw new RuntimeException(_("Can't create ClassLoader for script, bad directory: {0} for reason {1}", getOut(), e.getMessage()), e); + throw new RuntimeException(_("Can't create ClassLoader for script, bad directory: {0} for reason {1}", cp, e.getMessage()), e); } loader = result; } Copied: trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/ActionProviderGeneratorGoal.java (from rev 746, trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/ActionGeneratorGoal.java) =================================================================== --- trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/ActionProviderGeneratorGoal.java (rev 0) +++ trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/ActionProviderGeneratorGoal.java 2008-07-21 13:21:26 UTC (rev 748) @@ -0,0 +1,157 @@ +/* *##% + * Copyright (C) 2007 + * JaxxPlugin, Code Lutin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +package org.codelutin.jaxx; + +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.codelutin.util.FileUtil; + +import java.io.File; +import java.io.IOException; + +/** + * Classe permettant de generer un {@link org.codelutin.jaxx.action.ActionProvider} et so fichier de d�claration. + * + * @author chemit + * @goal generate-actions-provider + * @phase process-resources + */ +public class ActionProviderGeneratorGoal extends AbstractActionGeneratorGoal { + + /** + * @description FQN de la classe � g�n�rer. + * @parameter expression="${jaxx.fqn}" + * @required + */ + protected String fqn; + + /** + * @description nom logique du provider � g�n�rer. + * @parameter expression="${jaxx.providerName}" + * @required + */ + protected String providerName; + + /** + * @description FQN de la classe d'action � utiliser. + * @parameter expression="${jaxx.fqnAction}" + * @required + */ + protected String fqnAction; + + /** + * template of ActionProvider + * $1 : package of class + * $2 : simple name of class + * $3 : abstract action fqn + * $4 : path to actions properties file + * $5 : provider name + */ + protected static final String PROVIDER_TEMPLATE = + "package %1$s;\n" + + "\n" + + "public class %2$s extends org.codelutin.jaxx.action.ActionProviderFromProperties<%3$s> {\n" + + "\n" + + " public %2$s() {\n" + + " super(\"%5$s\", %3$s.class, \"%4$s\");\n" + + " }\n" + + "\n" + + "}\n"; + + protected File generateJavaProvider; + + protected File generatedProviderDeclaration; + + + public void execute() throws MojoExecutionException, MojoFailureException { + + init(); + + try { + + generateProvider(); + + generateProviderDeclaration(); + + } catch (IOException e) { + getLog().error(e); + throw new MojoExecutionException(e.getMessage(), e); + } + } + + protected void generateProvider() throws IOException { + + int index = fqn.lastIndexOf("."); + String packageJava = fqn.substring(0, index); + String simpleJavaName = fqn.substring(index + 1); + + //TODO check if file was modified + String content = String.format(PROVIDER_TEMPLATE, + packageJava, + simpleJavaName, + fqnAction, + actionsFile, + providerName + ); + + FileUtil.writeString(generateJavaProvider, content); + } + + protected void generateProviderDeclaration() throws IOException { + + //TODO check if file was modified + + // just add the fqn inside the file :) + FileUtil.writeString(generatedProviderDeclaration, fqn); + + if (copyToCP) { + // save it also in classes (since we are in process-resources phase and resources has already been copied) + String path = generatedProviderDeclaration.getAbsolutePath().substring(baseResource.getAbsolutePath().length() + 1); + File compiledFile = new File(cp, path); + if (getLog().isDebugEnabled()) { + getLog().debug("copy to classapth generated file " + compiledFile); + } + File parent = compiledFile.getParentFile(); + if (!parent.exists()) { + parent.mkdirs(); + } + FileUtil.copy(generatedProviderDeclaration, compiledFile); + } + } + + protected void init() { + + generateJavaProvider = new File(src, fqn.replaceAll("\\.", java.io.File.separator) + ".java"); + + File parent = generateJavaProvider.getParentFile(); + if (!parent.exists()) { + parent.mkdirs(); + } + + generatedProviderDeclaration = new File(baseResource, "META-INF/services/org.codelutin.jaxx.action.ActionProvider"); + parent = generatedProviderDeclaration.getParentFile(); + if (!parent.exists()) { + parent.mkdirs(); + } + + } + + +} \ No newline at end of file
participants (1)
-
tchemit@users.labs.libre-entreprise.org