1 | |
package com.aurea.maven.plugins.sonic.esb; |
2 | |
|
3 | |
import java.io.File; |
4 | |
import java.io.FileOutputStream; |
5 | |
import java.io.IOException; |
6 | |
import java.net.URL; |
7 | |
import java.util.ArrayList; |
8 | |
import java.util.List; |
9 | |
|
10 | |
import javax.xml.xpath.XPathConstants; |
11 | |
|
12 | |
import org.apache.maven.execution.MavenSession; |
13 | |
import org.apache.maven.plugin.BuildPluginManager; |
14 | |
import org.apache.maven.plugin.MojoExecutionException; |
15 | |
import org.apache.maven.plugin.MojoFailureException; |
16 | |
import org.codehaus.plexus.util.FileUtils; |
17 | |
import org.codehaus.plexus.util.xml.XmlStreamReader; |
18 | |
import org.codehaus.plexus.util.xml.Xpp3Dom; |
19 | |
import org.codehaus.plexus.util.xml.Xpp3DomBuilder; |
20 | |
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
21 | |
import org.twdata.maven.mojoexecutor.MojoExecutor; |
22 | |
import org.twdata.maven.mojoexecutor.MojoExecutor.Element; |
23 | |
|
24 | |
import com.aurea.maven.plugins.sonic.utils.FileUtilities; |
25 | |
import com.aurea.maven.plugins.sonic.utils.Xpp3Utils; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | 0 | public class ConnectGenerateMojo extends AbstractESBConnectMojo { |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
protected BuildPluginManager pluginManager; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
protected MavenSession session; |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
protected final void doExecute() throws MojoExecutionException, MojoFailureException { |
55 | 0 | super.doExecute(); |
56 | |
|
57 | 0 | generateSonicConnectClient(); |
58 | 0 | } |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
protected void generateSonicConnectClient() throws MojoExecutionException { |
67 | 0 | if (wadlFiles == null || "".equals(wadlFiles)) { |
68 | 0 | return; |
69 | |
} |
70 | |
|
71 | 0 | if (wadlBaseUrl == null) { |
72 | 0 | wadlBaseUrl = ""; |
73 | |
} |
74 | |
|
75 | 0 | getLog().info("Generating Sonic Connect client code for WADL files ..."); |
76 | |
|
77 | 0 | final String outputDir = getOutputDirectory() + "/generated/src/main/java/connect"; |
78 | |
|
79 | |
try { |
80 | 0 | FileUtils.deleteDirectory(outputDir); |
81 | 0 | new File(outputDir).mkdirs(); |
82 | 0 | } catch (IOException e) { |
83 | 0 | e.printStackTrace(); |
84 | 0 | throw new MojoExecutionException(e.getMessage(), e); |
85 | 0 | } |
86 | |
|
87 | 0 | final String[] files = wadlFiles.split(","); |
88 | 0 | for (int i = 0; i < files.length; i++) { |
89 | 0 | final String wadl = files[i]; |
90 | |
|
91 | 0 | final List<MojoExecutor.Element> wadlOptions = new ArrayList<>(); |
92 | 0 | final MojoExecutor.Element wadlOption = MojoExecutor.element(MojoExecutor.name("wadlOption"), |
93 | |
MojoExecutor.element(MojoExecutor.name("wadl"), wadl)); |
94 | 0 | wadlOptions.add(wadlOption); |
95 | |
|
96 | 0 | final File wadlOutputDir = new File(outputDir, "wadl" + i); |
97 | |
|
98 | |
try { |
99 | 0 | MojoExecutor.executeMojo( |
100 | |
MojoExecutor.plugin(MojoExecutor.groupId("org.apache.cxf"), |
101 | |
MojoExecutor.artifactId("cxf-wadl2java-plugin"), MojoExecutor.version("3.1.7")), |
102 | |
MojoExecutor.goal("wadl2java"), |
103 | |
MojoExecutor.configuration(new MojoExecutor.Element[] { |
104 | |
MojoExecutor.element(MojoExecutor.name("sourceRoot"), wadlOutputDir.getAbsolutePath()), |
105 | |
MojoExecutor.element(MojoExecutor.name("defaultOptions"), |
106 | |
MojoExecutor.element(MojoExecutor.name("packagename"), wadlPackage)), |
107 | |
MojoExecutor.element(MojoExecutor.name("wadlOptions"), |
108 | |
wadlOptions.toArray(new Element[wadlOptions.size()])) }), |
109 | |
MojoExecutor.executionEnvironment(project, session, pluginManager)); |
110 | 0 | } catch (final MojoExecutionException ex) { |
111 | 0 | ex.printStackTrace(); |
112 | 0 | throw ex; |
113 | 0 | } |
114 | |
|
115 | 0 | final String[] generatedFiles = FileUtilities.getFileList(wadlOutputDir.getAbsolutePath(), "**/*.java", |
116 | |
null); |
117 | 0 | if (generatedFiles == null || generatedFiles.length == 0) { |
118 | 0 | getLog().warn("No generated files found for wadl = " + wadl); |
119 | 0 | continue; |
120 | |
} |
121 | |
|
122 | |
try { |
123 | 0 | String basePath = null; |
124 | 0 | XmlStreamReader reader = null; |
125 | |
|
126 | 0 | final File wadlFile = new File(wadl); |
127 | 0 | if (wadlFile.exists()) { |
128 | 0 | reader = new XmlStreamReader(wadlFile); |
129 | |
} else { |
130 | 0 | final URL wadlUrl = new URL(wadl); |
131 | 0 | reader = new XmlStreamReader(wadlUrl); |
132 | |
} |
133 | |
|
134 | 0 | final Xpp3Dom dom = Xpp3DomBuilder.build(reader); |
135 | 0 | basePath = (String) Xpp3Utils.evaluateXPath(dom, "//@base", XPathConstants.STRING); |
136 | |
|
137 | 0 | if (basePath == null) { |
138 | 0 | basePath = wadlBaseUrl; |
139 | 0 | } else if (!basePath.toLowerCase().startsWith("http")) { |
140 | 0 | basePath = wadlBaseUrl + basePath; |
141 | |
} |
142 | |
|
143 | 0 | for (final String f : generatedFiles) { |
144 | 0 | final File javaFile = new File(wadlOutputDir, f); |
145 | 0 | final String javaSource = new String(FileUtilities.readFile(javaFile.getAbsolutePath()), "UTF-8"); |
146 | |
|
147 | 0 | final int classDeclaration = calculateClassStart(javaSource); |
148 | 0 | if (classDeclaration < 0) { |
149 | 0 | getLog().warn("Can't find a valid class declaration for file = " + javaFile.getAbsolutePath()); |
150 | 0 | continue; |
151 | |
} |
152 | |
|
153 | 0 | final int resourceDeclaration = javaSource.indexOf("@Path"); |
154 | 0 | if (resourceDeclaration < 0 || resourceDeclaration > classDeclaration) { |
155 | 0 | getLog().warn( |
156 | |
"Can't find a valid resource declaration (@Path) for file = " |
157 | |
+ javaFile.getAbsolutePath()); |
158 | 0 | continue; |
159 | |
} |
160 | |
|
161 | 0 | final int importDeclaration = javaSource.indexOf("import javax.ws.rs.Path"); |
162 | 0 | if (importDeclaration < 0) { |
163 | 0 | getLog().warn( |
164 | |
"Can't find a valid import declaration (javax.ws.rs.Path) for file = " |
165 | |
+ javaFile.getAbsolutePath()); |
166 | 0 | continue; |
167 | |
} |
168 | |
|
169 | 0 | final StringBuilder sb = new StringBuilder(); |
170 | 0 | sb.append(javaSource.substring(0, importDeclaration)); |
171 | 0 | sb.append("import com.aurea.sonic.esb.connect.annotation.SonicConnect;\n"); |
172 | 0 | sb.append(javaSource.substring(importDeclaration, classDeclaration)); |
173 | 0 | sb.append("@SonicConnect(baseUrl = \"" + basePath + "\", client = true)\n"); |
174 | |
|
175 | 0 | String classSource = javaSource.substring(classDeclaration); |
176 | |
|
177 | |
|
178 | 0 | classSource = classSource.replace(" root(", " root_method("); |
179 | |
|
180 | |
|
181 | 0 | classSource = classSource.replace(" application.wadl(", " application_wadl("); |
182 | |
|
183 | 0 | sb.append(classSource); |
184 | |
|
185 | 0 | try (FileOutputStream fos = new FileOutputStream(javaFile)) { |
186 | 0 | fos.write(sb.toString().getBytes()); |
187 | 0 | } |
188 | |
} |
189 | 0 | } catch (IOException | XmlPullParserException e) { |
190 | 0 | e.printStackTrace(); |
191 | 0 | throw new MojoExecutionException(e.getMessage(), e); |
192 | 0 | } |
193 | |
} |
194 | 0 | } |
195 | |
|
196 | |
private int calculateClassStart(final String javaSource) { |
197 | 0 | int classDeclaration = javaSource.indexOf("public interface"); |
198 | 0 | if (classDeclaration < 0) { |
199 | 0 | classDeclaration = javaSource.indexOf("public class"); |
200 | |
} |
201 | 0 | return classDeclaration; |
202 | |
} |
203 | |
|
204 | |
} |