]> git.argeo.org Git - gpl/argeo-slc.git/blob - osgi/EquinoxExecMojo.java
Prepare next development cycle
[gpl/argeo-slc.git] / 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 System.setProperty("osgi.bundles", locationsStruct.osgiBootArtifact
210 .getFile().getCanonicalPath()
211 + "@start");
212 if (!systemProperties.containsKey("slc.osgi.locations"))
213 System.setProperty("slc.osgi.locations",
214 locationsStruct.osgiLocations);
215 for (Iterator keys = systemProperties.keySet().iterator(); keys
216 .hasNext();) {
217 Object key = keys.next();
218 Object value = systemProperties.get(key);
219 String strValue = null;
220 if (value != null) {
221 strValue = value.toString().trim();
222 strValue = strValue.replaceAll("\n", "");
223 strValue = strValue.replaceAll("\t", "");
224 }
225 System.setProperty(key.toString(), strValue);
226 }
227
228 // Program arguments
229 cmdList.addAll(Arrays.asList(args));
230 if (argsToAppend != null)
231 cmdList.addAll(Arrays.asList(argsToAppend));
232
233 String[] cmd = (String[]) cmdList.toArray(new String[0]);
234 System.out.println("Equinox arguments (non forked):");
235 printCommand(cmd);
236
237 EclipseStarter.startup(cmd, null);
238
239 if (wait) {
240 while (EclipseStarter.isRunning()) {
241 Thread.sleep(500);
242 }
243 } else {
244 Thread.sleep(pause);
245 }
246 }
247
248 protected void execForked(LocationsStruct locationsStruct) throws Exception {
249 // Set defaults
250 if (jvmArgs == null)
251 jvmArgs = defaultJvmArgs;
252 if (args == null)
253 args = defaultArgs;
254 if (systemProperties == null)
255 systemProperties = new HashMap();
256
257 if (!execDir.exists())
258 execDir.mkdirs();
259
260 // Build command
261 List cmdList = new ArrayList();
262 // JVM
263 cmdList.add(jvm);
264 // JVM arguments
265 cmdList.addAll(Arrays.asList(jvmArgs));
266
267 if (!"0".equals(debug))
268 cmdList
269 .add("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address="
270 + debug);
271
272 if (jvmArgsToAppend != null)
273 cmdList.addAll(Arrays.asList(jvmArgsToAppend));
274
275 // System properties
276 if (!systemProperties.containsKey("osgi.bundles"))
277 cmdList.add("-Dosgi.bundles="
278 + locationsStruct.osgiBootArtifact.getFile()
279 .getCanonicalPath() + "@start");
280 if (!systemProperties.containsKey("slc.osgi.locations"))
281 cmdList
282 .add("-Dslc.osgi.locations="
283 + locationsStruct.osgiLocations);
284 for (Iterator keys = systemProperties.keySet().iterator(); keys
285 .hasNext();) {
286 Object key = keys.next();
287 Object value = systemProperties.get(key);
288 String strValue = null;
289 if (value != null) {
290 strValue = value.toString().trim();
291 strValue = strValue.replaceAll("\n", "");
292 strValue = strValue.replaceAll("\t", "");
293 }
294 cmdList.add("-D" + key + "=" + strValue);
295 }
296
297 // Equinox jar
298 cmdList.add("-jar");
299 cmdList.add(locationsStruct.equinoxArtifact.getFile()
300 .getCanonicalPath());
301
302 // Program arguments
303 cmdList.addAll(Arrays.asList(args));
304 if (argsToAppend != null)
305 cmdList.addAll(Arrays.asList(argsToAppend));
306
307 String[] cmd = (String[]) cmdList.toArray(new String[0]);
308 System.out.println("Execute Equinox command (forked):");
309 printCommand(cmd);
310
311 SystemCall systemCall = new SystemCall(execDir.getCanonicalPath(), cmd,
312 true);
313 if (wait) {
314 systemCall.run();
315 } else {
316 new Thread(systemCall).start();
317 Thread.sleep(pause);
318 }
319 }
320
321 protected void printArtifact(Artifact artifact) {
322 if (getLog().isDebugEnabled())
323 getLog().debug(artifactToString(artifact));
324 }
325
326 protected void printCommand(String[] cmd) {
327 for (int i = 0; i < cmd.length; i++) {
328 boolean containsSpace = (cmd[i].indexOf(' ') >= 0)
329 || (cmd[i].indexOf('\t') >= 0);
330 if (containsSpace)
331 System.out.print('\"');
332 System.out.print(cmd[i]);
333 if (containsSpace)
334 System.out.print('\"');
335 System.out.print(' ');
336 }
337 }
338
339 protected static String artifactToString(Artifact artifact) {
340 return artifact.getGroupId() + ":" + artifact.getArtifactId() + ":"
341 + artifact.getType() + ":" + artifact.getClassifier() + ":"
342 + artifact.getVersion() + " (" + artifact.getFile() + ")";
343 }
344
345 protected class LocationsStruct {
346 protected Artifact equinoxArtifact = null;
347 protected Artifact osgiBootArtifact = null;
348 protected String osgiLocations = null;
349 }
350 }