]> git.argeo.org Git - gpl/argeo-slc.git/commitdiff
Use listener to track services
authorMathieu Baudier <mbaudier@argeo.org>
Thu, 30 Jul 2009 19:58:29 +0000 (19:58 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Thu, 30 Jul 2009 19:58:29 +0000 (19:58 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@2843 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

modules/agent/org.argeo.slc.agent/META-INF/spring/osgi.xml
runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/MapExecutionContext.java
runtime/org.argeo.slc.support.equinox/src/main/resources/META-INF/spring/osgi.xml
runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/BundlesManager.java
runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/OsgiExecutionModulesManager.java
runtime/org.argeo.slc.support.osgi/src/main/resources/org/argeo/slc/osgi/manager.xml [new file with mode: 0644]

index 9193ad2fdc3b35d2e138d41da547eddce57fefc8..f30b4b34de5d80416383be95c9e06eec3df886ea 100644 (file)
@@ -6,6 +6,8 @@
        http://www.springframework.org/schema/beans   \r
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">\r
 \r
+       <beans:import resource="classpath:org/argeo/slc/osgi/manager.xml" />\r
+\r
        <!-- Deprecated -->\r
        <service ref="treeTestResultLogger" interface="org.argeo.slc.test.TestResultListener" />\r
        <service ref="treeTestResultLogger"\r
index 564acab2219b26357558150013456cfb0fffb019..17c6d414051ec2a5e83b9566ffbb95bfb7ca730c 100644 (file)
@@ -8,8 +8,7 @@ import java.util.UUID;
 
 import org.argeo.slc.execution.ExecutionContext;
 
-public class MapExecutionContext implements
-               ExecutionContext {
+public class MapExecutionContext implements ExecutionContext {
        private final Map<String, Object> variables = Collections
                        .synchronizedMap(new HashMap<String, Object>());
 
@@ -32,4 +31,17 @@ public class MapExecutionContext implements
        public String getUuid() {
                return uuid;
        }
+
+       @Override
+       public boolean equals(Object obj) {
+               if (obj instanceof ExecutionContext)
+                       return uuid.equals(((ExecutionContext) obj).getUuid());
+               return false;
+       }
+
+       @Override
+       public String toString() {
+               return getClass().getSimpleName()+"#"+uuid;
+       }
+
 }
index 9dab0f29ea8662b5d93ba56a16fc03e3a1a0ac42..28600ad409821034324aaff5e6b767b447552835 100644 (file)
@@ -6,12 +6,14 @@
        http://www.springframework.org/schema/beans   \r
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">\r
 \r
+       <beans:import resource="classpath:org/argeo/slc/osgi/manager.xml" />\r
+\r
        <service interface="org.eclipse.osgi.framework.console.CommandProvider"\r
                ref="commandProvider" />\r
 \r
-<!-- \r
-       <service\r
+       <!--\r
+               <service\r
                interface="org.springframework.osgi.context.event.OsgiBundleApplicationContextListener"\r
                ref="bundlesManager" />\r
- -->\r
      -->\r
 </beans:beans>
\ No newline at end of file
index 22d1e4b455b347953b2b38d0806c13cbd7e46f3f..e24b7223b7736299125d5aae7f6ddd29b5a4d0f6 100644 (file)
@@ -299,16 +299,23 @@ public class BundlesManager implements BundleContextAware, FrameworkListener,
                        Assert.isTrue(
                                        osgiBundle.getName().equals(bundle.getSymbolicName()),
                                        "symbolic name consistent");
-                       Assert.isTrue(osgiBundle.getVersion().equals(
-                                       bundle.getHeaders().get(Constants.BUNDLE_VERSION)),
-                                       "version consistent");
+                       if (osgiBundle.getVersion() != null)
+                               Assert.isTrue(osgiBundle.getVersion().equals(
+                                               bundle.getHeaders().get(Constants.BUNDLE_VERSION)),
+                                               "version consistent");
                } else {
-                       for (Bundle b : bundleContext.getBundles()) {
+                       bundles: for (Bundle b : bundleContext.getBundles()) {
                                if (b.getSymbolicName().equals(osgiBundle.getName())) {
+                                       if (osgiBundle.getVersion() == null) {
+                                               bundle = b;
+                                               break bundles;
+                                       }
+
                                        if (b.getHeaders().get(Constants.BUNDLE_VERSION).equals(
                                                        osgiBundle.getVersion())) {
                                                bundle = b;
                                                osgiBundle.setInternalBundleId(b.getBundleId());
+                                               break bundles;
                                        }
                                }
                        }
index 5e61ddba08ad68aebb24dd56cb20d5be48435791..ca8fabaeee77e830b75dee88aff9ba1223e913b8 100644 (file)
@@ -1,9 +1,13 @@
 package org.argeo.slc.osgi;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -24,66 +28,109 @@ import org.osgi.framework.ServiceReference;
 import org.osgi.util.tracker.ServiceTracker;
 import org.springframework.beans.factory.DisposableBean;
 import org.springframework.beans.factory.InitializingBean;
+import org.springframework.osgi.service.importer.OsgiServiceLifecycleListener;
 
 public class OsgiExecutionModulesManager extends
                AbstractExecutionModulesManager implements InitializingBean,
-               DisposableBean {
+               DisposableBean, OsgiServiceLifecycleListener {
+       private final static String PROPERTY_CACHE_SERVICES = "slc.osgi.execution.cacheServices";
+
        private final static Log log = LogFactory
                        .getLog(OsgiExecutionModulesManager.class);
 
        private BundlesManager bundlesManager;
-       private ServiceTracker executionContexts;
+       private ServiceTracker executionContextsTracker;
+       private Map<OsgiBundle, ExecutionContext> executionContexts = new HashMap<OsgiBundle, ExecutionContext>();
+       private Map<OsgiBundle, ExecutionFlowDescriptorConverter> executionFlowDescriptorConverters = new HashMap<OsgiBundle, ExecutionFlowDescriptorConverter>();
+       private Map<OsgiBundle, Set<ExecutionFlow>> executionFlows = new HashMap<OsgiBundle, Set<ExecutionFlow>>();
        private ExecutionFlowDescriptorConverter defaultDescriptorConverter = new DefaultExecutionFlowDescriptorConverter();
 
+       private Boolean useCachedServices = Boolean.parseBoolean(System
+                       .getProperty(PROPERTY_CACHE_SERVICES, "true"));
+
        public ExecutionModuleDescriptor getExecutionModuleDescriptor(
                        String moduleName, String version) {
                ExecutionModuleDescriptor md = new ExecutionModuleDescriptor();
                md.setName(moduleName);
                md.setVersion(version);
                setMetadataFromBundle(md, null);
-               getExecutionFlowDescriptorConverter(moduleName, version)
-                               .addFlowsToDescriptor(md, listFlows(moduleName, version));
+               ExecutionFlowDescriptorConverter executionFlowDescriptorConverter = getExecutionFlowDescriptorConverter(
+                               moduleName, version);
+               if (executionFlowDescriptorConverter == null)
+                       throw new SlcException("No flow converter found.");
+               executionFlowDescriptorConverter.addFlowsToDescriptor(md, listFlows(
+                               moduleName, version));
                return md;
        }
 
-       public List<ExecutionModuleDescriptor> listExecutionModules() {
+       public synchronized List<ExecutionModuleDescriptor> listExecutionModules() {
                List<ExecutionModuleDescriptor> descriptors = new ArrayList<ExecutionModuleDescriptor>();
 
-               ServiceReference[] srs = executionContexts.getServiceReferences();
-               for (ServiceReference sr : srs) {
-                       ExecutionModuleDescriptor md = new ExecutionModuleDescriptor();
-                       setMetadataFromBundle(md, sr.getBundle());
-                       descriptors.add(md);
+               if (useCachedServices) {
+                       for (Iterator<OsgiBundle> iterator = executionContexts.keySet()
+                                       .iterator(); iterator.hasNext();) {
+                               OsgiBundle osgiBundle = iterator.next();
+                               ExecutionModuleDescriptor md = new ExecutionModuleDescriptor();
+                               setMetadataFromBundle(md, bundlesManager
+                                               .findRelatedBundle(osgiBundle));
+                               descriptors.add(md);
+                       }
+               } else {
+                       ServiceReference[] arr = executionContextsTracker
+                                       .getServiceReferences();
+                       if (arr == null) {
+                               log.error("Tracker returned null.");
+                               return descriptors;
+                       }
+
+                       List<ServiceReference> srs = Arrays.asList(arr);
+                       // ServiceReference[] srs =
+                       // executionContexts.getServiceReferences();
+                       for (ServiceReference sr : srs) {
+                               ExecutionModuleDescriptor md = new ExecutionModuleDescriptor();
+                               setMetadataFromBundle(md, sr.getBundle());
+                               descriptors.add(md);
+                       }
                }
                return descriptors;
        }
 
-       protected Map<String, ExecutionFlow> listFlows(String moduleName,
-                       String moduleVersion) {
-               // TODO: use service trackers?
-               // String filter = OsgiFilterUtils.unifyFilter(ExecutionFlow.class,
-               // null);
-
-               String filter = "(Bundle-SymbolicName=" + moduleName + ")";
-               ServiceReference[] sfs;
-               try {
-                       sfs = bundlesManager.getBundleContext().getServiceReferences(
-                                       ExecutionFlow.class.getName(), filter);
-               } catch (InvalidSyntaxException e) {
-                       throw new SlcException(
-                                       "Cannot retrieve service reference for flow " + filter, e);
-               }
+       protected synchronized Map<String, ExecutionFlow> listFlows(
+                       String moduleName, String moduleVersion) {
 
                Map<String, ExecutionFlow> flows = new HashMap<String, ExecutionFlow>();
-               for (ServiceReference sf : sfs) {
-                       ExecutionFlow flow = (ExecutionFlow) bundlesManager
-                                       .getBundleContext().getService(sf);
-                       flows.put(flow.getName(), flow);
+               if (useCachedServices) {
+                       Set<ExecutionFlow> flowsT = executionFlows.get(new OsgiBundle(
+                                       moduleName, moduleVersion));
+                       for (ExecutionFlow flow : flowsT)
+                               flows.put(flow.getName(), flow);
+               } else {
+
+                       // TODO: use service trackers?
+                       // String filter = OsgiFilterUtils.unifyFilter(ExecutionFlow.class,
+                       // null);
+
+                       String filter = "(Bundle-SymbolicName=" + moduleName + ")";
+                       ServiceReference[] sfs;
+                       try {
+                               sfs = bundlesManager.getBundleContext().getServiceReferences(
+                                               ExecutionFlow.class.getName(), filter);
+                       } catch (InvalidSyntaxException e) {
+                               throw new SlcException(
+                                               "Cannot retrieve service reference for flow " + filter,
+                                               e);
+                       }
+
+                       for (ServiceReference sf : sfs) {
+                               ExecutionFlow flow = (ExecutionFlow) bundlesManager
+                                               .getBundleContext().getService(sf);
+                               flows.put(flow.getName(), flow);
+                       }
                }
                return flows;
        }
 
-       public ExecutionFlow findExecutionFlow(String moduleName,
+       protected ExecutionFlow findExecutionFlow(String moduleName,
                        String moduleVersion, String flowName) {
                String filter = "(&(Bundle-SymbolicName=" + moduleName
                                + ")(org.springframework.osgi.bean.name=" + flowName + "))";
@@ -91,7 +138,7 @@ public class OsgiExecutionModulesManager extends
                                filter);
        }
 
-       public ExecutionContext findExecutionContext(String moduleName,
+       protected ExecutionContext findExecutionContext(String moduleName,
                        String moduleVersion) {
                String filter = "(&(Bundle-SymbolicName=" + moduleName
                                + ")(Bundle-Version=" + moduleVersion + "))";
@@ -99,8 +146,9 @@ public class OsgiExecutionModulesManager extends
                                filter);
        }
 
-       public ExecutionFlowDescriptorConverter findExecutionFlowDescriptorConverter(
+       protected ExecutionFlowDescriptorConverter findExecutionFlowDescriptorConverter(
                        String moduleName, String moduleVersion) {
+
                String filter = "(&(Bundle-SymbolicName=" + moduleName
                                + ")(Bundle-Version=" + moduleVersion + "))";
                return bundlesManager.getSingleService(
@@ -112,12 +160,14 @@ public class OsgiExecutionModulesManager extends
        }
 
        public void afterPropertiesSet() throws Exception {
-               executionContexts = bundlesManager.newTracker(ExecutionContext.class);
+               if (!useCachedServices)
+                       executionContextsTracker = bundlesManager
+                                       .newTracker(ExecutionContext.class);
        }
 
        public void destroy() throws Exception {
-               if (executionContexts != null)
-                       executionContexts.close();
+               if (executionContextsTracker != null)
+                       executionContextsTracker.close();
        }
 
        /**
@@ -168,15 +218,23 @@ public class OsgiExecutionModulesManager extends
                execute(realizedFlow);
        }
 
-       protected ExecutionFlowDescriptorConverter getExecutionFlowDescriptorConverter(
+       protected synchronized ExecutionFlowDescriptorConverter getExecutionFlowDescriptorConverter(
                        String moduleName, String moduleVersion) {
-               // Check whether a descriptor converter is published by this module
-               ExecutionFlowDescriptorConverter descriptorConverter = findExecutionFlowDescriptorConverter(
-                               moduleName, moduleVersion);
-               if (descriptorConverter == null)
-                       return defaultDescriptorConverter;
-               else
-                       return descriptorConverter;
+               if (useCachedServices) {
+                       OsgiBundle osgiBundle = new OsgiBundle(moduleName, moduleVersion);
+                       if (executionFlowDescriptorConverters.containsKey(osgiBundle))
+                               return executionFlowDescriptorConverters.get(osgiBundle);
+                       else
+                               return defaultDescriptorConverter;
+               } else {
+                       // Check whether a descriptor converter is published by this module
+                       ExecutionFlowDescriptorConverter descriptorConverter = findExecutionFlowDescriptorConverter(
+                                       moduleName, moduleVersion);
+                       if (descriptorConverter == null)
+                               return defaultDescriptorConverter;
+                       else
+                               return descriptorConverter;
+               }
        }
 
        public void execute(RealizedFlow realizedFlow) {
@@ -256,4 +314,93 @@ public class OsgiExecutionModulesManager extends
                else
                        return obj.toString();
        }
+
+       @SuppressWarnings("unchecked")
+       public synchronized void bind(Object service, Map properties)
+                       throws Exception {
+               if (service instanceof ExecutionContext) {
+                       ExecutionContext executionContext = (ExecutionContext) service;
+                       OsgiBundle osgiBundle = asOsgiBundle(properties);
+                       executionContexts.put(osgiBundle, executionContext);
+                       if (log.isTraceEnabled())
+                               log.debug("Registered execution context from " + osgiBundle);
+               } 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);
+
+               } 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)) {
+                               executionContexts.remove(osgiBundle);
+                               if (log.isTraceEnabled())
+                                       log.debug("Removed execution context from " + osgiBundle);
+                       }
+               } else if (service instanceof ExecutionFlow) {
+                       ExecutionFlow executionFlow = (ExecutionFlow) service;
+                       OsgiBundle osgiBundle = asOsgiBundle(properties);
+                       if (executionFlows.containsKey(osgiBundle)) {
+                               executionFlows.get(osgiBundle).remove(executionFlow);
+                               if (log.isTraceEnabled())
+                                       log.debug("Removed " + executionFlow + " from "
+                                                       + osgiBundle);
+                       }
+               } 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;
+       }
 }
diff --git a/runtime/org.argeo.slc.support.osgi/src/main/resources/org/argeo/slc/osgi/manager.xml b/runtime/org.argeo.slc.support.osgi/src/main/resources/org/argeo/slc/osgi/manager.xml
new file mode 100644 (file)
index 0000000..2f7a546
--- /dev/null
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+<beans:beans xmlns="http://www.springframework.org/schema/osgi"\r
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"\r
+       xsi:schemaLocation="http://www.springframework.org/schema/osgi  \r
+       http://www.springframework.org/schema/osgi/spring-osgi-1.1.xsd\r
+       http://www.springframework.org/schema/beans   \r
+       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">\r
+\r
+       <!-- No sets so that the equals methods are not called -->\r
+       <list id="executionFlowDescriptorConverters"\r
+               interface="org.argeo.slc.execution.ExecutionFlowDescriptorConverter"\r
+               cardinality="0..N">\r
+               <listener ref="modulesManager" />\r
+       </list>\r
+\r
+       <list id="executionContexts" interface="org.argeo.slc.execution.ExecutionContext"\r
+               cardinality="0..N">\r
+               <listener ref="modulesManager" />\r
+       </list>\r
+\r
+       <list id="executionFlows" interface="org.argeo.slc.execution.ExecutionFlow"\r
+               cardinality="0..N">\r
+               <listener ref="modulesManager" />\r
+       </list>\r
+\r
+</beans:beans>
\ No newline at end of file