2 * Copyright (C) 2007-2012 Mathieu Baudier
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.lib
.detached
;
19 import java
.io
.IOException
;
20 import java
.util
.ArrayList
;
21 import java
.util
.List
;
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
;
36 public class DetachedLauncher
extends JvmProcess
implements BundleContextAware
,
37 InitializingBean
, ResourceLoaderAware
{
38 private final static Log log
= LogFactory
.getLog(DetachedLauncher
.class);
40 private BundleContext bundleContext
= null;
41 private ResourceLoader resourceLoader
= null;
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";
49 private List
<String
> excludeBundleNames
= new ArrayList
<String
>();
50 private List
<String
> includeBundleUrls
= new ArrayList
<String
>();
53 * Required by Spring for JDK 1.4. see
54 * http://forum.springsource.org/showthread.php?t=74555
56 private Boolean prependXmlJars
= false;
58 public DetachedLauncher() {
60 setSynchronous(false);
61 setMainClass("org.argeo.osgi.boot.Launcher");
64 public void afterPropertiesSet() throws Exception
{
65 if (bundleContext
== null)
66 throw new SlcException("An OSGi bundle context is required.");
69 if (osgiFramework
== null)
71 .add(asResource(System
.getProperty("osgi.framework")));
73 getClasspath().add(osgiFramework
);
75 StringBuffer osgiBundles
= new StringBuffer("");
76 StringBuffer osgiLocations
= new StringBuffer("");
77 bundles
: for (Bundle bundle
: bundleContext
.getBundles()) {
78 String name
= bundle
.getSymbolicName();
80 if (excludeBundleNames
.contains(name
)) {
81 if (log
.isDebugEnabled())
82 log
.debug("Exclude bundle " + name
);
83 continue bundles
;// skip excluded
86 String originalLocation
= bundle
.getLocation();
87 if (log
.isTraceEnabled())
88 log
.trace("Original location of bundle " + name
+ ": "
90 String location
= removeInitialReference(originalLocation
);
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
));
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
);
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: "
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
);
130 getSystemProperties().setProperty("osgi.bundles",
131 osgiBundles
.toString());
132 getSystemProperties().setProperty("argeo.osgi.locations",
133 osgiLocations
.toString());
135 super.afterPropertiesSet();
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);
147 protected Resource
asResource(String location
) {
148 // Resource res = resourceLoader.getResource(location);
151 if (location
.startsWith("file:")) {
152 File file
= new File(location
.substring("file:".length()));
154 throw new SlcException("File " + file
+ " does not exist");
157 res
= new FileSystemResource(file
.getCanonicalFile());
158 } catch (IOException e
) {
159 throw new SlcException("Cannot create resource based on "
163 res
= resourceLoader
.getResource(location
);
165 if (log
.isDebugEnabled())
166 log
.debug("Converted " + location
+ " to " + res
);
170 public void setBundleContext(BundleContext bundleContext
) {
171 this.bundleContext
= bundleContext
;
174 public void setResourceLoader(ResourceLoader resourceLoader
) {
175 this.resourceLoader
= resourceLoader
;
178 public void setOsgibootBundleName(String osgibootBundleName
) {
179 this.osgibootBundleName
= osgibootBundleName
;
182 public void setXmlapisBundleName(String xmlapisBundleName
) {
183 this.xmlapisBundleName
= xmlapisBundleName
;
186 public void setXercesBundleName(String xercesBundleName
) {
187 this.xercesBundleName
= xercesBundleName
;
190 public void setOsgiFramework(Resource osgiFramework
) {
191 this.osgiFramework
= osgiFramework
;
194 public void setEquinoxBundleName(String equinoxBundleName
) {
195 this.equinoxBundleName
= equinoxBundleName
;
198 public void setPrependXmlJars(Boolean prependXmlJars
) {
199 this.prependXmlJars
= prependXmlJars
;
202 public void setExcludeBundleNames(List
<String
> excludeBundleNames
) {
203 this.excludeBundleNames
= excludeBundleNames
;
206 public void setIncludeBundleUrls(List
<String
> includeBundleUrls
) {
207 this.includeBundleUrls
= includeBundleUrls
;