]> 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
d9ff2f6be118c7f2fbd84e01fae688f1fb2d1ad5
[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 import java.util.ArrayList;
6 import java.util.List;
7
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;
20
21 public class DetachedLauncher extends JvmProcess implements BundleContextAware,
22 InitializingBean, ResourceLoaderAware {
23 private final static Log log = LogFactory.getLog(DetachedLauncher.class);
24
25 private BundleContext bundleContext = null;
26 private ResourceLoader resourceLoader = null;
27
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";
33
34 private List<String> excludeBundleNames = new ArrayList<String>();
35 private List<String> includeBundleUrls = new ArrayList<String>();
36
37 /**
38 * Required by Spring for JDK 1.4. see
39 * http://forum.springsource.org/showthread.php?t=74555
40 */
41 private Boolean prependXmlJars = false;
42
43 public DetachedLauncher() {
44 // Override defaults
45 setSynchronous(false);
46 setMainClass("org.argeo.slc.osgiboot.Launcher");
47 }
48
49 public void afterPropertiesSet() throws Exception {
50 if (bundleContext == null)
51 throw new SlcException("An OSGi bundle context is required.");
52
53 // Equinox jar
54 if (osgiFramework == null)
55 getClasspath()
56 .add(asResource(System.getProperty("osgi.framework")));
57 else
58 getClasspath().add(osgiFramework);
59
60 StringBuffer osgiBundles = new StringBuffer("");
61 StringBuffer osgiLocations = new StringBuffer("");
62 bundles: for (Bundle bundle : bundleContext.getBundles()) {
63 String name = bundle.getSymbolicName();
64
65 if (excludeBundleNames.contains(name))
66 continue bundles;// skip excluded
67
68 String location = bundle.getLocation();
69 location = removeInitialReference(location);
70
71 // Special bundles
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));
80
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('/',
86 File.separatorChar));
87 } else {
88 if (osgiBundles.length() != 0)
89 osgiBundles.append(',');
90 osgiBundles.append(location.replace('/', File.separatorChar));
91 }
92 }
93
94 for (String url : includeBundleUrls) {
95 if (osgiBundles.length() != 0)
96 osgiBundles.append(',');
97 osgiBundles.append(url);
98 }
99
100 getSystemProperties().setProperty("osgi.bundles",
101 osgiBundles.toString());
102 getSystemProperties().setProperty("slc.osgi.locations",
103 osgiLocations.toString());
104 }
105
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);
112 return location;
113 }
114
115 protected Resource asResource(String location) {
116 // Resource res = resourceLoader.getResource(location);
117
118 final Resource res;
119 if (location.startsWith("file:")) {
120 File file = new File(location.substring("file:".length()));
121 if (!file.exists())
122 throw new SlcException("File " + file + " does not exist");
123
124 try {
125 res = new FileSystemResource(file.getCanonicalFile());
126 } catch (IOException e) {
127 throw new SlcException("Cannot create resource based on "
128 + file, e);
129 }
130 } else
131 res = resourceLoader.getResource(location);
132
133 if (log.isDebugEnabled())
134 log.debug("Converted " + location + " to " + res);
135 return res;
136 }
137
138 public void setBundleContext(BundleContext bundleContext) {
139 this.bundleContext = bundleContext;
140 }
141
142 public void setResourceLoader(ResourceLoader resourceLoader) {
143 this.resourceLoader = resourceLoader;
144 }
145
146 public void setOsgibootBundleName(String osgibootBundleName) {
147 this.osgibootBundleName = osgibootBundleName;
148 }
149
150 public void setXmlapisBundleName(String xmlapisBundleName) {
151 this.xmlapisBundleName = xmlapisBundleName;
152 }
153
154 public void setXercesBundleName(String xercesBundleName) {
155 this.xercesBundleName = xercesBundleName;
156 }
157
158 public void setOsgiFramework(Resource osgiFramework) {
159 this.osgiFramework = osgiFramework;
160 }
161
162 public void setEquinoxBundleName(String equinoxBundleName) {
163 this.equinoxBundleName = equinoxBundleName;
164 }
165
166 public void setPrependXmlJars(Boolean prependXmlJars) {
167 this.prependXmlJars = prependXmlJars;
168 }
169
170 public void setExcludeBundleNames(List<String> excludeBundleNames) {
171 this.excludeBundleNames = excludeBundleNames;
172 }
173
174 public void setIncludeBundleUrls(List<String> includeBundleUrls) {
175 this.includeBundleUrls = includeBundleUrls;
176 }
177
178 }