Applied patch (FELIX-175) to fix some iPOJO bugs as well as introduce
new concepts for factories.


git-svn-id: https://svn.apache.org/repos/asf/incubator/felix/trunk@476513 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/ComponentManager.java b/ipojo/src/main/java/org/apache/felix/ipojo/ComponentManager.java
index 37bfe64..b580ed6 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/ComponentManager.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/ComponentManager.java
@@ -18,6 +18,8 @@
  */
 package org.apache.felix.ipojo;
 
+import org.osgi.framework.BundleContext;
+
 /**
  * The component manager class manages one instance of a component type.
  * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
@@ -65,6 +67,16 @@
      * @return the factory of the component
      */
     ComponentManagerFactory getFactory();
+    
+    /**
+     * @return the context of the component manager
+     */
+    BundleContext getContext();
+    
+    /**
+     * @return the name of the component instance
+     */
+    String getName();
 
 
 }
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/ComponentManagerFactory.java b/ipojo/src/main/java/org/apache/felix/ipojo/ComponentManagerFactory.java
index bd833ca..e45d5f6 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/ComponentManagerFactory.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/ComponentManagerFactory.java
@@ -26,6 +26,7 @@
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Properties;
 import java.util.logging.Level;
 
@@ -74,6 +75,12 @@
      * Component Type provided by this factory.
      */
     private Element m_componentMetadata;
+    
+    /**
+     * Factory Name / PID.
+     * Could be the component class name if the factory name is not set.
+     */
+    private String m_factoryName;
 
     /**
      * Service Registration of this factory (Facotry & ManagedServiceFactory).
@@ -84,6 +91,7 @@
      * Component-Type info exposed by the factory service.
      */
     private ComponentInfo m_componentInfo;
+    
 
     //End field
 
@@ -143,7 +151,7 @@
 
 
     /**
-     * @return the iPOJO activator reference
+     * @return the Bundle Context
      */
     public BundleContext getBundleContext() { return m_bundleContext; }
 
@@ -161,6 +169,11 @@
         m_bundleContext = bc;
         m_componentClassName = cm.getAttribute("className");
         m_componentMetadata = cm;
+        
+        // Get factory PID :
+        if (m_componentMetadata.containsAttribute("factory") && !m_componentMetadata.getAttribute("factory").equalsIgnoreCase("no")) { m_factoryName = m_componentMetadata.getAttribute("factory"); }
+        else { m_factoryName = m_componentMetadata.getAttribute("className"); }
+
     }
 
     /**
@@ -170,10 +183,14 @@
      * @param cm : metadata of the component
      */
     public ComponentManagerFactory(BundleContext bc, byte[] clazz, Element cm) {
-        m_bundleContext = bc;
+    	m_bundleContext = bc;
         m_clazz = clazz;
         m_componentClassName = cm.getAttribute("className");
         m_componentMetadata = cm;
+        
+        // Get factory PID :
+        if (m_componentMetadata.containsAttribute("factory") && !m_componentMetadata.getAttribute("factory").equalsIgnoreCase("no")) { m_factoryName = m_componentMetadata.getAttribute("factory"); }
+        else { m_factoryName = m_componentMetadata.getAttribute("className"); }
     }
 
     /**
@@ -206,14 +223,14 @@
      */
     public void start() {
         Activator.getLogger().log(Level.INFO, "[Bundle " + m_bundleContext.getBundle().getBundleId() + "] Start the component factory");
-
+        
         // Check if the factory should be exposed
         if (m_componentMetadata.containsAttribute("factory") && m_componentMetadata.getAttribute("factory").equalsIgnoreCase("no")) { return; }
         Properties props = new Properties();
         props.put("component.class", m_componentClassName);
 
         // create a ghost component
-        ComponentManagerImpl ghost = new ComponentManagerImpl(this);
+        ComponentManagerImpl ghost = new ComponentManagerImpl(this, m_bundleContext);
         ghost.configure(m_componentMetadata, new Properties());
         m_componentInfo = ghost.getComponentInfo();
 
@@ -221,9 +238,8 @@
         props.put("component.properties", m_componentInfo.getProperties());
         props.put("component.information", m_componentInfo.toString());
 
-        // Get factory PID :
-        if (m_componentMetadata.containsAttribute("name")) { props.put(Constants.SERVICE_PID, m_componentMetadata.getAttribute("name")); }
-        else { props.put(Constants.SERVICE_PID, m_componentMetadata.getAttribute("className")); }
+        // Add Facotry PID to the component properties
+        props.put(Constants.SERVICE_PID, m_factoryName);
 
         // Exposition of the factory service
         m_sr = m_bundleContext.registerService(new String[] {Factory.class.getName(), ManagedServiceFactory.class.getName()}, this, props);
@@ -273,7 +289,9 @@
      */
     public ComponentManager createComponent(Dictionary configuration) {
         Activator.getLogger().log(Level.INFO, "[Bundle " + m_bundleContext.getBundle().getBundleId() + "] Create a component and start it");
-        ComponentManagerImpl component = new ComponentManagerImpl(this);
+        IPojoContext context = new IPojoContext(m_bundleContext);
+        ComponentManagerImpl component = new ComponentManagerImpl(this, context);
+        context.setComponentInstance(component);
         component.configure(m_componentMetadata, configuration);
 
         String pid = null;
@@ -284,6 +302,27 @@
         component.start();
         return component;
     }
+    
+    /**
+     * @see org.apache.felix.ipojo.Factory#createComponent(java.util.Dictionary)
+     */
+    public ComponentManager createComponent(Dictionary configuration, ServiceContext serviceContext) {
+        Activator.getLogger().log(Level.INFO, "[Bundle " + m_bundleContext.getBundle().getBundleId() + "] Create a component and start it");
+        IPojoContext context = new IPojoContext(m_bundleContext, serviceContext);
+        ComponentManagerImpl component = new ComponentManagerImpl(this, context);
+        context.setComponentInstance(component);
+        component.configure(m_componentMetadata, configuration);
+
+        String pid = null;
+        if (configuration.get("name") != null) { pid = (String) configuration.get("name"); }
+        else { pid = m_componentMetadata.getAttribute("className"); }
+
+        m_componentManagers.put(pid, component);
+        component.start();
+        return component;
+    }
+    
+    
 
     /**
      * @see org.osgi.service.cm.ManagedServiceFactory#deleted(java.lang.String)
@@ -297,10 +336,7 @@
     /**
      * @see org.osgi.service.cm.ManagedServiceFactory#getName()
      */
-    public String getName() {
-        if (m_componentMetadata.containsAttribute("name")) { return m_componentMetadata.getAttribute("name"); }
-        else { return m_componentMetadata.getAttribute("className"); }
-    }
+    public String getName() { return getFactoryName(); }
 
     /**
      * @see org.osgi.service.cm.ManagedServiceFactory#updated(java.lang.String, java.util.Dictionary)
@@ -314,5 +350,28 @@
             cm.start(); // restart it
         }
     }
+    
+    /**
+     * @return the factory name
+     */
+    public String getFactoryName() { return m_factoryName; }
+    
+    /**
+     * Check if the given configuration is acceptable as a component instance configuration.
+     * This checks that a name is given in the configuration and if all the configurable properties have a value.
+     * @param conf : the configuration to check
+     * @return true when the configuration is acceptable
+     */
+    public boolean isAcceptable(Dictionary conf) {
+    	// First check that the configuration contains a name : 
+    	if(conf.get("name") == null) { return false; }
+    	List props = m_componentInfo.getProperties();
+    	for(int i = 0; i < props.size(); i++) {
+    		PropertyInfo pi = (PropertyInfo) props.get(i);
+    		// Failed if the props has no default value and the configuration does not push a value 
+    		if(pi.getValue() == null && conf.get(pi.getName()) == null) { return false; }
+    	}
+    	return true;
+    }
 
 }
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/ComponentManagerImpl.java b/ipojo/src/main/java/org/apache/felix/ipojo/ComponentManagerImpl.java
index af393e4..542c95c 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/ComponentManagerImpl.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/ComponentManagerImpl.java
@@ -43,6 +43,11 @@
      * Attached metadata of the managed component.
      */
     private ComponentMetadata m_metadata;
+    
+    /**
+     * Name of the component instance.
+     */
+    private String m_name;
 
     /**
      * The context of the component.
@@ -86,9 +91,9 @@
      * Construct a new Component Manager.
      * @param factory : the factory managing the component manager
      */
-    public ComponentManagerImpl(ComponentManagerFactory factory) {
+    public ComponentManagerImpl(ComponentManagerFactory factory, BundleContext bc) {
         m_factory = factory;
-        m_context = factory.getBundleContext();
+        m_context = bc;
         Activator.getLogger().log(Level.INFO, "[Bundle " + m_context.getBundle().getBundleId() + "] Create a component manager from the factory " + m_factory);
     }
 
@@ -109,9 +114,12 @@
         // Change the metadata
         m_metadata = new ComponentMetadata(cm);
 
-        // COmponentInfo initialization
+        // ComponentInfo initialization
         m_componentInfo = new ComponentInfo();
         m_componentInfo.setClassName(m_metadata.getClassName());
+        
+        // Add the name
+        m_name = (String) configuration.get("name");
 
         // Create the standard handlers and add these handlers to the list
         for (int i = 0; i < IPojoConfiguration.INTERNAL_HANDLERS.length; i++) {
@@ -174,6 +182,11 @@
         }
         return null;
     }
+    
+    /**
+     * @return the component instance name.
+     */
+    public String getComponentName() { return m_name; }
 
     // ===================== Lifecycle management =====================
 
@@ -330,7 +343,7 @@
             try {
                 Constructor constructor = m_clazz.getConstructor(new Class[] {ComponentManagerImpl.class, BundleContext.class});
                 constructor.setAccessible(true);
-                instance = constructor.newInstance(new Object[] {this, m_factory.getBundleContext()});
+                instance = constructor.newInstance(new Object[] {this, m_context});
             }
             catch (NoSuchMethodException e) {
                 Activator.getLogger().log(Level.INFO, "[" + m_metadata.getClassName() + "] createInstance -> No constructor with a bundle context");
@@ -537,7 +550,7 @@
         Handler[] list = (Handler[]) m_fieldRegistration.get(fieldName);
 
         for (int i = 0; list != null && i < list.length; i++) {
-            m_handlers[i].setterCallback(fieldName, objectValue);
+            list[i].setterCallback(fieldName, objectValue);
         }
     }
 
@@ -573,6 +586,8 @@
         Activator.getLogger().log(Level.INFO, "[" + m_metadata.getClassName() + "] Component Manager : " + m_state);
     }
 
+	public String getName() { return m_name; }
+
 
     // ======================= end Handlers Management =====================
 
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/Factory.java b/ipojo/src/main/java/org/apache/felix/ipojo/Factory.java
index 2a99e29..9d8e8c5 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/Factory.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/Factory.java
@@ -29,15 +29,31 @@
 
     /**
      * Create a component manager (i.e. component type instance).
-     * @param configuration : the configuration property for this component.
+     * @param configuration : the configuration properties for this component.
      * @return the created component manager.
      */
     ComponentManager createComponent(Dictionary configuration);
+    
+    /**
+     * Create a component manager (i.e. component type instance).
+     * This has these service interaction in the scope given in argument.
+     * @param configuration : the configuration properties for this component.
+     * @param serviceContext : the service context of the component.
+     * @return the created component manager.
+     */
+    ComponentManager createComponent(Dictionary configuration, ServiceContext serviceContext);
 
     /**
      * Get the component type information containing provided service, configuration properties ...
      * @return the compionent type information.
      */
     ComponentInfo getComponentInfo();
+    
+    /**
+     * Check if the given configuration is acceptable as a configuration of a component instance.
+     * @param conf : the configuration to test
+     * @return true if the configuration is acceptable
+     */
+    boolean isAcceptable(Dictionary conf);
 
 }
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/IPojoContext.java b/ipojo/src/main/java/org/apache/felix/ipojo/IPojoContext.java
new file mode 100644
index 0000000..07c96fd
--- /dev/null
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/IPojoContext.java
@@ -0,0 +1,201 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.ipojo;
+
+import java.io.File;
+import java.io.InputStream;
+import java.util.Dictionary;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.BundleListener;
+import org.osgi.framework.Filter;
+import org.osgi.framework.FrameworkListener;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceListener;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+
+/**
+ * The iPOJO Context is a BundleContext implementation allowing the separation between Bundle context and Service (Bundle) Context
+ * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
+ */
+public class IPojoContext implements BundleContext {
+	
+	/**
+	 * BundleContext used to access bundle method
+	 */
+	private BundleContext m_bundleContext;
+	
+	/**
+	 * Service Context used to access service interaction 
+	 */
+	private ServiceContext m_serviceContext;
+	
+	/**
+	 * Constructor.
+	 * Used when the service context = the bundle context
+	 * @param bc : bundle context
+	 */
+	public IPojoContext(BundleContext bc) {
+		m_bundleContext = bc;
+		m_serviceContext = new ServiceContextImpl(bc);
+	}
+	
+	/**
+	 * Constructor.
+	 * Used when the service context and the bundle context are different
+	 * @param bc : bundle context
+	 * @param sc : service context
+	 */
+	public IPojoContext(BundleContext bc, ServiceContext sc) {
+		m_bundleContext = bc;
+		m_serviceContext = sc;
+	}
+
+	/**
+	 * @see org.osgi.framework.BundleContext#addBundleListener(org.osgi.framework.BundleListener)
+	 */
+	public void addBundleListener(BundleListener listener) { m_bundleContext.addBundleListener(listener); }
+
+	/**
+	 * @see org.osgi.framework.BundleContext#addFrameworkListener(org.osgi.framework.FrameworkListener)
+	 */
+	public void addFrameworkListener(FrameworkListener listener) { m_bundleContext.addFrameworkListener(listener); }
+
+	/**
+	 * @see org.osgi.framework.BundleContext#addServiceListener(org.osgi.framework.ServiceListener, java.lang.String)
+	 */
+	public void addServiceListener(ServiceListener listener, String filter)
+			throws InvalidSyntaxException { m_serviceContext.addServiceListener(listener, filter); }
+
+	/**
+	 * @see org.osgi.framework.BundleContext#addServiceListener(org.osgi.framework.ServiceListener)
+	 */
+	public void addServiceListener(ServiceListener listener) { m_serviceContext.addServiceListener(listener); }
+
+	/**
+	 * @see org.osgi.framework.BundleContext#createFilter(java.lang.String)
+	 */
+	public Filter createFilter(String filter) throws InvalidSyntaxException { return m_bundleContext.createFilter(filter); }
+
+	/**
+	 * @see org.osgi.framework.BundleContext#getAllServiceReferences(java.lang.String, java.lang.String)
+	 */
+	public ServiceReference[] getAllServiceReferences(String clazz,
+			String filter) throws InvalidSyntaxException { return m_serviceContext.getAllServiceReferences(clazz, filter); }
+
+	/**
+	 * @see org.osgi.framework.BundleContext#getBundle()
+	 */
+	public Bundle getBundle() { return m_bundleContext.getBundle(); }
+
+	/**
+	 * @see org.osgi.framework.BundleContext#getBundle(long)
+	 */
+	public Bundle getBundle(long id) { return m_bundleContext.getBundle(id); }
+
+	/**
+	 * @see org.osgi.framework.BundleContext#getBundles()
+	 */
+	public Bundle[] getBundles() { return m_bundleContext.getBundles();	}
+
+	/**
+	 * @see org.osgi.framework.BundleContext#getDataFile(java.lang.String)
+	 */
+	public File getDataFile(String filename) { return m_bundleContext.getDataFile(filename); }
+
+	/**
+	 * @see org.osgi.framework.BundleContext#getProperty(java.lang.String)
+	 */
+	public String getProperty(String key) { return m_bundleContext.getProperty(key); }
+
+	/**
+	 * @see org.osgi.framework.BundleContext#getService(org.osgi.framework.ServiceReference)
+	 */
+	public Object getService(ServiceReference reference) { return m_serviceContext.getService(reference); }
+
+	/**
+	 * @see org.osgi.framework.BundleContext#getServiceReference(java.lang.String)
+	 */
+	public ServiceReference getServiceReference(String clazz) { return m_serviceContext.getServiceReference(clazz); }
+
+	/**
+	 * @see org.osgi.framework.BundleContext#getServiceReferences(java.lang.String, java.lang.String)
+	 */
+	public ServiceReference[] getServiceReferences(String clazz, String filter)
+			throws InvalidSyntaxException { return m_serviceContext.getServiceReferences(clazz, filter); }
+
+	/**
+	 * @see org.osgi.framework.BundleContext#installBundle(java.lang.String)
+	 */
+	public Bundle installBundle(String location) throws BundleException { return m_bundleContext.installBundle(location); }
+
+	/**
+	 * @see org.osgi.framework.BundleContext#installBundle(java.lang.String, java.io.InputStream)
+	 */
+	public Bundle installBundle(String location, InputStream input)
+			throws BundleException { return m_bundleContext.installBundle(location, input); }
+
+	/**
+	 * @see org.osgi.framework.BundleContext#registerService(java.lang.String[], java.lang.Object, java.util.Dictionary)
+	 */
+	public ServiceRegistration registerService(String[] clazzes,
+			Object service, Dictionary properties) { return m_serviceContext.registerService(clazzes, service, properties); }
+
+	/**
+	 * @see org.osgi.framework.BundleContext#registerService(java.lang.String, java.lang.Object, java.util.Dictionary)
+	 */
+	public ServiceRegistration registerService(String clazz, Object service,
+			Dictionary properties) { return m_serviceContext.registerService(clazz, service, properties); }
+
+	/**
+	 * @see org.osgi.framework.BundleContext#removeBundleListener(org.osgi.framework.BundleListener)
+	 */
+	public void removeBundleListener(BundleListener listener) { m_bundleContext.removeBundleListener(listener); }
+
+	/**
+	 * @see org.osgi.framework.BundleContext#removeFrameworkListener(org.osgi.framework.FrameworkListener)
+	 */
+	public void removeFrameworkListener(FrameworkListener listener) { m_bundleContext.removeFrameworkListener(listener); }
+
+	/**
+	 * @see org.osgi.framework.BundleContext#removeServiceListener(org.osgi.framework.ServiceListener)
+	 */
+	public void removeServiceListener(ServiceListener listener) { m_serviceContext.removeServiceListener(listener); }
+
+	/**
+	 * @see org.osgi.framework.BundleContext#ungetService(org.osgi.framework.ServiceReference)
+	 */
+	public boolean ungetService(ServiceReference reference) { return m_serviceContext.ungetService(reference); }
+	
+	/**
+	 * Set the component manager to the service context.
+	 * @param cm : the component manager
+	 */
+	public void setComponentInstance(ComponentManager cm) {m_serviceContext.setComponentInstance(cm); }
+	
+	/**
+	 * Get the component manager from the service context.
+	 * @return the component manager of the service context
+	 */
+	public ComponentManager getComponentInstance() { return m_serviceContext.getComponentInstance(); }
+
+}
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/ServiceContext.java b/ipojo/src/main/java/org/apache/felix/ipojo/ServiceContext.java
new file mode 100644
index 0000000..34ed9b8
--- /dev/null
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/ServiceContext.java
@@ -0,0 +1,54 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.ipojo;
+
+import java.util.Dictionary;
+
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceListener;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+
+public interface ServiceContext {
+	
+	void addServiceListener(ServiceListener listener, String filter) throws InvalidSyntaxException;
+	
+	void addServiceListener(ServiceListener listener);
+	
+	ServiceReference[] getAllServiceReferences(String clazz, String filter) throws InvalidSyntaxException;
+	
+	Object getService(ServiceReference reference);
+	
+	ServiceReference getServiceReference(String clazz);
+	
+	ServiceReference[] getServiceReferences(String clazz, String filter) throws InvalidSyntaxException;
+	
+	ServiceRegistration registerService(String[] clazzes, Object service, Dictionary properties);
+	
+	ServiceRegistration registerService(String clazz, Object service, Dictionary properties);
+	
+	void removeServiceListener(ServiceListener listener);
+	
+	boolean ungetService(ServiceReference reference);
+	
+	ComponentManager getComponentInstance();
+	
+	void setComponentInstance(ComponentManager cm);
+
+}
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/ServiceContextImpl.java b/ipojo/src/main/java/org/apache/felix/ipojo/ServiceContextImpl.java
new file mode 100644
index 0000000..55b3f58
--- /dev/null
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/ServiceContextImpl.java
@@ -0,0 +1,89 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.ipojo;
+
+import java.util.Dictionary;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceListener;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+
+public class ServiceContextImpl implements ServiceContext {
+	
+	private BundleContext m_context;
+	
+	private ComponentManagerImpl m_instance;
+	
+	public ServiceContextImpl(BundleContext bc) { m_context = bc; }
+
+	public void addServiceListener(ServiceListener listener, String filter)
+			throws InvalidSyntaxException {
+		m_context.addServiceListener(listener, filter);
+	}
+	
+	public void addServiceListener(ServiceListener listener) {
+		m_context.addServiceListener(listener);
+	}
+
+	public ServiceReference[] getAllServiceReferences(String clazz,
+			String filter) throws InvalidSyntaxException {
+		return m_context.getAllServiceReferences(clazz, filter);
+	}
+
+	public Object getService(ServiceReference reference) {
+		return m_context.getService(reference);
+	}
+
+	public ServiceReference getServiceReference(String clazz) {
+		return m_context.getServiceReference(clazz);
+	}
+
+	public ServiceReference[] getServiceReferences(String clazz, String filter)
+			throws InvalidSyntaxException {
+		return m_context.getServiceReferences(clazz, filter);
+	}
+
+	public ServiceRegistration registerService(String[] clazzes,
+			Object service, Dictionary properties) {
+		return m_context.registerService(clazzes, service, properties);
+	}
+
+	public ServiceRegistration registerService(String clazz, Object service,
+			Dictionary properties) {
+		return m_context.registerService(clazz, service, properties);
+	}
+
+	public void removeServiceListener(ServiceListener listener) {
+		m_context.removeServiceListener(listener);
+
+	}
+
+	public boolean ungetService(ServiceReference reference) {
+		return m_context.ungetService(reference);
+	}
+
+	public ComponentManager getComponentInstance() {
+		return m_instance;
+	}
+	
+	public void setComponentInstance(ComponentManager cm) { m_instance = (ComponentManagerImpl) cm; }
+
+}
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/dependency/Dependency.java b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/dependency/Dependency.java
index 6836743..e0426e1 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/dependency/Dependency.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/dependency/Dependency.java
@@ -525,7 +525,6 @@
                 m_state = RESOLVED;
             }
             // Register a listener :
-            //m_handler.getComponentManager().getContext().addServiceListener(this, filter); // Try without filter
             m_handler.getComponentManager().getContext().addServiceListener(this);
             m_filter = m_handler.getComponentManager().getContext().createFilter(filter); // Store the filter
             Activator.getLogger().log(Level.INFO, "[" + m_handler.getComponentManager().getComponentMetatada().getClassName() + "] Create a filter from : " + filter);
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/providedservice/ProvidedService.java b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/providedservice/ProvidedService.java
index b760c29..b0be97f 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/providedservice/ProvidedService.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/providedservice/ProvidedService.java
@@ -98,6 +98,12 @@
             Property prop = new Property(this, ((PropertyMetadata) psm.getProperties()[i]));
             addProperty(prop);
         }
+        //Add service pid and factory pid
+        //TODO : test this 
+        PropertyMetadata pid_meta = new PropertyMetadata(org.osgi.framework.Constants.SERVICE_PID, null, "java.lang.String", handler.getComponentManager().getComponentName()); 
+        PropertyMetadata factory_meta = new PropertyMetadata("factory.pid", null, "java.lang.String", handler.getComponentManager().getFactory().getFactoryName());
+        addProperty(new Property(this, pid_meta));
+        addProperty(new Property(this, factory_meta));
     }
 
     /**