tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rf...@apache.org
Subject svn commit: r831138 - /tuscany/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/TopologyManagerImpl.java
Date Thu, 29 Oct 2009 22:51:26 GMT
Author: rfeng
Date: Thu Oct 29 22:51:25 2009
New Revision: 831138

URL: http://svn.apache.org/viewvc?rev=831138&view=rev
Log:
Add synchronization against listeners and check if it is removed

Modified:
    tuscany/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/TopologyManagerImpl.java

Modified: tuscany/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/TopologyManagerImpl.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/TopologyManagerImpl.java?rev=831138&r1=831137&r2=831138&view=diff
==============================================================================
--- tuscany/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/TopologyManagerImpl.java
(original)
+++ tuscany/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/TopologyManagerImpl.java
Thu Oct 29 22:51:25 2009
@@ -104,7 +104,8 @@
         remoteAdmins.open();
 
         // DO NOT register EventHook.class.getName() as it cannot report existing services
-        String interfaceNames[] = new String[] {ListenerHook.class.getName(), RemoteServiceAdminListener.class.getName()};
+        String interfaceNames[] =
+            new String[] {ListenerHook.class.getName(), RemoteServiceAdminListener.class.getName()};
         // The registration will trigger the added() method before registration is assigned
         registration = context.registerService(interfaceNames, this, null);
 
@@ -189,39 +190,41 @@
      * @see org.osgi.framework.hooks.service.ListenerHook#added(java.util.Collection)
      */
     public void added(Collection listeners) {
-        try {
-            Collection<ListenerInfo> listenerInfos = (Collection<ListenerInfo>)listeners;
-            boolean changed = false;
-            for (ListenerInfo l : listenerInfos) {
-                if (!l.isRemoved() && l.getBundleContext() != context) {
-                    String key = l.getFilter();
-                    if (key == null) {
-                        // key = "";
-                        // FIXME: It should always match, let's ignore it for now
-                        logger.warning("Service listner without a filter is skipped: " +
l);
-                        continue;
-                    }
-                    Collection<ListenerInfo> infos = serviceListeners.get(key);
-                    if (infos == null) {
-                        infos = new HashSet<ListenerInfo>();
-                        serviceListeners.put(key, infos);
+        synchronized (serviceListeners) {
+            try {
+                Collection<ListenerInfo> listenerInfos = (Collection<ListenerInfo>)listeners;
+                boolean changed = false;
+                for (ListenerInfo l : listenerInfos) {
+                    if (!l.isRemoved() && l.getBundleContext() != context) {
+                        String key = l.getFilter();
+                        if (key == null) {
+                            // key = "";
+                            // FIXME: It should always match, let's ignore it for now
+                            logger.warning("Service listner without a filter is skipped:
" + l);
+                            continue;
+                        }
+                        Collection<ListenerInfo> infos = serviceListeners.get(key);
+                        if (infos == null) {
+                            infos = new HashSet<ListenerInfo>();
+                            serviceListeners.put(key, infos);
+                        }
+                        infos.add(l);
+                        changed = true;
                     }
-                    infos.add(l);
-                    changed = true;
                 }
-            }
-            if (changed) {
-                updateEndpointListenerScope();
-            }
-        } catch (Throwable e) {
-            logger.log(Level.SEVERE, e.getMessage(), e);
-            if (e instanceof Error) {
-                throw (Error)e;
-            } else if (e instanceof RuntimeException) {
-                throw (RuntimeException)e;
-            } else {
-                // Should not happen
-                throw new RuntimeException(e);
+                if (changed) {
+                    updateEndpointListenerScope();
+                }
+            } catch (Throwable e) {
+                logger.log(Level.SEVERE, e.getMessage(), e);
+                if (e instanceof Error) {
+                    throw (Error)e;
+                } else if (e instanceof RuntimeException) {
+                    throw (RuntimeException)e;
+                } else {
+                    // Should not happen
+                    throw new RuntimeException(e);
+                }
             }
         }
     }
@@ -235,60 +238,74 @@
     }
 
     private CollectionMap<Class<?>, ListenerInfo> findServiceListeners(EndpointDescription
endpointDescription,
-                                                                           String matchedFilter)
{
-        // First find all the listeners that have the matching filter
-        Collection<ListenerInfo> listeners = serviceListeners.get(matchedFilter);
-        if (listeners == null) {
-            return null;
-        }
-
-        // Try to partition the listeners by the interface classes 
-        List<String> interfaceNames = endpointDescription.getInterfaces();
-        CollectionMap<Class<?>, ListenerInfo> interfaceToListeners =
-            new CollectionMap<Class<?>, ListenerInfo>();
-        for (String i : interfaceNames) {
-            for (ListenerInfo listener : listeners) {
-                try {
-                    Class<?> interfaceClass = listener.getBundleContext().getBundle().loadClass(i);
-                    interfaceToListeners.putValue(interfaceClass, listener);
-                } catch (ClassNotFoundException e) {
-                    // Ignore the listener as it cannot load the interface class
+                                                                       String matchedFilter)
{
+        synchronized (serviceListeners) {
+
+            // First find all the listeners that have the matching filter
+            Collection<ListenerInfo> listeners = serviceListeners.get(matchedFilter);
+            if (listeners == null) {
+                return null;
+            }
+
+            // Try to partition the listeners by the interface classes 
+            List<String> interfaceNames = endpointDescription.getInterfaces();
+            CollectionMap<Class<?>, ListenerInfo> interfaceToListeners = new
CollectionMap<Class<?>, ListenerInfo>();
+            for (String i : interfaceNames) {
+                for (Iterator<ListenerInfo> it = listeners.iterator(); it.hasNext();)
{
+                    try {
+                        ListenerInfo listener = it.next();
+                        if (listener.isRemoved()) {
+                            it.remove();
+                            continue;
+                        }
+                        try {
+                            Class<?> interfaceClass = listener.getBundleContext().getBundle().loadClass(i);
+                            interfaceToListeners.putValue(interfaceClass, listener);
+                        } catch (IllegalStateException e) {
+                            logger.log(Level.WARNING, e.getMessage(), e);
+                            // Ignore the exception
+                        }
+                    } catch (ClassNotFoundException e) {
+                        // Ignore the listener as it cannot load the interface class
+                    }
                 }
             }
+            return interfaceToListeners;
         }
-        return interfaceToListeners;
     }
 
     /**
      * @see org.osgi.framework.hooks.service.ListenerHook#removed(java.util.Collection)
      */
     public void removed(Collection listeners) {
-        try {
-            Collection<ListenerInfo> listenerInfos = (Collection<ListenerInfo>)listeners;
-            boolean changed = false;
-            for (ListenerInfo l : listenerInfos) {
-                if (registration != null && l.getBundleContext() != context) {
-                    String key = l.getFilter();
-                    if (key == null) {
-                        continue;
-                    }
-                    if (serviceListeners.removeValue(key, l)) {
-                        changed = true;
+        synchronized (serviceListeners) {
+            try {
+                Collection<ListenerInfo> listenerInfos = (Collection<ListenerInfo>)listeners;
+                boolean changed = false;
+                for (ListenerInfo l : listenerInfos) {
+                    if (registration != null && l.getBundleContext() != context)
{
+                        String key = l.getFilter();
+                        if (key == null) {
+                            continue;
+                        }
+                        if (serviceListeners.removeValue(key, l)) {
+                            changed = true;
+                        }
                     }
                 }
-            }
-            if (changed) {
-                updateEndpointListenerScope();
-            }
-        } catch (Throwable e) {
-            logger.log(Level.SEVERE, e.getMessage(), e);
-            if (e instanceof Error) {
-                throw (Error)e;
-            } else if (e instanceof RuntimeException) {
-                throw (RuntimeException)e;
-            } else {
-                // Should not happen
-                throw new RuntimeException(e);
+                if (changed) {
+                    updateEndpointListenerScope();
+                }
+            } catch (Throwable e) {
+                logger.log(Level.SEVERE, e.getMessage(), e);
+                if (e instanceof Error) {
+                    throw (Error)e;
+                } else if (e instanceof RuntimeException) {
+                    throw (RuntimeException)e;
+                } else {
+                    // Should not happen
+                    throw new RuntimeException(e);
+                }
             }
         }
     }
@@ -385,9 +402,9 @@
             remoteAdmins.close();
             remoteAdmins = null;
         }
-        for (Collection<ListenerInfo> infos : serviceListeners.values()) {
+        synchronized (serviceListeners) {
+            serviceListeners.clear();
         }
-        serviceListeners.clear();
     }
 
 }



Mime
View raw message