001: package org.objectweb.celtix.maven_plugin;
002:
003: import java.io.File;
004: import java.net.MalformedURLException;
005: import java.net.URL;
006: import java.net.URLClassLoader;
007: import java.util.ArrayList;
008: import java.util.Iterator;
009: import java.util.List;
010:
011: import org.apache.maven.plugin.AbstractMojo;
012: import org.apache.maven.plugin.MojoExecutionException;
013: import org.apache.maven.project.MavenProject;
014: import org.apache.tools.ant.ExitException;
015: import org.apache.tools.ant.util.optional.NoExitSecurityManager;
016: import org.objectweb.celtix.resource.ClassLoaderResolver;
017: import org.objectweb.celtix.resource.DefaultResourceManager;
018: import org.objectweb.celtix.tools.generators.spring.BeanGenerator;
019:
020: /**
021: * @goal beangen
022: * @description Celtix BeanGen Tool
023: */
024: public class BeanGenMojo extends AbstractMojo {
025: /**
026: * @parameter
027: */
028: String testSourceRoot;
029:
030: /**
031: * @parameter expression="${project.build.directory}/generated/src/main/java"
032: * @required
033: */
034: String sourceRoot;
035:
036: /**
037: * @parameter expression="${basedir}/src/main/resources"
038: * @required
039: */
040: String resourcesRoot;
041:
042: /**
043: * @parameter expression="${project}"
044: * @required
045: */
046: MavenProject project;
047:
048: /**
049: * @parameter expression="${project.compileClasspathElements}"
050: * @required
051: */
052: List classpathElements;
053:
054: /**
055: * @parameter
056: */
057: String beanfiles[];
058:
059: public void execute() throws MojoExecutionException {
060: String outputDir = testSourceRoot == null ? sourceRoot
061: : testSourceRoot;
062: File outputDirFile = new File(outputDir);
063: outputDirFile.mkdirs();
064: long timestamp = CodegenUtils.getCodegenTimestamp();
065:
066: List list = new ArrayList();
067: List doneFiles = new ArrayList();
068: list.add("-d");
069: list.add(outputDir);
070: for (int x = 0; x < beanfiles.length; x++) {
071: File file = new File(beanfiles[x]);
072: File doneFile = new File(outputDirFile, "."
073: + file.getName() + ".DONE");
074: if (!doneFile.exists()
075: || file.lastModified() > doneFile.lastModified()
076: || timestamp > doneFile.lastModified()) {
077: list.add(beanfiles[x]);
078: doneFiles.add(doneFile);
079: }
080: }
081:
082: List urlList = new ArrayList();
083: Iterator it = classpathElements.iterator();
084: File file = new File(resourcesRoot);
085: try {
086: urlList.add(file.toURL());
087: } catch (MalformedURLException e) {
088: //ignore
089: }
090: while (it.hasNext()) {
091: String el = (String) it.next();
092: file = new File(el);
093: try {
094: urlList.add(file.toURL());
095: } catch (MalformedURLException e) {
096: //ignore
097: }
098: }
099:
100: URLClassLoader loader = new URLClassLoader((URL[]) urlList
101: .toArray(new URL[urlList.size()]), this .getClass()
102: .getClassLoader());
103:
104: if (list.size() > 2) {
105: SecurityManager oldSm = System.getSecurityManager();
106: try {
107: try {
108: DefaultResourceManager.instance()
109: .addResourceResolver(
110: new ClassLoaderResolver(loader));
111: System
112: .setSecurityManager(new NoExitSecurityManager());
113:
114: BeanGenerator.main((String[]) list
115: .toArray(new String[list.size()]));
116: throw new ExitException(0);
117: } catch (ExitException e) {
118: if (e.getStatus() == 0) {
119: it = doneFiles.iterator();
120: while (it.hasNext()) {
121: File doneFile = (File) it.next();
122: doneFile.delete();
123: doneFile.createNewFile();
124: }
125: } else {
126: throw e;
127: }
128: } finally {
129: System.setSecurityManager(oldSm);
130: DefaultResourceManager.clearInstance();
131: }
132: } catch (Throwable e) {
133: e.printStackTrace();
134: throw new MojoExecutionException(e.getMessage(), e);
135: }
136: }
137:
138: if (project != null && sourceRoot != null) {
139: project.addCompileSourceRoot(sourceRoot);
140: }
141: if (project != null && testSourceRoot != null) {
142: project.addTestCompileSourceRoot(testSourceRoot);
143: }
144: }
145: }
|