View Javadoc

1   /*
2    * Copyright (C) 2003-2012 David E. Berry
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17   *
18   * A copy of the GNU Lesser General Public License may also be found at
19   * http://www.gnu.org/licenses/lgpl.txt
20   */
21  package org.synchronoss.cpo.plugin;
22  
23  import org.apache.maven.plugin.*;
24  import org.apache.maven.project.MavenProject;
25  import org.synchronoss.cpo.exporter.CpoClassSourceGenerator;
26  import org.synchronoss.cpo.meta.CpoMetaDescriptor;
27  import org.synchronoss.cpo.meta.domain.CpoClass;
28  
29  import java.io.*;
30  import java.util.StringTokenizer;
31  
32  /**
33   * Plugin goal that will generate the cpo classes based on the xml configuration file
34   *
35   * @requiresDependencyResolution
36   * @goal generatejavasource
37   * @phase generate-sources
38   * @configurator include-project-dependencies
39   */
40  public class GenerateJavaSources extends AbstractMojo {
41  
42    private enum Scopes {
43      test
44    }
45  
46    /**
47     * @parameter expression="${cpoConfig}"
48     * @required
49     */
50    private String cpoConfig;
51  
52    /**
53     * Default output directory
54     *
55     * @parameter expression="${project.build.directory}/generated-sources/cpo"
56     * @required
57     */
58    private String outputDir;
59  
60    /**
61     * Output directory for test scope executions
62     *
63     * @parameter expression="${project.build.directory}/generated-test-sources/cpo"
64     * @required
65     */
66    private String testOutputDir;
67  
68    /**
69     * @parameter expression="${scope}" default-value="compile"
70     * @required
71     */
72    private String scope;
73  
74    /**
75     * @parameter expression="${filter}" default-value=".*"
76     */
77    private String filter;
78  
79    /**
80     * A reference to the Maven Project metadata.
81     *
82     * @parameter expression="${project}"
83     * @required
84     * @readonly
85     */
86    protected MavenProject project;
87  
88    private final String JAVA_EXT = ".java";
89    private final String META_DESCRIPTOR_NAME = "Generator-" + System.currentTimeMillis();
90  
91  	public void execute() throws MojoExecutionException {
92      getLog().info("Cpo config: " + cpoConfig);
93  
94      File srcDir;
95  
96      if (Scopes.test.toString().equals(scope)) {
97        // test scope, so use test output directory and add to test compile path
98        srcDir = new File(testOutputDir);
99        project.addTestCompileSourceRoot(srcDir.getAbsolutePath());
100       getLog().debug("Adding " + srcDir.getAbsolutePath() + " to the project's test compile sources.");
101     } else {
102       // default scope, so use output directory and add to compile path
103       srcDir = new File(outputDir);
104       project.addCompileSourceRoot(srcDir.getAbsolutePath());
105       getLog().debug("Adding " + srcDir.getAbsolutePath() + " to the project's compile sources.");
106     }
107 
108     getLog().info("Generating cpo java sources to " + srcDir);
109 
110     File outputDirectory = new File(project.getBuild().getOutputDirectory());
111     if (!outputDirectory.exists()) {
112       if (!outputDirectory.mkdirs()) {
113         throw new MojoExecutionException("Unable to create output directory: " + outputDirectory.getAbsolutePath());
114       }
115     }
116 
117     try {
118       CpoMetaDescriptor metaDescriptor = CpoMetaDescriptor.getInstance(META_DESCRIPTOR_NAME, cpoConfig, true);
119 
120       for (CpoClass cpoClass : metaDescriptor.getCpoClasses()) {
121 
122         String className = cpoClass.getName();
123 
124         // check the filter
125         if (filter != null && className.matches(filter)) {
126           File classDir = srcDir;
127           if (className.lastIndexOf(".") != -1) {
128             String packageName = className.substring(0, className.lastIndexOf("."));
129             StringTokenizer tok = new StringTokenizer(packageName, ".");
130             while (tok.hasMoreTokens()) {
131               String dirName = tok.nextToken();
132               classDir = new File(classDir, dirName);
133             }
134             className = className.substring(className.lastIndexOf(".") + 1);
135           }
136 
137           if (!classDir.exists()) {
138             if (!classDir.mkdirs()) {
139               throw new MojoExecutionException("Unable to create class directories: " + classDir.getAbsolutePath());
140             }
141           }
142           File javaFile = new File(classDir, className + JAVA_EXT);
143 
144           getLog().info("cpo-plugin generated " + javaFile.getAbsolutePath());
145 
146           CpoClassSourceGenerator classSourceGenerator = new CpoClassSourceGenerator(metaDescriptor);
147           cpoClass.acceptMetaDFVisitor(classSourceGenerator);
148 
149           FileWriter cw = new FileWriter(javaFile);
150           cw.write(classSourceGenerator.getSourceCode());
151           cw.flush();
152           cw.close();
153         }
154       }
155     } catch (Exception ex) {
156       throw new MojoExecutionException("Exception caught", ex);
157     }
158 	}
159 }
160