You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by pa...@apache.org on 2006/08/12 11:24:37 UTC

svn commit: r431001 - /incubator/felix/trunk/org.apache.felix.eventadmin.bridge.upnp/src/main/java/org/apache/felix/eventadmin/bridge/upnp/UPnPEventToEventAdminBridge.java

Author: pauls
Date: Sat Aug 12 02:24:37 2006
New Revision: 431001

URL: http://svn.apache.org/viewvc?rev=431001&view=rev
Log:
Remove dependencies on java > 1.3 and an unnecessary inner class plus some clean-up.

Modified:
    incubator/felix/trunk/org.apache.felix.eventadmin.bridge.upnp/src/main/java/org/apache/felix/eventadmin/bridge/upnp/UPnPEventToEventAdminBridge.java

Modified: incubator/felix/trunk/org.apache.felix.eventadmin.bridge.upnp/src/main/java/org/apache/felix/eventadmin/bridge/upnp/UPnPEventToEventAdminBridge.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/org.apache.felix.eventadmin.bridge.upnp/src/main/java/org/apache/felix/eventadmin/bridge/upnp/UPnPEventToEventAdminBridge.java?rev=431001&r1=431000&r2=431001&view=diff
==============================================================================
--- incubator/felix/trunk/org.apache.felix.eventadmin.bridge.upnp/src/main/java/org/apache/felix/eventadmin/bridge/upnp/UPnPEventToEventAdminBridge.java (original)
+++ incubator/felix/trunk/org.apache.felix.eventadmin.bridge.upnp/src/main/java/org/apache/felix/eventadmin/bridge/upnp/UPnPEventToEventAdminBridge.java Sat Aug 12 02:24:37 2006
@@ -59,13 +59,13 @@
         + EventConstants.EVENT_TOPIC + "=org/osgi/service/upnp/\\*)("
         + EventConstants.EVENT_TOPIC + "=org/osgi/service/upnp/UPnPEvent)))";
 
-    private final Object m_lock = new Object();
+    final Object m_lock = new Object();
 
     // The references to the EventAdmins
-    private final Set m_adminRefs = new HashSet();
+    final Set m_adminRefs = new HashSet();
 
     // The references to the EventHandlers
-    private final Set m_handlerRefs = new HashSet();
+    final Set m_handlerRefs = new HashSet();
 
     private final BundleContext m_context;
 
@@ -181,7 +181,7 @@
     // least one EventHandler (i.e., !m_handlerRefs.isEmpty()) present and it is
     // not already registers. Respectively, it unregisters itself in case one of
     // the above is false.
-    private void check()
+    void check()
     {
         // do we need to be registered?
         if(m_adminRefs.isEmpty() || m_handlerRefs.isEmpty())
@@ -237,27 +237,24 @@
                 }
             }
 
-            // parts will one be empty if there is no handler with a valid
-            // filter
-            // and we only need to register with the new filter if it doesn't
-            // equal
-            // the last filter
+            // parts will only be empty if there is no handler with a valid
+            // filter and we only need to register with the new filter if it 
+            // doesn't equals the last filter
             if(!parts.isEmpty() && !parts.equals(last))
             {
                 last = parts;
 
                 try
                 {
-                    change(new Hashtable()
-                    {
-                        {
-                            put(UPnPEventListener.UPNP_FILTER, m_context
-                                .createFilter(result.append(")").toString()
-                                    .replaceAll("upnp.serviceId",
-                                        UPnPService.ID).replaceAll(
-                                        "upnp.deviceId", UPnPDevice.ID)));
-                        }
-                    });
+                    final Hashtable properties = new Hashtable();
+                    
+                    properties.put(UPnPEventListener.UPNP_FILTER, 
+                        m_context.createFilter(replaceAll(replaceAll(
+                        result.append(")").toString().toCharArray(),
+                        serviceChars, UPnPService.ID).toCharArray(), 
+                        deviceChars, UPnPDevice.ID)));
+                    
+                    change(properties);
                 } catch(InvalidSyntaxException e)
                 {
                     // This will never happen
@@ -266,6 +263,52 @@
             }
         }
     }
+    
+    private static final char[] serviceChars = new char[]{'u','p','n','p','.','s','e','r','v','i','c','e','i','d'};
+    private static final char[] deviceChars = new char[]{'u','p','n','p','.','d','e','v','i','c','e','i','d'};
+    
+    private String replaceAll(final char[] source, final char[] pattern, final String target)
+    {
+        StringBuffer result = new StringBuffer();
+        
+        int pos = 0, matchPos = 0;
+        
+        while(true)
+        {   
+            if(pattern[matchPos] == Character.toLowerCase(source[pos]))
+            {
+                matchPos++;
+                if(matchPos == pattern.length)
+                {
+                    result.append(target);
+                    matchPos = 0;
+                }
+            }
+            else if(matchPos > 0 )
+            {
+                result.append(source, pos - matchPos, matchPos + 1);
+                matchPos = 0;
+            }
+            else
+            {
+                result.append(source[pos]);
+            }
+            
+            pos++;
+            
+            if(pos >= source.length)
+            {
+                if(matchPos > 0)
+                {
+                    result.append(source, pos - matchPos, matchPos);
+                }
+                
+                break;
+            }
+        }
+        
+        return result.toString();
+    }
 
     private void change(final Dictionary filter)
     {
@@ -361,17 +404,16 @@
                     }
                 };
 
-                eventAdmin.postEvent(new Event(
-                    "org/osgi/service/upnp/UPnPEvent", new Hashtable()
-                    {
-                        {
-                            put(UPnPDevice.ID, deviceId);
-                            put(UPnPService.ID, serviceId);
-                            put("upnp.serviceId", serviceId);
-                            put("upnp.deviceId", deviceId);
-                            put("upnp.events", immutableEvents);
-                        }
-                    }));
+                final Hashtable properties = new Hashtable();
+                
+                properties.put(UPnPDevice.ID, deviceId);
+                properties.put(UPnPService.ID, serviceId);
+                properties.put("upnp.serviceId", serviceId);
+                properties.put("upnp.deviceId", deviceId);
+                properties.put("upnp.events", immutableEvents);
+                
+                eventAdmin.postEvent(
+                    new Event("org/osgi/service/upnp/UPnPEvent", properties));
 
                 m_context.ungetService(ref);
             }