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