1 package org
.argeo
.slc
.lib
.detached
;
4 import java
.io
.IOException
;
6 import org
.apache
.commons
.logging
.Log
;
7 import org
.apache
.commons
.logging
.LogFactory
;
8 import org
.argeo
.slc
.SlcException
;
9 import org
.argeo
.slc
.core
.execution
.tasks
.JvmProcess
;
10 import org
.osgi
.framework
.Bundle
;
11 import org
.osgi
.framework
.BundleContext
;
12 import org
.springframework
.beans
.factory
.InitializingBean
;
13 import org
.springframework
.context
.ResourceLoaderAware
;
14 import org
.springframework
.core
.io
.FileSystemResource
;
15 import org
.springframework
.core
.io
.Resource
;
16 import org
.springframework
.core
.io
.ResourceLoader
;
17 import org
.springframework
.osgi
.context
.BundleContextAware
;
19 public class DetachedLauncher
extends JvmProcess
implements BundleContextAware
,
20 InitializingBean
, ResourceLoaderAware
{
21 private final static Log log
= LogFactory
.getLog(DetachedLauncher
.class);
23 private BundleContext bundleContext
= null;
24 private ResourceLoader resourceLoader
= null;
26 private Resource osgiFramework
= null;
27 private String osgibootBundleName
= "org.argeo.slc.osgiboot";
28 private String equinoxBundleName
= "org.eclipse.osgi";
29 private String xmlapisBundleName
= "com.springsource.org.apache.xmlcommons";
30 private String xercesBundleName
= "com.springsource.org.apache.xerces";
33 * Required by Spring for JDK 1.4. see
34 * http://forum.springsource.org/showthread.php?t=74555
36 private Boolean prependXmlJars
= false;
38 public DetachedLauncher() {
40 setSynchronous(false);
41 setMainClass("org.argeo.slc.osgiboot.Launcher");
44 public void afterPropertiesSet() throws Exception
{
45 if (bundleContext
== null)
46 throw new SlcException("An OSGi bundle context is required.");
49 if (osgiFramework
== null)
51 .add(asResource(System
.getProperty("osgi.framework")));
53 getClasspath().add(osgiFramework
);
55 StringBuffer osgiBundles
= new StringBuffer("");
56 StringBuffer osgiLocations
= new StringBuffer("");
57 bundles
: for (Bundle bundle
: bundleContext
.getBundles()) {
58 String name
= bundle
.getSymbolicName();
59 String location
= bundle
.getLocation();
62 if (osgibootBundleName
.equals(name
))
63 getClasspath().add(findOsgiboot(bundle
));
64 else if (equinoxBundleName
.equals(name
))
65 continue bundles
;// skip framework
66 else if (xmlapisBundleName
.equals(name
) && prependXmlJars
)
67 getPBootClasspath().add(asResource(bundle
.getLocation()));
68 else if (xercesBundleName
.equals(name
) && prependXmlJars
)
69 getPBootClasspath().add(asResource(bundle
.getLocation()));
71 if (location
.startsWith("file:")) {
72 File file
= new File(location
.substring("file:".length()));
73 if (osgiLocations
.length() != 0)
74 osgiLocations
.append(File
.pathSeparatorChar
);
75 osgiLocations
.append(file
.getPath().replace('/',
78 if (osgiBundles
.length() != 0)
79 osgiBundles
.append(',');
80 osgiBundles
.append(location
.replace('/', File
.separatorChar
));
84 getSystemProperties().setProperty("osgi.bundles",
85 osgiBundles
.toString());
86 getSystemProperties().setProperty("slc.osgi.locations",
87 osgiLocations
.toString());
90 protected Resource
findOsgiboot(Bundle bundle
) {
91 String location
= bundle
.getLocation();
92 if (location
.startsWith("initial@reference:file:"))
93 location
= System
.getProperty("osgi.install.area")
94 + location
.substring("initial@reference:file:".length());
95 if (location
.charAt(location
.length() - 1) == '/')
96 location
.substring(0, location
.length() - 1);
97 return asResource(location
);
100 protected Resource
asResource(String location
) {
101 // Resource res = resourceLoader.getResource(location);
104 if (location
.startsWith("file:")) {
105 File file
= new File(location
.substring("file:".length()));
107 throw new SlcException("File " + file
+ " does not exist");
110 res
= new FileSystemResource(file
.getCanonicalFile());
111 } catch (IOException e
) {
112 throw new SlcException("Cannot create resource based on "
116 res
= resourceLoader
.getResource(location
);
118 if (log
.isDebugEnabled())
119 log
.debug("Converted " + location
+ " to " + res
);
123 public void setBundleContext(BundleContext bundleContext
) {
124 this.bundleContext
= bundleContext
;
127 public void setResourceLoader(ResourceLoader resourceLoader
) {
128 this.resourceLoader
= resourceLoader
;
131 public void setOsgibootBundleName(String osgibootBundleName
) {
132 this.osgibootBundleName
= osgibootBundleName
;
135 public void setXmlapisBundleName(String xmlapisBundleName
) {
136 this.xmlapisBundleName
= xmlapisBundleName
;
139 public void setXercesBundleName(String xercesBundleName
) {
140 this.xercesBundleName
= xercesBundleName
;
143 public void setOsgiFramework(Resource osgiFramework
) {
144 this.osgiFramework
= osgiFramework
;
147 public void setEquinoxBundleName(String equinoxBundleName
) {
148 this.equinoxBundleName
= equinoxBundleName
;
151 public void setPrependXmlJars(Boolean prependXmlJars
) {
152 this.prependXmlJars
= prependXmlJars
;