]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.lib.detached/src/main/java/org/argeo/slc/lib/detached/DetachedLauncher.java
825a9717d91efec1fbb1cb1b0a2996775a1abd2d
[gpl/argeo-slc.git] / runtime / org.argeo.slc.lib.detached / src / main / java / org / argeo / slc / lib / detached / DetachedLauncher.java
1 package org.argeo.slc.lib.detached;
2
3 import java.io.File;
4 import java.io.IOException;
5
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;
18
19 public class DetachedLauncher extends JvmProcess implements BundleContextAware,
20 InitializingBean, ResourceLoaderAware {
21 private final static Log log = LogFactory.getLog(DetachedLauncher.class);
22
23 private BundleContext bundleContext = null;
24 private ResourceLoader resourceLoader = null;
25
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";
31
32 /**
33 * Required by Spring for JDK 1.4. see
34 * http://forum.springsource.org/showthread.php?t=74555
35 */
36 private Boolean prependXmlJars = false;
37
38 public DetachedLauncher() {
39 // Override defaults
40 setSynchronous(false);
41 setMainClass("org.argeo.slc.osgiboot.Launcher");
42 }
43
44 public void afterPropertiesSet() throws Exception {
45 if (bundleContext == null)
46 throw new SlcException("An OSGi bundle context is required.");
47
48 // Equinox jar
49 if (osgiFramework == null)
50 getClasspath()
51 .add(asResource(System.getProperty("osgi.framework")));
52 else
53 getClasspath().add(osgiFramework);
54
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();
60
61 // Special bundles
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()));
70
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('/',
76 File.separatorChar));
77 } else {
78 if (osgiBundles.length() != 0)
79 osgiBundles.append(',');
80 osgiBundles.append(location.replace('/', File.separatorChar));
81 }
82 }
83
84 getSystemProperties().setProperty("osgi.bundles",
85 osgiBundles.toString());
86 getSystemProperties().setProperty("slc.osgi.locations",
87 osgiLocations.toString());
88 }
89
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);
98 }
99
100 protected Resource asResource(String location) {
101 // Resource res = resourceLoader.getResource(location);
102
103 final Resource res;
104 if (location.startsWith("file:")) {
105 File file = new File(location.substring("file:".length()));
106 if (!file.exists())
107 throw new SlcException("File " + file + " does not exist");
108
109 try {
110 res = new FileSystemResource(file.getCanonicalFile());
111 } catch (IOException e) {
112 throw new SlcException("Cannot create resource based on "
113 + file, e);
114 }
115 } else
116 res = resourceLoader.getResource(location);
117
118 if (log.isDebugEnabled())
119 log.debug("Converted " + location + " to " + res);
120 return res;
121 }
122
123 public void setBundleContext(BundleContext bundleContext) {
124 this.bundleContext = bundleContext;
125 }
126
127 public void setResourceLoader(ResourceLoader resourceLoader) {
128 this.resourceLoader = resourceLoader;
129 }
130
131 public void setOsgibootBundleName(String osgibootBundleName) {
132 this.osgibootBundleName = osgibootBundleName;
133 }
134
135 public void setXmlapisBundleName(String xmlapisBundleName) {
136 this.xmlapisBundleName = xmlapisBundleName;
137 }
138
139 public void setXercesBundleName(String xercesBundleName) {
140 this.xercesBundleName = xercesBundleName;
141 }
142
143 public void setOsgiFramework(Resource osgiFramework) {
144 this.osgiFramework = osgiFramework;
145 }
146
147 public void setEquinoxBundleName(String equinoxBundleName) {
148 this.equinoxBundleName = equinoxBundleName;
149 }
150
151 public void setPrependXmlJars(Boolean prependXmlJars) {
152 this.prependXmlJars = prependXmlJars;
153 }
154
155 }