1 package org
.argeo
.slc
.maven
.plugins
.osgi
;
4 import java
.util
.ArrayList
;
5 import java
.util
.Arrays
;
6 import java
.util
.HashMap
;
7 import java
.util
.Iterator
;
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
;
20 * Starts Equinox runtime
24 public class EquinoxExecMojo
extends AbstractOsgiMojo
{
26 private MavenDependencyManager mavenDependencyManager
;
31 * @parameter expression="${equinoxArtifactId}"
32 * default-value="org.eclipse.osgi"
35 protected String equinoxArtifactId
;
38 * OSGIBoot artifact id
40 * @parameter expression="${osgiBootArtifactId}"
41 * default-value="org.argeo.slc.osgiboot"
44 protected String osgiBootArtifactId
;
49 * @parameter expression="${jvm}" default-value="${java.home}/bin/java"
57 * @parameter alias="${jvmArgs}"
59 protected String
[] jvmArgs
;
62 * JVM arguments to append
64 * @parameter alias="${jvmArgsToAppend}"
66 protected String
[] jvmArgsToAppend
;
68 protected String
[] defaultJvmArgs
= { "-Xmx128m" };
71 * Debug port (0 deactivate)
73 * @parameter expression="${debug}" default-value="0"
76 protected String debug
;
81 * @parameter alias="${args}"
83 protected String
[] args
;
86 * Equinox args to append
88 * @parameter alias="${argsToAppend}"
90 protected String
[] argsToAppend
;
92 protected String
[] defaultArgs
= { "-console", "-configuration",
93 "target/slc/conf", "-data", "target/slc/data" };
96 * JVM system properties
98 * @parameter alias="${systemProperties}"
100 protected Map systemProperties
;
103 * Execution directory
105 * @parameter expression="${execDir}"
106 * default-value="${project.build.directory}/exec"
109 protected File execDir
;
112 * Whether to create a new JVM
114 * @parameter expression="${fork}" default-value="false"
117 protected boolean fork
;
120 * Whether to wait for the runtime to exit
122 * @parameter expression="${wait}" default-value="true"
125 protected boolean wait
;
128 * Number of milliseconds to pause after having started the server (when
131 * @parameter expression="${pause}" default-value="0"
134 protected long pause
;
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()));
143 String originalUserDir
= System
.getProperty("user.dir");
145 LocationsStruct locationsStruct
= listOsgiLocations();
147 execForked(locationsStruct
);
149 execNonForked(locationsStruct
);
150 } catch (Exception e
) {
151 throw new MojoExecutionException("Cannot execute OSGi runtime", e
);
153 System
.setProperty("user.dir", originalUserDir
);
157 protected LocationsStruct
listOsgiLocations() throws Exception
{
158 LocationsStruct locationsStruct
= new LocationsStruct();
160 Set dependencies
= mavenDependencyManager
161 .getTransitiveProjectDependencies(project
, remoteRepos
, local
);
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
);
170 if (depArtifact
.getArtifactId().equals(equinoxArtifactId
)) {
171 locationsStruct
.equinoxArtifact
= depArtifact
;
172 } else if (depArtifact
.getArtifactId().equals(osgiBootArtifactId
)) {
173 locationsStruct
.osgiBootArtifact
= depArtifact
;
175 bundleArtifacts
.add(depArtifact
);
177 if ("jar".equals(depArtifact
.getType())) {
178 // Add to OSGi locations
182 osgiLocations
.append(File
.pathSeparatorChar
);
184 osgiLocations
.append(depArtifact
.getFile()
186 .replace(File
.separatorChar
, '/'));
190 locationsStruct
.osgiLocations
= osgiLocations
.toString();
191 return locationsStruct
;
194 protected void execNonForked(LocationsStruct locationsStruct
)
200 // if (!execDir.exists())
202 // System.setProperty("user.dir", execDir.getCanonicalPath());
205 List cmdList
= new ArrayList();
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()
216 if (!systemProperties
.containsKey("slc.osgi.locations"))
217 System
.setProperty("slc.osgi.locations",
218 locationsStruct
.osgiLocations
);
220 for (Iterator keys
= systemProperties
.keySet().iterator(); keys
222 Object key
= keys
.next();
223 Object value
= systemProperties
.get(key
);
224 String strValue
= null;
226 strValue
= value
.toString().trim();
227 strValue
= strValue
.replaceAll("\n", "");
228 strValue
= strValue
.replaceAll("\t", "");
230 System
.setProperty(key
.toString(), strValue
);
234 cmdList
.addAll(Arrays
.asList(args
));
235 if (argsToAppend
!= null)
236 cmdList
.addAll(Arrays
.asList(argsToAppend
));
238 String
[] cmd
= (String
[]) cmdList
.toArray(new String
[0]);
239 System
.out
.println("Equinox arguments (non forked):");
242 EclipseStarter
.startup(cmd
, null);
245 while (EclipseStarter
.isRunning()) {
253 protected void execForked(LocationsStruct locationsStruct
) throws Exception
{
256 jvmArgs
= defaultJvmArgs
;
259 if (systemProperties
== null)
260 systemProperties
= new HashMap();
262 if (!execDir
.exists())
266 List cmdList
= new ArrayList();
270 cmdList
.addAll(Arrays
.asList(jvmArgs
));
272 if (!"0".equals(debug
))
274 .add("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address="
277 if (jvmArgsToAppend
!= null)
278 cmdList
.addAll(Arrays
.asList(jvmArgsToAppend
));
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"))
287 .add("-Dslc.osgi.locations="
288 + locationsStruct
.osgiLocations
);
289 for (Iterator keys
= systemProperties
.keySet().iterator(); keys
291 Object key
= keys
.next();
292 Object value
= systemProperties
.get(key
);
293 String strValue
= null;
295 strValue
= value
.toString().trim();
296 strValue
= strValue
.replaceAll("\n", "");
297 strValue
= strValue
.replaceAll("\t", "");
299 cmdList
.add("-D" + key
+ "=" + strValue
);
304 cmdList
.add(locationsStruct
.equinoxArtifact
.getFile()
305 .getCanonicalPath());
308 cmdList
.addAll(Arrays
.asList(args
));
309 if (argsToAppend
!= null)
310 cmdList
.addAll(Arrays
.asList(argsToAppend
));
312 String
[] cmd
= (String
[]) cmdList
.toArray(new String
[0]);
313 System
.out
.println("Execute Equinox command (forked):");
316 SystemCall systemCall
= new SystemCall(execDir
.getCanonicalPath(), cmd
,
321 new Thread(systemCall
).start();
326 protected void printArtifact(Artifact artifact
) {
327 if (getLog().isDebugEnabled())
328 getLog().debug(artifactToString(artifact
));
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);
336 System
.out
.print('\"');
337 System
.out
.print(cmd
[i
]);
339 System
.out
.print('\"');
340 System
.out
.print(' ');
344 protected static String
artifactToString(Artifact artifact
) {
345 return artifact
.getGroupId() + ":" + artifact
.getArtifactId() + ":"
346 + artifact
.getType() + ":" + artifact
.getClassifier() + ":"
347 + artifact
.getVersion() + " (" + artifact
.getFile() + ")";
350 protected class LocationsStruct
{
351 protected Artifact equinoxArtifact
= null;
352 protected Artifact osgiBootArtifact
= null;
353 protected String osgiLocations
= null;