Author: justin
Date: Tue Jan 7 20:03:51 2014
New Revision: 1556337
URL: http://svn.apache.org/r1556337
Log:
using slingscripthelper for osgi service injection if available to take
advantage of automatic unget
Added:
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/RequestOSGiModel.java
Modified:
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/injectors/OSGiServiceInjector.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/OSGiInjectionTest.java
Modified:
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/injectors/OSGiServiceInjector.java
URL:
http://svn.apache.org/viewvc/sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/injectors/OSGiServiceInjector.java?rev=1556337&r1=1556336&r2=1556337&view=diff
==============================================================================
---
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/injectors/OSGiServiceInjector.java
(original)
+++
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/injectors/OSGiServiceInjector.java
Tue Jan 7 20:03:51 2014
@@ -25,9 +25,14 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.List;
+import javax.servlet.ServletRequest;
+
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.scripting.SlingBindings;
+import org.apache.sling.api.scripting.SlingScriptHelper;
import org.apache.sling.yamf.annotations.Filter;
import org.apache.sling.yamf.spi.Injector;
import org.osgi.framework.BundleContext;
@@ -62,56 +67,88 @@ public class OSGiServiceInjector impleme
filterString = filter.value();
}
- return getValue(type, filterString);
+ return getValue(adaptable, type, filterString);
}
- private Object getService(Class<?> type, String filter) {
- try {
- ServiceReference[] refs =
bundleContext.getServiceReferences(type.getName(), filter);
- if (refs == null || refs.length == 0) {
+ private <T> Object getService(Object adaptable, Class<T> type, String
filter) {
+ SlingScriptHelper helper = getScriptHelper(adaptable);
+
+ if (helper != null) {
+ T[] services = helper.getServices(type, filter);
+ if (services == null || services.length == 0) {
return null;
} else {
- return bundleContext.getService(refs[0]);
+ return services[0];
+ }
+ } else {
+ try {
+ ServiceReference[] refs =
bundleContext.getServiceReferences(type.getName(), filter);
+ if (refs == null || refs.length == 0) {
+ return null;
+ } else {
+ return bundleContext.getService(refs[0]);
+ }
+ } catch (InvalidSyntaxException e) {
+ log.error("invalid filter expression", e);
+ return null;
}
- } catch (InvalidSyntaxException e) {
- log.error("invalid filter expression", e);
- return null;
}
}
- private Object[] getServices(Class<?> type, String filter) {
- try {
- ServiceReference[] refs =
bundleContext.getServiceReferences(type.getName(), filter);
- if (refs == null || refs.length == 0) {
- return null;
- } else {
- List<Object> services = new ArrayList<Object>();
- for (ServiceReference ref : refs) {
- Object service = bundleContext.getService(ref);
- if (service != null) {
- services.add(service);
+ private <T> Object[] getServices(Object adaptable, Class<T> type, String
filter) {
+ SlingScriptHelper helper = getScriptHelper(adaptable);
+
+ if (helper != null) {
+ T[] services = helper.getServices(type, filter);
+ return services;
+ } else {
+ try {
+ ServiceReference[] refs =
bundleContext.getServiceReferences(type.getName(), filter);
+ if (refs == null || refs.length == 0) {
+ return null;
+ } else {
+ List<Object> services = new ArrayList<Object>();
+ for (ServiceReference ref : refs) {
+ Object service = bundleContext.getService(ref);
+ if (service != null) {
+ services.add(service);
+ }
}
+ return services.toArray();
}
- return services.toArray();
+ } catch (InvalidSyntaxException e) {
+ log.error("invalid filter expression", e);
+ return null;
}
- } catch (InvalidSyntaxException e) {
- log.error("invalid filter expression", e);
+ }
+ }
+
+ private SlingScriptHelper getScriptHelper(Object adaptable) {
+ if (adaptable instanceof ServletRequest) {
+ ServletRequest request = (ServletRequest) adaptable;
+ SlingBindings bindings = (SlingBindings)
request.getAttribute(SlingBindings.class.getName());
+ if (bindings != null) {
+ return bindings.getSling();
+ } else {
+ return null;
+ }
+ } else {
return null;
}
}
- private Object getValue(Type type, String filterString) {
+ private Object getValue(Object adaptable, Type type, String filterString) {
if (type instanceof Class) {
Class<?> injectedClass = (Class<?>) type;
if (injectedClass.isArray()) {
- Object[] services =
getServices(injectedClass.getComponentType(), filterString);
+ Object[] services = getServices(adaptable,
injectedClass.getComponentType(), filterString);
Object arr =
Array.newInstance(injectedClass.getComponentType(), services.length);
for (int i = 0; i < services.length; i++) {
Array.set(arr, i, services[i]);
}
return arr;
} else {
- return getService(injectedClass, filterString);
+ return getService(adaptable, injectedClass, filterString);
}
} else if (type instanceof ParameterizedType) {
ParameterizedType ptype = (ParameterizedType) type;
@@ -124,7 +161,7 @@ public class OSGiServiceInjector impleme
}
Class<?> serviceType = (Class<?>)
ptype.getActualTypeArguments()[0];
- Object[] services = getServices(serviceType, filterString);
+ Object[] services = getServices(adaptable, serviceType,
filterString);
return Arrays.asList(services);
} else {
log.warn("Cannot handle type {}", type);
Modified:
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/OSGiInjectionTest.java
URL:
http://svn.apache.org/viewvc/sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/OSGiInjectionTest.java?rev=1556337&r1=1556336&r2=1556337&view=diff
==============================================================================
---
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/OSGiInjectionTest.java
(original)
+++
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/OSGiInjectionTest.java
Tue Jan 7 20:03:51 2014
@@ -21,17 +21,22 @@ import static org.mockito.Mockito.*;
import java.util.Collections;
+import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.scripting.SlingBindings;
+import org.apache.sling.api.scripting.SlingScriptHelper;
import org.apache.sling.yamf.impl.injectors.OSGiServiceInjector;
import org.apache.sling.yamf.testmodels.classes.ArrayOSGiModel;
import org.apache.sling.yamf.testmodels.classes.CollectionOSGiModel;
import org.apache.sling.yamf.testmodels.classes.ListOSGiModel;
+import org.apache.sling.yamf.testmodels.classes.RequestOSGiModel;
import org.apache.sling.yamf.testmodels.classes.SetOSGiModel;
import org.apache.sling.yamf.testmodels.classes.SimpleOSGiModel;
import org.apache.sling.yamf.testmodels.interfaces.ServiceInterface;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.osgi.framework.BundleContext;
@@ -49,6 +54,11 @@ public class OSGiInjectionTest {
@Mock
private BundleContext bundleContext;
+ @Mock
+ private SlingScriptHelper helper;
+
+ private SlingBindings bindings = new SlingBindings();
+
@Before
public void setup() {
factory = new YamfAdapterFactory();
@@ -56,8 +66,9 @@ public class OSGiInjectionTest {
OSGiServiceInjector injectorFactory = new OSGiServiceInjector();
injectorFactory.activate(componentCtx);
- factory.bindInjector(injectorFactory,
- Collections.<String, Object>
singletonMap(Constants.SERVICE_ID, 0L));
+ factory.bindInjector(injectorFactory, Collections.<String, Object>
singletonMap(Constants.SERVICE_ID, 0L));
+
+ bindings.setSling(helper);
}
@Test
@@ -79,6 +90,23 @@ public class OSGiInjectionTest {
}
@Test
+ public void testRequestOSGiModel() throws Exception {
+ ServiceInterface service = mock(ServiceInterface.class);
+
+ SlingHttpServletRequest request = mock(SlingHttpServletRequest.class);
+
when(request.getAttribute(SlingBindings.class.getName())).thenReturn(bindings);
+
+ when(helper.getServices(ServiceInterface.class, null)).thenReturn(new
ServiceInterface[] { service });
+
+ RequestOSGiModel model = factory.getAdapter(request,
RequestOSGiModel.class);
+ assertNotNull(model);
+ assertNotNull(model.getService());
+ assertEquals(service, model.getService());
+
+ verifyNoMoreInteractions(bundleContext);
+ }
+
+ @Test
public void testListOSGiModel() throws Exception {
ServiceReference ref1 = mock(ServiceReference.class);
ServiceInterface service1 = mock(ServiceInterface.class);
@@ -125,6 +153,7 @@ public class OSGiInjectionTest {
verifyNoMoreInteractions(res);
}
+
@Test
public void testCollectionOSGiModel() throws Exception {
ServiceReference ref1 = mock(ServiceReference.class);
Added:
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/RequestOSGiModel.java
URL:
http://svn.apache.org/viewvc/sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/RequestOSGiModel.java?rev=1556337&view=auto
==============================================================================
---
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/RequestOSGiModel.java
(added)
+++
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/RequestOSGiModel.java
Tue Jan 7 20:03:51 2014
@@ -0,0 +1,35 @@
+/*
+ * 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.yamf.testmodels.classes;
+
+import javax.inject.Inject;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.yamf.annotations.Model;
+import org.apache.sling.yamf.testmodels.interfaces.ServiceInterface;
+
+@Model(adaptables = SlingHttpServletRequest.class)
+public class RequestOSGiModel {
+
+ @Inject
+ private ServiceInterface service;
+
+ public ServiceInterface getService() {
+ return service;
+ }
+
+}