felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rickh...@apache.org
Subject svn commit: r440759 - /incubator/felix/trunk/scr/src/main/java/org/apache/felix/scr/ComponentManagerImpl.java
Date Wed, 06 Sep 2006 16:17:20 GMT
Author: rickhall
Date: Wed Sep  6 09:17:19 2006
New Revision: 440759

URL: http://svn.apache.org/viewvc?view=rev&rev=440759
Log:
Applies patch (FELIX-128) to implement ComponentContext methods.

Modified:
    incubator/felix/trunk/scr/src/main/java/org/apache/felix/scr/ComponentManagerImpl.java

Modified: incubator/felix/trunk/scr/src/main/java/org/apache/felix/scr/ComponentManagerImpl.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/scr/src/main/java/org/apache/felix/scr/ComponentManagerImpl.java?view=diff&rev=440759&r1=440758&r2=440759
==============================================================================
--- incubator/felix/trunk/scr/src/main/java/org/apache/felix/scr/ComponentManagerImpl.java
(original)
+++ incubator/felix/trunk/scr/src/main/java/org/apache/felix/scr/ComponentManagerImpl.java
Wed Sep  6 09:17:19 2006
@@ -23,6 +23,7 @@
 import java.util.List;
 import java.util.Set;
 
+import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceFactory;
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.framework.BundleContext;
@@ -203,7 +204,7 @@
 	            
 	            // 112.4.4 The class must be public and have a public constructor without arguments
so component instances
 	            // may be created by the SCR with the newInstance method on Class
-	            m_componentContext = new ComponentContextImpl();
+	            m_componentContext = new ComponentContextImpl(null);
 	            m_implementationObject = c.newInstance();
 	        }
 	        catch (Exception ex)
@@ -909,33 +910,126 @@
      */
     class ComponentContextImpl implements ComponentContext {
 
+        private Bundle m_usingBundle;
+
+        ComponentContextImpl(Bundle usingBundle)
+        {
+            m_usingBundle = usingBundle;
+        }
+
     	public Dictionary getProperties() {
     		//TODO: 112.11.3.5 The Dictionary is read-only and cannot be modified
     		return m_componentMetadata.getProperties();
     	}
 
-    	public Object locateService(String arg0) {
-    		// TODO implement this method
-    		return null;
-    	}
-
-    	public Object locateService(String arg0, ServiceReference arg1) {
-    		// TODO implement this method
-    		return null;
+        public Object locateService(String name) {
+            DependencyManager dm = getDependencyManager(name);
+            if (dm == null || dm.m_boundServicesRefs.isEmpty())
+            {
+                return null;
+            }
+            
+            ServiceReference selectedRef;
+            if (dm.m_boundServicesRefs.size() == 1)
+            {
+                // short cut for single bound service
+                selectedRef = (ServiceReference) dm.m_boundServicesRefs.iterator().next();
+            }
+            else
+            {
+                // is it correct to assume an ordered bound services set ? 
+                int maxRanking = Integer.MIN_VALUE;
+                long minId = Long.MAX_VALUE;
+                selectedRef = null;
+                
+                Iterator it = dm.m_boundServicesRefs.iterator();
+                while (it.hasNext())
+                {
+                    ServiceReference ref = (ServiceReference) it.next();
+                    Integer rank = (Integer) ref.getProperty(Constants.SERVICE_RANKING);
+                    int ranking = (rank == null) ? Integer.MIN_VALUE : rank.intValue();
+                    long id = ((Long) ref.getProperty(Constants.SERVICE_ID)).longValue();
+                    if (maxRanking < ranking || (maxRanking == ranking && id <
minId))
+                    {
+                        maxRanking = ranking;
+                        minId = id;
+                        selectedRef = ref;
+                    }
+                }
+            }
+    
+            // this is not realistic, as at least one service is available
+            // whose service id is smaller than Long.MAX_VALUE, still be sure
+            if (selectedRef == null)
+            {
+                return null;
+            }
+            
+            // return the service for the selected reference
+            return getBundleContext().getService(selectedRef);
+   	    }
+
+        public Object locateService(String name, ServiceReference ref) {
+            DependencyManager dm = getDependencyManager(name);
+            if (dm == null || dm.m_boundServicesRefs.isEmpty())
+            {
+                return null;
+            }
+            
+            // is it correct to assume an ordered bound services set ? 
+            Iterator it = dm.m_boundServicesRefs.iterator();
+            while (it.hasNext())
+            {
+                if (it.next().equals(ref))
+                {
+                    return getBundleContext().getService(ref);
+                }
+            }
+            
+            // no matching name and service reference found
+            return null;
     	}
 
-    	public Object[] locateServices(String arg0) {
-    		// TODO implement this method
-    		return null;
+        public Object[] locateServices(String name) {
+            DependencyManager dm = getDependencyManager(name);
+            if (dm == null || dm.m_boundServicesRefs.isEmpty())
+            {
+                return null;
+            }
+            
+            Object[] services = new Object[dm.m_boundServicesRefs.size()];
+            Iterator it = dm.m_boundServicesRefs.iterator();
+            for (int i=0; i < services.length && it.hasNext(); i++)
+            {
+                ServiceReference ref = (ServiceReference) it.next();
+                services[i] = getBundleContext().getService(ref);
+            }
+            return services;
     	}
 
+        private DependencyManager getDependencyManager(String name) {
+            Iterator it = m_dependencyManagers.iterator();
+            while (it.hasNext())
+            {
+                DependencyManager dm = (DependencyManager)it.next();
+                
+                // if any of the dependency managers is unable to bind (it is invalid), the
component is deactivated
+                if (name.equals(dm.m_dependencyMetadata.getName()))
+                {
+                    return dm;
+                }
+            }
+            
+            // not found
+            return null;
+        }
+        
     	public BundleContext getBundleContext() {
     		return m_activator.getBundleContext();
     	}
 
     	public Bundle getUsingBundle() {
-    		// TODO implement this method
-    		return null;
+            return m_usingBundle;
     	}
 
     	public ComponentInstance getComponentInstance() {
@@ -983,7 +1077,7 @@
 	            
 	            // 112.4.4 The class must be public and have a public constructor without arguments
so component instances
 	            // may be created by the SCR with the newInstance method on Class
-	            m_componentContext = new ComponentContextImpl();
+	            m_componentContext = new ComponentContextImpl(arg0);
 	            m_implementationObject = c.newInstance();
 	        }
 	        catch (Exception ex)



Mime
View raw message