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
.core
.execution
.AbstractExecutionModulesManager
;
18 import org
.argeo
.slc
.core
.execution
.DefaultExecutionFlowDescriptorConverter
;
19 import org
.argeo
.slc
.deploy
.ModuleDescriptor
;
20 import org
.argeo
.slc
.execution
.ExecutionContext
;
21 import org
.argeo
.slc
.execution
.ExecutionFlow
;
22 import org
.argeo
.slc
.execution
.ExecutionFlowDescriptor
;
23 import org
.argeo
.slc
.execution
.ExecutionFlowDescriptorConverter
;
24 import org
.argeo
.slc
.execution
.ExecutionModuleDescriptor
;
25 import org
.argeo
.slc
.execution
.ExecutionModulesListener
;
26 import org
.argeo
.slc
.process
.RealizedFlow
;
27 import org
.osgi
.framework
.Bundle
;
28 import org
.osgi
.framework
.Constants
;
29 import org
.osgi
.framework
.InvalidSyntaxException
;
30 import org
.osgi
.framework
.ServiceReference
;
31 import org
.osgi
.util
.tracker
.ServiceTracker
;
32 import org
.springframework
.beans
.factory
.DisposableBean
;
33 import org
.springframework
.beans
.factory
.InitializingBean
;
34 import org
.springframework
.osgi
.service
.importer
.OsgiServiceLifecycleListener
;
36 public class OsgiExecutionModulesManager
extends
37 AbstractExecutionModulesManager
implements InitializingBean
,
38 DisposableBean
, OsgiServiceLifecycleListener
{
41 // Force usage of vanilla Xalan when in OSGi
42 // We would like to do it in a cleaner way
43 // but the integration of Xalan and Xerces in the JRE
44 // makes it very difficult
45 // Suggestions welcome!
46 Properties systemProperties
= System
.getProperties();
47 // if (!systemProperties
48 // .containsKey("javax.xml.parsers.DocumentBuilderFactory"))
49 // System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
50 // "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
52 // (!systemProperties.containsKey("javax.xml.parsers.SAXParserFactory"))
53 // System.setProperty("javax.xml.parsers.SAXParserFactory",
54 // "org.apache.xerces.jaxp.SAXParserFactoryImpl");
56 .containsKey("javax.xml.transform.TransformerFactory"))
57 System
.setProperty("javax.xml.transform.TransformerFactory",
58 "org.apache.xalan.processor.TransformerFactoryImpl");
61 private final static String PROPERTY_CACHE_SERVICES
= "slc.osgi.execution.cacheServices";
63 private final static Log log
= LogFactory
64 .getLog(OsgiExecutionModulesManager
.class);
66 private BundlesManager bundlesManager
;
67 private ServiceTracker executionContextsTracker
;
68 private Map
<OsgiBundle
, ExecutionContext
> executionContexts
= new HashMap
<OsgiBundle
, ExecutionContext
>();
69 private Map
<OsgiBundle
, ExecutionFlowDescriptorConverter
> executionFlowDescriptorConverters
= new HashMap
<OsgiBundle
, ExecutionFlowDescriptorConverter
>();
70 private Map
<OsgiBundle
, Set
<ExecutionFlow
>> executionFlows
= new HashMap
<OsgiBundle
, Set
<ExecutionFlow
>>();
71 private ExecutionFlowDescriptorConverter defaultDescriptorConverter
= new DefaultExecutionFlowDescriptorConverter();
73 private Boolean useCachedServices
= Boolean
.parseBoolean(System
74 .getProperty(PROPERTY_CACHE_SERVICES
, "true"));
76 public synchronized ExecutionModuleDescriptor
getExecutionModuleDescriptor(
77 String moduleName
, String version
) {
78 ExecutionModuleDescriptor md
= new ExecutionModuleDescriptor();
79 if (useCachedServices
) {
80 OsgiBundle osgiBundle
= null;
81 BasicNameVersion nameVersion
= new BasicNameVersion(moduleName
,
83 bundles
: for (Iterator
<OsgiBundle
> iterator
= executionContexts
84 .keySet().iterator(); iterator
.hasNext();) {
85 OsgiBundle ob
= iterator
.next();
86 if (ob
.equals(nameVersion
)) {
91 if (osgiBundle
== null)
92 throw new SlcException("No execution module registered for "
94 md
.setName(osgiBundle
.getName());
95 md
.setVersion(osgiBundle
.getVersion());
96 md
.setLabel(osgiBundle
.getLabel());
97 md
.setDescription(osgiBundle
.getDescription());
99 md
.setName(moduleName
);
100 md
.setVersion(version
);
101 setMetadataFromBundle(md
, null);
103 ExecutionFlowDescriptorConverter executionFlowDescriptorConverter
= getExecutionFlowDescriptorConverter(
104 moduleName
, version
);
105 if (executionFlowDescriptorConverter
== null)
106 throw new SlcException("No flow converter found.");
107 executionFlowDescriptorConverter
.addFlowsToDescriptor(md
, listFlows(
108 moduleName
, version
));
112 public synchronized List
<ExecutionModuleDescriptor
> listExecutionModules() {
113 List
<ExecutionModuleDescriptor
> descriptors
= new ArrayList
<ExecutionModuleDescriptor
>();
115 if (useCachedServices
) {
116 for (Iterator
<OsgiBundle
> iterator
= executionContexts
.keySet()
117 .iterator(); iterator
.hasNext();) {
118 OsgiBundle osgiBundle
= iterator
.next();
119 ExecutionModuleDescriptor md
= new ExecutionModuleDescriptor();
120 setMetadataFromBundle(md
, bundlesManager
121 .findRelatedBundle(osgiBundle
));
125 ServiceReference
[] arr
= executionContextsTracker
126 .getServiceReferences();
128 log
.error("Tracker returned null.");
132 List
<ServiceReference
> srs
= Arrays
.asList(arr
);
133 // ServiceReference[] srs =
134 // executionContexts.getServiceReferences();
135 for (ServiceReference sr
: srs
) {
136 ExecutionModuleDescriptor md
= new ExecutionModuleDescriptor();
137 setMetadataFromBundle(md
, sr
.getBundle());
144 protected synchronized Map
<String
, ExecutionFlow
> listFlows(
145 String moduleName
, String moduleVersion
) {
147 Map
<String
, ExecutionFlow
> flows
= new HashMap
<String
, ExecutionFlow
>();
148 if (useCachedServices
) {
149 Set
<ExecutionFlow
> flowsT
= executionFlows
.get(new OsgiBundle(
150 moduleName
, moduleVersion
));
151 for (ExecutionFlow flow
: flowsT
)
152 flows
.put(flow
.getName(), flow
);
155 // TODO: use service trackers?
156 // String filter = OsgiFilterUtils.unifyFilter(ExecutionFlow.class,
159 String filter
= "(Bundle-SymbolicName=" + moduleName
+ ")";
160 ServiceReference
[] sfs
;
162 sfs
= bundlesManager
.getBundleContext().getServiceReferences(
163 ExecutionFlow
.class.getName(), filter
);
164 } catch (InvalidSyntaxException e
) {
165 throw new SlcException(
166 "Cannot retrieve service reference for flow " + filter
,
170 for (ServiceReference sf
: sfs
) {
171 ExecutionFlow flow
= (ExecutionFlow
) bundlesManager
172 .getBundleContext().getService(sf
);
173 flows
.put(flow
.getName(), flow
);
179 protected ExecutionFlow
findExecutionFlow(String moduleName
,
180 String moduleVersion
, String flowName
) {
181 String filter
= "(&(Bundle-SymbolicName=" + moduleName
182 + ")(org.springframework.osgi.bean.name=" + flowName
+ "))";
183 return bundlesManager
.getSingleServiceStrict(ExecutionFlow
.class,
187 protected ExecutionContext
findExecutionContext(String moduleName
,
188 String moduleVersion
) {
189 String filter
= "(&(Bundle-SymbolicName=" + moduleName
190 + ")(Bundle-Version=" + moduleVersion
+ "))";
191 return bundlesManager
.getSingleServiceStrict(ExecutionContext
.class,
195 protected ExecutionFlowDescriptorConverter
findExecutionFlowDescriptorConverter(
196 String moduleName
, String moduleVersion
) {
198 String filter
= "(&(Bundle-SymbolicName=" + moduleName
199 + ")(Bundle-Version=" + moduleVersion
+ "))";
200 return bundlesManager
.getSingleService(
201 ExecutionFlowDescriptorConverter
.class, filter
);
204 public void setBundlesManager(BundlesManager bundlesManager
) {
205 this.bundlesManager
= bundlesManager
;
208 public void afterPropertiesSet() throws Exception
{
209 if (!useCachedServices
)
210 executionContextsTracker
= bundlesManager
211 .newTracker(ExecutionContext
.class);
214 public void destroy() throws Exception
{
215 if (executionContextsTracker
!= null)
216 executionContextsTracker
.close();
220 * Builds a minimal realized flow, based on the provided information
221 * (typically from the command line).
224 * a bundle id, or a pattern contained in a bundle symbolic name
226 * the execution flow name
227 * @return a minimal realized flow, to be used in an execution
229 public RealizedFlow
findRealizedFlow(String module
, String executionName
) {
230 // First check whether we have a bundleId
231 Long bundleId
= null;
233 bundleId
= Long
.parseLong(module
);
234 } catch (NumberFormatException e
) {
238 // Look for bundle names containing pattern
239 OsgiBundle bundle
= null;
240 if (bundleId
!= null) {
241 bundle
= bundlesManager
.getBundle(bundleId
);
243 bundle
= bundlesManager
.findFromPattern(module
);
246 if (bundle
!= null) {
247 RealizedFlow launch
= new RealizedFlow();
248 launch
.setModuleName(bundle
.getName());
249 launch
.setModuleVersion(bundle
.getVersion());
250 ExecutionFlowDescriptor descriptor
= new ExecutionFlowDescriptor();
251 descriptor
.setName(executionName
);
252 launch
.setFlowDescriptor(descriptor
);
256 .warn("Could not find any execution module matching these requirements.");
261 public void updateAndExecute(RealizedFlow realizedFlow
) {
262 OsgiBundle osgiBundle
= new OsgiBundle(realizedFlow
);
263 bundlesManager
.upgradeSynchronous(osgiBundle
);
264 execute(realizedFlow
);
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 void execute(RealizedFlow realizedFlow
) {
287 if (log
.isTraceEnabled())
288 log
.trace("Executing " + realizedFlow
);
290 String moduleName
= realizedFlow
.getModuleName();
291 String moduleVersion
= realizedFlow
.getModuleVersion();
293 Map
<?
extends String
, ?
extends Object
> variablesToAdd
= getExecutionFlowDescriptorConverter(
294 moduleName
, moduleVersion
).convertValues(
295 realizedFlow
.getFlowDescriptor());
296 ExecutionContext executionContext
= findExecutionContext(moduleName
,
298 for (String key
: variablesToAdd
.keySet())
299 executionContext
.setVariable(key
, variablesToAdd
.get(key
));
301 ExecutionFlow flow
= findExecutionFlow(moduleName
, moduleVersion
,
302 realizedFlow
.getFlowDescriptor().getName());
305 // Actually runs the flow, IN THIS THREAD
313 public ModuleDescriptor
getModuleDescriptor(String moduleName
,
315 return getExecutionModuleDescriptor(moduleName
, version
);
318 public List
<ModuleDescriptor
> listModules() {
319 Bundle
[] bundles
= bundlesManager
.getBundleContext().getBundles();
320 List
<ModuleDescriptor
> lst
= new ArrayList
<ModuleDescriptor
>();
321 for (Bundle bundle
: bundles
) {
322 ModuleDescriptor moduleDescriptor
= new ModuleDescriptor();
323 setMetadataFromBundle(moduleDescriptor
, bundle
);
324 lst
.add(moduleDescriptor
);
329 protected void setMetadataFromBundle(ModuleDescriptor md
, Bundle bundle
) {
332 if (md
.getName() == null || md
.getVersion() == null)
333 throw new SlcException("Name and version not available.");
335 Bundle
[] bundles
= bundlesManager
.getBundleContext().getBundles();
336 for (Bundle b
: bundles
) {
337 if (b
.getSymbolicName().equals(md
.getName())
338 && md
.getVersion().equals(
339 getHeaderSafe(b
, Constants
.BUNDLE_VERSION
))) {
348 throw new SlcException("Cannot find bundle.");
350 md
.setName(bdl
.getSymbolicName());
351 md
.setVersion(getHeaderSafe(bdl
, Constants
.BUNDLE_VERSION
));
352 md
.setLabel(getHeaderSafe(bdl
, Constants
.BUNDLE_NAME
));
353 md
.setDescription(getHeaderSafe(bdl
, Constants
.BUNDLE_DESCRIPTION
));
356 private String
getHeaderSafe(Bundle bundle
, Object key
) {
357 Object obj
= bundle
.getHeaders().get(key
);
361 return obj
.toString();
364 @SuppressWarnings("unchecked")
365 public synchronized void bind(Object service
, Map properties
)
367 if (service
instanceof ExecutionContext
) {
368 ExecutionContext executionContext
= (ExecutionContext
) service
;
369 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
370 Bundle bundle
= bundlesManager
.findRelatedBundle(osgiBundle
);
371 osgiBundle
.setLabel(getHeaderSafe(bundle
, Constants
.BUNDLE_NAME
));
372 osgiBundle
.setDescription(getHeaderSafe(bundle
,
373 Constants
.BUNDLE_DESCRIPTION
));
374 executionContexts
.put(osgiBundle
, executionContext
);
375 if (log
.isTraceEnabled())
376 log
.debug("Registered execution context from " + osgiBundle
);
378 for (ExecutionModulesListener listener
: getExecutionModulesListeners())
379 listener
.executionModuleAdded(osgiBundle
, executionContext
);
381 } else if (service
instanceof ExecutionFlow
) {
382 ExecutionFlow executionFlow
= (ExecutionFlow
) service
;
383 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
384 if (!executionFlows
.containsKey(osgiBundle
)) {
385 executionFlows
.put(osgiBundle
, new HashSet());
387 executionFlows
.get(osgiBundle
).add(executionFlow
);
388 if (log
.isTraceEnabled())
390 .debug("Registered " + executionFlow
+ " from "
392 for (ExecutionModulesListener listener
: getExecutionModulesListeners())
393 listener
.executionFlowAdded(osgiBundle
, executionFlow
);
395 } else if (service
instanceof ExecutionFlowDescriptorConverter
) {
396 ExecutionFlowDescriptorConverter executionFlowDescriptorConverter
= (ExecutionFlowDescriptorConverter
) service
;
397 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
398 executionFlowDescriptorConverters
.put(osgiBundle
,
399 executionFlowDescriptorConverter
);
400 if (log
.isTraceEnabled())
402 .debug("Registered execution flow descriptor converter from "
409 @SuppressWarnings("unchecked")
410 public synchronized void unbind(Object service
, Map properties
)
412 if (service
instanceof ExecutionContext
) {
413 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
414 if (executionContexts
.containsKey(osgiBundle
)) {
415 ExecutionContext executionContext
= executionContexts
417 if (log
.isTraceEnabled())
418 log
.debug("Removed execution context from " + osgiBundle
);
420 for (ExecutionModulesListener listener
: getExecutionModulesListeners())
421 listener
.executionModuleRemoved(osgiBundle
,
424 } else if (service
instanceof ExecutionFlow
) {
425 ExecutionFlow executionFlow
= (ExecutionFlow
) service
;
426 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
427 if (executionFlows
.containsKey(osgiBundle
)) {
428 Set flows
= executionFlows
.get(osgiBundle
);
429 flows
.remove(executionFlow
);
430 if (log
.isTraceEnabled())
431 log
.debug("Removed " + executionFlow
+ " from "
433 if (flows
.size() == 0) {
434 executionFlows
.remove(osgiBundle
);
435 if (log
.isTraceEnabled())
436 log
.debug("Removed flows set from " + osgiBundle
);
438 for (ExecutionModulesListener listener
: getExecutionModulesListeners())
439 listener
.executionFlowRemoved(osgiBundle
, executionFlow
);
441 } else if (service
instanceof ExecutionFlowDescriptorConverter
) {
442 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
443 if (executionFlowDescriptorConverters
.containsKey(osgiBundle
)) {
444 executionFlowDescriptorConverters
.remove(osgiBundle
);
445 if (log
.isTraceEnabled())
447 .debug("Removed execution flow descriptor converter from "
455 @SuppressWarnings("unchecked")
456 private OsgiBundle
asOsgiBundle(Map properties
) {
457 String bundleSymbolicName
= checkAndGet(Constants
.BUNDLE_SYMBOLICNAME
,
459 String bundleVersion
= checkAndGet(Constants
.BUNDLE_VERSION
, properties
);
460 return new OsgiBundle(bundleSymbolicName
, bundleVersion
);
463 @SuppressWarnings("unchecked")
464 private String
checkAndGet(Object key
, Map properties
) {
465 if (!properties
.containsKey(key
) || properties
.get(key
) == null)
466 throw new SlcException(key
+ " not set in " + properties
);
468 return properties
.get(key
).toString();
471 public void setDefaultDescriptorConverter(
472 ExecutionFlowDescriptorConverter defaultDescriptorConverter
) {
473 this.defaultDescriptorConverter
= defaultDescriptorConverter
;