package bndtools.builder;

import java.util.ArrayList;
import java.util.List;

import org.bndtools.build.api.BuildListener;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.util.tracker.ServiceTracker;

import bndtools.Plugin;
import bndtools.api.ILogger;

public class BuildListeners {

    private final List<BuildListener> listeners;
    private final ServiceTracker listenerTracker;

    public BuildListeners(ILogger logger) {
        IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(Plugin.PLUGIN_ID, "buildListeners");
        listeners = new ArrayList<BuildListener>(elements.length);

        for (IConfigurationElement elem : elements) {
            try {
                BuildListener listener = (BuildListener) elem.createExecutableExtension("class");
                listeners.add(listener);
            } catch (Exception e) {
                logger.logError("Unable to instantiate build listener: " + elem.getAttribute("name"), e);
            }
        }

        BundleContext context = FrameworkUtil.getBundle(BuildListeners.class).getBundleContext();

        listenerTracker = new ServiceTracker(context, BuildListener.class.getName(), null);
        listenerTracker.open();
    }

    public void fireBuildStarting(IProject project) {
        for (BuildListener listener : listeners) {
            listener.buildStarting(project);
        }

        Object[] services = listenerTracker.getServices();
        for (Object service : services) {
            if (service != null) {
                BuildListener listener = (BuildListener) service;
                listener.buildStarting(project);
            }
        }
    }

    public void fireBuiltBundles(IProject project, IPath[] paths) {
        for (BuildListener listener : listeners) {
            listener.builtBundles(project, paths);
        }
        Object[] services = listenerTracker.getServices();
        for (Object service : services) {
            if (service != null) {
                BuildListener listener = (BuildListener) service;
                listener.builtBundles(project, paths);
            }
        }
    }

    /**
     * Call this to make sure that any references to the listeners are no longer held.
     */
    public void release() {
        listeners.clear();
        listenerTracker.close();
    }

}

@@ -1682,32 +1682,66 @@
 .getServices();%0A
+        if (services != null)%0A    
         for (Obj
@@ -1770,32 +1770,36 @@
 ) %7B%0A            
+    
 if (service != n
@@ -1813,32 +1813,36 @@
                 
+   
+ 
 BuildListener li
@@ -1883,32 +1883,36 @@
                 
+    
 listener.buildSt
@@ -1932,34 +1932,42 @@
 t);%0A            
-%7D%0A
+    %7D%0A    
         %7D%0A    %7D%0A

Fix NPE in build