Author: tchemit
Date: 2008-07-22 22:16:19 +0000 (Tue, 22 Jul 2008)
New Revision: 762
Modified:
trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/AbstractActionGeneratorGoal.java
trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/ActionGeneratorGoal.java
trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/ActionProviderGeneratorGoal.java
trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/JaxxGeneratorGoal.java
trunk/lutinjaxx/maven/src/test/java/org/codelutin/jaxx/CompilerTest.java
Log:
refactor goals
Modified: trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/AbstractActionGeneratorGoal.java
===================================================================
--- trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/AbstractActionGeneratorGoal.java 2008-07-22 22:08:37 UTC (rev 761)
+++ trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/AbstractActionGeneratorGoal.java 2008-07-22 22:16:19 UTC (rev 762)
@@ -14,53 +14,17 @@
*/
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 {
+public abstract class AbstractActionGeneratorGoal extends AbstractJaxxMojo {
/**
- * @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.
+ * @description Nom du fichier d'actions � 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
+ * @description flag to copy generated resource files to outClass
* @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-22 22:08:37 UTC (rev 761)
+++ trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/ActionGeneratorGoal.java 2008-07-22 22:16:19 UTC (rev 762)
@@ -19,13 +19,7 @@
package org.codelutin.jaxx;
-import org.apache.maven.artifact.Artifact;
-import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.plugin.MojoFailureException;
-import org.apache.maven.plugin.logging.Log;
-import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.DirectoryScanner;
-import static org.codelutin.i18n.I18n._;
import org.codelutin.jaxx.action.ActionConfig;
import org.codelutin.jaxx.action.SelectActionConfig;
import org.codelutin.jaxx.action.ToggleActionConfig;
@@ -35,11 +29,9 @@
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
-import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Properties;
-import java.util.Set;
/**
* Classe permettant de generer le fichier de propri�t�s des actions d�tect�es.
@@ -51,6 +43,12 @@
public class ActionGeneratorGoal extends AbstractActionGeneratorGoal {
/**
+ * @description R�pertoire sources des fichiers java � traiter.
+ * @parameter expression="${jaxx.src}" default-value="${basedir}/src/main/java"
+ */
+ protected File src;
+
+ /**
* @description pour filter
* @parameter expression="${jaxx.includes}"
*/
@@ -64,30 +62,25 @@
protected String[] files;
+ protected URLClassLoader loader;
+
private static final String[] INCLUDES = {"**\\/*.java"};
- private ClassLoader loader;
- public void execute() throws MojoExecutionException, MojoFailureException {
+ protected void doExecute() throws Exception {
- init();
-
if (verbose) {
printInit();
}
Properties result = loadProperties();
- try {
- saveProperties(result);
- } catch (IOException e) {
- throw new MojoExecutionException(e.getMessage(), e);
- }
+ saveProperties(result);
}
- protected Properties loadProperties() {
+ protected Properties loadProperties() throws ClassNotFoundException {
Properties result = new SortedProperties();
for (String file : files) {
String fqn = file.substring(0, file.length() - 5).replaceAll("\\/", ".");
@@ -96,70 +89,68 @@
getLog().debug("fqn to treate " + fqn);
}
- try {
- Class<?> clazz = Class.forName(fqn, false, loader);
+ Class<?> clazz = Class.forName(fqn, false, loader);
- // try a ActionConfig
- ActionConfig actionConfig = clazz.getAnnotation(ActionConfig.class);
- if (actionConfig != null) {
- if (actionConfig.multiNames().length > 0) {
- // multinames
- registerEntry(clazz, result, actionConfig.multiNames());
- continue;
- }
+ // try a ActionConfig
+ ActionConfig actionConfig = clazz.getAnnotation(ActionConfig.class);
+ if (actionConfig != null) {
- if (!actionConfig.actionCommand().isEmpty()) {
- registerEntry(clazz, result, actionConfig.actionCommand());
- continue;
- }
+ if (actionConfig.multiNames().length > 0) {
+ // multinames
+ registerEntry(clazz, result, actionConfig.multiNames());
+ continue;
+ }
- getLog().warn("could not treate class " + clazz);
+ if (!actionConfig.actionCommand().isEmpty()) {
+ registerEntry(clazz, result, actionConfig.actionCommand());
continue;
}
- // try a SelectActionConfig
- SelectActionConfig selectActionConfig = clazz.getAnnotation(SelectActionConfig.class);
- if (selectActionConfig != null) {
- if (selectActionConfig.multiNames().length > 0) {
- // multinames
- registerEntry(clazz, result, selectActionConfig.multiNames());
- continue;
- }
+ getLog().warn("could not treate class " + clazz);
+ continue;
+ }
- if (!selectActionConfig.actionCommand().isEmpty()) {
- registerEntry(clazz, result, selectActionConfig.actionCommand());
- continue;
- }
- getLog().warn("could not treate class " + clazz);
+ // try a SelectActionConfig
+ SelectActionConfig selectActionConfig = clazz.getAnnotation(SelectActionConfig.class);
+ if (selectActionConfig != null) {
+ if (selectActionConfig.multiNames().length > 0) {
+ // multinames
+ registerEntry(clazz, result, selectActionConfig.multiNames());
continue;
}
- // try a toggleAction
- ToggleActionConfig toggleActionConfig = clazz.getAnnotation(ToggleActionConfig.class);
- if (toggleActionConfig != null) {
- if (toggleActionConfig.multiNames().length > 0) {
- // multinames
- registerEntry(clazz, result, toggleActionConfig.multiNames());
- continue;
- }
- if (!toggleActionConfig.actionCommand().isEmpty()) {
- registerEntry(clazz, result, toggleActionConfig.actionCommand());
- continue;
- }
+ if (!selectActionConfig.actionCommand().isEmpty()) {
+ registerEntry(clazz, result, selectActionConfig.actionCommand());
+ continue;
+ }
+ getLog().warn("could not treate class " + clazz);
+ continue;
+ }
- getLog().warn("could not treate class " + clazz);
+ // try a toggleAction
+ ToggleActionConfig toggleActionConfig = clazz.getAnnotation(ToggleActionConfig.class);
+ if (toggleActionConfig != null) {
+ if (toggleActionConfig.multiNames().length > 0) {
+ // multinames
+ registerEntry(clazz, result, toggleActionConfig.multiNames());
+ continue;
}
- } catch (ClassNotFoundException e) {
- getLog().error("could not find class " + e.getMessage());
+ if (!toggleActionConfig.actionCommand().isEmpty()) {
+ registerEntry(clazz, result, toggleActionConfig.actionCommand());
+ continue;
+ }
+
+ getLog().warn("could not treate class " + clazz);
}
+
}
return result;
}
protected void saveProperties(Properties result) throws IOException {
OutputStream writer = null;
- File generatedFile = new File(baseResource, actionsFile);
+ File generatedFile = new File(outResource, actionsFile);
File parent = generatedFile.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
@@ -172,16 +163,14 @@
} finally {
if (writer != null) {
-
writer.flush();
writer.close();
-
}
}
if (copyToCP) {
// save it also in classes
- File compiledFile = new File(cp, actionsFile);
+ File compiledFile = new File(outClass, actionsFile);
if (getLog().isDebugEnabled()) {
getLog().debug("copy to classapth generated file " + compiledFile);
}
@@ -194,7 +183,11 @@
}
+ @Override
protected void init() {
+
+ super.init();
+
DirectoryScanner ds;
ds = new DirectoryScanner();
ds.setBasedir(src);
@@ -226,63 +219,49 @@
for (String file : files) {
getLog().debug(file);
}
- ClassLoader cl = Thread.currentThread().getContextClassLoader();
- getLog().debug(cl.toString());
- if (cl.getClass().getSimpleName().equals("RealmClassLoader")) {
- try {
- java.lang.reflect.Method m = cl.getClass().getDeclaredMethod("getURLs");
- m.setAccessible(true);
- URL[] urls = (URL[]) m.invoke(cl);
+ printCP();
+ }
- for (URL url : urls) {
- getLog().debug("ulr in class loader " + url);
- }
- } catch (Exception e) {
- getLog().warn("??? : " + e.getMessage(), e);
- }
+ protected void printCP() {
+
+ getLog().debug(loader.toString());
+
+ for (URL url : loader.getURLs()) {
+ getLog().debug("url in class loader " + url);
}
}
- @SuppressWarnings({"unchecked"})
- protected URLClassLoader initClassLoader(MavenProject project, Log log) {
- URLClassLoader loader = null;
- if (project != null) {
- URLClassLoader result;
- try {
- Set<Artifact> compileClasspathElements = project.getArtifacts();
- URL[] url = new URL[compileClasspathElements.size() + 1];
- url[0] = cp.toURI().toURL();
- int i = 1;
- for (Artifact artifact : compileClasspathElements) {
- File file = new File(artifact.getFile().getAbsolutePath());
- if (file.getName().endsWith(".jar")) {
- url[i] = new URL("jar", "", file.toURI().toURL().toString() + "!/");
- } else {
- url[i] = file.toURI().toURL();
- }
- i++;
- }
- //ClassLoader parent = Thread.currentThread().getContextClassLoader();
- if (compileClasspathElements.size() == 0) {
- result = new URLClassLoader(url, getClass().getClassLoader());
- } else {
- result = new URLClassLoader(url, getClass().getClassLoader());
- }
- } catch (MalformedURLException 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}", cp, e.getMessage()), e);
- }
- loader = result;
- }
- if (log.isDebugEnabled() && loader != null) {
- for (URL entry : loader.getURLs()) {
- log.info("cp url " + entry);
- }
- }
- return loader;
+ public File getSrc() {
+ return src;
}
+ public void setSrc(File src) {
+ this.src = src;
+ }
+ public String[] getIncludes() {
+ return includes;
+ }
+
+ public void setIncludes(String[] includes) {
+ this.includes = includes;
+ }
+
+ public String[] getExcludes() {
+ return excludes;
+ }
+
+ public void setExcludes(String[] excludes) {
+ this.excludes = excludes;
+ }
+
+ public String[] getFiles() {
+ return files;
+ }
+
+ public void setFiles(String[] files) {
+ this.files = files;
+ }
+
}
\ No newline at end of file
Modified: trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/ActionProviderGeneratorGoal.java
===================================================================
--- trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/ActionProviderGeneratorGoal.java 2008-07-22 22:08:37 UTC (rev 761)
+++ trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/ActionProviderGeneratorGoal.java 2008-07-22 22:16:19 UTC (rev 762)
@@ -19,15 +19,14 @@
package org.codelutin.jaxx;
-import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.plugin.MojoFailureException;
+import org.codelutin.jaxx.action.ActionProvider;
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.
+ * Classe permettant de generer un {@link org.codelutin.jaxx.action.ActionProvider} et son fichier de d�claration.
*
* @author chemit
* @goal generate-actions-provider
@@ -80,20 +79,12 @@
protected File generatedProviderDeclaration;
- public void execute() throws MojoExecutionException, MojoFailureException {
+ protected void doExecute() throws Exception {
- init();
+ generateProvider();
- try {
+ generateProviderDeclaration();
- generateProvider();
-
- generateProviderDeclaration();
-
- } catch (IOException e) {
- getLog().error(e);
- throw new MojoExecutionException(e.getMessage(), e);
- }
}
protected void generateProvider() throws IOException {
@@ -123,8 +114,8 @@
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);
+ String path = generatedProviderDeclaration.getAbsolutePath().substring(outResource.getAbsolutePath().length() + 1);
+ File compiledFile = new File(outClass, path);
if (getLog().isDebugEnabled()) {
getLog().debug("copy to classapth generated file " + compiledFile);
}
@@ -136,22 +127,26 @@
}
}
+ @Override
protected void init() {
- generateJavaProvider = new File(src, fqn.replaceAll("\\.", java.io.File.separator) + ".java");
+ super.init();
+ generateJavaProvider = new File(outJava, 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");
+ generatedProviderDeclaration = new File(outResource, "META-INF/services/" + ActionProvider.class.getName());
parent = generatedProviderDeclaration.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
+ fixCompileSourceRoots();
+
}
-
}
\ No newline at end of file
Modified: trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/JaxxGeneratorGoal.java
===================================================================
--- trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/JaxxGeneratorGoal.java 2008-07-22 22:08:37 UTC (rev 761)
+++ trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/JaxxGeneratorGoal.java 2008-07-22 22:16:19 UTC (rev 762)
@@ -21,15 +21,10 @@
import jaxx.compiler.CompilerOptions;
import jaxx.compiler.JAXXCompiler;
-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;
-import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.DirectoryScanner;
import java.io.File;
-import java.net.URL;
/**
* Classe permettant de transformer des sources jaxx vers du source java.
@@ -38,32 +33,15 @@
* @goal generate
* @phase process-resources
*/
-public class JaxxGeneratorGoal extends AbstractMojo {
+public class JaxxGeneratorGoal extends AbstractJaxxMojo {
+
/**
- * The maven project.
- *
- * @parameter expression="${project}"
- * @read-only
- * @required
- */
- private MavenProject project;
-
- /**
* @description R�pertoire sources des fichiers jaxx � g�n�rer.
* @parameter expression="${jaxx.src}" default-value="${basedir}/src/uimodel"
*/
protected File src;
+
/**
- * @description R�pertoire de destination des fichiers � g�n�rer.
- * @parameter expression="${jaxx.out}" default-value="${basedir}/target/gen/java"
- */
- protected File out;
- /**
- * @description R�pertoire de destination des fichiers � compiler.
- * @parameter expression="${jaxx.classOut}" default-value="${basedir}/target/test-classes"
- */
- protected File classOut;
- /**
* @description pour optimizer le code compil� ou g�n�r� ?
* @parameter expression="${jaxx.optimize}" default-value="false"
*/
@@ -81,58 +59,34 @@
*/
protected String[] includes;
- /**
- * @description verbose
- * @parameter expression="${jaxx.verbose}" default-value="${maven.verbose}"
- */
- protected boolean verbose;
-
protected String[] files;
private static final String[] INCLUDES = {"**\\/*.jaxx"};
- public void execute() throws MojoExecutionException, MojoFailureException {
- if (!out.exists()) {
- out.mkdirs();
- }
+ protected CompilerOptions options;
+ protected void init() {
+ super.init();
+
fixCompileSourceRoots();
DirectoryScanner ds;
ds = new DirectoryScanner();
- ds.setBasedir(getSrc());
+ ds.setBasedir(src);
boolean noIncludes = includes == null || includes.length == 0;
ds.setIncludes(noIncludes ? INCLUDES : includes);
ds.scan();
files = ds.getIncludedFiles();
- log.info("jaxx - found " + files.length + " file(s) to generate. ");
- CompilerOptions options = toCompilerOptions();
- if (verbose) {
- log.debug("classPath: " + options.getClassPath());
- log.debug("javaOut : " + options.getTargetDirectory());
- log.debug("classOut : " + options.getJavacTargetDirectory());
- log.debug("javacOpts: " + options.getJavacOpts());
- log.debug("optiomize: " + options.getOptimize());
- for (String file : files) {
- log.debug(file);
- }
- ClassLoader cl = Thread.currentThread().getContextClassLoader();
- log.debug(cl.toString());
- if (cl.getClass().getSimpleName().equals("RealmClassLoader")) {
- try {
- java.lang.reflect.Method m = cl.getClass().getDeclaredMethod("getURLs");
- m.setAccessible(true);
- URL[] urls = (URL[]) m.invoke(cl);
+ getLog().info("jaxx - found " + files.length + " file(s) to generate. ");
+ options = toCompilerOptions();
- for (URL url : urls) {
- log.debug("ulr in class loader " + url);
- }
- } catch (Exception e) {
- log.warn("??? : " + e.getMessage(), e);
- }
- }
+ if (verbose) {
+ printInit();
}
+ }
+
+ protected void doExecute() throws Exception {
if (!JAXXCompiler.compile(src, files, options)) {
throw new MojoFailureException("Aborting due to errors reported by jaxxc");
}
@@ -140,67 +94,56 @@
public CompilerOptions toCompilerOptions() {
CompilerOptions result = new CompilerOptions();
- result.setClassPath(getSrc().getPath());
- if (getJavaOpts() != null && !"".equals(getJavaOpts())) {
- result.setJavacOpts(getJavaOpts());
+ result.setClassPath(src.getPath());
+ if (javaOpts != null && !"".equals(javaOpts)) {
+ result.setJavacOpts(javaOpts);
}
result.setKeepJavaFiles(true);
- result.setOptimize(isOptimize());
- result.setJavacTargetDirectory(getClassOut());
- result.setTargetDirectory(getOut());
+ result.setOptimize(optimize);
+ result.setJavacTargetDirectory(outClass);
+ result.setTargetDirectory(outJava);
return result;
}
- protected Log log = getLog();
+ protected void printInit() {
+ getLog().debug("classPath: " + options.getClassPath());
+ getLog().debug("javaOut : " + options.getTargetDirectory());
+ getLog().debug("outClass : " + options.getJavacTargetDirectory());
+ getLog().debug("javacOpts: " + options.getJavacOpts());
+ getLog().debug("optiomize: " + options.getOptimize());
+ for (String file : files) {
+ getLog().debug(file);
+ }
+ }
public File getSrc() {
return src;
}
+ public void setSrc(File src) {
+ this.src = src;
+ }
+
public boolean isOptimize() {
return optimize;
}
- public File getOut() {
- return out;
+ public void setOptimize(boolean optimize) {
+ this.optimize = optimize;
}
public String getJavaOpts() {
return javaOpts;
}
- public File getClassOut() {
- return classOut;
+ public void setJavaOpts(String javaOpts) {
+ this.javaOpts = javaOpts;
}
public String[] getIncludes() {
return includes;
}
- public void setJavaOpts(String javaOpts) {
- this.javaOpts = javaOpts;
- }
-
- public void setOptimize(boolean optimize) {
- this.optimize = optimize;
- }
-
- public void setOut(File out) {
- this.out = out;
- }
-
- public void setClassOut(File classOut) {
- this.classOut = classOut;
- }
-
- public void setSrc(File src) {
- this.src = src;
- }
-
- public void setVerbose(boolean verbose) {
- this.verbose = verbose;
- }
-
public void setIncludes(String[] includes) {
this.includes = includes;
}
@@ -209,9 +152,8 @@
return files;
}
- protected void fixCompileSourceRoots() {
- if (!project.getCompileSourceRoots().contains(out.getPath())) {
- project.addCompileSourceRoot(out.getPath());
- }
- }
+ public void setFiles(String[] files) {
+ this.files = files;
+ }
+
}
\ No newline at end of file
Modified: trunk/lutinjaxx/maven/src/test/java/org/codelutin/jaxx/CompilerTest.java
===================================================================
--- trunk/lutinjaxx/maven/src/test/java/org/codelutin/jaxx/CompilerTest.java 2008-07-22 22:08:37 UTC (rev 761)
+++ trunk/lutinjaxx/maven/src/test/java/org/codelutin/jaxx/CompilerTest.java 2008-07-22 22:16:19 UTC (rev 762)
@@ -56,14 +56,14 @@
String packageName = anno.packageName();
goal = new JaxxGeneratorGoal();
goal.setSrc(srcDir);
- goal.setOut(outDir);
- goal.setClassOut(classOutDir);
+ goal.setOutJava(outDir);
+ goal.setOutClass(classOutDir);
goal.setIncludes(new String[]{"**\\/" + PREFIX_PACKAGE + "\\/" + packageName + "\\/*.jaxx"});
goal.setOptimize(anno.optimize());
goal.setVerbose(anno.verbose());
//TODO Use the maven plexus TestCase inorder to include dependencies
goal.setJavaOpts(anno.javaOpts());
- System.out.println(getName());
+ goal.getLog().info(getName());
}