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 if (log
.isDebugEnabled())
67 log
.debug("Exclude bundle " + name
);
68 continue bundles
;// skip excluded
71 String originalLocation
= bundle
.getLocation();
72 if (log
.isTraceEnabled())
73 log
.trace("Original location of bundle " + name
+ ": "
75 String location
= removeInitialReference(originalLocation
);
78 if (osgibootBundleName
.equals(name
))
79 getClasspath().add(asResource(location
));
80 else if (equinoxBundleName
.equals(name
))
81 continue bundles
;// skip framework
82 else if (xmlapisBundleName
.equals(name
) && prependXmlJars
)
83 getPBootClasspath().add(asResource(location
));
84 else if (xercesBundleName
.equals(name
) && prependXmlJars
)
85 getPBootClasspath().add(asResource(location
));
87 if (location
.startsWith("file:")) {
88 File file
= new File(location
.substring("file:".length()));
89 if (osgiLocations
.length() != 0)
90 osgiLocations
.append(File
.pathSeparatorChar
);
91 location
= file
.getPath().replace('/', File
.separatorChar
);
92 osgiLocations
.append(location
);
93 if (log
.isTraceEnabled())
94 log
.trace("Added bundle " + name
95 + " to slc.osgi.locations: " + location
);
97 if (osgiBundles
.length() != 0)
98 osgiBundles
.append(',');
99 location
= location
.replace('/', File
.separatorChar
);
100 osgiBundles
.append(location
);
101 if (log
.isTraceEnabled())
102 log
.trace("Added bundle " + name
+ " to osgi.bundles: "
107 for (String url
: includeBundleUrls
) {
108 if (osgiBundles
.length() != 0)
109 osgiBundles
.append(',');
110 osgiBundles
.append(url
);
111 if (log
.isDebugEnabled())
112 log
.debug("Include url" + url
);
115 getSystemProperties().setProperty("osgi.bundles",
116 osgiBundles
.toString());
117 getSystemProperties().setProperty("slc.osgi.locations",
118 osgiLocations
.toString());
120 super.afterPropertiesSet();
123 protected String
removeInitialReference(String location
) {
124 if (location
.startsWith("initial@reference:file:"))
125 location
= System
.getProperty("osgi.install.area")
126 + location
.substring("initial@reference:file:".length());
127 if (location
.charAt(location
.length() - 1) == '/')
128 location
.substring(0, location
.length() - 1);
132 protected Resource
asResource(String location
) {
133 // Resource res = resourceLoader.getResource(location);
136 if (location
.startsWith("file:")) {
137 File file
= new File(location
.substring("file:".length()));
139 throw new SlcException("File " + file
+ " does not exist");
142 res
= new FileSystemResource(file
.getCanonicalFile());
143 } catch (IOException e
) {
144 throw new SlcException("Cannot create resource based on "
148 res
= resourceLoader
.getResource(location
);
150 if (log
.isDebugEnabled())
151 log
.debug("Converted " + location
+ " to " + res
);
155 public void setBundleContext(BundleContext bundleContext
) {
156 this.bundleContext
= bundleContext
;
159 public void setResourceLoader(ResourceLoader resourceLoader
) {
160 this.resourceLoader
= resourceLoader
;
163 public void setOsgibootBundleName(String osgibootBundleName
) {
164 this.osgibootBundleName
= osgibootBundleName
;
167 public void setXmlapisBundleName(String xmlapisBundleName
) {
168 this.xmlapisBundleName
= xmlapisBundleName
;
171 public void setXercesBundleName(String xercesBundleName
) {
172 this.xercesBundleName
= xercesBundleName
;
175 public void setOsgiFramework(Resource osgiFramework
) {
176 this.osgiFramework
= osgiFramework
;
179 public void setEquinoxBundleName(String equinoxBundleName
) {
180 this.equinoxBundleName
= equinoxBundleName
;
183 public void setPrependXmlJars(Boolean prependXmlJars
) {
184 this.prependXmlJars
= prependXmlJars
;
187 public void setExcludeBundleNames(List
<String
> excludeBundleNames
) {
188 this.excludeBundleNames
= excludeBundleNames
;
191 public void setIncludeBundleUrls(List
<String
> includeBundleUrls
) {
192 this.includeBundleUrls
= includeBundleUrls
;