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