felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rickh...@apache.org
Subject svn commit: r501123 - in /incubator/felix/trunk/framework/src/main/java/org/apache/felix/framework: BundleContextImpl.java Felix.java
Date Mon, 29 Jan 2007 17:38:04 GMT
Author: rickhall
Date: Mon Jan 29 09:37:55 2007
New Revision: 501123

URL: http://svn.apache.org/viewvc?view=rev&rev=501123
Log:
Applied patch (FELIX-32) to implement getAllServiceReferences() method.

Modified:
    incubator/felix/trunk/framework/src/main/java/org/apache/felix/framework/BundleContextImpl.java
    incubator/felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java

Modified: incubator/felix/trunk/framework/src/main/java/org/apache/felix/framework/BundleContextImpl.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/framework/src/main/java/org/apache/felix/framework/BundleContextImpl.java?view=diff&rev=501123&r1=501122&r2=501123
==============================================================================
--- incubator/felix/trunk/framework/src/main/java/org/apache/felix/framework/BundleContextImpl.java
(original)
+++ incubator/felix/trunk/framework/src/main/java/org/apache/felix/framework/BundleContextImpl.java
Mon Jan 29 09:37:55 2007
@@ -356,59 +356,8 @@
     {
         checkValidity();
         
-        // TODO: Implement BundleContext.getAllServiceReferences()
-        Object sm = System.getSecurityManager();
-        
-        if (sm != null)
-        {
-            ServiceReference[] refs = null;
-            
-            if (refs == null)
-            {
-                return refs;
-            }
-            
-            List result = new ArrayList();
-            
-            for (int i = 0;i < refs.length;i++)
-            {
-                String[] objectClass = (String[]) refs[i].getProperty(
-                    Constants.OBJECTCLASS);
-                
-                if (objectClass == null)
-                {
-                    continue;
-                }
-                
-                for (int j = 0;j < objectClass.length;j++)
-                {
-                    try
-                    {
-                        ((SecurityManager) sm).checkPermission(new ServicePermission(
-                            objectClass[j], ServicePermission.GET));
-                        
-                        result.add(refs[i]);
-                        
-                        break;
-                    } 
-                    catch (Exception e)
-                    {
-                        
-                    }
-                }
-            }
-            
-            if (result.isEmpty())
-            {
-                return null;
-            }
-            
-            return (ServiceReference[]) result.toArray(new ServiceReference[result.size()]);
-        }
-        else
-        {
-            return null;
-        }
+        return m_felix.getAllowedServiceReferences(m_bundle, clazz, filter, false);
+
     }
 
     public ServiceReference[] getServiceReferences(String clazz, String filter)
@@ -416,58 +365,8 @@
     {
         checkValidity();
         
-        Object sm = System.getSecurityManager();
-        
-        if (sm != null)
-        {
-            ServiceReference[] refs = m_felix.getServiceReferences(m_bundle, clazz, filter);
-            
-            if (refs == null)
-            {
-                return refs;
-            }
-            
-            List result = new ArrayList();
-            
-            for (int i = 0;i < refs.length;i++)
-            {
-                String[] objectClass = (String[]) refs[i].getProperty(
-                    Constants.OBJECTCLASS);
-                
-                if (objectClass == null)
-                {
-                    continue;
-                }
-                
-                for (int j = 0;j < objectClass.length;j++)
-                {
-                    try
-                    {
-                        ((SecurityManager) sm).checkPermission(new ServicePermission(
-                            objectClass[j], ServicePermission.GET));
-                        
-                        result.add(refs[i]);
-                        
-                        break;
-                    } 
-                    catch (Exception e)
-                    {
-                        
-                    }
-                }
-            }
-            
-            if (result.isEmpty())
-            {
-                return null;
-            }
-            
-            return (ServiceReference[]) result.toArray(new ServiceReference[result.size()]);
-        }
-        else
-        {
-            return m_felix.getServiceReferences(m_bundle, clazz, filter);
-        }
+        return m_felix.getAllowedServiceReferences(m_bundle, clazz, filter, true);
+
     }
 
     public Object getService(ServiceReference ref)

Modified: incubator/felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java?view=diff&rev=501123&r1=501122&r2=501123
==============================================================================
--- incubator/felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java (original)
+++ incubator/felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java Mon
Jan 29 09:37:55 2007
@@ -504,7 +504,7 @@
         {
             StartLevel sl = (StartLevel) getService(
                 getBundle(0),
-                getServiceReferences((BundleImpl) getBundle(0), StartLevel.class.getName(),
null)[0]);
+                getServiceReferences((BundleImpl) getBundle(0), StartLevel.class.getName(),
null, true)[0]);
             if (sl instanceof StartLevelImpl)
             {
                 ((StartLevelImpl) sl).setStartLevelAndWait(startLevel);
@@ -586,7 +586,7 @@
         {
             StartLevelImpl sl = (StartLevelImpl) getService(
                 getBundle(0),
-                getServiceReferences((BundleImpl) getBundle(0), StartLevel.class.getName(),
null)[0]);
+                getServiceReferences((BundleImpl) getBundle(0), StartLevel.class.getName(),
null, true)[0]);
             sl.setStartLevelAndWait(0);
         }
         catch (InvalidSyntaxException ex)
@@ -2263,8 +2263,18 @@
         return reg;
     }
 
+    /**
+     * Retrieves an array of {@link ServiceReference} objects based on calling bundle,
+     * service class name, and filter expression.  Optionally checks for isAssignable to
+     * make sure that the service can be cast to the
+     * @param bundle Calling Bundle
+     * @param className Service Classname or <code>null</code> for all
+     * @param expr Filter Criteria or <code>null</code>
+     * @return Array of ServiceReference objects that meet the criteria
+     * @throws InvalidSyntaxException
+     */
     protected ServiceReference[] getServiceReferences(
-        BundleImpl bundle, String className, String expr)
+        BundleImpl bundle, String className, String expr, boolean checkAssignable)
         throws InvalidSyntaxException
     {
         // Define filter if expression is not null.
@@ -2277,23 +2287,20 @@
         // Ask the service registry for all matching service references.
         List refList = m_registry.getServiceReferences(className, filter);
 
-        // The returned reference list must be filtered for two cases:
-        // 1) The requesting bundle may not be wired to the same class
-        //    as the providing bundle (i.e, different versions), so filter
-        //    any services for which the requesting bundle might get a
-        //    class cast exception.
-        // 2) Security is enabled and the requesting bundle does not have
-        //    permission access the service.
-        for (int refIdx = 0; (refList != null) && (refIdx < refList.size()); refIdx++)
+        // Filter on assignable references
+        if (checkAssignable)
         {
-            // Get the current service reference.
-            ServiceReference ref = (ServiceReference) refList.get(refIdx);
-
-            // Now check for castability.
-            if (!Felix.isServiceAssignable(bundle, ref))
+            for (int refIdx = 0; (refList != null) && (refIdx < refList.size());
refIdx++)
             {
-                refList.remove(refIdx);
-                refIdx--;
+                // Get the current service reference.
+                ServiceReference ref = (ServiceReference) refList.get(refIdx);
+
+                // Now check for castability.
+                if (!Felix.isServiceAssignable(bundle, ref))
+                {
+                    refList.remove(refIdx);
+                    refIdx--;
+                }
             }
         }
 
@@ -2303,6 +2310,67 @@
         }
 
         return null;
+    }
+
+    /**
+     * Retrieves Array of {@link ServiceReference} objects based on calling bundle, service
class name,
+     * optional filter expression, and optionally filters further on the version.
+     * If running under a {@link SecurityManager}, checks that the calling bundle has permissions
to
+     * see the service references and removes references that aren't.
+     * @param bundle Calling Bundle
+     * @param className Service Classname or <code>null</code> for all
+     * @param expr Filter Criteria or <code>null</code>
+     * @param checkAssignable <code>true</code> to check for isAssignable, <code>false</code>
to return all versions
+     * @return Array of ServiceReference objects that meet the criteria
+     * @throws InvalidSyntaxException
+     */
+    protected ServiceReference[] getAllowedServiceReferences(
+        BundleImpl bundle, String className, String expr, boolean checkAssignable)
+        throws InvalidSyntaxException
+    {
+        ServiceReference[] refs = getServiceReferences(bundle, className, expr, checkAssignable);
+
+        Object sm = System.getSecurityManager();
+
+        if (sm == null)
+        {
+            return refs;
+        }
+
+        List result = new ArrayList();
+
+        for (int i = 0;i < refs.length;i++)
+        {
+            String[] objectClass = (String[]) refs[i].getProperty(Constants.OBJECTCLASS);
+
+            if (objectClass == null)
+            {
+                continue;
+            }
+
+            for (int j = 0; j < objectClass.length; j++)
+            {
+                try
+                {
+                    ((SecurityManager) sm).checkPermission(new ServicePermission(
+                        objectClass[j], ServicePermission.GET));
+                    result.add(refs[i]);
+                    break;
+                }
+                catch (Exception ex)
+                {
+                    // Ignore, since we are just testing permission.
+                }
+            }
+        }
+
+        if (result.isEmpty())
+        {
+            return null;
+        }
+
+        return (ServiceReference[]) result.toArray(new ServiceReference[result.size()]);
+
     }
 
     /**



Mime
View raw message