View Javadoc

1   package com.aurea.maven.plugins.sonic;
2   
3   import java.io.File;
4   import java.io.IOException;
5   
6   import org.apache.maven.execution.MavenSession;
7   import org.apache.maven.plugin.BuildPluginManager;
8   import org.apache.maven.plugin.MojoExecutionException;
9   import org.apache.maven.plugin.MojoFailureException;
10  import org.codehaus.plexus.util.FileUtils;
11  import org.twdata.maven.mojoexecutor.MojoExecutor;
12  
13  import com.aurea.maven.plugins.sonic.utils.ZipUtilities;
14  import com.progress.sonic.utilities.esb.admin.ESBArtifactCopyJob;
15  import com.progress.sonic.utilities.mfutils.MFUtils;
16  import com.progress.sonic.utilities.mfutils.NullArtifactNotificationListener;
17  import com.sonicsw.deploy.artifact.SonicFSArtifact;
18  import com.sonicsw.deploy.storage.FileArtifactStorage;
19  import com.sonicsw.deploy.tools.common.ExportPropertiesArtifact;
20  
21  /**
22   * exports from the Sonic Domain the ESB artifacts from the current Maven
23   * artifact into /deploy/generated-src.
24   * 
25   * @author Jamie Townsend
26   * 
27   * @goal unpack-dependencies
28   */
29  public class SonicUnpackDependenciesMojo extends AbstractSonicMojo {
30  
31  	private static final int DOMAIN_PING_TIMEOUT = 1000;
32  
33  	/**
34  	 * The Sonic Domain to connect to.
35  	 * 
36  	 * @parameter property="sonicesb.devDomainName" default-value="Domain1"
37  	 * @required
38  	 */
39  	private String domain;
40  
41  	/**
42  	 * The user name used for the connection.
43  	 * 
44  	 * @parameter property="sonicesb.devDomainUser"
45  	 *            default-value="Administrator"
46  	 * @required
47  	 */
48  	private String user;
49  
50  	/**
51  	 * The password used for the connection.
52  	 * 
53  	 * @parameter property="sonicesb.devDomainPassword"
54  	 *            default-value="Administrator"
55  	 * @required
56  	 */
57  	private String password;
58  
59  	/**
60  	 * The management connection url.
61  	 * 
62  	 * @parameter property="sonicesb.devDomainUrl"
63  	 *            default-value="tcp://localhost:2506"
64  	 * @required
65  	 */
66  	private String mgmtUrl;
67  
68  	/**
69  	 * The Maven Session Object.
70  	 * 
71  	 * @parameter default-value="${session}"
72  	 * @required
73  	 * @readonly
74  	 */
75  	private MavenSession session;
76  
77  	/**
78  	 * The Maven PluginManager Object.
79  	 * 
80  	 * @component
81  	 * @required
82  	 */
83  	protected BuildPluginManager pluginManager;
84  
85  	private File markersDirectory = null;
86  
87  	/**
88     * 
89     */
90  	protected MFUtils mfUtils = null;
91  
92  	/**
93  	 * @return MFUtils
94  	 * @throws MojoExecutionException
95  	 *             if something goes wrong
96  	 */
97  	protected MFUtils getMFUtils() throws MojoExecutionException {
98  
99  		if (mfUtils == null) {
100 			getLog().info("Connecting to Sonic Domain ...");
101 			getLog().debug("Domain: " + getDomainName());
102 			getLog().debug("ManagementURL: " + getManagementUrl());
103 			getLog().debug("User: " + getAdminUser());
104 			getLog().debug("Password: " + getAdminPassword());
105 			mfUtils = new MFUtils(getDomainName(), getManagementUrl(), getAdminUser(), getAdminPassword());
106 		}
107 		return mfUtils;
108 	}
109 
110 	@Override
111 	protected void doExecute() throws MojoExecutionException, MojoFailureException {
112 		try {
113 			markersDirectory = new File(getDependencyDirectory(), "markers");
114 			unpackSonicCompile();
115 			// TBO: The unpackDxsiXars is now running all the time. It will take
116 			// artifacts from the dxsi-distribution
117 			// and load it into the SonicDS for operational work.
118 			unpackDxsiXars();
119 			unpackTopologyConfig();
120 			// Tests will need to point out if the unpack sonic provided options
121 			// is really needed.
122 			// Removed it in order to allow for SDP Package (SDM)
123 			// interdependencies in provided mode.
124 
125 			// Inserting unpack Sonic Provided but only to allow Service Types
126 			// being provided and getting the rules from the snippets
127 			unpackSonicProvided();
128 			copyDependentJars();
129 			copySonicExtensions();
130 		} catch (Exception ex) {
131 			if (ex.getCause() != null && ex.getCause().getMessage() != null
132 					&& ex.getCause().getMessage().contains("MDEP-98")) {
133 				// KNOW ISSUE: throws an exception when using Eclipse Workspace
134 				// referenced projects
135 				ex.printStackTrace();
136 			} else {
137 				throw ex;
138 			}
139 
140 		}
141 	}
142 
143 	private void unpackTopologyConfig() throws MojoExecutionException {
144 		MojoExecutor.executeMojo(MojoExecutor.plugin(MojoExecutor.groupId("org.apache.maven.plugins"),
145 				MojoExecutor.artifactId("maven-dependency-plugin"), MojoExecutor.version("2.7")), MojoExecutor
146 				.goal("unpack-dependencies"), MojoExecutor.configuration(new MojoExecutor.Element[] {
147 				MojoExecutor.element(MojoExecutor.name("outputDirectory"), getDependencyDirectory() + "/topology"),
148 				MojoExecutor.element(MojoExecutor.name("excludeScope"), "provided"),
149 				MojoExecutor.element(MojoExecutor.name("markersDirectory"), markersDirectory.getAbsolutePath()),
150 				MojoExecutor.element(MojoExecutor.name("includeTypes"), "topology"),
151 				MojoExecutor.element(MojoExecutor.name("useRepositoryLayout"), "false"),
152 				MojoExecutor.element(MojoExecutor.name("useSubDirectoryPerType"), "false") }), MojoExecutor
153 				.executionEnvironment(project, session, pluginManager));
154 		try {
155 			FileUtils.deleteDirectory(markersDirectory);
156 		} catch (IOException e) {
157 			// This should never happen and if it does, it shouldn't really
158 			// matter
159 			e.printStackTrace();
160 		}
161 
162 	}
163 
164 	private void unpackSonicCompile() throws MojoExecutionException {
165 		// projects
166 
167 		MojoExecutor.executeMojo(MojoExecutor.plugin(MojoExecutor.groupId("org.apache.maven.plugins"),
168 				MojoExecutor.artifactId("maven-dependency-plugin"), MojoExecutor.version("2.7")), MojoExecutor
169 				.goal("unpack-dependencies"), MojoExecutor.configuration(new MojoExecutor.Element[] {
170 				MojoExecutor.element(MojoExecutor.name("outputDirectory"), getDependencyDirectory() + "/xar/compile"),
171 				MojoExecutor.element(MojoExecutor.name("excludeScope"), "provided"),
172 				MojoExecutor.element(MojoExecutor.name("markersDirectory"), markersDirectory.getAbsolutePath()),
173 				MojoExecutor.element(MojoExecutor.name("includeTypes"), "zip,esb,esbstyp,connect"),
174 				MojoExecutor.element(MojoExecutor.name("useRepositoryLayout"), "true"),
175 				MojoExecutor.element(MojoExecutor.name("useSubDirectoryPerType"), "true") }), MojoExecutor
176 				.executionEnvironment(project, session, pluginManager));
177 
178 		try {
179 			FileUtils.deleteDirectory(markersDirectory);
180 		} catch (IOException e) {
181 			// This should never happen and if it does, it shouldn't really
182 			// matter
183 			e.printStackTrace();
184 		}
185 	}
186 
187 	private void unpackDxsiXars() throws MojoExecutionException {
188 		File dxsiExtractDir = new File(getDependencyDirectory(), "dxsi");
189 		File dependencyTmpDir = new File(dxsiExtractDir, "dependencyTmp");
190 		File xarTmpDir = new File(dxsiExtractDir, "xarTmp");
191 
192 		MojoExecutor.executeMojo(MojoExecutor.plugin(MojoExecutor.groupId("org.apache.maven.plugins"),
193 				MojoExecutor.artifactId("maven-dependency-plugin"), MojoExecutor.version("2.7")), MojoExecutor
194 				.goal("unpack-dependencies"), MojoExecutor.configuration(new MojoExecutor.Element[] {
195 				MojoExecutor.element(MojoExecutor.name("outputDirectory"), dependencyTmpDir.getPath()),
196 				MojoExecutor.element(MojoExecutor.name("excludeScope"), "provided"),
197 				MojoExecutor.element(MojoExecutor.name("includeScope"), "compile"),
198 				MojoExecutor.element(MojoExecutor.name("markersDirectory"), markersDirectory.getAbsolutePath()),
199 				MojoExecutor.element(MojoExecutor.name("includeTypes"), "dxsi"),
200 				MojoExecutor.element(MojoExecutor.name("useRepositoryLayout"), "true"),
201 				MojoExecutor.element(MojoExecutor.name("useSubDirectoryPerType"), "true") }), MojoExecutor
202 				.executionEnvironment(project, session, pluginManager));
203 
204 		// TBO
205 		// Extracting the xar file to a temp location.
206 		String[] xarExtension = { "xar" };
207 		String[] xarFiles = FileUtils.getFilesFromExtension(dependencyTmpDir.getPath(), xarExtension);
208 		for (String xarFilename : xarFiles) {
209 			ZipUtilities.doUnzipAction(xarFilename, xarTmpDir.getPath());
210 		}
211 
212 		// Removing the ESB Directory ... avoiding the import of the service
213 		// instance and endpoint.
214 		try {
215 			FileUtils.deleteDirectory(new File(xarTmpDir, "ESB"));
216 		} catch (IOException e1) {
217 			// TODO Auto-generated catch block
218 			e1.printStackTrace();
219 		}
220 
221 		// Uploading the xar content but ignore the Endpoint and Service
222 		// Instance
223 		try {
224 
225 			FileArtifactStorage fas = new FileArtifactStorage();
226 			fas.addNotificationListener(new NullArtifactNotificationListener());
227 			fas.setRoot(xarTmpDir.getAbsolutePath());
228 
229 			ExportPropertiesArtifact expProperties = new ExportPropertiesArtifact();
230 			// resources root e.g. JARs
231 			expProperties.addRoot(new SonicFSArtifact("/"));
232 			// TBO: Ignoring the ESB-Endpoint and ServiceInstance. These need to
233 			// be uploaded manually
234 			// TBO: By default they are not included ... no ignore needed.
235 			// Creating Ignores List
236 			// expProperties.addIgnore(new
237 			// ESBArtifact(ESBArtifact.CONNECTION_TYPE, "**"));
238 			// expProperties.addIgnore(new
239 			// ESBArtifact(ESBArtifact.ENDPOINT_TYPE, "**"));
240 			// expProperties.addIgnore(new ESBArtifact(ESBArtifact.SERVICE_TYPE,
241 			// "**"));
242 			// ignores.add(new ESBArtifact(ESBArtifact.SERVICE_TYPE,
243 			// "DataXtendSIServiceType"));
244 			// just to make sure that no workspace related files get imported,
245 			// although that should never happen
246 			expProperties.addIgnore(new SonicFSArtifact("workspace/"));
247 			// expProperties.addIgnore(ignores.toArray(new ESBArtifact[] {} ));
248 
249 			if (!MFUtils.pingDomain(getManagementUrl(), DOMAIN_PING_TIMEOUT)) {
250 				getLog().warn("Could not connect to Sonic Domain at " + getManagementUrl());
251 			} else {
252 				new ESBArtifactCopyJob(fas, getMFUtils().getDSArtifactStorage(), expProperties, false).copy();
253 				getLog().info("DXSI Environment Imported!");
254 			}
255 		} catch (Exception e) {
256 			getLog().error("Failed to import ESB Service Type into local DS", e);
257 		} finally {
258 			getMFUtils().cleanup();
259 		}
260 
261 		// TBO: REMARK: 2013 - Do we need to delete these directories at this
262 		// point? It might be that these are still needed at the time we extract
263 		// the generated resource
264 		// This is not an issue for the process build in the workbench but is an
265 		// issue for the head-less build cycle due to the difference in
266 		// dependencies.
267 		// try {
268 		// FileUtils.deleteDirectory(markersDirectory);
269 		// FileUtils.deleteDirectory(dependencyTmpDir);
270 		// FileUtils.deleteDirectory(xarTmpDir);
271 		// } catch (IOException e) {
272 		// // This should never happen and if it does, it shouldn't really
273 		// matter
274 		// e.printStackTrace();
275 		// }
276 	}
277 
278 	/**
279 	 * @return String
280 	 * @throws MojoExecutionException
281 	 *             if something goes wrong
282 	 */
283 	protected String getDomainName() throws MojoExecutionException {
284 
285 		return domain;
286 	}
287 
288 	/**
289 	 * @return String
290 	 * @throws MojoExecutionException
291 	 *             if something goes wrong
292 	 */
293 	protected String getAdminUser() throws MojoExecutionException {
294 
295 		return user;
296 	}
297 
298 	/**
299 	 * @return String
300 	 * @throws MojoExecutionException
301 	 *             if something goes wrong
302 	 */
303 	protected String getAdminPassword() throws MojoExecutionException {
304 
305 		return password;
306 	}
307 
308 	/**
309 	 * @return String
310 	 * @throws MojoExecutionException
311 	 *             if something goes wrong
312 	 */
313 	protected String getManagementUrl() throws MojoExecutionException {
314 
315 		return mgmtUrl;
316 	}
317 
318 	@SuppressWarnings("unused")
319 	private void unpackSonicProvided() throws MojoExecutionException {
320 
321 		MojoExecutor.executeMojo(MojoExecutor.plugin(MojoExecutor.groupId("org.apache.maven.plugins"),
322 				MojoExecutor.artifactId("maven-dependency-plugin"), MojoExecutor.version("2.7")), MojoExecutor
323 				.goal("unpack-dependencies"), MojoExecutor.configuration(new MojoExecutor.Element[] {
324 				MojoExecutor.element(MojoExecutor.name("outputDirectory"), getDependencyDirectory() + "/xar/provided"),
325 				MojoExecutor.element(MojoExecutor.name("excludeScope"), "compile"),
326 				MojoExecutor.element(MojoExecutor.name("includeScope"), "provided"),
327 				MojoExecutor.element(MojoExecutor.name("markersDirectory"), markersDirectory.getAbsolutePath()),
328 				MojoExecutor.element(MojoExecutor.name("includeTypes"), "esbstyp,esb"), // "zip,esb,esbstyp,dxsi,connect"
329 				MojoExecutor.element(MojoExecutor.name("useRepositoryLayout"), "true"),
330 				MojoExecutor.element(MojoExecutor.name("useSubDirectoryPerType"), "true") }), MojoExecutor
331 				.executionEnvironment(project, session, pluginManager));
332 
333 		try {
334 			FileUtils.deleteDirectory(markersDirectory);
335 		} catch (IOException e) {
336 			// This should never happen and if it does, it shouldn't really
337 			// matter
338 			e.printStackTrace();
339 		}
340 	}
341 
342 	private void copyDependentJars() throws MojoExecutionException {
343 
344 		getLog().debug("TBO-DEBUG- COPY JAR FILES");
345 		getLog().debug("TBO-DEBUG- Output Directory: " + getDependencyDirectory() + "/jar");
346 
347 		MojoExecutor.executeMojo(MojoExecutor.plugin(MojoExecutor.groupId("org.apache.maven.plugins"),
348 				MojoExecutor.artifactId("maven-dependency-plugin"), MojoExecutor.version("2.7")), MojoExecutor
349 				.goal("copy-dependencies"), MojoExecutor.configuration(new MojoExecutor.Element[] {
350 				MojoExecutor.element(MojoExecutor.name("outputDirectory"), getDependencyDirectory() + "/jar"),
351 				MojoExecutor.element(MojoExecutor.name("excludeScope"), "test"),
352 				MojoExecutor.element(MojoExecutor.name("includeScope"), "runtime"),
353 				MojoExecutor.element(MojoExecutor.name("markersDirectory"), markersDirectory.getAbsolutePath()),
354 				MojoExecutor.element(MojoExecutor.name("excludeGroupIds"),
355 						"com.aurea.sonic.mq,com.aurea.sonic.esb,junit,org.mockito"),
356 				MojoExecutor.element(MojoExecutor.name("excludeArtifactIds"), "esb_mockups"),
357 				MojoExecutor.element(MojoExecutor.name("includeTypes"), "jar"),
358 				MojoExecutor.element(MojoExecutor.name("useRepositoryLayout"), "true"),
359 				MojoExecutor.element(MojoExecutor.name("useSubDirectoryPerType"), "false") }), MojoExecutor
360 				.executionEnvironment(project, session, pluginManager));
361 
362 		getLog().debug("TBO-DEBUG- COPY JAR FILES - END");
363 
364 		try {
365 			FileUtils.deleteDirectory(markersDirectory);
366 		} catch (IOException e) {
367 
368 			e.printStackTrace();
369 		}
370 	}
371 
372 	private void copySonicExtensions() throws MojoExecutionException {
373 
374 		MojoExecutor.executeMojo(MojoExecutor.plugin(MojoExecutor.groupId("org.apache.maven.plugins"),
375 				MojoExecutor.artifactId("maven-dependency-plugin"), MojoExecutor.version("2.7")), MojoExecutor
376 				.goal("copy-dependencies"), MojoExecutor.configuration(new MojoExecutor.Element[] {
377 				MojoExecutor.element(MojoExecutor.name("outputDirectory"), getDependencyDirectory() + "/jar"),
378 				MojoExecutor.element(MojoExecutor.name("includeScope"), "runtime"),
379 				MojoExecutor.element(MojoExecutor.name("markersDirectory"), markersDirectory.getAbsolutePath()),
380 				MojoExecutor.element(MojoExecutor.name("includeGroupIds"),
381 						"com.progress.sonic.mqx,com.progress.sonic.esbx,com.aurea.sonic.mqx,com.aurea.sonic.esbx"),
382 				MojoExecutor.element(MojoExecutor.name("includeTypes"), "jar"),
383 				MojoExecutor.element(MojoExecutor.name("useRepositoryLayout"), "true"),
384 				MojoExecutor.element(MojoExecutor.name("useSubDirectoryPerType"), "false") }), MojoExecutor
385 				.executionEnvironment(project, session, pluginManager));
386 
387 		try {
388 			FileUtils.deleteDirectory(markersDirectory);
389 		} catch (IOException e) {
390 			// This should never happen and if it does, it shouldn't really
391 			// matter
392 			e.printStackTrace();
393 		}
394 	}
395 
396 }