1 package org
.argeo
.slc
.osgi
;
3 import java
.util
.ArrayList
;
4 import java
.util
.Arrays
;
5 import java
.util
.HashMap
;
6 import java
.util
.HashSet
;
7 import java
.util
.Iterator
;
10 import java
.util
.Properties
;
13 import org
.apache
.commons
.logging
.Log
;
14 import org
.apache
.commons
.logging
.LogFactory
;
15 import org
.argeo
.slc
.SlcException
;
16 import org
.argeo
.slc
.build
.BasicNameVersion
;
17 import org
.argeo
.slc
.build
.NameVersion
;
18 import org
.argeo
.slc
.core
.execution
.AbstractExecutionModulesManager
;
19 import org
.argeo
.slc
.core
.execution
.DefaultExecutionFlowDescriptorConverter
;
20 import org
.argeo
.slc
.deploy
.ModuleDescriptor
;
21 import org
.argeo
.slc
.execution
.ExecutionContext
;
22 import org
.argeo
.slc
.execution
.ExecutionFlow
;
23 import org
.argeo
.slc
.execution
.ExecutionFlowDescriptor
;
24 import org
.argeo
.slc
.execution
.ExecutionFlowDescriptorConverter
;
25 import org
.argeo
.slc
.execution
.ExecutionModuleDescriptor
;
26 import org
.argeo
.slc
.execution
.ExecutionModulesListener
;
27 import org
.argeo
.slc
.process
.RealizedFlow
;
28 import org
.osgi
.framework
.Bundle
;
29 import org
.osgi
.framework
.Constants
;
30 import org
.osgi
.framework
.InvalidSyntaxException
;
31 import org
.osgi
.framework
.ServiceReference
;
32 import org
.osgi
.util
.tracker
.ServiceTracker
;
33 import org
.springframework
.beans
.factory
.DisposableBean
;
34 import org
.springframework
.beans
.factory
.InitializingBean
;
35 import org
.springframework
.osgi
.service
.importer
.OsgiServiceLifecycleListener
;
37 public class OsgiExecutionModulesManager
extends
38 AbstractExecutionModulesManager
implements InitializingBean
,
39 DisposableBean
, OsgiServiceLifecycleListener
{
42 // Force usage of vanilla Xalan when in OSGi
43 // We would like to do it in a cleaner way
44 // but the integration of Xalan and Xerces in the JRE
45 // makes it very difficult
46 // Suggestions welcome!
47 Properties systemProperties
= System
.getProperties();
48 // if (!systemProperties
49 // .containsKey("javax.xml.parsers.DocumentBuilderFactory"))
50 // System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
51 // "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
53 // (!systemProperties.containsKey("javax.xml.parsers.SAXParserFactory"))
54 // System.setProperty("javax.xml.parsers.SAXParserFactory",
55 // "org.apache.xerces.jaxp.SAXParserFactoryImpl");
57 .containsKey("javax.xml.transform.TransformerFactory"))
58 System
.setProperty("javax.xml.transform.TransformerFactory",
59 "org.apache.xalan.processor.TransformerFactoryImpl");
62 private final static String PROPERTY_CACHE_SERVICES
= "slc.osgi.execution.cacheServices";
64 private final static Log log
= LogFactory
65 .getLog(OsgiExecutionModulesManager
.class);
67 private BundlesManager bundlesManager
;
68 private ServiceTracker executionContextsTracker
;
69 private Map
<OsgiBundle
, ExecutionContext
> executionContexts
= new HashMap
<OsgiBundle
, ExecutionContext
>();
70 private Map
<OsgiBundle
, ExecutionFlowDescriptorConverter
> executionFlowDescriptorConverters
= new HashMap
<OsgiBundle
, ExecutionFlowDescriptorConverter
>();
71 private Map
<OsgiBundle
, Set
<ExecutionFlow
>> executionFlows
= new HashMap
<OsgiBundle
, Set
<ExecutionFlow
>>();
72 private ExecutionFlowDescriptorConverter defaultDescriptorConverter
= new DefaultExecutionFlowDescriptorConverter();
74 private Boolean useCachedServices
= Boolean
.parseBoolean(System
75 .getProperty(PROPERTY_CACHE_SERVICES
, "true"));
77 public synchronized ExecutionModuleDescriptor
getExecutionModuleDescriptor(
78 String moduleName
, String version
) {
79 ExecutionModuleDescriptor md
= new ExecutionModuleDescriptor();
80 if (useCachedServices
) {
81 OsgiBundle osgiBundle
= null;
82 BasicNameVersion nameVersion
= new BasicNameVersion(moduleName
,
84 bundles
: for (Iterator
<OsgiBundle
> iterator
= executionContexts
85 .keySet().iterator(); iterator
.hasNext();) {
86 OsgiBundle ob
= iterator
.next();
87 if (ob
.equals(nameVersion
)) {
92 if (osgiBundle
== null)
93 throw new SlcException("No execution module registered for "
95 md
.setName(osgiBundle
.getName());
96 md
.setVersion(osgiBundle
.getVersion());
97 md
.setLabel(osgiBundle
.getLabel());
98 md
.setDescription(osgiBundle
.getDescription());
100 md
.setName(moduleName
);
101 md
.setVersion(version
);
102 setMetadataFromBundle(md
, null);
104 ExecutionFlowDescriptorConverter executionFlowDescriptorConverter
= getExecutionFlowDescriptorConverter(
105 moduleName
, version
);
106 if (executionFlowDescriptorConverter
== null)
107 throw new SlcException("No flow converter found.");
108 executionFlowDescriptorConverter
.addFlowsToDescriptor(md
, listFlows(
109 moduleName
, version
));
113 public synchronized List
<ExecutionModuleDescriptor
> listExecutionModules() {
114 List
<ExecutionModuleDescriptor
> descriptors
= new ArrayList
<ExecutionModuleDescriptor
>();
116 if (useCachedServices
) {
117 for (Iterator
<OsgiBundle
> iterator
= executionContexts
.keySet()
118 .iterator(); iterator
.hasNext();) {
119 OsgiBundle osgiBundle
= iterator
.next();
120 ExecutionModuleDescriptor md
= new ExecutionModuleDescriptor();
121 setMetadataFromBundle(md
, bundlesManager
122 .findRelatedBundle(osgiBundle
));
126 ServiceReference
[] arr
= executionContextsTracker
127 .getServiceReferences();
129 log
.error("Tracker returned null.");
133 List
<ServiceReference
> srs
= Arrays
.asList(arr
);
134 // ServiceReference[] srs =
135 // executionContexts.getServiceReferences();
136 for (ServiceReference sr
: srs
) {
137 ExecutionModuleDescriptor md
= new ExecutionModuleDescriptor();
138 setMetadataFromBundle(md
, sr
.getBundle());
145 protected synchronized Map
<String
, ExecutionFlow
> listFlows(
146 String moduleName
, String moduleVersion
) {
148 Map
<String
, ExecutionFlow
> flows
= new HashMap
<String
, ExecutionFlow
>();
149 if (useCachedServices
) {
150 Set
<ExecutionFlow
> flowsT
= executionFlows
.get(new OsgiBundle(
151 moduleName
, moduleVersion
));
152 for (ExecutionFlow flow
: flowsT
)
153 flows
.put(flow
.getName(), flow
);
156 // TODO: use service trackers?
157 // String filter = OsgiFilterUtils.unifyFilter(ExecutionFlow.class,
160 String filter
= "(Bundle-SymbolicName=" + moduleName
+ ")";
161 ServiceReference
[] sfs
;
163 sfs
= bundlesManager
.getBundleContext().getServiceReferences(
164 ExecutionFlow
.class.getName(), filter
);
165 } catch (InvalidSyntaxException e
) {
166 throw new SlcException(
167 "Cannot retrieve service reference for flow " + filter
,
171 for (ServiceReference sf
: sfs
) {
172 ExecutionFlow flow
= (ExecutionFlow
) bundlesManager
173 .getBundleContext().getService(sf
);
174 flows
.put(flow
.getName(), flow
);
180 protected ExecutionFlow
findExecutionFlow(String moduleName
,
181 String moduleVersion
, String flowName
) {
182 String filter
= "(&(Bundle-SymbolicName=" + moduleName
183 + ")(org.springframework.osgi.bean.name=" + flowName
+ "))";
184 return bundlesManager
.getSingleServiceStrict(ExecutionFlow
.class,
188 protected ExecutionContext
findExecutionContext(String moduleName
,
189 String moduleVersion
) {
190 String filter
= "(&(Bundle-SymbolicName=" + moduleName
191 + ")(Bundle-Version=" + moduleVersion
+ "))";
192 return bundlesManager
.getSingleServiceStrict(ExecutionContext
.class,
196 protected ExecutionFlowDescriptorConverter
findExecutionFlowDescriptorConverter(
197 String moduleName
, String moduleVersion
) {
199 String filter
= "(&(Bundle-SymbolicName=" + moduleName
200 + ")(Bundle-Version=" + moduleVersion
+ "))";
201 return bundlesManager
.getSingleService(
202 ExecutionFlowDescriptorConverter
.class, filter
);
205 public void setBundlesManager(BundlesManager bundlesManager
) {
206 this.bundlesManager
= bundlesManager
;
209 public void afterPropertiesSet() throws Exception
{
210 if (!useCachedServices
)
211 executionContextsTracker
= bundlesManager
212 .newTracker(ExecutionContext
.class);
215 public void destroy() throws Exception
{
216 if (executionContextsTracker
!= null)
217 executionContextsTracker
.close();
221 * Builds a minimal realized flow, based on the provided information
222 * (typically from the command line).
225 * a bundle id, or a pattern contained in a bundle symbolic name
227 * the execution flow name
228 * @return a minimal realized flow, to be used in an execution
230 public RealizedFlow
findRealizedFlow(String module
, String executionName
) {
231 // First check whether we have a bundleId
232 Long bundleId
= null;
234 bundleId
= Long
.parseLong(module
);
235 } catch (NumberFormatException e
) {
239 // Look for bundle names containing pattern
240 OsgiBundle bundle
= null;
241 if (bundleId
!= null) {
242 bundle
= bundlesManager
.getBundle(bundleId
);
244 bundle
= bundlesManager
.findFromPattern(module
);
247 if (bundle
!= null) {
248 RealizedFlow launch
= new RealizedFlow();
249 launch
.setModuleName(bundle
.getName());
250 launch
.setModuleVersion(bundle
.getVersion());
251 ExecutionFlowDescriptor descriptor
= new ExecutionFlowDescriptor();
252 descriptor
.setName(executionName
);
253 launch
.setFlowDescriptor(descriptor
);
257 .warn("Could not find any execution module matching these requirements.");
262 public void upgrade(NameVersion nameVersion
) {
263 OsgiBundle osgiBundle
= new OsgiBundle(nameVersion
);
264 bundlesManager
.upgradeSynchronous(osgiBundle
);
267 protected synchronized ExecutionFlowDescriptorConverter
getExecutionFlowDescriptorConverter(
268 String moduleName
, String moduleVersion
) {
269 if (useCachedServices
) {
270 OsgiBundle osgiBundle
= new OsgiBundle(moduleName
, moduleVersion
);
271 if (executionFlowDescriptorConverters
.containsKey(osgiBundle
))
272 return executionFlowDescriptorConverters
.get(osgiBundle
);
274 return defaultDescriptorConverter
;
276 // Check whether a descriptor converter is published by this module
277 ExecutionFlowDescriptorConverter descriptorConverter
= findExecutionFlowDescriptorConverter(
278 moduleName
, moduleVersion
);
279 if (descriptorConverter
== null)
280 return defaultDescriptorConverter
;
282 return descriptorConverter
;
286 public ModuleDescriptor
getModuleDescriptor(String moduleName
,
288 return getExecutionModuleDescriptor(moduleName
, version
);
291 public List
<ModuleDescriptor
> listModules() {
292 Bundle
[] bundles
= bundlesManager
.getBundleContext().getBundles();
293 List
<ModuleDescriptor
> lst
= new ArrayList
<ModuleDescriptor
>();
294 for (Bundle bundle
: bundles
) {
295 ModuleDescriptor moduleDescriptor
= new ModuleDescriptor();
296 setMetadataFromBundle(moduleDescriptor
, bundle
);
297 lst
.add(moduleDescriptor
);
302 protected void setMetadataFromBundle(ModuleDescriptor md
, Bundle bundle
) {
305 if (md
.getName() == null || md
.getVersion() == null)
306 throw new SlcException("Name and version not available.");
308 Bundle
[] bundles
= bundlesManager
.getBundleContext().getBundles();
309 for (Bundle b
: bundles
) {
310 if (b
.getSymbolicName().equals(md
.getName())
311 && md
.getVersion().equals(
312 getHeaderSafe(b
, Constants
.BUNDLE_VERSION
))) {
321 throw new SlcException("Cannot find bundle.");
323 md
.setName(bdl
.getSymbolicName());
324 md
.setVersion(getHeaderSafe(bdl
, Constants
.BUNDLE_VERSION
));
325 md
.setLabel(getHeaderSafe(bdl
, Constants
.BUNDLE_NAME
));
326 md
.setDescription(getHeaderSafe(bdl
, Constants
.BUNDLE_DESCRIPTION
));
329 private String
getHeaderSafe(Bundle bundle
, Object key
) {
330 Object obj
= bundle
.getHeaders().get(key
);
334 return obj
.toString();
337 @SuppressWarnings("unchecked")
338 public synchronized void bind(Object service
, Map properties
)
340 if (service
instanceof ExecutionContext
) {
341 ExecutionContext executionContext
= (ExecutionContext
) service
;
342 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
343 Bundle bundle
= bundlesManager
.findRelatedBundle(osgiBundle
);
344 osgiBundle
.setLabel(getHeaderSafe(bundle
, Constants
.BUNDLE_NAME
));
345 osgiBundle
.setDescription(getHeaderSafe(bundle
,
346 Constants
.BUNDLE_DESCRIPTION
));
347 executionContexts
.put(osgiBundle
, executionContext
);
348 if (log
.isTraceEnabled())
349 log
.debug("Registered execution context from " + osgiBundle
);
351 for (ExecutionModulesListener listener
: getExecutionModulesListeners())
352 listener
.executionModuleAdded(osgiBundle
, executionContext
);
354 } else if (service
instanceof ExecutionFlow
) {
355 ExecutionFlow executionFlow
= (ExecutionFlow
) service
;
356 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
357 if (!executionFlows
.containsKey(osgiBundle
)) {
358 executionFlows
.put(osgiBundle
, new HashSet());
360 executionFlows
.get(osgiBundle
).add(executionFlow
);
361 if (log
.isTraceEnabled())
363 .debug("Registered " + executionFlow
+ " from "
365 for (ExecutionModulesListener listener
: getExecutionModulesListeners())
366 listener
.executionFlowAdded(osgiBundle
, executionFlow
);
368 } else if (service
instanceof ExecutionFlowDescriptorConverter
) {
369 ExecutionFlowDescriptorConverter executionFlowDescriptorConverter
= (ExecutionFlowDescriptorConverter
) service
;
370 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
371 executionFlowDescriptorConverters
.put(osgiBundle
,
372 executionFlowDescriptorConverter
);
373 if (log
.isTraceEnabled())
375 .debug("Registered execution flow descriptor converter from "
382 @SuppressWarnings("unchecked")
383 public synchronized void unbind(Object service
, Map properties
)
385 if (service
instanceof ExecutionContext
) {
386 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
387 if (executionContexts
.containsKey(osgiBundle
)) {
388 ExecutionContext executionContext
= executionContexts
390 if (log
.isTraceEnabled())
391 log
.debug("Removed execution context from " + osgiBundle
);
393 for (ExecutionModulesListener listener
: getExecutionModulesListeners())
394 listener
.executionModuleRemoved(osgiBundle
,
397 } else if (service
instanceof ExecutionFlow
) {
398 ExecutionFlow executionFlow
= (ExecutionFlow
) service
;
399 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
400 if (executionFlows
.containsKey(osgiBundle
)) {
401 Set flows
= executionFlows
.get(osgiBundle
);
402 flows
.remove(executionFlow
);
403 if (log
.isTraceEnabled())
404 log
.debug("Removed " + executionFlow
+ " from "
406 if (flows
.size() == 0) {
407 executionFlows
.remove(osgiBundle
);
408 if (log
.isTraceEnabled())
409 log
.debug("Removed flows set from " + osgiBundle
);
411 for (ExecutionModulesListener listener
: getExecutionModulesListeners())
412 listener
.executionFlowRemoved(osgiBundle
, executionFlow
);
414 } else if (service
instanceof ExecutionFlowDescriptorConverter
) {
415 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
416 if (executionFlowDescriptorConverters
.containsKey(osgiBundle
)) {
417 executionFlowDescriptorConverters
.remove(osgiBundle
);
418 if (log
.isTraceEnabled())
420 .debug("Removed execution flow descriptor converter from "
428 @SuppressWarnings("unchecked")
429 private OsgiBundle
asOsgiBundle(Map properties
) {
430 String bundleSymbolicName
= checkAndGet(Constants
.BUNDLE_SYMBOLICNAME
,
432 String bundleVersion
= checkAndGet(Constants
.BUNDLE_VERSION
, properties
);
433 return new OsgiBundle(bundleSymbolicName
, bundleVersion
);
436 @SuppressWarnings("unchecked")
437 private String
checkAndGet(Object key
, Map properties
) {
438 if (!properties
.containsKey(key
) || properties
.get(key
) == null)
439 throw new SlcException(key
+ " not set in " + properties
);
441 return properties
.get(key
).toString();
444 public void setDefaultDescriptorConverter(
445 ExecutionFlowDescriptorConverter defaultDescriptorConverter
) {
446 this.defaultDescriptorConverter
= defaultDescriptorConverter
;