servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r707957 - in /servicemix/smx4/specs/trunk/locator/src/main/java/org/apache/servicemix/specs/locator: Activator.java OsgiLocator.java
Date Sun, 26 Oct 2008 09:36:16 GMT
Author: gnodet
Date: Sun Oct 26 02:36:16 2008
New Revision: 707957

URL: http://svn.apache.org/viewvc?rev=707957&view=rev
Log:
SMX4-145: OSGi specs bundle listener may register duplicate entries leading to using invalid
bundles when creating factories

Modified:
    servicemix/smx4/specs/trunk/locator/src/main/java/org/apache/servicemix/specs/locator/Activator.java
    servicemix/smx4/specs/trunk/locator/src/main/java/org/apache/servicemix/specs/locator/OsgiLocator.java

Modified: servicemix/smx4/specs/trunk/locator/src/main/java/org/apache/servicemix/specs/locator/Activator.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/specs/trunk/locator/src/main/java/org/apache/servicemix/specs/locator/Activator.java?rev=707957&r1=707956&r2=707957&view=diff
==============================================================================
--- servicemix/smx4/specs/trunk/locator/src/main/java/org/apache/servicemix/specs/locator/Activator.java
(original)
+++ servicemix/smx4/specs/trunk/locator/src/main/java/org/apache/servicemix/specs/locator/Activator.java
Sun Oct 26 02:36:16 2008
@@ -61,9 +61,14 @@
     public synchronized void start(BundleContext bundleContext) throws Exception {
         this.bundleContext = bundleContext;
         debugPrintln("activating");
+        debugPrintln("adding bundle listener");
         bundleContext.addBundleListener(this);
+        debugPrintln("checking existing bundles");
         for (Bundle bundle : bundleContext.getBundles()) {
-            register(bundle);
+            if (bundle.getState() == Bundle.RESOLVED || bundle.getState() == Bundle.STARTING
||
+                    bundle.getState() == Bundle.ACTIVE || bundle.getState() == Bundle.STOPPING)
{
+                register(bundle);
+            }
         }
         debugPrintln("activated");
     }
@@ -102,29 +107,12 @@
                     map = new HashMap<String, Callable<Class>>();
                     factories.put(bundle.getBundleId(), map);
                 }
-                map.put(factoryId, new Callable<Class>() {
-                    public Class call() throws Exception {
-                        try {
-                            debugPrintln("creating factory for key: " + factoryId);
-                            BufferedReader br = new BufferedReader(new InputStreamReader(u.openStream(),
"UTF-8"));
-                            String factoryClassName = br.readLine();
-                            br.close();
-                            debugPrintln("factory implementation: " + factoryClassName);
-                            return bundle.loadClass(factoryClassName);
-                        } catch (Exception e) {
-                            debugPrintln("exception caught while creating factory: " + e);
-                            throw e;
-                        } catch (Error e) {
-                            debugPrintln("error caught while creating factory: " + e);
-                            throw e;
-                        }
-                    }
-                });
+                map.put(factoryId, new BundleFactoryLoader(factoryId, u, bundle));
             }
         }
         if (map != null) {
             for (Map.Entry<String, Callable<Class>> entry : map.entrySet()) {
-                debugPrintln("registering service for key " + entry.getKey());
+                debugPrintln("registering service for key " + entry.getKey() + "with value
" + entry.getValue());
                 OsgiLocator.register(entry.getKey(), entry.getValue());
             }
         }
@@ -134,9 +122,57 @@
         Map<String, Callable<Class>> map = factories.remove(bundleId);
         if (map != null) {
             for (Map.Entry<String, Callable<Class>> entry : map.entrySet()) {
-                debugPrintln("unregistering service for key " + entry.getKey());
+                debugPrintln("unregistering service for key " + entry.getKey() + "with value
" + entry.getValue());
                 OsgiLocator.unregister(entry.getKey(), entry.getValue());
             }
         }
     }
+
+    private class BundleFactoryLoader implements Callable<Class> {
+        private final String factoryId;
+        private final URL u;
+        private final Bundle bundle;
+
+        public BundleFactoryLoader(String factoryId, URL u, Bundle bundle) {
+            this.factoryId = factoryId;
+            this.u = u;
+            this.bundle = bundle;
+        }
+
+        public Class call() throws Exception {
+            try {
+                debugPrintln("creating factory for key: " + factoryId);
+                BufferedReader br = new BufferedReader(new InputStreamReader(u.openStream(),
"UTF-8"));
+                String factoryClassName = br.readLine();
+                br.close();
+                debugPrintln("factory implementation: " + factoryClassName);
+                return bundle.loadClass(factoryClassName);
+            } catch (Exception e) {
+                debugPrintln("exception caught while creating factory: " + e);
+                throw e;
+            } catch (Error e) {
+                debugPrintln("error caught while creating factory: " + e);
+                throw e;
+            }
+        }
+
+        @Override
+        public String toString() {
+           return u.toString();
+        }
+
+        @Override
+        public int hashCode() {
+           return u.hashCode();
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (obj instanceof BundleFactoryLoader) {
+                return u.equals(((BundleFactoryLoader) obj).u);
+            } else {
+                return false;
+            }
+        }
+    }
 }

Modified: servicemix/smx4/specs/trunk/locator/src/main/java/org/apache/servicemix/specs/locator/OsgiLocator.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/specs/trunk/locator/src/main/java/org/apache/servicemix/specs/locator/OsgiLocator.java?rev=707957&r1=707956&r2=707957&view=diff
==============================================================================
--- servicemix/smx4/specs/trunk/locator/src/main/java/org/apache/servicemix/specs/locator/OsgiLocator.java
(original)
+++ servicemix/smx4/specs/trunk/locator/src/main/java/org/apache/servicemix/specs/locator/OsgiLocator.java
Sun Oct 26 02:36:16 2008
@@ -21,17 +21,19 @@
 import java.util.Map;
 import java.util.HashMap;
 import java.util.ArrayList;
+import java.util.Set;
+import java.util.HashSet;
 
 public class OsgiLocator {
 
-    private static Map<String, List<Callable<Class>>> factories;
+    private static Map<String, Set<Callable<Class>>> factories;
 
     private OsgiLocator() {
     }
 
     public static synchronized void unregister(String id, Callable<Class> factory)
{
         if (factories != null) {
-            List<Callable<Class>> l = factories.get(id);
+            Set<Callable<Class>> l = factories.get(id);
             if (l != null) {
                 l.remove(factory);
             }
@@ -40,11 +42,11 @@
 
     public static synchronized void register(String id, Callable<Class> factory) {
         if (factories == null) {
-            factories = new HashMap<String, List<Callable<Class>>>();
+            factories = new HashMap<String, Set<Callable<Class>>>();
         }
-        List<Callable<Class>> l = factories.get(id);
+        Set<Callable<Class>> l = factories.get(id);
         if (l ==  null) {
-            l = new ArrayList<Callable<Class>>();
+            l = new HashSet<Callable<Class>>();
             factories.put(id, l);
         }
         l.add(factory);
@@ -52,9 +54,9 @@
 
     public static synchronized Class locate(String factoryId) {
         if (factories != null) {
-            List<Callable<Class>> l = factories.get(factoryId);
+            Set<Callable<Class>> l = factories.get(factoryId);
             if (l != null && !l.isEmpty()) {
-                Callable<Class> c = l.get(0);
+                Callable<Class> c = l.iterator().next();
                 try {
                     return c.call();
                 } catch (Exception e) {
@@ -67,7 +69,7 @@
 	public static synchronized List<Class> locateAll(String factoryId) {
 		List<Class> classes = new ArrayList<Class>();
         if (factories != null) {
-            List<Callable<Class>> l = factories.get(factoryId);
+            Set<Callable<Class>> l = factories.get(factoryId);
             if (l != null) {
                 for (Callable<Class> c : l) {
                 	try {



Mime
View raw message