]> 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
Update headers
[gpl/argeo-slc.git] / runtime / org.argeo.slc.lib.detached / src / main / java / org / argeo / slc / lib / detached / DetachedLauncher.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.lib.detached;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.argeo.slc.SlcException;
26 import org.argeo.slc.core.execution.tasks.JvmProcess;
27 import org.osgi.framework.Bundle;
28 import org.osgi.framework.BundleContext;
29 import org.springframework.beans.factory.InitializingBean;
30 import org.springframework.context.ResourceLoaderAware;
31 import org.springframework.core.io.FileSystemResource;
32 import org.springframework.core.io.Resource;
33 import org.springframework.core.io.ResourceLoader;
34 import org.springframework.osgi.context.BundleContextAware;
35
36 public class DetachedLauncher extends JvmProcess implements BundleContextAware,
37 InitializingBean, ResourceLoaderAware {
38 private final static Log log = LogFactory.getLog(DetachedLauncher.class);
39
40 private BundleContext bundleContext = null;
41 private ResourceLoader resourceLoader = null;
42
43 private Resource osgiFramework = null;
44 private String osgibootBundleName = "org.argeo.osgi.boot";
45 private String equinoxBundleName = "org.eclipse.osgi";
46 private String xmlapisBundleName = "com.springsource.org.apache.xmlcommons";
47 private String xercesBundleName = "com.springsource.org.apache.xerces";
48
49 private List<String> excludeBundleNames = new ArrayList<String>();
50 private List<String> includeBundleUrls = new ArrayList<String>();
51
52 /**
53 * Required by Spring for JDK 1.4. see
54 * http://forum.springsource.org/showthread.php?t=74555
55 */
56 private Boolean prependXmlJars = false;
57
58 public DetachedLauncher() {
59 // Override defaults
60 setSynchronous(false);
61 setMainClass("org.argeo.osgi.boot.Launcher");
62 }
63
64 public void afterPropertiesSet() throws Exception {
65 if (bundleContext == null)
66 throw new SlcException("An OSGi bundle context is required.");
67
68 // Equinox jar
69 if (osgiFramework == null)
70 getClasspath()
71 .add(asResource(System.getProperty("osgi.framework")));
72 else
73 getClasspath().add(osgiFramework);
74
75 StringBuffer osgiBundles = new StringBuffer("");
76 StringBuffer osgiLocations = new StringBuffer("");
77 bundles: for (Bundle bundle : bundleContext.getBundles()) {
78 String name = bundle.getSymbolicName();
79
80 if (excludeBundleNames.contains(name)) {
81 if (log.isDebugEnabled())
82 log.debug("Exclude bundle " + name);
83 continue bundles;// skip excluded
84 }
85
86 String originalLocation = bundle.getLocation();
87 if (log.isTraceEnabled())
88 log.trace("Original location of bundle " + name + ": "
89 + originalLocation);
90 String location = removeInitialReference(originalLocation);
91
92 // Special bundles
93 if (osgibootBundleName.equals(name))
94 getClasspath().add(asResource(location));
95 else if (equinoxBundleName.equals(name))
96 continue bundles;// skip framework
97 else if (xmlapisBundleName.equals(name) && prependXmlJars)
98 getPBootClasspath().add(asResource(location));
99 else if (xercesBundleName.equals(name) && prependXmlJars)
100 getPBootClasspath().add(asResource(location));
101
102 if (location.startsWith("file:")) {
103 File file = new File(location.substring("file:".length()));
104 if (osgiLocations.length() != 0)
105 osgiLocations.append(File.pathSeparatorChar);
106 location = file.getPath().replace('/', File.separatorChar);
107 osgiLocations.append(location);
108 if (log.isTraceEnabled())
109 log.trace("Added bundle " + name
110 + " to argeo.osgi.locations: " + location);
111 } else {
112 if (osgiBundles.length() != 0)
113 osgiBundles.append(',');
114 location = location.replace('/', File.separatorChar);
115 osgiBundles.append(location);
116 if (log.isTraceEnabled())
117 log.trace("Added bundle " + name + " to osgi.bundles: "
118 + location);
119 }
120 }
121
122 for (String url : includeBundleUrls) {
123 if (osgiBundles.length() != 0)
124 osgiBundles.append(',');
125 osgiBundles.append(url);
126 if (log.isDebugEnabled())
127 log.debug("Include url" + url);
128 }
129
130 getSystemProperties().setProperty("osgi.bundles",
131 osgiBundles.toString());
132 getSystemProperties().setProperty("argeo.osgi.locations",
133 osgiLocations.toString());
134
135 super.afterPropertiesSet();
136 }
137
138 protected String removeInitialReference(String location) {
139 if (location.startsWith("initial@reference:file:"))
140 location = System.getProperty("osgi.install.area")
141 + location.substring("initial@reference:file:".length());
142 if (location.charAt(location.length() - 1) == '/')
143 location.substring(0, location.length() - 1);
144 return location;
145 }
146
147 protected Resource asResource(String location) {
148 // Resource res = resourceLoader.getResource(location);
149
150 final Resource res;
151 if (location.startsWith("file:")) {
152 File file = new File(location.substring("file:".length()));
153 if (!file.exists())
154 throw new SlcException("File " + file + " does not exist");
155
156 try {
157 res = new FileSystemResource(file.getCanonicalFile());
158 } catch (IOException e) {
159 throw new SlcException("Cannot create resource based on "
160 + file, e);
161 }
162 } else
163 res = resourceLoader.getResource(location);
164
165 if (log.isDebugEnabled())
166 log.debug("Converted " + location + " to " + res);
167 return res;
168 }
169
170 public void setBundleContext(BundleContext bundleContext) {
171 this.bundleContext = bundleContext;
172 }
173
174 public void setResourceLoader(ResourceLoader resourceLoader) {
175 this.resourceLoader = resourceLoader;
176 }
177
178 public void setOsgibootBundleName(String osgibootBundleName) {
179 this.osgibootBundleName = osgibootBundleName;
180 }
181
182 public void setXmlapisBundleName(String xmlapisBundleName) {
183 this.xmlapisBundleName = xmlapisBundleName;
184 }
185
186 public void setXercesBundleName(String xercesBundleName) {
187 this.xercesBundleName = xercesBundleName;
188 }
189
190 public void setOsgiFramework(Resource osgiFramework) {
191 this.osgiFramework = osgiFramework;
192 }
193
194 public void setEquinoxBundleName(String equinoxBundleName) {
195 this.equinoxBundleName = equinoxBundleName;
196 }
197
198 public void setPrependXmlJars(Boolean prependXmlJars) {
199 this.prependXmlJars = prependXmlJars;
200 }
201
202 public void setExcludeBundleNames(List<String> excludeBundleNames) {
203 this.excludeBundleNames = excludeBundleNames;
204 }
205
206 public void setIncludeBundleUrls(List<String> includeBundleUrls) {
207 this.includeBundleUrls = includeBundleUrls;
208 }
209
210 }