1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
34
35
36
37
38
39
40 public class GenerateJavaSources extends AbstractMojo {
41
42 private enum Scopes {
43 test
44 }
45
46
47
48
49
50 private String cpoConfig;
51
52
53
54
55
56
57
58 private String outputDir;
59
60
61
62
63
64
65
66 private String testOutputDir;
67
68
69
70
71
72 private String scope;
73
74
75
76
77 private String filter;
78
79
80
81
82
83
84
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
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
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
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