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 System
.setProperty("osgi.bundles", locationsStruct
.osgiBootArtifact
210 .getFile().getCanonicalPath()
212 if (!systemProperties
.containsKey("slc.osgi.locations"))
213 System
.setProperty("slc.osgi.locations",
214 locationsStruct
.osgiLocations
);
215 for (Iterator keys
= systemProperties
.keySet().iterator(); keys
217 Object key
= keys
.next();
218 Object value
= systemProperties
.get(key
);
219 String strValue
= null;
221 strValue
= value
.toString().trim();
222 strValue
= strValue
.replaceAll("\n", "");
223 strValue
= strValue
.replaceAll("\t", "");
225 System
.setProperty(key
.toString(), strValue
);
229 cmdList
.addAll(Arrays
.asList(args
));
230 if (argsToAppend
!= null)
231 cmdList
.addAll(Arrays
.asList(argsToAppend
));
233 String
[] cmd
= (String
[]) cmdList
.toArray(new String
[0]);
234 System
.out
.println("Equinox arguments (non forked):");
237 EclipseStarter
.startup(cmd
, null);
240 while (EclipseStarter
.isRunning()) {
248 protected void execForked(LocationsStruct locationsStruct
) throws Exception
{
251 jvmArgs
= defaultJvmArgs
;
254 if (systemProperties
== null)
255 systemProperties
= new HashMap();
257 if (!execDir
.exists())
261 List cmdList
= new ArrayList();
265 cmdList
.addAll(Arrays
.asList(jvmArgs
));
267 if (!"0".equals(debug
))
269 .add("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address="
272 if (jvmArgsToAppend
!= null)
273 cmdList
.addAll(Arrays
.asList(jvmArgsToAppend
));
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"))
282 .add("-Dslc.osgi.locations="
283 + locationsStruct
.osgiLocations
);
284 for (Iterator keys
= systemProperties
.keySet().iterator(); keys
286 Object key
= keys
.next();
287 Object value
= systemProperties
.get(key
);
288 String strValue
= null;
290 strValue
= value
.toString().trim();
291 strValue
= strValue
.replaceAll("\n", "");
292 strValue
= strValue
.replaceAll("\t", "");
294 cmdList
.add("-D" + key
+ "=" + strValue
);
299 cmdList
.add(locationsStruct
.equinoxArtifact
.getFile()
300 .getCanonicalPath());
303 cmdList
.addAll(Arrays
.asList(args
));
304 if (argsToAppend
!= null)
305 cmdList
.addAll(Arrays
.asList(argsToAppend
));
307 String
[] cmd
= (String
[]) cmdList
.toArray(new String
[0]);
308 System
.out
.println("Execute Equinox command (forked):");
311 SystemCall systemCall
= new SystemCall(execDir
.getCanonicalPath(), cmd
,
316 new Thread(systemCall
).start();
321 protected void printArtifact(Artifact artifact
) {
322 if (getLog().isDebugEnabled())
323 getLog().debug(artifactToString(artifact
));
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);
331 System
.out
.print('\"');
332 System
.out
.print(cmd
[i
]);
334 System
.out
.print('\"');
335 System
.out
.print(' ');
339 protected static String
artifactToString(Artifact artifact
) {
340 return artifact
.getGroupId() + ":" + artifact
.getArtifactId() + ":"
341 + artifact
.getType() + ":" + artifact
.getClassifier() + ":"
342 + artifact
.getVersion() + " (" + artifact
.getFile() + ")";
345 protected class LocationsStruct
{
346 protected Artifact equinoxArtifact
= null;
347 protected Artifact osgiBootArtifact
= null;
348 protected String osgiLocations
= null;