Bug 374637 - NPE in ManagedServiceTracker.add
diff --git a/bundles/org.eclipse.equinox.cm.test/META-INF/MANIFEST.MF b/bundles/org.eclipse.equinox.cm.test/META-INF/MANIFEST.MF
index a1b9aca..93f0f5c 100644
--- a/bundles/org.eclipse.equinox.cm.test/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.equinox.cm.test/META-INF/MANIFEST.MF
@@ -5,8 +5,10 @@
 Bundle-Version: 1.0.0
 Bundle-Activator: org.eclipse.equinox.cm.test.Activator
 Import-Package: junit.framework;version="3.8.1",
+ org.eclipse.equinox.log,
  org.osgi.framework;version="1.3.0",
  org.osgi.service.cm;version="1.2.0",
  org.osgi.service.event;version="1.1.0",
+ org.osgi.service.log;version="1.3.0",
  org.osgi.service.packageadmin;version="1.2.0"
 Eclipse-LazyStart: true
diff --git a/bundles/org.eclipse.equinox.cm.test/src/org/eclipse/equinox/cm/test/ManagedServiceTest.java b/bundles/org.eclipse.equinox.cm.test/src/org/eclipse/equinox/cm/test/ManagedServiceTest.java
index 2d671c3..e1612ed 100644
--- a/bundles/org.eclipse.equinox.cm.test/src/org/eclipse/equinox/cm/test/ManagedServiceTest.java
+++ b/bundles/org.eclipse.equinox.cm.test/src/org/eclipse/equinox/cm/test/ManagedServiceTest.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 IBM Corporation and others
+ * Copyright (c) 2007, 2012 IBM Corporation and others
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -13,8 +13,11 @@
 import java.util.Dictionary;
 import java.util.Properties;
 import junit.framework.TestCase;
+import org.eclipse.equinox.log.ExtendedLogReaderService;
+import org.eclipse.equinox.log.LogFilter;
 import org.osgi.framework.*;
 import org.osgi.service.cm.*;
+import org.osgi.service.log.*;
 
 public class ManagedServiceTest extends TestCase {
 
@@ -85,6 +88,47 @@
 		config.delete();
 	}
 
+	public void testBug374637() throws Exception {
+
+		ManagedService ms = new ManagedService() {
+
+			public void updated(Dictionary properties) throws ConfigurationException {
+				// nothing
+			}
+		};
+
+		ExtendedLogReaderService reader = (ExtendedLogReaderService) Activator.getBundleContext().getService(Activator.getBundleContext().getServiceReference(ExtendedLogReaderService.class));
+		synchronized (lock) {
+			locked = false;
+		}
+		LogListener listener = new LogListener() {
+			public void logged(LogEntry entry) {
+				synchronized (lock) {
+					locked = true;
+					lock.notifyAll();
+				}
+			}
+		};
+		reader.addLogListener(listener, new LogFilter() {
+			public boolean isLoggable(Bundle bundle, String loggerName, int logLevel) {
+				return logLevel == LogService.LOG_ERROR;
+			}
+		});
+		Dictionary dict = new Properties();
+		dict.put(Constants.SERVICE_PID, "test");
+		ServiceRegistration reg1 = Activator.getBundleContext().registerService(ManagedService.class.getName(), ms, dict);
+		ServiceRegistration reg2 = Activator.getBundleContext().registerService(ManagedService.class.getName(), ms, dict);
+
+		reg1.unregister();
+		reg2.unregister();
+		reader.removeLogListener(listener);
+
+		synchronized (lock) {
+			lock.wait(1000);
+			assertFalse("Got a error log", locked);
+		}
+	}
+
 	public void testGeneralManagedService() throws Exception {
 		updateCount = 0;
 		ManagedService ms = new ManagedService() {
diff --git a/bundles/org.eclipse.equinox.cm/META-INF/MANIFEST.MF b/bundles/org.eclipse.equinox.cm/META-INF/MANIFEST.MF
index 547f2f8..5efd2b3 100644
--- a/bundles/org.eclipse.equinox.cm/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.equinox.cm/META-INF/MANIFEST.MF
@@ -4,7 +4,7 @@
 Bundle-Vendor: %providerName
 Bundle-Localization: plugin
 Bundle-SymbolicName: org.eclipse.equinox.cm
-Bundle-Version: 1.0.300.qualifier
+Bundle-Version: 1.0.400.qualifier
 Bundle-Activator: org.eclipse.equinox.internal.cm.Activator
 Import-Package: org.osgi.framework;version="1.3.0",
  org.osgi.service.cm;version="[1.3,1.5)",
diff --git a/bundles/org.eclipse.equinox.cm/src/org/eclipse/equinox/internal/cm/ManagedServiceTracker.java b/bundles/org.eclipse.equinox.cm/src/org/eclipse/equinox/internal/cm/ManagedServiceTracker.java
index 0148cc0..cf10b68 100644
--- a/bundles/org.eclipse.equinox.cm/src/org/eclipse/equinox/internal/cm/ManagedServiceTracker.java
+++ b/bundles/org.eclipse.equinox.cm/src/org/eclipse/equinox/internal/cm/ManagedServiceTracker.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2005, 2008 Cognos Incorporated, IBM Corporation and others.
+ * Copyright (c) 2005, 2012 Cognos Incorporated, IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -93,8 +93,10 @@
 
 	private void add(ServiceReference reference, String pid, ManagedService service) {
 		ConfigurationImpl config = configurationStore.findConfiguration(pid);
-		if (config == null && trackManagedService(pid, reference, service)) {
-			asynchUpdated(service, null);
+		if (config == null) {
+			if (trackManagedService(pid, reference, service)) {
+				asynchUpdated(service, null);
+			}
 		} else {
 			try {
 				config.lock();