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
.osgi
;
18 import java
.lang
.management
.ManagementFactory
;
19 import java
.util
.ArrayList
;
20 import java
.util
.Dictionary
;
21 import java
.util
.Enumeration
;
22 import java
.util
.HashMap
;
23 import java
.util
.HashSet
;
24 import java
.util
.Iterator
;
25 import java
.util
.List
;
29 import javax
.management
.MBeanServer
;
30 import javax
.management
.ObjectName
;
31 import javax
.management
.StandardMBean
;
33 import org
.apache
.commons
.logging
.Log
;
34 import org
.apache
.commons
.logging
.LogFactory
;
35 import org
.argeo
.slc
.BasicNameVersion
;
36 import org
.argeo
.slc
.NameVersion
;
37 import org
.argeo
.slc
.SlcException
;
38 import org
.argeo
.slc
.core
.execution
.AbstractExecutionModulesManager
;
39 import org
.argeo
.slc
.core
.execution
.DefaultExecutionFlowDescriptorConverter
;
40 import org
.argeo
.slc
.deploy
.Module
;
41 import org
.argeo
.slc
.deploy
.ModuleDescriptor
;
42 import org
.argeo
.slc
.execution
.ExecutionContext
;
43 import org
.argeo
.slc
.execution
.ExecutionFlow
;
44 import org
.argeo
.slc
.execution
.ExecutionFlowDescriptor
;
45 import org
.argeo
.slc
.execution
.ExecutionFlowDescriptorConverter
;
46 import org
.argeo
.slc
.execution
.ExecutionModuleDescriptor
;
47 import org
.argeo
.slc
.execution
.ExecutionModulesListener
;
48 import org
.argeo
.slc
.process
.RealizedFlow
;
49 import org
.osgi
.framework
.Bundle
;
50 import org
.osgi
.framework
.BundleEvent
;
51 import org
.osgi
.framework
.BundleException
;
52 import org
.osgi
.framework
.BundleListener
;
53 import org
.osgi
.framework
.Constants
;
54 import org
.osgi
.framework
.launch
.Framework
;
55 import org
.springframework
.context
.ApplicationContext
;
56 import org
.springframework
.osgi
.service
.importer
.OsgiServiceLifecycleListener
;
58 /** Execution modules manager implementation based on an OSGi runtime. */
59 public class OsgiExecutionModulesManager
extends
60 AbstractExecutionModulesManager
implements
61 OsgiServiceLifecycleListener
, BundleListener
{
63 private final static Log log
= LogFactory
64 .getLog(OsgiExecutionModulesManager
.class);
66 private BundlesManager bundlesManager
;
67 private Map
<OsgiBundle
, ExecutionContext
> executionContexts
= new HashMap
<OsgiBundle
, ExecutionContext
>();
68 private Map
<OsgiBundle
, ExecutionFlowDescriptorConverter
> executionFlowDescriptorConverters
= new HashMap
<OsgiBundle
, ExecutionFlowDescriptorConverter
>();
69 private Map
<OsgiBundle
, Set
<ExecutionFlow
>> executionFlows
= new HashMap
<OsgiBundle
, Set
<ExecutionFlow
>>();
70 private ExecutionFlowDescriptorConverter defaultDescriptorConverter
= new DefaultExecutionFlowDescriptorConverter();
72 private List
<ExecutionModulesListener
> executionModulesListeners
= new ArrayList
<ExecutionModulesListener
>();
74 private Boolean registerFlowsToJmx
= false;
76 public void init() throws Exception
{
77 bundlesManager
.getBundleContext().addBundleListener(this);
79 final String module
= System
.getProperty(UNIQUE_LAUNCH_MODULE_PROPERTY
);
80 final String flow
= System
.getProperty(UNIQUE_LAUNCH_FLOW_PROPERTY
);
82 // launch a flow and stops
83 new Thread("Unique Flow") {
86 executeFlowAndExit(module
, null, flow
);
92 public void destroy() {
93 bundlesManager
.getBundleContext().removeBundleListener(this);
96 /** Executes a single flow and <b>stops the JVM</b> */
97 protected void executeFlowAndExit(final String module
,
98 final String version
, final String flow
) {
99 if (log
.isDebugEnabled())
100 log
.debug("Launch unique flow " + flow
+ " from module " + module
);
102 OsgiBundle osgiBundle
= bundlesManager
.findFromPattern(module
);
103 Bundle moduleBundle
= bundlesManager
.findRelatedBundle(osgiBundle
);
104 bundlesManager
.startSynchronous(moduleBundle
);
105 RealizedFlow lastLaunch
= findRealizedFlow(module
, flow
);
106 if (lastLaunch
== null)
107 throw new SlcException("Cannot find launch for " + module
+ " "
110 } catch (Exception e
) {
112 "Error in unique flow " + flow
+ " from module " + module
,
115 if (log
.isDebugEnabled())
116 log
.debug("Shutdown OSGi runtime...");
117 Framework framework
= (Framework
) bundlesManager
.getBundleContext()
120 // shutdown framework
122 // wait 1 min for shutdown
123 framework
.waitForStop(60 * 1000);
126 } catch (Exception e
) {
133 public synchronized ExecutionModuleDescriptor
getExecutionModuleDescriptor(
134 String moduleName
, String version
) {
135 ExecutionModuleDescriptor md
= new ExecutionModuleDescriptor();
136 OsgiBundle osgiBundle
= null;
137 BasicNameVersion nameVersion
= new BasicNameVersion(moduleName
, version
);
138 bundles
: for (Iterator
<OsgiBundle
> iterator
= executionContexts
139 .keySet().iterator(); iterator
.hasNext();) {
140 OsgiBundle ob
= iterator
.next();
141 if (ob
.equals(nameVersion
)) {
146 if (osgiBundle
== null)
147 throw new SlcException("No execution module registered for "
149 md
.setName(osgiBundle
.getName());
150 md
.setVersion(osgiBundle
.getVersion());
151 md
.setTitle(osgiBundle
.getTitle());
152 md
.setDescription(osgiBundle
.getDescription());
154 ExecutionFlowDescriptorConverter executionFlowDescriptorConverter
= getExecutionFlowDescriptorConverter(
155 moduleName
, version
);
156 if (executionFlowDescriptorConverter
== null)
157 throw new SlcException("No flow converter found.");
158 executionFlowDescriptorConverter
.addFlowsToDescriptor(md
,
159 listFlows(moduleName
, version
));
163 public synchronized List
<ExecutionModuleDescriptor
> listExecutionModules() {
164 List
<ExecutionModuleDescriptor
> descriptors
= new ArrayList
<ExecutionModuleDescriptor
>();
166 for (Iterator
<OsgiBundle
> iterator
= executionContexts
.keySet()
167 .iterator(); iterator
.hasNext();) {
168 OsgiBundle osgiBundle
= iterator
.next();
169 ExecutionModuleDescriptor md
= new ExecutionModuleDescriptor();
170 setMetadataFromBundle(md
,
171 bundlesManager
.findRelatedBundle(osgiBundle
));
177 protected synchronized Map
<String
, ExecutionFlow
> listFlows(
178 String moduleName
, String moduleVersion
) {
180 Map
<String
, ExecutionFlow
> flows
= new HashMap
<String
, ExecutionFlow
>();
181 OsgiBundle key
= new OsgiBundle(moduleName
, moduleVersion
);
182 if (!executionFlows
.containsKey(key
))
184 Set
<ExecutionFlow
> flowsT
= executionFlows
.get(key
);
185 for (ExecutionFlow flow
: flowsT
)
186 flows
.put(flow
.getName(), flow
);
190 protected ExecutionFlow
findExecutionFlow(String moduleName
,
191 String moduleVersion
, String flowName
) {
192 String filter
= "(&(Bundle-SymbolicName=" + moduleName
193 + ")(org.springframework.osgi.bean.name=" + flowName
+ "))";
194 return bundlesManager
.getSingleServiceStrict(ExecutionFlow
.class,
198 protected ExecutionContext
findExecutionContext(String moduleName
,
199 String moduleVersion
) {
200 String filter
= "(&(Bundle-SymbolicName=" + moduleName
201 + ")(Bundle-Version=" + moduleVersion
+ "))";
202 return bundlesManager
.getSingleServiceStrict(ExecutionContext
.class,
206 protected ExecutionFlowDescriptorConverter
findExecutionFlowDescriptorConverter(
207 String moduleName
, String moduleVersion
) {
208 String filter
= "(&(Bundle-SymbolicName=" + moduleName
209 + ")(Bundle-Version=" + moduleVersion
+ "))";
210 return bundlesManager
.getSingleService(
211 ExecutionFlowDescriptorConverter
.class, filter
, false);
215 * Builds a minimal realized flow, based on the provided information
216 * (typically from the command line).
219 * a bundle id, or a pattern contained in a bundle symbolic name
221 * the execution flow name
222 * @return a minimal realized flow, to be used in an execution
224 public RealizedFlow
findRealizedFlow(String module
, String executionName
) {
225 // First check whether we have a bundleId
226 Long bundleId
= null;
228 bundleId
= Long
.parseLong(module
);
229 } catch (NumberFormatException e
) {
233 // Look for bundle names containing pattern
234 OsgiBundle bundle
= null;
235 if (bundleId
!= null) {
236 bundle
= bundlesManager
.getBundle(bundleId
);
238 bundle
= bundlesManager
.findFromPattern(module
);
241 if (bundle
!= null) {
242 RealizedFlow launch
= new RealizedFlow();
243 launch
.setModuleName(bundle
.getName());
244 launch
.setModuleVersion(bundle
.getVersion());
245 ExecutionFlowDescriptor descriptor
= new ExecutionFlowDescriptor();
246 descriptor
.setName(executionName
);
247 launch
.setFlowDescriptor(descriptor
);
250 log
.warn("Could not find any execution module matching these requirements.");
255 public void upgrade(NameVersion nameVersion
) {
256 OsgiBundle osgiBundle
= new OsgiBundle(nameVersion
);
257 bundlesManager
.upgradeSynchronous(osgiBundle
);
260 protected synchronized ExecutionFlowDescriptorConverter
getExecutionFlowDescriptorConverter(
261 String moduleName
, String moduleVersion
) {
262 OsgiBundle osgiBundle
= new OsgiBundle(moduleName
, moduleVersion
);
263 return getExecutionFlowDescriptorConverter(osgiBundle
);
266 protected synchronized ExecutionFlowDescriptorConverter
getExecutionFlowDescriptorConverter(
267 OsgiBundle osgiBundle
) {
268 if (executionFlowDescriptorConverters
.containsKey(osgiBundle
))
269 return executionFlowDescriptorConverters
.get(osgiBundle
);
271 return defaultDescriptorConverter
;
274 public ModuleDescriptor
getModuleDescriptor(String moduleName
,
276 return getExecutionModuleDescriptor(moduleName
, version
);
279 public List
<ModuleDescriptor
> listModules() {
280 Bundle
[] bundles
= bundlesManager
.getBundleContext().getBundles();
281 List
<ModuleDescriptor
> lst
= new ArrayList
<ModuleDescriptor
>();
282 for (Bundle bundle
: bundles
) {
283 ModuleDescriptor moduleDescriptor
= new ModuleDescriptor();
284 setMetadataFromBundle(moduleDescriptor
, bundle
);
285 lst
.add(moduleDescriptor
);
290 public void start(NameVersion nameVersion
) {
292 Bundle bundle
= bundlesManager
.findRelatedBundle(new OsgiBundle(
294 bundlesManager
.startSynchronous(bundle
);
295 if (isSpringInstrumented(bundle
)) {
296 // Wait for Spring application context to be ready
297 String filter
= "(Bundle-SymbolicName="
298 + bundle
.getSymbolicName() + ")";
300 bundlesManager
.getServiceRefSynchronous(
301 ApplicationContext
.class.getName(), filter
);
302 } catch (Exception e
) {
303 // stop if application context not found
308 } catch (Exception e
) {
309 throw new SlcException("Cannot start " + nameVersion
, e
);
313 /** Do it calmly in order to avoid NPE */
314 private Boolean
isSpringInstrumented(Bundle bundle
) {
315 Dictionary
<?
, ?
> headers
= bundle
.getHeaders();
316 if (headers
!= null && headers
.get("Spring-Context") != null)
318 Enumeration
<?
> springEntryPaths
= bundle
319 .getEntryPaths("/META-INF/spring");
320 if (springEntryPaths
!= null && springEntryPaths
.hasMoreElements())
325 public void stop(NameVersion nameVersion
) {
327 Bundle bundle
= bundlesManager
.findRelatedBundle(new OsgiBundle(
329 bundlesManager
.stopSynchronous(bundle
);
330 } catch (BundleException e
) {
331 throw new SlcException("Cannot stop " + nameVersion
, e
);
335 protected void setMetadataFromBundle(ModuleDescriptor md
, Bundle bundle
) {
338 if (md
.getName() == null || md
.getVersion() == null)
339 throw new SlcException("Name and version not available.");
341 Bundle
[] bundles
= bundlesManager
.getBundleContext().getBundles();
342 for (Bundle b
: bundles
) {
343 if (b
.getSymbolicName().equals(md
.getName())
344 && md
.getVersion().equals(
345 getHeaderSafe(b
, Constants
.BUNDLE_VERSION
))) {
354 throw new SlcException("Cannot find bundle.");
356 md
.setName(bdl
.getSymbolicName());
357 md
.setVersion(getHeaderSafe(bdl
, Constants
.BUNDLE_VERSION
));
358 md
.setTitle(getHeaderSafe(bdl
, Constants
.BUNDLE_NAME
));
359 md
.setDescription(getHeaderSafe(bdl
, Constants
.BUNDLE_DESCRIPTION
));
361 // copy manifets header to meta data
362 Dictionary
<?
, ?
> headers
= bundle
.getHeaders();
363 Enumeration
<?
> keys
= headers
.keys();
364 while (keys
.hasMoreElements()) {
365 Object key
= keys
.nextElement();
366 Object value
= headers
.get(key
);
368 md
.getMetadata().put(key
.toString(), value
.toString());
372 if (bundle
.getState() == Bundle
.ACTIVE
373 || bundle
.getState() == Bundle
.STARTING
)
376 md
.setStarted(false);
379 private String
getHeaderSafe(Bundle bundle
, Object key
) {
380 Object obj
= bundle
.getHeaders().get(key
);
384 return obj
.toString();
391 /** Registers an execution context. */
392 public synchronized void register(ExecutionContext executionContext
,
393 Map
<String
, String
> properties
) {
394 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
395 Bundle bundle
= bundlesManager
.findRelatedBundle(osgiBundle
);
396 osgiBundle
.setTitle(getHeaderSafe(bundle
, Constants
.BUNDLE_NAME
));
397 osgiBundle
.setDescription(getHeaderSafe(bundle
,
398 Constants
.BUNDLE_DESCRIPTION
));
399 executionContexts
.put(osgiBundle
, executionContext
);
400 if (log
.isTraceEnabled())
401 log
.trace("Registered execution context from " + osgiBundle
);
403 ModuleDescriptor md
= osgiBundle
.getModuleDescriptor();
405 for (ExecutionModulesListener listener
: executionModulesListeners
)
406 listener
.executionModuleAdded(md
);
409 /** Unregisters an execution context. */
410 public synchronized void unregister(ExecutionContext executionContext
,
411 Map
<String
, String
> properties
) {
412 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
413 if (executionContexts
.containsKey(osgiBundle
)) {
414 executionContexts
.remove(osgiBundle
);
415 if (log
.isTraceEnabled())
416 log
.trace("Removed execution context from " + osgiBundle
);
418 ModuleDescriptor md
= osgiBundle
.getModuleDescriptor();
419 md
.setStarted(false);
420 for (ExecutionModulesListener listener
: executionModulesListeners
)
421 listener
.executionModuleRemoved(md
);
425 /** Registers an execution flow. */
426 public synchronized void register(ExecutionFlow executionFlow
,
427 Map
<String
, String
> properties
) {
428 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
429 if (!executionFlows
.containsKey(osgiBundle
)) {
430 executionFlows
.put(osgiBundle
, new HashSet
<ExecutionFlow
>());
432 executionFlows
.get(osgiBundle
).add(executionFlow
);
433 if (log
.isTraceEnabled())
434 log
.trace("Registered " + executionFlow
+ " from " + osgiBundle
);
437 if (registerFlowsToJmx
)
438 registerMBean(osgiBundle
, executionFlow
);
439 ExecutionFlowDescriptorConverter efdc
= getExecutionFlowDescriptorConverter(osgiBundle
);
440 for (ExecutionModulesListener listener
: executionModulesListeners
)
441 listener
.executionFlowAdded(osgiBundle
.getModuleDescriptor(),
442 efdc
.getExecutionFlowDescriptor(executionFlow
));
445 /** Unregisters an execution flow. */
446 public synchronized void unregister(ExecutionFlow executionFlow
,
447 Map
<String
, String
> properties
) {
448 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
449 if (executionFlows
.containsKey(osgiBundle
)) {
450 Set
<ExecutionFlow
> flows
= executionFlows
.get(osgiBundle
);
451 flows
.remove(executionFlow
);
452 if (log
.isTraceEnabled())
453 log
.trace("Removed " + executionFlow
+ " from " + osgiBundle
);
454 if (flows
.size() == 0) {
455 executionFlows
.remove(osgiBundle
);
456 if (log
.isTraceEnabled())
457 log
.trace("Removed flows set from " + osgiBundle
);
461 if (registerFlowsToJmx
)
462 unregisterMBean(osgiBundle
, executionFlow
);
463 ExecutionFlowDescriptorConverter efdc
= getExecutionFlowDescriptorConverter(osgiBundle
);
464 for (ExecutionModulesListener listener
: executionModulesListeners
)
465 listener
.executionFlowRemoved(osgiBundle
.getModuleDescriptor(),
466 efdc
.getExecutionFlowDescriptor(executionFlow
));
470 /** Registers an execution module listener. */
471 public synchronized void register(
472 ExecutionModulesListener executionModulesListener
,
473 Map
<String
, String
> properties
) {
474 // sync with current state
475 for (OsgiBundle osgiBundle
: executionContexts
.keySet()) {
476 executionModulesListener
.executionModuleAdded(osgiBundle
477 .getModuleDescriptor());
479 for (OsgiBundle osgiBundle
: executionFlows
.keySet()) {
480 ExecutionFlowDescriptorConverter efdc
= getExecutionFlowDescriptorConverter(osgiBundle
);
481 for (ExecutionFlow executionFlow
: executionFlows
.get(osgiBundle
))
482 executionModulesListener
.executionFlowAdded(
483 osgiBundle
.getModuleDescriptor(),
484 efdc
.getExecutionFlowDescriptor(executionFlow
));
486 executionModulesListeners
.add(executionModulesListener
);
489 /** Unregisters an execution module listener. */
490 public synchronized void unregister(
491 ExecutionModulesListener executionModulesListener
,
492 Map
<String
, String
> properties
) {
493 executionModulesListeners
.remove(executionModulesListener
);
497 * INTERFACE IMPLEMENTATIONS
500 public void bundleChanged(BundleEvent evt
) {
501 Bundle bundle
= evt
.getBundle();
502 if (bundle
.getHeaders().get(
503 ExecutionModuleDescriptor
.SLC_EXECUTION_MODULE
) != null) {
504 OsgiBundle osgiBundle
= new OsgiBundle(bundle
);
505 if (evt
.getType() == BundleEvent
.INSTALLED
)
506 for (ExecutionModulesListener listener
: executionModulesListeners
)
507 listener
.executionModuleAdded(osgiBundle
508 .getModuleDescriptor());
509 else if (evt
.getType() == BundleEvent
.UNINSTALLED
)
510 for (ExecutionModulesListener listener
: executionModulesListeners
)
511 listener
.executionModuleRemoved(osgiBundle
512 .getModuleDescriptor());
517 @SuppressWarnings({ "rawtypes" })
518 public synchronized void bind(Object service
, Map properties
)
520 if (service
instanceof ExecutionFlowDescriptorConverter
) {
521 ExecutionFlowDescriptorConverter executionFlowDescriptorConverter
= (ExecutionFlowDescriptorConverter
) service
;
522 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
523 executionFlowDescriptorConverters
.put(osgiBundle
,
524 executionFlowDescriptorConverter
);
525 if (log
.isTraceEnabled())
526 log
.debug("Registered execution flow descriptor converter from "
533 @SuppressWarnings("rawtypes")
534 public synchronized void unbind(Object service
, Map properties
)
536 if (service
instanceof ExecutionFlowDescriptorConverter
) {
537 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
538 if (executionFlowDescriptorConverters
.containsKey(osgiBundle
)) {
539 executionFlowDescriptorConverters
.remove(osgiBundle
);
540 if (log
.isTraceEnabled())
541 log
.debug("Removed execution flow descriptor converter from "
552 protected MBeanServer
getMBeanServer() {
553 return ManagementFactory
.getPlatformMBeanServer();
556 public void registerMBean(Module module
, ExecutionFlow executionFlow
) {
558 StandardMBean mbean
= new StandardMBean(executionFlow
,
559 ExecutionFlow
.class);
560 getMBeanServer().registerMBean(mbean
,
561 flowMBeanName(module
, executionFlow
));
562 } catch (Exception e
) {
563 String msg
= "Cannot register execution flow " + executionFlow
565 throw new SlcException(msg
, e
);
569 public void unregisterMBean(Module module
, ExecutionFlow executionFlow
) {
571 getMBeanServer().unregisterMBean(
572 flowMBeanName(module
, executionFlow
));
573 } catch (Exception e
) {
574 String msg
= "Cannot unregister execution flow " + executionFlow
576 throw new SlcException(msg
, e
);
580 @SuppressWarnings("deprecation")
581 protected ObjectName
flowMBeanName(Module module
,
582 ExecutionFlow executionFlow
) {
583 String executionModulesPrefix
= "SLCExecutionModules";
584 String path
= executionFlow
.getPath();
585 String name
= executionFlow
.getName();
586 if (path
== null && name
.indexOf('/') >= 0) {
587 path
= name
.substring(0, name
.lastIndexOf('/'));
588 name
= name
.substring(name
.lastIndexOf('/'));
591 StringBuffer buf
= new StringBuffer(executionModulesPrefix
+ ":"
592 + "module=" + module
.getName() + " [" + module
.getVersion()
595 if (path
!= null && !path
.equals("")) {
597 for (String token
: path
.split("/")) {
598 if (!token
.equals("")) {
599 buf
.append("path").append(depth
).append('=');
600 // in order to have directories first
602 buf
.append(token
).append(',');
607 buf
.append("name=").append(name
);
609 return new ObjectName(buf
.toString());
610 } catch (Exception e
) {
611 throw new SlcException("Cannot generate object name based on "
619 @SuppressWarnings("rawtypes")
620 private OsgiBundle
asOsgiBundle(Map properties
) {
621 String bundleSymbolicName
= checkAndGet(Constants
.BUNDLE_SYMBOLICNAME
,
623 String bundleVersion
= checkAndGet(Constants
.BUNDLE_VERSION
, properties
);
624 return new OsgiBundle(bundleSymbolicName
, bundleVersion
);
627 @SuppressWarnings("rawtypes")
628 private String
checkAndGet(Object key
, Map properties
) {
629 if (!properties
.containsKey(key
) || properties
.get(key
) == null)
630 throw new SlcException(key
+ " not set in " + properties
);
632 return properties
.get(key
).toString();
635 public void setBundlesManager(BundlesManager bundlesManager
) {
636 this.bundlesManager
= bundlesManager
;
639 public void setDefaultDescriptorConverter(
640 ExecutionFlowDescriptorConverter defaultDescriptorConverter
) {
641 this.defaultDescriptorConverter
= defaultDescriptorConverter
;
644 public void setRegisterFlowsToJmx(Boolean registerFlowsToJmx
) {
645 this.registerFlowsToJmx
= registerFlowsToJmx
;