Author: justin
Date: Tue Dec 24 00:24:22 2013
New Revision: 1553233

URL: http://svn.apache.org/r1553233
Log:
add support for child resource injection as well as the ability to adapt an 
injection if necessary

Added:
    
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/injectors/ChildResourceInjector.java
      - copied, changed from r1553145, 
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/injectors/RequestAttributeInjector.java
    
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/ChildModel.java
    
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/interfaces/ChildResourceModel.java
    
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/interfaces/ChildValueMapModel.java
    
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/interfaces/ParentModel.java
Modified:
    
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/YamfAdapterFactory.java
    
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/main/java/org/apache/sling/yamf/impl/injectors/RequestAttributeInjector.java
    
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/ResourceModelClassesTest.java

Modified: 
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/YamfAdapterFactory.java
URL: 
http://svn.apache.org/viewvc/sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/YamfAdapterFactory.java?rev=1553233&r1=1553232&r2=1553233&view=diff
==============================================================================
--- 
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/YamfAdapterFactory.java
 (original)
+++ 
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/YamfAdapterFactory.java
 Tue Dec 24 00:24:22 2013
@@ -44,6 +44,7 @@ import org.apache.felix.scr.annotations.
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.ReferencePolicy;
+import org.apache.sling.api.adapter.Adaptable;
 import org.apache.sling.api.adapter.AdapterFactory;
 import org.apache.sling.commons.osgi.ServiceUtil;
 import org.apache.sling.yamf.annotations.Default;
@@ -445,6 +446,12 @@ public class YamfAdapterFactory implemen
 
     private boolean setField(Field field, Object createdObject, Object value) {
         if (value != null) {
+            if (!isAcceptableType(field.getClass(), value) && value instanceof 
Adaptable) {
+                value = ((Adaptable)value).adaptTo(field.getClass());
+                if (value == null) {
+                    return false;
+                }
+            }
             boolean accessible = field.isAccessible();
             try {
                 if (!accessible) {
@@ -466,7 +473,13 @@ public class YamfAdapterFactory implemen
     }
 
     private boolean setMethod(Method method, Map<Method, Object> methods, 
Object value) {
-        if (value != null && (isAcceptableType(method.getReturnType(), 
value))) {
+        if (value != null) {
+            if (!isAcceptableType(method.getReturnType(), value) && value 
instanceof Adaptable) {
+                value = ((Adaptable)value).adaptTo(method.getReturnType());
+                if (value == null) {
+                    return false;
+                }
+            }
             methods.put(method, value);
             return true;
         } else {

Copied: 
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/injectors/ChildResourceInjector.java
 (from r1553145, 
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/injectors/RequestAttributeInjector.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/ChildResourceInjector.java?p2=sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/injectors/ChildResourceInjector.java&p1=sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/injectors/RequestAttributeInjector.java&r1=1553145&r2=1553233&rev=1553233&view=diff
==============================================================================
--- 
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/injectors/RequestAttributeInjector.java
 (original)
+++ 
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/injectors/ChildResourceInjector.java
 Tue Dec 24 00:24:22 2013
@@ -19,41 +19,25 @@ package org.apache.sling.yamf.impl.injec
 import java.lang.reflect.AnnotatedElement;
 import java.lang.reflect.Type;
 
-import javax.servlet.ServletRequest;
-
 import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Service;
-import org.apache.sling.yamf.annotations.Source;
+import org.apache.sling.api.resource.Resource;
 import org.apache.sling.yamf.spi.Injector;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 @Component
 @Service
-public class RequestAttributeInjector implements Injector {
+public class ChildResourceInjector implements Injector {
 
-    private static final Logger log = 
LoggerFactory.getLogger(RequestAttributeInjector.class);
-    
     @Override
     public String getName() {
-        return "request-attributes";
+        return "child-resources";
     }
 
     @Override
     public Object getValue(Object adaptable, String name, Type declaredType, 
AnnotatedElement element) {
-        if (!(adaptable instanceof ServletRequest)) {
-            return null;
-        } else if (declaredType instanceof Class<?>) {
-            Class<?> clazz = (Class<?>) declaredType;
-            Object attribute = ((ServletRequest)adaptable).getAttribute(name);
-            if (clazz.isInstance(attribute)) {
-                return attribute;
-            } else {
-                return null;
-            }
+        if (adaptable instanceof Resource) {
+            return ((Resource) adaptable).getChild(name);
         } else {
-            log.warn("BindingsInjector doesn't support non-class types");
             return null;
         }
     }

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=1553233&r1=1553232&r2=1553233&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 Dec 24 00:24:22 2013
@@ -27,10 +27,8 @@ import java.util.List;
 
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Service;
 import org.apache.sling.yamf.annotations.Filter;
-import org.apache.sling.yamf.annotations.Source;
 import org.apache.sling.yamf.spi.Injector;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.InvalidSyntaxException;

Modified: 
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/injectors/RequestAttributeInjector.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/RequestAttributeInjector.java?rev=1553233&r1=1553232&r2=1553233&view=diff
==============================================================================
--- 
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/injectors/RequestAttributeInjector.java
 (original)
+++ 
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/injectors/RequestAttributeInjector.java
 Tue Dec 24 00:24:22 2013
@@ -22,9 +22,7 @@ import java.lang.reflect.Type;
 import javax.servlet.ServletRequest;
 
 import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Service;
-import org.apache.sling.yamf.annotations.Source;
 import org.apache.sling.yamf.spi.Injector;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;

Modified: 
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/ResourceModelClassesTest.java
URL: 
http://svn.apache.org/viewvc/sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/ResourceModelClassesTest.java?rev=1553233&r1=1553232&r2=1553233&view=diff
==============================================================================
--- 
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/ResourceModelClassesTest.java
 (original)
+++ 
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/ResourceModelClassesTest.java
 Tue Dec 24 00:24:22 2013
@@ -16,21 +16,29 @@
  */
 package org.apache.sling.yamf.impl;
 
-import static org.mockito.Mockito.*;
 import static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
 
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
+import org.apache.commons.lang.RandomStringUtils;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ValueMap;
 import org.apache.sling.api.wrappers.ValueMapDecorator;
+import org.apache.sling.yamf.impl.injectors.ChildResourceInjector;
 import org.apache.sling.yamf.impl.injectors.ValueMapInjector;
+import org.apache.sling.yamf.testmodels.classes.ChildModel;
 import org.apache.sling.yamf.testmodels.classes.ResourceModelWithRequiredField;
 import org.apache.sling.yamf.testmodels.classes.SimplePropertyModel;
+import org.apache.sling.yamf.testmodels.interfaces.ChildResourceModel;
+import org.apache.sling.yamf.testmodels.interfaces.ChildValueMapModel;
+import org.apache.sling.yamf.testmodels.interfaces.ParentModel;
 import org.junit.Before;
 import org.junit.Test;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
 import org.osgi.framework.Constants;
 
 public class ResourceModelClassesTest {
@@ -41,6 +49,8 @@ public class ResourceModelClassesTest {
     public void setup() {
         factory = new YamfAdapterFactory();
         factory.bindInjector(new ValueMapInjector(),
+                Collections.<String, Object> 
singletonMap(Constants.SERVICE_ID, 1L));
+        factory.bindInjector(new ChildResourceInjector(),
                 Collections.<String, Object> 
singletonMap(Constants.SERVICE_ID, 0L));
     }
 
@@ -80,4 +90,59 @@ public class ResourceModelClassesTest {
         verify(vm).get("required", String.class);
     }
 
+    @Test
+    public void testChildResource() {
+        Resource child = mock(Resource.class);
+
+        Resource res = mock(Resource.class);
+        when(res.getChild("firstChild")).thenReturn(child);
+
+        ChildResourceModel model = factory.getAdapter(res, 
ChildResourceModel.class);
+        assertNotNull(model);
+        assertEquals(child, model.getFirstChild());
+    }
+
+    @Test
+    public void testChildValueMap() {
+        ValueMap map = ValueMapDecorator.EMPTY;
+
+        Resource child = mock(Resource.class);
+        when(child.adaptTo(ValueMap.class)).thenReturn(map);
+
+        Resource res = mock(Resource.class);
+        when(res.getChild("firstChild")).thenReturn(child);
+
+        ChildValueMapModel model = factory.getAdapter(res, 
ChildValueMapModel.class);
+        assertNotNull(model);
+        assertEquals(map, model.getFirstChild());
+    }
+
+    @Test
+    public void testChildModel() {
+        Object value = RandomStringUtils.randomAlphabetic(10);
+        Map<String, Object> props = Collections.singletonMap("property", 
value);
+        ValueMap map = new ValueMapDecorator(props);
+
+        final Resource child = mock(Resource.class);
+        when(child.adaptTo(ValueMap.class)).thenReturn(map);
+        when(child.adaptTo(ChildModel.class)).thenAnswer(new 
Answer<ChildModel>() {
+
+            @Override
+            public ChildModel answer(InvocationOnMock invocation) throws 
Throwable {
+                return factory.getAdapter(child, ChildModel.class);
+            }
+
+        });
+
+        Resource res = mock(Resource.class);
+        when(res.getChild("firstChild")).thenReturn(child);
+
+        ParentModel model = factory.getAdapter(res, ParentModel.class);
+        assertNotNull(model);
+        
+        ChildModel childModel = model.getFirstChild();
+        assertNotNull(childModel);
+        assertEquals(value, childModel.getProperty());
+    }
+
 }

Added: 
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/ChildModel.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/ChildModel.java?rev=1553233&view=auto
==============================================================================
--- 
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/ChildModel.java
 (added)
+++ 
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/ChildModel.java
 Tue Dec 24 00:24:22 2013
@@ -0,0 +1,33 @@
+/*
+ * 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.resource.Resource;
+import org.apache.sling.yamf.annotations.Model;
+
+@Model(adaptables = Resource.class)
+public class ChildModel {
+
+    @Inject
+    private String property;
+    
+    public String getProperty() {
+        return property;
+    }
+}

Added: 
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/interfaces/ChildResourceModel.java
URL: 
http://svn.apache.org/viewvc/sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/interfaces/ChildResourceModel.java?rev=1553233&view=auto
==============================================================================
--- 
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/interfaces/ChildResourceModel.java
 (added)
+++ 
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/interfaces/ChildResourceModel.java
 Tue Dec 24 00:24:22 2013
@@ -0,0 +1,30 @@
+/*
+ * 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.interfaces;
+
+import javax.inject.Inject;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.yamf.annotations.Model;
+
+@Model(adaptables = Resource.class)
+public interface ChildResourceModel {
+
+    @Inject
+    public Resource getFirstChild();
+
+}

Added: 
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/interfaces/ChildValueMapModel.java
URL: 
http://svn.apache.org/viewvc/sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/interfaces/ChildValueMapModel.java?rev=1553233&view=auto
==============================================================================
--- 
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/interfaces/ChildValueMapModel.java
 (added)
+++ 
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/interfaces/ChildValueMapModel.java
 Tue Dec 24 00:24:22 2013
@@ -0,0 +1,31 @@
+/*
+ * 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.interfaces;
+
+import javax.inject.Inject;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.yamf.annotations.Model;
+
+@Model(adaptables = Resource.class)
+public interface ChildValueMapModel {
+
+    @Inject
+    public ValueMap getFirstChild();
+
+}

Added: 
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/interfaces/ParentModel.java
URL: 
http://svn.apache.org/viewvc/sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/interfaces/ParentModel.java?rev=1553233&view=auto
==============================================================================
--- 
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/interfaces/ParentModel.java
 (added)
+++ 
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/interfaces/ParentModel.java
 Tue Dec 24 00:24:22 2013
@@ -0,0 +1,30 @@
+/*
+ * 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.interfaces;
+
+import javax.inject.Inject;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.yamf.annotations.Model;
+import org.apache.sling.yamf.testmodels.classes.ChildModel;
+
+@Model(adaptables = Resource.class)
+public interface ParentModel {
+
+    @Inject
+    public ChildModel getFirstChild();
+}


Reply via email to