1 package org.objectweb.celtix.maven_plugin; 2 3 import java.io.File ; 4 import java.net.MalformedURLException ; 5 import java.net.URL ; 6 import java.net.URLClassLoader ; 7 import java.util.ArrayList ; 8 import java.util.Iterator ; 9 import java.util.List ; 10 11 import org.apache.maven.plugin.AbstractMojo; 12 import org.apache.maven.plugin.MojoExecutionException; 13 import org.apache.maven.project.MavenProject; 14 import org.apache.tools.ant.ExitException; 15 import org.apache.tools.ant.util.optional.NoExitSecurityManager; 16 import org.objectweb.celtix.resource.ClassLoaderResolver; 17 import org.objectweb.celtix.resource.DefaultResourceManager; 18 import org.objectweb.celtix.tools.generators.spring.BeanGenerator; 19 20 24 public class BeanGenMojo extends AbstractMojo { 25 28 String testSourceRoot; 29 30 34 String sourceRoot; 35 36 40 String resourcesRoot; 41 42 46 MavenProject project; 47 48 52 List classpathElements; 53 54 57 String beanfiles[]; 58 59 60 public void execute() throws MojoExecutionException { 61 String outputDir = testSourceRoot == null ? sourceRoot : testSourceRoot; 62 File outputDirFile = new File (outputDir); 63 outputDirFile.mkdirs(); 64 long timestamp = CodegenUtils.getCodegenTimestamp(); 65 66 List list = new ArrayList (); 67 List doneFiles = new ArrayList (); 68 list.add("-d"); 69 list.add(outputDir); 70 for (int x = 0; x < beanfiles.length; x++) { 71 File file = new File (beanfiles[x]); 72 File doneFile = new File (outputDirFile, "." + file.getName() + ".DONE"); 73 if (!doneFile.exists() 74 || file.lastModified() > doneFile.lastModified() 75 || timestamp > doneFile.lastModified()) { 76 list.add(beanfiles[x]); 77 doneFiles.add(doneFile); 78 } 79 } 80 81 List urlList = new ArrayList (); 82 Iterator it = classpathElements.iterator(); 83 File file = new File (resourcesRoot); 84 try { 85 urlList.add(file.toURL()); 86 } catch (MalformedURLException e) { 87 } 89 while (it.hasNext()) { 90 String el = (String )it.next(); 91 file = new File (el); 92 try { 93 urlList.add(file.toURL()); 94 } catch (MalformedURLException e) { 95 } 97 } 98 99 URLClassLoader loader = new URLClassLoader ((URL [])urlList.toArray(new URL [urlList.size()]), 100 this.getClass().getClassLoader()); 101 102 if (list.size() > 2) { 103 SecurityManager oldSm = System.getSecurityManager(); 104 try { 105 try { 106 DefaultResourceManager.instance().addResourceResolver(new ClassLoaderResolver(loader)); 107 System.setSecurityManager(new NoExitSecurityManager()); 108 109 BeanGenerator.main((String [])list.toArray(new String [list.size()])); 110 throw new ExitException(0); 111 } catch (ExitException e) { 112 if (e.getStatus() == 0) { 113 it = doneFiles.iterator(); 114 while (it.hasNext()) { 115 File doneFile = (File )it.next(); 116 doneFile.delete(); 117 doneFile.createNewFile(); 118 } 119 } else { 120 throw e; 121 } 122 } finally { 123 System.setSecurityManager(oldSm); 124 DefaultResourceManager.clearInstance(); 125 } 126 } catch (Throwable e) { 127 e.printStackTrace(); 128 throw new MojoExecutionException(e.getMessage(), e); 129 } 130 } 131 132 if (project != null && sourceRoot != null) { 133 project.addCompileSourceRoot(sourceRoot); 134 } 135 if (project != null && testSourceRoot != null) { 136 project.addTestCompileSourceRoot(testSourceRoot); 137 } 138 } 139 } 140 | Popular Tags |