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