]> git.argeo.org Git - gpl/argeo-slc.git/blob - DetachedLauncher.java
daf99b374e71c64e54e851c9a62cab171979936d
[gpl/argeo-slc.git] / 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.osgi.boot";
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 if (log.isDebugEnabled())
67 log.debug("Exclude bundle " + name);
68 continue bundles;// skip excluded
69 }
70
71 String originalLocation = bundle.getLocation();
72 if (log.isTraceEnabled())
73 log.trace("Original location of bundle " + name + ": "
74 + originalLocation);
75 String location = removeInitialReference(originalLocation);
76
77 // Special bundles
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));
86
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 argeo.osgi.locations: " + location);
96 } else {
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: "
103 + location);
104 }
105 }
106
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);
113 }
114
115 getSystemProperties().setProperty("osgi.bundles",
116 osgiBundles.toString());
117 getSystemProperties().setProperty("argeo.osgi.locations",
118 osgiLocations.toString());
119
120 super.afterPropertiesSet();
121 }
122
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);
129 return location;
130 }
131
132 protected Resource asResource(String location) {
133 // Resource res = resourceLoader.getResource(location);
134
135 final Resource res;
136 if (location.startsWith("file:")) {
137 File file = new File(location.substring("file:".length()));
138 if (!file.exists())
139 throw new SlcException("File " + file + " does not exist");
140
141 try {
142 res = new FileSystemResource(file.getCanonicalFile());
143 } catch (IOException e) {
144 throw new SlcException("Cannot create resource based on "
145 + file, e);
146 }
147 } else
148 res = resourceLoader.getResource(location);
149
150 if (log.isDebugEnabled())
151 log.debug("Converted " + location + " to " + res);
152 return res;
153 }
154
155 public void setBundleContext(BundleContext bundleContext) {
156 this.bundleContext = bundleContext;
157 }
158
159 public void setResourceLoader(ResourceLoader resourceLoader) {
160 this.resourceLoader = resourceLoader;
161 }
162
163 public void setOsgibootBundleName(String osgibootBundleName) {
164 this.osgibootBundleName = osgibootBundleName;
165 }
166
167 public void setXmlapisBundleName(String xmlapisBundleName) {
168 this.xmlapisBundleName = xmlapisBundleName;
169 }
170
171 public void setXercesBundleName(String xercesBundleName) {
172 this.xercesBundleName = xercesBundleName;
173 }
174
175 public void setOsgiFramework(Resource osgiFramework) {
176 this.osgiFramework = osgiFramework;
177 }
178
179 public void setEquinoxBundleName(String equinoxBundleName) {
180 this.equinoxBundleName = equinoxBundleName;
181 }
182
183 public void setPrependXmlJars(Boolean prependXmlJars) {
184 this.prependXmlJars = prependXmlJars;
185 }
186
187 public void setExcludeBundleNames(List<String> excludeBundleNames) {
188 this.excludeBundleNames = excludeBundleNames;
189 }
190
191 public void setIncludeBundleUrls(List<String> includeBundleUrls) {
192 this.includeBundleUrls = includeBundleUrls;
193 }
194
195 }