+
+ @SuppressWarnings("unchecked")
+ public synchronized void bind(Object service, Map properties)
+ throws Exception {
+ if (service instanceof ExecutionContext) {
+ ExecutionContext executionContext = (ExecutionContext) service;
+ OsgiBundle osgiBundle = asOsgiBundle(properties);
+ Bundle bundle = bundlesManager.findRelatedBundle(osgiBundle);
+ osgiBundle.setLabel(getHeaderSafe(bundle, Constants.BUNDLE_NAME));
+ osgiBundle.setDescription(getHeaderSafe(bundle,
+ Constants.BUNDLE_DESCRIPTION));
+ executionContexts.put(osgiBundle, executionContext);
+ if (log.isTraceEnabled())
+ log.debug("Registered execution context from " + osgiBundle);
+ // Notify
+ for (ExecutionModulesListener listener : getExecutionModulesListeners())
+ listener.executionModuleAdded(osgiBundle, executionContext);
+
+ } else if (service instanceof ExecutionFlow) {
+ ExecutionFlow executionFlow = (ExecutionFlow) service;
+ OsgiBundle osgiBundle = asOsgiBundle(properties);
+ if (!executionFlows.containsKey(osgiBundle)) {
+ executionFlows.put(osgiBundle, new HashSet());
+ }
+ executionFlows.get(osgiBundle).add(executionFlow);
+ if (log.isTraceEnabled())
+ log
+ .debug("Registered " + executionFlow + " from "
+ + osgiBundle);
+ for (ExecutionModulesListener listener : getExecutionModulesListeners())
+ listener.executionFlowAdded(osgiBundle, executionFlow);
+
+ } else if (service instanceof ExecutionFlowDescriptorConverter) {
+ ExecutionFlowDescriptorConverter executionFlowDescriptorConverter = (ExecutionFlowDescriptorConverter) service;
+ OsgiBundle osgiBundle = asOsgiBundle(properties);
+ executionFlowDescriptorConverters.put(osgiBundle,
+ executionFlowDescriptorConverter);
+ if (log.isTraceEnabled())
+ log
+ .debug("Registered execution flow descriptor converter from "
+ + osgiBundle);
+ } else {
+ // ignore
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ public synchronized void unbind(Object service, Map properties)
+ throws Exception {
+ if (service instanceof ExecutionContext) {
+ OsgiBundle osgiBundle = asOsgiBundle(properties);
+ if (executionContexts.containsKey(osgiBundle)) {
+ ExecutionContext executionContext = executionContexts
+ .remove(osgiBundle);
+ if (log.isTraceEnabled())
+ log.debug("Removed execution context from " + osgiBundle);
+ // Notify
+ for (ExecutionModulesListener listener : getExecutionModulesListeners())
+ listener.executionModuleRemoved(osgiBundle,
+ executionContext);
+ }
+ } else if (service instanceof ExecutionFlow) {
+ ExecutionFlow executionFlow = (ExecutionFlow) service;
+ OsgiBundle osgiBundle = asOsgiBundle(properties);
+ if (executionFlows.containsKey(osgiBundle)) {
+ Set flows = executionFlows.get(osgiBundle);
+ flows.remove(executionFlow);
+ if (log.isTraceEnabled())
+ log.debug("Removed " + executionFlow + " from "
+ + osgiBundle);
+ if (flows.size() == 0) {
+ executionFlows.remove(osgiBundle);
+ if (log.isTraceEnabled())
+ log.debug("Removed flows set from " + osgiBundle);
+ }
+ for (ExecutionModulesListener listener : getExecutionModulesListeners())
+ listener.executionFlowRemoved(osgiBundle, executionFlow);
+ }
+ } else if (service instanceof ExecutionFlowDescriptorConverter) {
+ OsgiBundle osgiBundle = asOsgiBundle(properties);
+ if (executionFlowDescriptorConverters.containsKey(osgiBundle)) {
+ executionFlowDescriptorConverters.remove(osgiBundle);
+ if (log.isTraceEnabled())
+ log
+ .debug("Removed execution flow descriptor converter from "
+ + osgiBundle);
+ }
+ } else {
+ // ignore
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private OsgiBundle asOsgiBundle(Map properties) {
+ String bundleSymbolicName = checkAndGet(Constants.BUNDLE_SYMBOLICNAME,
+ properties);
+ String bundleVersion = checkAndGet(Constants.BUNDLE_VERSION, properties);
+ return new OsgiBundle(bundleSymbolicName, bundleVersion);
+ }
+
+ @SuppressWarnings("unchecked")
+ private String checkAndGet(Object key, Map properties) {
+ if (!properties.containsKey(key) || properties.get(key) == null)
+ throw new SlcException(key + " not set in " + properties);
+ else
+ return properties.get(key).toString();
+ }
+
+ public void setDefaultDescriptorConverter(
+ ExecutionFlowDescriptorConverter defaultDescriptorConverter) {
+ this.defaultDescriptorConverter = defaultDescriptorConverter;
+ }