[SCR] Fix potential NPE in concurrent scenarios
Also included unit tests.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1690926 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/manager/SingleComponentManager.java b/scr/src/main/java/org/apache/felix/scr/impl/manager/SingleComponentManager.java
index 5c4ffa0..63038e8 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/manager/SingleComponentManager.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/manager/SingleComponentManager.java
@@ -450,7 +450,7 @@
{
servicePids.addAll((List)configPropServicePids);
}
- else
+ else
{
servicePids.add(configPropServicePids.toString());
}
@@ -458,7 +458,7 @@
{
servicePids.add((String)m_factoryProperties.get(Constants.SERVICE_PID));
}
-
+
if ( servicePids.size() == 1 )
{
props.put(Constants.SERVICE_PID, servicePids.get(0));
@@ -761,10 +761,11 @@
public S getService( Bundle bundle, ServiceRegistration<S> serviceRegistration )
{
boolean success = getServiceInternal();
- if ( success )
+ ComponentContextImpl<S> componentContext = m_componentContext;
+ if ( success && componentContext != null)
{
m_useCount.incrementAndGet();
- return m_componentContext.getImplementationObject( true );
+ return componentContext.getImplementationObject( true );
}
else
{
diff --git a/scr/src/test/java/org/apache/felix/scr/impl/manager/SingleComponentManagerTest.java b/scr/src/test/java/org/apache/felix/scr/impl/manager/SingleComponentManagerTest.java
new file mode 100644
index 0000000..596f9bb
--- /dev/null
+++ b/scr/src/test/java/org/apache/felix/scr/impl/manager/SingleComponentManagerTest.java
@@ -0,0 +1,94 @@
+/*
+ * 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.scr.impl.manager;
+
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+
+import java.lang.reflect.Field;
+
+import org.apache.felix.scr.impl.config.ComponentContainer;
+import org.apache.felix.scr.impl.helper.ComponentMethods;
+import org.apache.felix.scr.impl.metadata.ComponentMetadata;
+import org.apache.felix.scr.impl.metadata.DSVersion;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+
+public class SingleComponentManagerTest
+{
+ @Test
+ public void testGetService() throws Exception {
+ ComponentMetadata cm = new ComponentMetadata(DSVersion.DS13);
+ cm.setImplementationClassName("foo.bar.SomeClass");
+ cm.validate(null);
+
+ @SuppressWarnings("unchecked")
+ ComponentContainer<Object> cc = Mockito.mock(ComponentContainer.class);
+ Mockito.when(cc.getComponentMetadata()).thenReturn(cm);
+
+ SingleComponentManager<Object> scm = new SingleComponentManager<Object>(cc, new ComponentMethods()) {
+ @Override
+ boolean getServiceInternal()
+ {
+ return true;
+ }
+ };
+
+ BundleContext bc = Mockito.mock(BundleContext.class);
+ Bundle b = Mockito.mock(Bundle.class);
+ Mockito.when(b.getBundleContext()).thenReturn(bc);
+
+ ComponentContextImpl<Object> cci = new ComponentContextImpl<Object>(scm, b);
+ Object implObj = new Object();
+ cci.setImplementationObject(implObj);
+ cci.setImplementationAccessible(true);
+
+ Field f = SingleComponentManager.class.getDeclaredField("m_componentContext");
+ f.setAccessible(true);
+ f.set(scm, cci);
+
+ scm.m_internalEnabled = true;
+ assertSame(implObj, scm.getService(null, null));
+ }
+
+ @Test
+ public void testGetServiceWithNullComponentContext()
+ {
+ ComponentMetadata cm = new ComponentMetadata(DSVersion.DS13);
+ cm.setImplementationClassName("foo.bar.SomeClass");
+ cm.validate(null);
+
+ @SuppressWarnings("unchecked")
+ ComponentContainer<Object> cc = Mockito.mock(ComponentContainer.class);
+ Mockito.when(cc.getComponentMetadata()).thenReturn(cm);
+
+ SingleComponentManager<?> scm = new SingleComponentManager<Object>(cc, new ComponentMethods()) {
+ @Override
+ boolean getServiceInternal()
+ {
+ return true;
+ }
+ };
+ scm.m_internalEnabled = true;
+ assertNull("m_componentContext is null, this should not cause an NPE",
+ scm.getService(null, null));
+ }
+}