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 boolean isSpringInstrumented
= bundle
.getEntryPaths(
296 "/META-INF/spring").hasMoreElements()
297 || bundle
.getHeaders().get("Spring-Context") == null;
298 if (isSpringInstrumented
) {
299 // Wait for Spring application context to be ready
300 String filter
= "(Bundle-SymbolicName="
301 + bundle
.getSymbolicName() + ")";
303 bundlesManager
.getServiceRefSynchronous(
304 ApplicationContext
.class.getName(), filter
);
305 } catch (Exception e
) {
306 // stop if application context not found
311 } catch (Exception e
) {
312 throw new SlcException("Cannot start " + nameVersion
, e
);
316 public void stop(NameVersion nameVersion
) {
318 Bundle bundle
= bundlesManager
.findRelatedBundle(new OsgiBundle(
320 bundlesManager
.stopSynchronous(bundle
);
321 } catch (BundleException e
) {
322 throw new SlcException("Cannot stop " + nameVersion
, e
);
326 protected void setMetadataFromBundle(ModuleDescriptor md
, Bundle bundle
) {
329 if (md
.getName() == null || md
.getVersion() == null)
330 throw new SlcException("Name and version not available.");
332 Bundle
[] bundles
= bundlesManager
.getBundleContext().getBundles();
333 for (Bundle b
: bundles
) {
334 if (b
.getSymbolicName().equals(md
.getName())
335 && md
.getVersion().equals(
336 getHeaderSafe(b
, Constants
.BUNDLE_VERSION
))) {
345 throw new SlcException("Cannot find bundle.");
347 md
.setName(bdl
.getSymbolicName());
348 md
.setVersion(getHeaderSafe(bdl
, Constants
.BUNDLE_VERSION
));
349 md
.setTitle(getHeaderSafe(bdl
, Constants
.BUNDLE_NAME
));
350 md
.setDescription(getHeaderSafe(bdl
, Constants
.BUNDLE_DESCRIPTION
));
352 // copy manifets header to meta data
353 Dictionary
<?
, ?
> headers
= bundle
.getHeaders();
354 Enumeration
<?
> keys
= headers
.keys();
355 while (keys
.hasMoreElements()) {
356 Object key
= keys
.nextElement();
357 Object value
= headers
.get(key
);
359 md
.getMetadata().put(key
.toString(), value
.toString());
363 if (bundle
.getState() == Bundle
.ACTIVE
364 || bundle
.getState() == Bundle
.STARTING
)
367 md
.setStarted(false);
370 private String
getHeaderSafe(Bundle bundle
, Object key
) {
371 Object obj
= bundle
.getHeaders().get(key
);
375 return obj
.toString();
382 /** Registers an execution context. */
383 public synchronized void register(ExecutionContext executionContext
,
384 Map
<String
, String
> properties
) {
385 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
386 Bundle bundle
= bundlesManager
.findRelatedBundle(osgiBundle
);
387 osgiBundle
.setTitle(getHeaderSafe(bundle
, Constants
.BUNDLE_NAME
));
388 osgiBundle
.setDescription(getHeaderSafe(bundle
,
389 Constants
.BUNDLE_DESCRIPTION
));
390 executionContexts
.put(osgiBundle
, executionContext
);
391 if (log
.isTraceEnabled())
392 log
.trace("Registered execution context from " + osgiBundle
);
394 ModuleDescriptor md
= osgiBundle
.getModuleDescriptor();
396 for (ExecutionModulesListener listener
: executionModulesListeners
)
397 listener
.executionModuleAdded(md
);
400 /** Unregisters an execution context. */
401 public synchronized void unregister(ExecutionContext executionContext
,
402 Map
<String
, String
> properties
) {
403 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
404 if (executionContexts
.containsKey(osgiBundle
)) {
405 executionContexts
.remove(osgiBundle
);
406 if (log
.isTraceEnabled())
407 log
.trace("Removed execution context from " + osgiBundle
);
409 ModuleDescriptor md
= osgiBundle
.getModuleDescriptor();
410 md
.setStarted(false);
411 for (ExecutionModulesListener listener
: executionModulesListeners
)
412 listener
.executionModuleRemoved(md
);
416 /** Registers an execution flow. */
417 public synchronized void register(ExecutionFlow executionFlow
,
418 Map
<String
, String
> properties
) {
419 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
420 if (!executionFlows
.containsKey(osgiBundle
)) {
421 executionFlows
.put(osgiBundle
, new HashSet
<ExecutionFlow
>());
423 executionFlows
.get(osgiBundle
).add(executionFlow
);
424 if (log
.isTraceEnabled())
425 log
.trace("Registered " + executionFlow
+ " from " + osgiBundle
);
428 if (registerFlowsToJmx
)
429 registerMBean(osgiBundle
, executionFlow
);
430 ExecutionFlowDescriptorConverter efdc
= getExecutionFlowDescriptorConverter(osgiBundle
);
431 for (ExecutionModulesListener listener
: executionModulesListeners
)
432 listener
.executionFlowAdded(osgiBundle
.getModuleDescriptor(),
433 efdc
.getExecutionFlowDescriptor(executionFlow
));
436 /** Unregisters an execution flow. */
437 public synchronized void unregister(ExecutionFlow executionFlow
,
438 Map
<String
, String
> properties
) {
439 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
440 if (executionFlows
.containsKey(osgiBundle
)) {
441 Set
<ExecutionFlow
> flows
= executionFlows
.get(osgiBundle
);
442 flows
.remove(executionFlow
);
443 if (log
.isTraceEnabled())
444 log
.trace("Removed " + executionFlow
+ " from " + osgiBundle
);
445 if (flows
.size() == 0) {
446 executionFlows
.remove(osgiBundle
);
447 if (log
.isTraceEnabled())
448 log
.trace("Removed flows set from " + osgiBundle
);
452 if (registerFlowsToJmx
)
453 unregisterMBean(osgiBundle
, executionFlow
);
454 ExecutionFlowDescriptorConverter efdc
= getExecutionFlowDescriptorConverter(osgiBundle
);
455 for (ExecutionModulesListener listener
: executionModulesListeners
)
456 listener
.executionFlowRemoved(osgiBundle
.getModuleDescriptor(),
457 efdc
.getExecutionFlowDescriptor(executionFlow
));
461 /** Registers an execution module listener. */
462 public synchronized void register(
463 ExecutionModulesListener executionModulesListener
,
464 Map
<String
, String
> properties
) {
465 // sync with current state
466 for (OsgiBundle osgiBundle
: executionContexts
.keySet()) {
467 executionModulesListener
.executionModuleAdded(osgiBundle
468 .getModuleDescriptor());
470 for (OsgiBundle osgiBundle
: executionFlows
.keySet()) {
471 ExecutionFlowDescriptorConverter efdc
= getExecutionFlowDescriptorConverter(osgiBundle
);
472 for (ExecutionFlow executionFlow
: executionFlows
.get(osgiBundle
))
473 executionModulesListener
.executionFlowAdded(
474 osgiBundle
.getModuleDescriptor(),
475 efdc
.getExecutionFlowDescriptor(executionFlow
));
477 executionModulesListeners
.add(executionModulesListener
);
480 /** Unregisters an execution module listener. */
481 public synchronized void unregister(
482 ExecutionModulesListener executionModulesListener
,
483 Map
<String
, String
> properties
) {
484 executionModulesListeners
.remove(executionModulesListener
);
488 * INTERFACE IMPLEMENTATIONS
491 public void bundleChanged(BundleEvent evt
) {
492 Bundle bundle
= evt
.getBundle();
493 if (bundle
.getHeaders().get(
494 ExecutionModuleDescriptor
.SLC_EXECUTION_MODULE
) != null) {
495 OsgiBundle osgiBundle
= new OsgiBundle(bundle
);
496 if (evt
.getType() == BundleEvent
.INSTALLED
)
497 for (ExecutionModulesListener listener
: executionModulesListeners
)
498 listener
.executionModuleAdded(osgiBundle
499 .getModuleDescriptor());
500 else if (evt
.getType() == BundleEvent
.UNINSTALLED
)
501 for (ExecutionModulesListener listener
: executionModulesListeners
)
502 listener
.executionModuleRemoved(osgiBundle
503 .getModuleDescriptor());
508 @SuppressWarnings({ "rawtypes" })
509 public synchronized void bind(Object service
, Map properties
)
511 if (service
instanceof ExecutionFlowDescriptorConverter
) {
512 ExecutionFlowDescriptorConverter executionFlowDescriptorConverter
= (ExecutionFlowDescriptorConverter
) service
;
513 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
514 executionFlowDescriptorConverters
.put(osgiBundle
,
515 executionFlowDescriptorConverter
);
516 if (log
.isTraceEnabled())
517 log
.debug("Registered execution flow descriptor converter from "
524 @SuppressWarnings("rawtypes")
525 public synchronized void unbind(Object service
, Map properties
)
527 if (service
instanceof ExecutionFlowDescriptorConverter
) {
528 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
529 if (executionFlowDescriptorConverters
.containsKey(osgiBundle
)) {
530 executionFlowDescriptorConverters
.remove(osgiBundle
);
531 if (log
.isTraceEnabled())
532 log
.debug("Removed execution flow descriptor converter from "
543 protected MBeanServer
getMBeanServer() {
544 return ManagementFactory
.getPlatformMBeanServer();
547 public void registerMBean(Module module
, ExecutionFlow executionFlow
) {
549 StandardMBean mbean
= new StandardMBean(executionFlow
,
550 ExecutionFlow
.class);
551 getMBeanServer().registerMBean(mbean
,
552 flowMBeanName(module
, executionFlow
));
553 } catch (Exception e
) {
554 String msg
= "Cannot register execution flow " + executionFlow
556 throw new SlcException(msg
, e
);
560 public void unregisterMBean(Module module
, ExecutionFlow executionFlow
) {
562 getMBeanServer().unregisterMBean(
563 flowMBeanName(module
, executionFlow
));
564 } catch (Exception e
) {
565 String msg
= "Cannot unregister execution flow " + executionFlow
567 throw new SlcException(msg
, e
);
571 @SuppressWarnings("deprecation")
572 protected ObjectName
flowMBeanName(Module module
,
573 ExecutionFlow executionFlow
) {
574 String executionModulesPrefix
= "SLCExecutionModules";
575 String path
= executionFlow
.getPath();
576 String name
= executionFlow
.getName();
577 if (path
== null && name
.indexOf('/') >= 0) {
578 path
= name
.substring(0, name
.lastIndexOf('/'));
579 name
= name
.substring(name
.lastIndexOf('/'));
582 StringBuffer buf
= new StringBuffer(executionModulesPrefix
+ ":"
583 + "module=" + module
.getName() + " [" + module
.getVersion()
586 if (path
!= null && !path
.equals("")) {
588 for (String token
: path
.split("/")) {
589 if (!token
.equals("")) {
590 buf
.append("path").append(depth
).append('=');
591 // in order to have directories first
593 buf
.append(token
).append(',');
598 buf
.append("name=").append(name
);
600 return new ObjectName(buf
.toString());
601 } catch (Exception e
) {
602 throw new SlcException("Cannot generate object name based on "
610 @SuppressWarnings("rawtypes")
611 private OsgiBundle
asOsgiBundle(Map properties
) {
612 String bundleSymbolicName
= checkAndGet(Constants
.BUNDLE_SYMBOLICNAME
,
614 String bundleVersion
= checkAndGet(Constants
.BUNDLE_VERSION
, properties
);
615 return new OsgiBundle(bundleSymbolicName
, bundleVersion
);
618 @SuppressWarnings("rawtypes")
619 private String
checkAndGet(Object key
, Map properties
) {
620 if (!properties
.containsKey(key
) || properties
.get(key
) == null)
621 throw new SlcException(key
+ " not set in " + properties
);
623 return properties
.get(key
).toString();
626 public void setBundlesManager(BundlesManager bundlesManager
) {
627 this.bundlesManager
= bundlesManager
;
630 public void setDefaultDescriptorConverter(
631 ExecutionFlowDescriptorConverter defaultDescriptorConverter
) {
632 this.defaultDescriptorConverter
= defaultDescriptorConverter
;
635 public void setRegisterFlowsToJmx(Boolean registerFlowsToJmx
) {
636 this.registerFlowsToJmx
= registerFlowsToJmx
;