]> git.argeo.org Git - gpl/argeo-slc.git/blob - maven-argeo-osgi-plugin/src/main/java/org/argeo/slc/maven/plugins/osgi/EquinoxExecMojo.java
ef849a5d6e1a7132cfd9e2dad2dc01b815675411
[gpl/argeo-slc.git] / maven-argeo-osgi-plugin / src / main / java / org / argeo / slc / maven / plugins / osgi / EquinoxExecMojo.java
1 package org.argeo.slc.maven.plugins.osgi;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.HashMap;
7 import java.util.Iterator;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.Set;
11
12 import org.apache.maven.artifact.Artifact;
13 import org.apache.maven.plugin.MojoExecutionException;
14 import org.apache.maven.plugin.MojoFailureException;
15 import org.argeo.slc.maven.plugin.MavenDependencyManager;
16 import org.argeo.slc.maven.plugin.SystemCall;
17 import org.eclipse.core.runtime.adaptor.EclipseStarter;
18
19 /**
20 * Starts Equinox runtime
21 *
22 * @goal equinox
23 * */
24 public class EquinoxExecMojo extends AbstractOsgiMojo {
25 /** @component */
26 private MavenDependencyManager mavenDependencyManager;
27
28 /**
29 * Equinox artifact id
30 *
31 * @parameter expression="${equinoxArtifactId}"
32 * default-value="org.eclipse.osgi"
33 * @required
34 */
35 protected String equinoxArtifactId;
36
37 /**
38 * OSGIBoot artifact id
39 *
40 * @parameter expression="${osgiBootArtifactId}"
41 * default-value="org.argeo.slc.osgiboot"
42 * @required
43 */
44 protected String osgiBootArtifactId;
45
46 /**
47 * Java executable
48 *
49 * @parameter expression="${jvm}" default-value="${java.home}/bin/java"
50 * @required
51 */
52 protected String jvm;
53
54 /**
55 * JVM arguments
56 *
57 * @parameter alias="${jvmArgs}"
58 */
59 protected String[] jvmArgs;
60
61 /**
62 * JVM arguments to append
63 *
64 * @parameter alias="${jvmArgsToAppend}"
65 */
66 protected String[] jvmArgsToAppend;
67
68 protected String[] defaultJvmArgs = { "-Xmx128m" };
69
70 /**
71 * Debug port (0 deactivate)
72 *
73 * @parameter expression="${debug}" default-value="0"
74 * @required
75 */
76 protected String debug;
77
78 /**
79 * Equinox args
80 *
81 * @parameter alias="${args}"
82 */
83 protected String[] args;
84
85 /**
86 * Equinox args to append
87 *
88 * @parameter alias="${argsToAppend}"
89 */
90 protected String[] argsToAppend;
91
92 protected String[] defaultArgs = { "-console", "-configuration",
93 "target/slc/conf", "-data", "target/slc/data" };
94
95 /**
96 * JVM system properties
97 *
98 * @parameter alias="${systemProperties}"
99 */
100 protected Map systemProperties;
101
102 /**
103 * Execution directory
104 *
105 * @parameter expression="${execDir}"
106 * default-value="${project.build.directory}/exec"
107 * @required
108 */
109 protected File execDir;
110
111 /**
112 * Whether to create a new JVM
113 *
114 * @parameter expression="${fork}" default-value="false"
115 * @required
116 */
117 protected boolean fork;
118
119 /**
120 * Whether to wait for the runtime to exit
121 *
122 * @parameter expression="${wait}" default-value="true"
123 * @required
124 */
125 protected boolean wait;
126
127 /**
128 * Number of milliseconds to pause after having started the server (when
129 * ${wait}=false)
130 *
131 * @parameter expression="${pause}" default-value="0"
132 * @required
133 */
134 protected long pause;
135
136 public void execute() throws MojoExecutionException, MojoFailureException {
137 if ("bundles".equals(project.getArtifact().getType())) {
138 System.out.println("Skip artifact of type bundles "
139 + artifactToString(project.getArtifact()));
140 return;
141 }
142
143 String originalUserDir = System.getProperty("user.dir");
144 try {
145 LocationsStruct locationsStruct = listOsgiLocations();
146 if (fork)
147 execForked(locationsStruct);
148 else
149 execNonForked(locationsStruct);
150 } catch (Exception e) {
151 throw new MojoExecutionException("Cannot execute OSGi runtime", e);
152 } finally {
153 System.setProperty("user.dir", originalUserDir);
154 }
155 }
156
157 protected LocationsStruct listOsgiLocations() throws Exception {
158 LocationsStruct locationsStruct = new LocationsStruct();
159
160 Set dependencies = mavenDependencyManager
161 .getTransitiveProjectDependencies(project, remoteRepos, local);
162
163 StringBuffer osgiLocations = new StringBuffer();
164 List bundleArtifacts = new ArrayList();
165 boolean first = true;
166 for (Iterator it = dependencies.iterator(); it.hasNext();) {
167 Artifact depArtifact = (Artifact) it.next();
168 printArtifact(depArtifact);
169
170 if (depArtifact.getArtifactId().equals(equinoxArtifactId)) {
171 locationsStruct.equinoxArtifact = depArtifact;
172 } else if (depArtifact.getArtifactId().equals(osgiBootArtifactId)) {
173 locationsStruct.osgiBootArtifact = depArtifact;
174 } else {
175 bundleArtifacts.add(depArtifact);
176
177 if ("jar".equals(depArtifact.getType())) {
178 // Add to OSGi locations
179 if (first)
180 first = false;
181 else
182 osgiLocations.append(File.pathSeparatorChar);
183
184 osgiLocations.append(depArtifact.getFile()
185 .getCanonicalPath()
186 .replace(File.separatorChar, '/'));
187 }
188 }
189 }
190 locationsStruct.osgiLocations = osgiLocations.toString();
191 return locationsStruct;
192 }
193
194 protected void execNonForked(LocationsStruct locationsStruct)
195 throws Exception {
196 // Set defaults
197 if (args == null)
198 args = defaultArgs;
199
200 // if (!execDir.exists())
201 // execDir.mkdirs();
202 // System.setProperty("user.dir", execDir.getCanonicalPath());
203
204 // Build command
205 List cmdList = new ArrayList();
206
207 // System properties
208 if (!systemProperties.containsKey("osgi.bundles")) {
209 if (locationsStruct.osgiBootArtifact == null)
210 throw new Exception("No SLC OSGi boot bundle available.");
211 System.setProperty("osgi.bundles", locationsStruct.osgiBootArtifact
212 .getFile().getCanonicalPath()
213 + "@start");
214 }
215
216 if (!systemProperties.containsKey("slc.osgi.locations"))
217 System.setProperty("slc.osgi.locations",
218 locationsStruct.osgiLocations);
219
220 for (Iterator keys = systemProperties.keySet().iterator(); keys
221 .hasNext();) {
222 Object key = keys.next();
223 Object value = systemProperties.get(key);
224 String strValue = null;
225 if (value != null) {
226 strValue = value.toString().trim();
227 strValue = strValue.replaceAll("\n", "");
228 strValue = strValue.replaceAll("\t", "");
229 }
230 System.setProperty(key.toString(), strValue);
231 }
232
233 // Program arguments
234 cmdList.addAll(Arrays.asList(args));
235 if (argsToAppend != null)
236 cmdList.addAll(Arrays.asList(argsToAppend));
237
238 String[] cmd = (String[]) cmdList.toArray(new String[0]);
239 System.out.println("Equinox arguments (non forked):");
240 printCommand(cmd);
241
242 EclipseStarter.startup(cmd, null);
243
244 if (wait) {
245 while (EclipseStarter.isRunning()) {
246 Thread.sleep(500);
247 }
248 } else {
249 Thread.sleep(pause);
250 }
251 }
252
253 protected void execForked(LocationsStruct locationsStruct) throws Exception {
254 // Set defaults
255 if (jvmArgs == null)
256 jvmArgs = defaultJvmArgs;
257 if (args == null)
258 args = defaultArgs;
259 if (systemProperties == null)
260 systemProperties = new HashMap();
261
262 if (!execDir.exists())
263 execDir.mkdirs();
264
265 // Build command
266 List cmdList = new ArrayList();
267 // JVM
268 cmdList.add(jvm);
269 // JVM arguments
270 cmdList.addAll(Arrays.asList(jvmArgs));
271
272 if (!"0".equals(debug))
273 cmdList
274 .add("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address="
275 + debug);
276
277 if (jvmArgsToAppend != null)
278 cmdList.addAll(Arrays.asList(jvmArgsToAppend));
279
280 // System properties
281 if (!systemProperties.containsKey("osgi.bundles"))
282 cmdList.add("-Dosgi.bundles="
283 + locationsStruct.osgiBootArtifact.getFile()
284 .getCanonicalPath() + "@start");
285 if (!systemProperties.containsKey("slc.osgi.locations"))
286 cmdList
287 .add("-Dslc.osgi.locations="
288 + locationsStruct.osgiLocations);
289 for (Iterator keys = systemProperties.keySet().iterator(); keys
290 .hasNext();) {
291 Object key = keys.next();
292 Object value = systemProperties.get(key);
293 String strValue = null;
294 if (value != null) {
295 strValue = value.toString().trim();
296 strValue = strValue.replaceAll("\n", "");
297 strValue = strValue.replaceAll("\t", "");
298 }
299 cmdList.add("-D" + key + "=" + strValue);
300 }
301
302 // Equinox jar
303 cmdList.add("-jar");
304 cmdList.add(locationsStruct.equinoxArtifact.getFile()
305 .getCanonicalPath());
306
307 // Program arguments
308 cmdList.addAll(Arrays.asList(args));
309 if (argsToAppend != null)
310 cmdList.addAll(Arrays.asList(argsToAppend));
311
312 String[] cmd = (String[]) cmdList.toArray(new String[0]);
313 System.out.println("Execute Equinox command (forked):");
314 printCommand(cmd);
315
316 SystemCall systemCall = new SystemCall(execDir.getCanonicalPath(), cmd,
317 true);
318 if (wait) {
319 systemCall.run();
320 } else {
321 new Thread(systemCall).start();
322 Thread.sleep(pause);
323 }
324 }
325
326 protected void printArtifact(Artifact artifact) {
327 if (getLog().isDebugEnabled())
328 getLog().debug(artifactToString(artifact));
329 }
330
331 protected void printCommand(String[] cmd) {
332 for (int i = 0; i < cmd.length; i++) {
333 boolean containsSpace = (cmd[i].indexOf(' ') >= 0)
334 || (cmd[i].indexOf('\t') >= 0);
335 if (containsSpace)
336 System.out.print('\"');
337 System.out.print(cmd[i]);
338 if (containsSpace)
339 System.out.print('\"');
340 System.out.print(' ');
341 }
342 }
343
344 protected static String artifactToString(Artifact artifact) {
345 return artifact.getGroupId() + ":" + artifact.getArtifactId() + ":"
346 + artifact.getType() + ":" + artifact.getClassifier() + ":"
347 + artifact.getVersion() + " (" + artifact.getFile() + ")";
348 }
349
350 protected class LocationsStruct {
351 protected Artifact equinoxArtifact = null;
352 protected Artifact osgiBootArtifact = null;
353 protected String osgiLocations = null;
354 }
355 }