Author: justin
Date: Mon Mar  3 17:58:16 2014
New Revision: 1573637

URL: http://svn.apache.org/r1573637
Log:
SLING-3429 - fix NPE in OSGiServiceInjector which impacted multi-valued 
properties

Added:
    
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/OptionalArrayOSGiModel.java
    
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/OptionalListOSGiModel.java
Modified:
    
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/OSGiServiceInjector.java
    
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/OSGiInjectionTest.java

Modified: 
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/OSGiServiceInjector.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/OSGiServiceInjector.java?rev=1573637&r1=1573636&r2=1573637&view=diff
==============================================================================
--- 
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/OSGiServiceInjector.java
 (original)
+++ 
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/OSGiServiceInjector.java
 Mon Mar  3 17:58:16 2014
@@ -148,6 +148,9 @@ public class OSGiServiceInjector impleme
             Class<?> injectedClass = (Class<?>) type;
             if (injectedClass.isArray()) {
                 Object[] services = getServices(adaptable, 
injectedClass.getComponentType(), filterString, callbackRegistry);
+                if (services == null) {
+                    return null;
+                }
                 Object arr = 
Array.newInstance(injectedClass.getComponentType(), services.length);
                 for (int i = 0; i < services.length; i++) {
                     Array.set(arr, i, services[i]);
@@ -168,6 +171,9 @@ public class OSGiServiceInjector impleme
 
             Class<?> serviceType = (Class<?>) 
ptype.getActualTypeArguments()[0];
             Object[] services = getServices(adaptable, serviceType, 
filterString, callbackRegistry);
+            if (services == null) {
+                return null;
+            }
             return Arrays.asList(services);
         } else {
             log.warn("Cannot handle type {}", type);

Modified: 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/OSGiInjectionTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/OSGiInjectionTest.java?rev=1573637&r1=1573636&r2=1573637&view=diff
==============================================================================
--- 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/OSGiInjectionTest.java
 (original)
+++ 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/OSGiInjectionTest.java
 Mon Mar  3 17:58:16 2014
@@ -30,6 +30,8 @@ import org.apache.sling.models.impl.inje
 import org.apache.sling.models.testmodels.classes.ArrayOSGiModel;
 import org.apache.sling.models.testmodels.classes.CollectionOSGiModel;
 import org.apache.sling.models.testmodels.classes.ListOSGiModel;
+import org.apache.sling.models.testmodels.classes.OptionalArrayOSGiModel;
+import org.apache.sling.models.testmodels.classes.OptionalListOSGiModel;
 import org.apache.sling.models.testmodels.classes.RequestOSGiModel;
 import org.apache.sling.models.testmodels.classes.SetOSGiModel;
 import org.apache.sling.models.testmodels.classes.SimpleOSGiModel;
@@ -161,6 +163,29 @@ public class OSGiInjectionTest {
     }
 
     @Test
+    public void testOptionalArrayOSGiModel() throws Exception {
+
+        Resource res = mock(Resource.class);
+
+        OptionalArrayOSGiModel model = factory.getAdapter(res, 
OptionalArrayOSGiModel.class);
+        assertNotNull(model);
+        assertNull(model.getServices());
+
+        verifyNoMoreInteractions(res);
+    }
+
+    @Test
+    public void testOptionalListOSGiModel() throws Exception {
+        Resource res = mock(Resource.class);
+
+        OptionalListOSGiModel model = factory.getAdapter(res, 
OptionalListOSGiModel.class);
+        assertNotNull(model);
+        assertNull(model.getServices());
+
+        verifyNoMoreInteractions(res);
+    }
+
+    @Test
     public void testCollectionOSGiModel() throws Exception {
         ServiceReference ref1 = mock(ServiceReference.class);
         ServiceInterface service1 = mock(ServiceInterface.class);

Added: 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/OptionalArrayOSGiModel.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/OptionalArrayOSGiModel.java?rev=1573637&view=auto
==============================================================================
--- 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/OptionalArrayOSGiModel.java
 (added)
+++ 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/OptionalArrayOSGiModel.java
 Mon Mar  3 17:58:16 2014
@@ -0,0 +1,36 @@
+/*
+ * 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.sling.models.testmodels.classes;
+
+import javax.inject.Inject;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.models.annotations.Model;
+import org.apache.sling.models.annotations.Optional;
+import org.apache.sling.models.testmodels.interfaces.ServiceInterface;
+
+@Model(adaptables = Resource.class)
+public class OptionalArrayOSGiModel {
+    
+    @Inject @Optional
+    private ServiceInterface[] services;
+    
+    public ServiceInterface[] getServices() {
+        return services;
+    }
+
+}

Added: 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/OptionalListOSGiModel.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/OptionalListOSGiModel.java?rev=1573637&view=auto
==============================================================================
--- 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/OptionalListOSGiModel.java
 (added)
+++ 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/OptionalListOSGiModel.java
 Mon Mar  3 17:58:16 2014
@@ -0,0 +1,38 @@
+/*
+ * 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.sling.models.testmodels.classes;
+
+import java.util.List;
+
+import javax.inject.Inject;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.models.annotations.Model;
+import org.apache.sling.models.annotations.Optional;
+import org.apache.sling.models.testmodels.interfaces.ServiceInterface;
+
+@Model(adaptables = Resource.class)
+public class OptionalListOSGiModel {
+    
+    @Inject @Optional
+    private List<ServiceInterface> services;
+    
+    public List<ServiceInterface> getServices() {
+        return services;
+    }
+
+}


Reply via email to