]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/org.argeo.slc.spring/src/org/argeo/slc/osgi/OsgiExecutionModulesManager.java
Adapt to changes in Argeo Commons
[gpl/argeo-slc.git] / legacy / org.argeo.slc.spring / src / org / argeo / slc / osgi / OsgiExecutionModulesManager.java
1 package org.argeo.slc.osgi;
2
3 import java.lang.management.ManagementFactory;
4 import java.util.ArrayList;
5 import java.util.Dictionary;
6 import java.util.Enumeration;
7 import java.util.HashMap;
8 import java.util.HashSet;
9 import java.util.Iterator;
10 import java.util.List;
11 import java.util.Map;
12 import java.util.Set;
13
14 import javax.management.MBeanServer;
15 import javax.management.ObjectName;
16 import javax.management.StandardMBean;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.argeo.slc.DefaultNameVersion;
21 import org.argeo.slc.NameVersion;
22 import org.argeo.slc.SlcException;
23 import org.argeo.slc.core.execution.DefaultExecutionFlowDescriptorConverter;
24 import org.argeo.slc.deploy.Module;
25 import org.argeo.slc.deploy.ModuleDescriptor;
26 import org.argeo.slc.execution.ExecutionContext;
27 import org.argeo.slc.execution.ExecutionFlow;
28 import org.argeo.slc.execution.ExecutionFlowDescriptor;
29 import org.argeo.slc.execution.ExecutionFlowDescriptorConverter;
30 import org.argeo.slc.execution.ExecutionModuleDescriptor;
31 import org.argeo.slc.execution.ExecutionModulesListener;
32 import org.argeo.slc.execution.RealizedFlow;
33 import org.argeo.slc.runtime.AbstractExecutionModulesManager;
34 import org.eclipse.gemini.blueprint.service.importer.OsgiServiceLifecycleListener;
35 import org.osgi.framework.Bundle;
36 import org.osgi.framework.BundleEvent;
37 import org.osgi.framework.BundleException;
38 import org.osgi.framework.BundleListener;
39 import org.osgi.framework.Constants;
40 import org.osgi.framework.launch.Framework;
41 import org.springframework.context.ApplicationContext;
42
43 /** Execution modules manager implementation based on an OSGi runtime. */
44 public class OsgiExecutionModulesManager extends
45 AbstractExecutionModulesManager implements
46 OsgiServiceLifecycleListener, BundleListener {
47
48 private final static Log log = LogFactory
49 .getLog(OsgiExecutionModulesManager.class);
50
51 private BundlesManager bundlesManager;
52 private Map<OsgiBundle, ExecutionContext> executionContexts = new HashMap<OsgiBundle, ExecutionContext>();
53 private Map<OsgiBundle, ExecutionFlowDescriptorConverter> executionFlowDescriptorConverters = new HashMap<OsgiBundle, ExecutionFlowDescriptorConverter>();
54 private Map<OsgiBundle, Set<ExecutionFlow>> executionFlows = new HashMap<OsgiBundle, Set<ExecutionFlow>>();
55 private ExecutionFlowDescriptorConverter defaultDescriptorConverter = new DefaultExecutionFlowDescriptorConverter();
56
57 private List<ExecutionModulesListener> executionModulesListeners = new ArrayList<ExecutionModulesListener>();
58
59 private Boolean registerFlowsToJmx = false;
60
61 public void init() throws Exception {
62 bundlesManager.getBundleContext().addBundleListener(this);
63
64 final String module = System.getProperty(UNIQUE_LAUNCH_MODULE_PROPERTY);
65 final String flow = System.getProperty(UNIQUE_LAUNCH_FLOW_PROPERTY);
66 if (module != null) {
67 // launch a flow and stops
68 new Thread("Unique Flow") {
69 @Override
70 public void run() {
71 executeFlowAndExit(module, null, flow);
72 }
73 }.start();
74 }
75 }
76
77 public void destroy() {
78 bundlesManager.getBundleContext().removeBundleListener(this);
79 }
80
81 /** Executes a single flow and <b>stops the JVM</b> */
82 protected void executeFlowAndExit(final String module,
83 final String version, final String flow) {
84 if (log.isDebugEnabled())
85 log.debug("Launch unique flow " + flow + " from module " + module);
86 try {
87 OsgiBundle osgiBundle = bundlesManager.findFromPattern(module);
88 if (osgiBundle == null)
89 throw new SlcException("No OSGi bundle found for " + module);
90 // Bundle moduleBundle =
91 // bundlesManager.findRelatedBundle(osgiBundle);
92 start(osgiBundle);
93
94 RealizedFlow lastLaunch = findRealizedFlow(module, flow);
95 if (lastLaunch == null)
96 throw new SlcException("Cannot find launch for " + module + " "
97 + flow);
98 execute(lastLaunch);
99 } catch (Exception e) {
100 log.error(
101 "Error in unique flow " + flow + " from module " + module,
102 e);
103 } finally {
104 if (log.isDebugEnabled())
105 log.debug("Shutdown OSGi runtime...");
106 Framework framework = (Framework) bundlesManager.getBundleContext()
107 .getBundle(0);
108 try {
109 // shutdown framework
110 framework.stop();
111 // wait 1 min for shutdown
112 framework.waitForStop(60 * 1000);
113 // close VM
114 System.exit(0);
115 } catch (Exception e) {
116 e.printStackTrace();
117 System.exit(1);
118 }
119 }
120 }
121
122 // public void startExectionModule(String moduleName, String moduleVersion)
123 // {
124 // try {
125 // ServiceReference[] sr = bundlesManager.getServiceRefSynchronous(
126 // ApplicationContext.class.getName(),
127 // "org.springframework.context.service.name=" + moduleName);
128 // // bundlesManager.startSynchronous(moduleBundle);
129 // if (sr == null || sr.length == 0)
130 // throw new SlcException(
131 // "Cannot find execution module application context "
132 // + moduleName);
133 // } catch (InvalidSyntaxException e) {
134 // throw new SlcException("Cannot start exeuction module "
135 // + moduleName, e);
136 // }
137 // }
138
139 public synchronized ExecutionModuleDescriptor getExecutionModuleDescriptor(
140 String moduleName, String version) {
141 ExecutionModuleDescriptor md = new ExecutionModuleDescriptor();
142 OsgiBundle osgiBundle = null;
143 DefaultNameVersion nameVersion = new DefaultNameVersion(moduleName,
144 version);
145 bundles: for (Iterator<OsgiBundle> iterator = executionContexts
146 .keySet().iterator(); iterator.hasNext();) {
147 OsgiBundle ob = iterator.next();
148 if (nameVersion.getVersion() != null) {
149 if (ob.equals(nameVersion)) {
150 osgiBundle = ob;
151 break bundles;
152 }
153 } else {
154 if (ob.getName().equals(nameVersion.getName())) {
155 osgiBundle = ob;
156 break bundles;
157 }
158 }
159 }
160 if (osgiBundle == null)
161 throw new SlcException("No execution module registered for "
162 + nameVersion);
163 md.setName(osgiBundle.getName());
164 md.setVersion(osgiBundle.getVersion());
165 md.setTitle(osgiBundle.getTitle());
166 md.setDescription(osgiBundle.getDescription());
167
168 ExecutionFlowDescriptorConverter executionFlowDescriptorConverter = getExecutionFlowDescriptorConverter(
169 moduleName, version);
170 if (executionFlowDescriptorConverter == null)
171 throw new SlcException("No flow converter found.");
172 executionFlowDescriptorConverter.addFlowsToDescriptor(md,
173 listFlows(moduleName, version));
174 return md;
175 }
176
177 public synchronized List<ExecutionModuleDescriptor> listExecutionModules() {
178 List<ExecutionModuleDescriptor> descriptors = new ArrayList<ExecutionModuleDescriptor>();
179
180 for (Iterator<OsgiBundle> iterator = executionContexts.keySet()
181 .iterator(); iterator.hasNext();) {
182 OsgiBundle osgiBundle = iterator.next();
183 ExecutionModuleDescriptor md = new ExecutionModuleDescriptor();
184 setMetadataFromBundle(md,
185 bundlesManager.findRelatedBundle(osgiBundle));
186 descriptors.add(md);
187 }
188 return descriptors;
189 }
190
191 protected synchronized Map<String, ExecutionFlow> listFlows(
192 String moduleName, String moduleVersion) {
193
194 Map<String, ExecutionFlow> flows = new HashMap<String, ExecutionFlow>();
195 OsgiBundle key = bundlesManager.findRelatedBundle(moduleName,
196 moduleVersion);
197 if (!executionFlows.containsKey(key))
198 return flows;
199 Set<ExecutionFlow> flowsT = executionFlows.get(key);
200 for (ExecutionFlow flow : flowsT)
201 flows.put(flow.getName(), flow);
202 return flows;
203 }
204
205 protected ExecutionFlow findExecutionFlow(String moduleName,
206 String moduleVersion, String flowName) {
207 String filter = moduleVersion == null || moduleVersion.equals("0.0.0") ? "(&(Bundle-SymbolicName="
208 + moduleName
209 + ")(org.eclipse.gemini.blueprint.bean.name="
210 + flowName + "))"
211 : "(&(Bundle-SymbolicName=" + moduleName + ")(Bundle-Version="
212 + moduleVersion
213 + ")(org.eclipse.gemini.blueprint.bean.name="
214 + flowName + "))";
215 return bundlesManager.getSingleServiceStrict(ExecutionFlow.class,
216 filter, true);
217 }
218
219 protected ExecutionContext findExecutionContext(String moduleName,
220 String moduleVersion) {
221 String filter = moduleFilter(moduleName, moduleVersion);
222 return bundlesManager.getSingleServiceStrict(ExecutionContext.class,
223 filter, true);
224 }
225
226 protected ExecutionFlowDescriptorConverter findExecutionFlowDescriptorConverter(
227 String moduleName, String moduleVersion) {
228 String filter = moduleFilter(moduleName, moduleVersion);
229 return bundlesManager.getSingleService(
230 ExecutionFlowDescriptorConverter.class, filter, false);
231 }
232
233 /** Only based on symbolic name if version is null or "0.0.0" */
234 protected String moduleFilter(String moduleName, String moduleVersion) {
235 return moduleVersion == null || moduleVersion.equals("0.0.0") ? "(Bundle-SymbolicName="
236 + moduleName + ")"
237 : "(&(Bundle-SymbolicName=" + moduleName + ")(Bundle-Version="
238 + moduleVersion + "))";
239
240 }
241
242 /**
243 * Builds a minimal realized flow, based on the provided information
244 * (typically from the command line).
245 *
246 * @param module
247 * a bundle id, or a pattern contained in a bundle symbolic name
248 * @param module
249 * the execution flow name
250 * @return a minimal realized flow, to be used in an execution
251 */
252 public RealizedFlow findRealizedFlow(String module, String executionName) {
253 // First check whether we have a bundleId
254 Long bundleId = null;
255 try {
256 bundleId = Long.parseLong(module);
257 } catch (NumberFormatException e) {
258 // silent
259 }
260
261 // Look for bundle names containing pattern
262 OsgiBundle bundle = null;
263 if (bundleId != null) {
264 bundle = bundlesManager.getBundle(bundleId);
265 } else {
266 bundle = bundlesManager.findFromPattern(module);
267 }
268
269 if (bundle != null) {
270 RealizedFlow launch = new RealizedFlow();
271 launch.setModuleName(bundle.getName());
272 launch.setModuleVersion(bundle.getVersion());
273 ExecutionFlowDescriptor descriptor = new ExecutionFlowDescriptor();
274 descriptor.setName(executionName);
275 launch.setFlowDescriptor(descriptor);
276 return launch;
277 } else {
278 log.warn("Could not find any execution module matching these requirements.");
279 return null;
280 }
281 }
282
283 public void upgrade(NameVersion nameVersion) {
284 OsgiBundle osgiBundle = new OsgiBundle(nameVersion);
285 bundlesManager.upgradeSynchronous(osgiBundle);
286 }
287
288 protected synchronized ExecutionFlowDescriptorConverter getExecutionFlowDescriptorConverter(
289 String moduleName, String moduleVersion) {
290 return findExecutionFlowDescriptorConverter(moduleName, moduleVersion);
291 // OsgiBundle osgiBundle = new OsgiBundle(moduleName, moduleVersion);
292 // return getExecutionFlowDescriptorConverter(osgiBundle);
293 }
294
295 protected synchronized ExecutionFlowDescriptorConverter getExecutionFlowDescriptorConverter(
296 OsgiBundle osgiBundle) {
297 if (executionFlowDescriptorConverters.containsKey(osgiBundle))
298 return executionFlowDescriptorConverters.get(osgiBundle);
299 else
300 return defaultDescriptorConverter;
301 }
302
303 public ModuleDescriptor getModuleDescriptor(String moduleName,
304 String version) {
305 return getExecutionModuleDescriptor(moduleName, version);
306 }
307
308 public List<ModuleDescriptor> listModules() {
309 Bundle[] bundles = bundlesManager.getBundleContext().getBundles();
310 List<ModuleDescriptor> lst = new ArrayList<ModuleDescriptor>();
311 for (Bundle bundle : bundles) {
312 ModuleDescriptor moduleDescriptor = new ModuleDescriptor();
313 setMetadataFromBundle(moduleDescriptor, bundle);
314 lst.add(moduleDescriptor);
315 }
316 return lst;
317 }
318
319 public void start(NameVersion nameVersion) {
320 try {
321 Bundle bundle = bundlesManager.findRelatedBundle(new OsgiBundle(
322 nameVersion));
323 if (bundle == null)
324 throw new SlcException("Could not find bundle for "
325 + nameVersion);
326
327 bundlesManager.startSynchronous(bundle);
328 if (isSpringInstrumented(bundle)) {
329 // Wait for Spring application context to be ready
330 String filter = "(Bundle-SymbolicName="
331 + bundle.getSymbolicName() + ")";
332 try {
333 bundlesManager.getServiceRefSynchronous(
334 ApplicationContext.class, filter);
335 } catch (Exception e) {
336 // stop if application context not found
337 bundle.stop();
338 throw e;
339 }
340 }
341 } catch (Exception e) {
342 throw new SlcException("Cannot start " + nameVersion, e);
343 }
344 }
345
346 /** Do it calmly in order to avoid NPE */
347 private Boolean isSpringInstrumented(Bundle bundle) {
348 Dictionary<?, ?> headers = bundle.getHeaders();
349 if (headers != null && headers.get("Spring-Context") != null)
350 return true;
351 Enumeration<?> springEntryPaths = bundle
352 .getEntryPaths("/META-INF/spring");
353 if (springEntryPaths != null && springEntryPaths.hasMoreElements())
354 return true;
355 return false;
356 }
357
358 public void stop(NameVersion nameVersion) {
359 try {
360 Bundle bundle = bundlesManager.findRelatedBundle(new OsgiBundle(
361 nameVersion));
362 bundlesManager.stopSynchronous(bundle);
363 } catch (BundleException e) {
364 throw new SlcException("Cannot stop " + nameVersion, e);
365 }
366 }
367
368 protected void setMetadataFromBundle(ModuleDescriptor md, Bundle bundle) {
369 Bundle bdl = bundle;
370 if (bdl == null) {
371 if (md.getName() == null || md.getVersion() == null)
372 throw new SlcException("Name and version not available.");
373
374 Bundle[] bundles = bundlesManager.getBundleContext().getBundles();
375 for (Bundle b : bundles) {
376 if (b.getSymbolicName().equals(md.getName())
377 && md.getVersion().equals(
378 getHeaderSafe(b, Constants.BUNDLE_VERSION))) {
379 bdl = b;
380 break;
381 }
382 }
383
384 }
385
386 if (bdl == null)
387 throw new SlcException("Cannot find bundle.");
388
389 md.setName(bdl.getSymbolicName());
390 md.setVersion(getHeaderSafe(bdl, Constants.BUNDLE_VERSION));
391 md.setTitle(getHeaderSafe(bdl, Constants.BUNDLE_NAME));
392 md.setDescription(getHeaderSafe(bdl, Constants.BUNDLE_DESCRIPTION));
393
394 // copy manifets header to meta data
395 Dictionary<?, ?> headers = bundle.getHeaders();
396 Enumeration<?> keys = headers.keys();
397 while (keys.hasMoreElements()) {
398 Object key = keys.nextElement();
399 Object value = headers.get(key);
400 if (value != null)
401 md.getMetadata().put(key.toString(), value.toString());
402 }
403
404 // check if started
405 if (bundle.getState() == Bundle.ACTIVE
406 || bundle.getState() == Bundle.STARTING)
407 md.setStarted(true);
408 else
409 md.setStarted(false);
410 }
411
412 private String getHeaderSafe(Bundle bundle, Object key) {
413 Object obj = bundle.getHeaders().get(key);
414 if (obj == null)
415 return null;
416 else
417 return obj.toString();
418 }
419
420 /*
421 * REGISTRATION
422 */
423
424 /** Registers an execution context. */
425 public synchronized void register(ExecutionContext executionContext,
426 Map<String, String> properties) {
427 OsgiBundle osgiBundle = asOsgiBundle(properties);
428 Bundle bundle = bundlesManager.findRelatedBundle(osgiBundle);
429 osgiBundle.setTitle(getHeaderSafe(bundle, Constants.BUNDLE_NAME));
430 osgiBundle.setDescription(getHeaderSafe(bundle,
431 Constants.BUNDLE_DESCRIPTION));
432 executionContexts.put(osgiBundle, executionContext);
433 if (log.isTraceEnabled())
434 log.trace("Registered execution context from " + osgiBundle);
435 // Notify
436 ModuleDescriptor md = osgiBundle.getModuleDescriptor();
437 md.setStarted(true);
438 for (ExecutionModulesListener listener : executionModulesListeners)
439 listener.executionModuleAdded(md);
440 }
441
442 /** Unregisters an execution context. */
443 public synchronized void unregister(ExecutionContext executionContext,
444 Map<String, String> properties) {
445 // FIXME why are properties null?
446 if (properties == null)
447 return;
448 OsgiBundle osgiBundle = asOsgiBundle(properties);
449 if (executionContexts.containsKey(osgiBundle)) {
450 executionContexts.remove(osgiBundle);
451 if (log.isTraceEnabled())
452 log.trace("Removed execution context from " + osgiBundle);
453 // Notify
454 ModuleDescriptor md = osgiBundle.getModuleDescriptor();
455 md.setStarted(false);
456 for (ExecutionModulesListener listener : executionModulesListeners)
457 listener.executionModuleRemoved(md);
458 }
459 }
460
461 /** Registers an execution flow. */
462 public synchronized void register(ExecutionFlow executionFlow,
463 Map<String, String> properties) {
464 OsgiBundle osgiBundle = asOsgiBundle(properties);
465 if (!executionFlows.containsKey(osgiBundle)) {
466 executionFlows.put(osgiBundle, new HashSet<ExecutionFlow>());
467 }
468 executionFlows.get(osgiBundle).add(executionFlow);
469 if (log.isTraceEnabled())
470 log.trace("Registered " + executionFlow + " from " + osgiBundle);
471
472 // notifications
473 if (registerFlowsToJmx)
474 registerMBean(osgiBundle, executionFlow);
475 ExecutionFlowDescriptorConverter efdc = getExecutionFlowDescriptorConverter(osgiBundle);
476 for (ExecutionModulesListener listener : executionModulesListeners)
477 listener.executionFlowAdded(osgiBundle.getModuleDescriptor(),
478 efdc.getExecutionFlowDescriptor(executionFlow));
479 }
480
481 /** Unregisters an execution flow. */
482 public synchronized void unregister(ExecutionFlow executionFlow,
483 Map<String, String> properties) {
484 // FIXME why are properties null?
485 if (properties == null)
486 return;
487 OsgiBundle osgiBundle = asOsgiBundle(properties);
488 if (executionFlows.containsKey(osgiBundle)) {
489 Set<ExecutionFlow> flows = executionFlows.get(osgiBundle);
490 flows.remove(executionFlow);
491 if (log.isTraceEnabled())
492 log.trace("Removed " + executionFlow + " from " + osgiBundle);
493 if (flows.size() == 0) {
494 executionFlows.remove(osgiBundle);
495 if (log.isTraceEnabled())
496 log.trace("Removed flows set from " + osgiBundle);
497 }
498
499 // notifications
500 if (registerFlowsToJmx)
501 unregisterMBean(osgiBundle, executionFlow);
502 ExecutionFlowDescriptorConverter efdc = getExecutionFlowDescriptorConverter(osgiBundle);
503 for (ExecutionModulesListener listener : executionModulesListeners)
504 listener.executionFlowRemoved(osgiBundle.getModuleDescriptor(),
505 efdc.getExecutionFlowDescriptor(executionFlow));
506 }
507 }
508
509 /** Registers an execution module listener. */
510 public synchronized void register(
511 ExecutionModulesListener executionModulesListener,
512 Map<String, String> properties) {
513 // sync with current state
514 for (OsgiBundle osgiBundle : executionContexts.keySet()) {
515 executionModulesListener.executionModuleAdded(osgiBundle
516 .getModuleDescriptor());
517 }
518 for (OsgiBundle osgiBundle : executionFlows.keySet()) {
519 ExecutionFlowDescriptorConverter efdc = getExecutionFlowDescriptorConverter(osgiBundle);
520 for (ExecutionFlow executionFlow : executionFlows.get(osgiBundle))
521 executionModulesListener.executionFlowAdded(
522 osgiBundle.getModuleDescriptor(),
523 efdc.getExecutionFlowDescriptor(executionFlow));
524 }
525 executionModulesListeners.add(executionModulesListener);
526 }
527
528 /** Unregisters an execution module listener. */
529 public synchronized void unregister(
530 ExecutionModulesListener executionModulesListener,
531 Map<String, String> properties) {
532 executionModulesListeners.remove(executionModulesListener);
533 }
534
535 /*
536 * INTERFACE IMPLEMENTATIONS
537 */
538
539 public void bundleChanged(BundleEvent evt) {
540 Bundle bundle = evt.getBundle();
541 if (bundle.getHeaders().get(
542 ExecutionModuleDescriptor.SLC_EXECUTION_MODULE) != null) {
543 OsgiBundle osgiBundle = new OsgiBundle(bundle);
544 if (evt.getType() == BundleEvent.INSTALLED)
545 for (ExecutionModulesListener listener : executionModulesListeners)
546 listener.executionModuleAdded(osgiBundle
547 .getModuleDescriptor());
548 else if (evt.getType() == BundleEvent.UNINSTALLED)
549 for (ExecutionModulesListener listener : executionModulesListeners)
550 listener.executionModuleRemoved(osgiBundle
551 .getModuleDescriptor());
552 }
553
554 }
555
556 @SuppressWarnings({ "rawtypes" })
557 public synchronized void bind(Object service, Map properties)
558 throws Exception {
559 if (service instanceof ExecutionFlowDescriptorConverter) {
560 ExecutionFlowDescriptorConverter executionFlowDescriptorConverter = (ExecutionFlowDescriptorConverter) service;
561 OsgiBundle osgiBundle = asOsgiBundle(properties);
562 executionFlowDescriptorConverters.put(osgiBundle,
563 executionFlowDescriptorConverter);
564 if (log.isTraceEnabled())
565 log.debug("Registered execution flow descriptor converter from "
566 + osgiBundle);
567 } else {
568 // ignore
569 }
570 }
571
572 @SuppressWarnings("rawtypes")
573 public synchronized void unbind(Object service, Map properties)
574 throws Exception {
575 if (service instanceof ExecutionFlowDescriptorConverter) {
576 OsgiBundle osgiBundle = asOsgiBundle(properties);
577 if (executionFlowDescriptorConverters.containsKey(osgiBundle)) {
578 executionFlowDescriptorConverters.remove(osgiBundle);
579 if (log.isTraceEnabled())
580 log.debug("Removed execution flow descriptor converter from "
581 + osgiBundle);
582 }
583 } else {
584 // ignore
585 }
586 }
587
588 /*
589 * JMX
590 */
591 protected MBeanServer getMBeanServer() {
592 return ManagementFactory.getPlatformMBeanServer();
593 }
594
595 public void registerMBean(Module module, ExecutionFlow executionFlow) {
596 try {
597 StandardMBean mbean = new StandardMBean(executionFlow,
598 ExecutionFlow.class);
599 getMBeanServer().registerMBean(mbean,
600 flowMBeanName(module, executionFlow));
601 } catch (Exception e) {
602 String msg = "Cannot register execution flow " + executionFlow
603 + " as mbean";
604 throw new SlcException(msg, e);
605 }
606 }
607
608 public void unregisterMBean(Module module, ExecutionFlow executionFlow) {
609 try {
610 getMBeanServer().unregisterMBean(
611 flowMBeanName(module, executionFlow));
612 } catch (Exception e) {
613 String msg = "Cannot unregister execution flow " + executionFlow
614 + " as mbean";
615 throw new SlcException(msg, e);
616 }
617 }
618
619 protected ObjectName flowMBeanName(Module module,
620 ExecutionFlow executionFlow) {
621 String executionModulesPrefix = "SLCExecutionModules";
622 // String path = executionFlow.getPath();
623 String name = executionFlow.getName();
624 // if (path == null && name.indexOf('/') >= 0) {
625 // path = name.substring(0, name.lastIndexOf('/'));
626 // name = name.substring(name.lastIndexOf('/'));
627 // }
628
629 StringBuffer buf = new StringBuffer(executionModulesPrefix + ":"
630 + "module=" + module.getName() + " [" + module.getVersion()
631 + "],");
632
633 // if (path != null && !path.equals("")) {
634 // int depth = 0;
635 // for (String token : path.split("/")) {
636 // if (!token.equals("")) {
637 // buf.append("path").append(depth).append('=');
638 // // in order to have directories first
639 // buf.append('/');
640 // buf.append(token).append(',');
641 // depth++;
642 // }
643 // }
644 // }
645 buf.append("name=").append(name);
646 try {
647 return new ObjectName(buf.toString());
648 } catch (Exception e) {
649 throw new SlcException("Cannot generate object name based on "
650 + buf, e);
651 }
652 }
653
654 /*
655 * UTILITIES
656 */
657 @SuppressWarnings("rawtypes")
658 private OsgiBundle asOsgiBundle(Map properties) {
659 String bundleSymbolicName = checkAndGet(Constants.BUNDLE_SYMBOLICNAME,
660 properties);
661 String bundleVersion = checkAndGet(Constants.BUNDLE_VERSION, properties);
662 return new OsgiBundle(bundleSymbolicName, bundleVersion);
663 }
664
665 @SuppressWarnings("rawtypes")
666 private String checkAndGet(Object key, Map properties) {
667 if (!properties.containsKey(key) || properties.get(key) == null)
668 throw new SlcException(key + " not set in " + properties);
669 else
670 return properties.get(key).toString();
671 }
672
673 public void setBundlesManager(BundlesManager bundlesManager) {
674 this.bundlesManager = bundlesManager;
675 }
676
677 public void setDefaultDescriptorConverter(
678 ExecutionFlowDescriptorConverter defaultDescriptorConverter) {
679 this.defaultDescriptorConverter = defaultDescriptorConverter;
680 }
681
682 public void setRegisterFlowsToJmx(Boolean registerFlowsToJmx) {
683 this.registerFlowsToJmx = registerFlowsToJmx;
684 }
685
686 }