package no.steria.osgi.jsr330activator.implementation;

import java.lang.reflect.Method;

import org.osgi.framework.BundleContext;

/**
 * Implementation of {@link Injection} that injects using a method.
 *
 * @author Steinar Bang
 *
 */
class InjectionMethod extends InjectionBase {

    private Object provider;
    private Method method;
    private Object currentService;

    public InjectionMethod(Object provider, Method method) {
    	this.provider = provider;
    	this.method = method;
    	this.method.setAccessible(true);
    }

    @Override
    public void unGet(BundleContext bundleContext) {
    	signalDisconnectToProvider();
        super.unGet(bundleContext);
    }

    public Class<?> getInjectedServiceType() {
        return method.getParameterTypes()[0];
    }

    public boolean isInjected() {
        return currentService != null;
    }

    public void doInject(Object service) {
    	try {
            method.invoke(provider, service);
            currentService = service;
        } catch (Exception e) {
        }
    }

    public void doRetract(Object service) {
        try {
            method.invoke(provider, (String)null);
            currentService = null;
        } catch (Exception e) {
        }
    }

    private void signalDisconnectToProvider() {
        doRetract(null);
    }

}

@@ -87,50 +87,8 @@
 d;%0A%0A
-import org.osgi.framework.BundleContext;%0A%0A
 /**%0A
@@ -496,153 +496,8 @@
  %7D%0A%0A
-    @Override%0A    public void unGet(BundleContext bundleContext) %7B%0A    %09signalDisconnectToProvider();%0A        super.unGet(bundleContext);%0A    %7D%0A%0A
     
@@ -1027,32 +1027,32 @@
 (Exception e) %7B%0A
+
         %7D%0A    %7D%0A
@@ -1056,86 +1056,6 @@
  %7D%0A%0A
-    private void signalDisconnectToProvider() %7B%0A        doRetract(null);%0A    %7D%0A%0A
 %7D%0A

Revert "Fix #2 by retracting injected services on bundle shutdown."