1 package org
.argeo
.slc
.lib
.detached
;
4 import java
.io
.IOException
;
5 import java
.util
.ArrayList
;
8 import org
.apache
.commons
.logging
.Log
;
9 import org
.apache
.commons
.logging
.LogFactory
;
10 import org
.argeo
.slc
.SlcException
;
11 import org
.argeo
.slc
.core
.execution
.tasks
.JvmProcess
;
12 import org
.osgi
.framework
.Bundle
;
13 import org
.osgi
.framework
.BundleContext
;
14 import org
.springframework
.beans
.factory
.InitializingBean
;
15 import org
.springframework
.context
.ResourceLoaderAware
;
16 import org
.springframework
.core
.io
.FileSystemResource
;
17 import org
.springframework
.core
.io
.Resource
;
18 import org
.springframework
.core
.io
.ResourceLoader
;
19 import org
.springframework
.osgi
.context
.BundleContextAware
;
21 public class DetachedLauncher
extends JvmProcess
implements BundleContextAware
,
22 InitializingBean
, ResourceLoaderAware
{
23 private final static Log log
= LogFactory
.getLog(DetachedLauncher
.class);
25 private BundleContext bundleContext
= null;
26 private ResourceLoader resourceLoader
= null;
28 private Resource osgiFramework
= null;
29 private String osgibootBundleName
= "org.argeo.slc.osgiboot";
30 private String equinoxBundleName
= "org.eclipse.osgi";
31 private String xmlapisBundleName
= "com.springsource.org.apache.xmlcommons";
32 private String xercesBundleName
= "com.springsource.org.apache.xerces";
34 private List
<String
> excludeBundleNames
= new ArrayList
<String
>();
35 private List
<String
> includeBundleUrls
= new ArrayList
<String
>();
38 * Required by Spring for JDK 1.4. see
39 * http://forum.springsource.org/showthread.php?t=74555
41 private Boolean prependXmlJars
= false;
43 public DetachedLauncher() {
45 setSynchronous(false);
46 setMainClass("org.argeo.slc.osgiboot.Launcher");
49 public void afterPropertiesSet() throws Exception
{
50 if (bundleContext
== null)
51 throw new SlcException("An OSGi bundle context is required.");
54 if (osgiFramework
== null)
56 .add(asResource(System
.getProperty("osgi.framework")));
58 getClasspath().add(osgiFramework
);
60 StringBuffer osgiBundles
= new StringBuffer("");
61 StringBuffer osgiLocations
= new StringBuffer("");
62 bundles
: for (Bundle bundle
: bundleContext
.getBundles()) {
63 String name
= bundle
.getSymbolicName();
65 if (excludeBundleNames
.contains(name
))
66 continue bundles
;// skip excluded
68 String location
= bundle
.getLocation();
69 location
= removeInitialReference(location
);
72 if (osgibootBundleName
.equals(name
))
73 getClasspath().add(asResource(location
));
74 else if (equinoxBundleName
.equals(name
))
75 continue bundles
;// skip framework
76 else if (xmlapisBundleName
.equals(name
) && prependXmlJars
)
77 getPBootClasspath().add(asResource(location
));
78 else if (xercesBundleName
.equals(name
) && prependXmlJars
)
79 getPBootClasspath().add(asResource(location
));
81 if (location
.startsWith("file:")) {
82 File file
= new File(location
.substring("file:".length()));
83 if (osgiLocations
.length() != 0)
84 osgiLocations
.append(File
.pathSeparatorChar
);
85 osgiLocations
.append(file
.getPath().replace('/',
88 if (osgiBundles
.length() != 0)
89 osgiBundles
.append(',');
90 osgiBundles
.append(location
.replace('/', File
.separatorChar
));
94 for (String url
: includeBundleUrls
) {
95 if (osgiBundles
.length() != 0)
96 osgiBundles
.append(',');
97 osgiBundles
.append(url
);
100 getSystemProperties().setProperty("osgi.bundles",
101 osgiBundles
.toString());
102 getSystemProperties().setProperty("slc.osgi.locations",
103 osgiLocations
.toString());
106 protected String
removeInitialReference(String location
) {
107 if (location
.startsWith("initial@reference:file:"))
108 location
= System
.getProperty("osgi.install.area")
109 + location
.substring("initial@reference:file:".length());
110 if (location
.charAt(location
.length() - 1) == '/')
111 location
.substring(0, location
.length() - 1);
115 protected Resource
asResource(String location
) {
116 // Resource res = resourceLoader.getResource(location);
119 if (location
.startsWith("file:")) {
120 File file
= new File(location
.substring("file:".length()));
122 throw new SlcException("File " + file
+ " does not exist");
125 res
= new FileSystemResource(file
.getCanonicalFile());
126 } catch (IOException e
) {
127 throw new SlcException("Cannot create resource based on "
131 res
= resourceLoader
.getResource(location
);
133 if (log
.isDebugEnabled())
134 log
.debug("Converted " + location
+ " to " + res
);
138 public void setBundleContext(BundleContext bundleContext
) {
139 this.bundleContext
= bundleContext
;
142 public void setResourceLoader(ResourceLoader resourceLoader
) {
143 this.resourceLoader
= resourceLoader
;
146 public void setOsgibootBundleName(String osgibootBundleName
) {
147 this.osgibootBundleName
= osgibootBundleName
;
150 public void setXmlapisBundleName(String xmlapisBundleName
) {
151 this.xmlapisBundleName
= xmlapisBundleName
;
154 public void setXercesBundleName(String xercesBundleName
) {
155 this.xercesBundleName
= xercesBundleName
;
158 public void setOsgiFramework(Resource osgiFramework
) {
159 this.osgiFramework
= osgiFramework
;
162 public void setEquinoxBundleName(String equinoxBundleName
) {
163 this.equinoxBundleName
= equinoxBundleName
;
166 public void setPrependXmlJars(Boolean prependXmlJars
) {
167 this.prependXmlJars
= prependXmlJars
;
170 public void setExcludeBundleNames(List
<String
> excludeBundleNames
) {
171 this.excludeBundleNames
= excludeBundleNames
;
174 public void setIncludeBundleUrls(List
<String
> includeBundleUrls
) {
175 this.includeBundleUrls
= includeBundleUrls
;