You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ju...@apache.org on 2013/12/19 18:01:22 UTC
svn commit: r1552355 [1/2] - in /sling/whiteboard/justin: ./ yamf/
yamf/org.apache.sling.yamf.api/ yamf/org.apache.sling.yamf.api/src/
yamf/org.apache.sling.yamf.api/src/main/
yamf/org.apache.sling.yamf.api/src/main/java/
yamf/org.apache.sling.yamf.api...
Author: justin
Date: Thu Dec 19 17:01:20 2013
New Revision: 1552355
URL: http://svn.apache.org/r1552355
Log:
adding initial version of YAMF
Added:
sling/whiteboard/justin/.gitignore
sling/whiteboard/justin/yamf/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/pom.xml
sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/apache/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/apache/sling/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/apache/sling/yamf/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/apache/sling/yamf/api/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/apache/sling/yamf/api/Filter.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/apache/sling/yamf/api/Model.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/apache/sling/yamf/api/Optional.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/apache/sling/yamf/api/Projection.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/pom.xml
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/
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/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/injectors/BindingsInjector.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/main/java/org/apache/sling/yamf/impl/injectors/ValueMapInjector.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/spi/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/spi/Injector.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/OSGiInjectionTest.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/PostConstructTest.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/ProjectionTest.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/RequestInjectionTest.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/ResourceModelClassesTest.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/ResourceModelInterfacesTest.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/ArrayOSGiModel.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/BindingsModel.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/CollectionOSGiModel.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/ListOSGiModel.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/ProjectionModel.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/ResourceModelWithRequiredField.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/SetOSGiModel.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/SimpleOSGiModel.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/SimplePropertyModel.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/SubClass.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/SuperClass.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/interfaces/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/interfaces/ResourceModelWithRequiredField.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/interfaces/ServiceInterface.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/interfaces/SimplePropertyModel.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.server-side-tests/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.server-side-tests/pom.xml
sling/whiteboard/justin/yamf/org.apache.sling.yamf.server-side-tests/src/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.server-side-tests/src/main/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.server-side-tests/src/main/java/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.server-side-tests/src/main/java/org/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.server-side-tests/src/main/java/org/apache/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.server-side-tests/src/main/java/org/apache/sling/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.server-side-tests/src/main/java/org/apache/sling/yamf/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.server-side-tests/src/main/java/org/apache/sling/yamf/it/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.server-side-tests/src/main/java/org/apache/sling/yamf/it/SimpleTest.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.server-side-tests/src/main/java/org/apache/sling/yamf/it/models/
sling/whiteboard/justin/yamf/org.apache.sling.yamf.server-side-tests/src/main/java/org/apache/sling/yamf/it/models/TestModel.java
Added: sling/whiteboard/justin/.gitignore
URL: http://svn.apache.org/viewvc/sling/whiteboard/justin/.gitignore?rev=1552355&view=auto
==============================================================================
--- sling/whiteboard/justin/.gitignore (added)
+++ sling/whiteboard/justin/.gitignore Thu Dec 19 17:01:20 2013
@@ -0,0 +1,4 @@
+.project
+.settings/
+.classpath
+target/
Added: sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/pom.xml
URL: http://svn.apache.org/viewvc/sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/pom.xml?rev=1552355&view=auto
==============================================================================
--- sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/pom.xml (added)
+++ sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/pom.xml Thu Dec 19 17:01:20 2013
@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+ 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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>sling</artifactId>
+ <version>18</version>
+ </parent>
+
+ <artifactId>org.apache.sling.yamf.api</artifactId>
+ <packaging>bundle</packaging>
+ <version>0.0.1-SNAPSHOT</version>
+ <name>YAMF API</name>
+ <description>Yet Another Model Factory API</description>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-scr-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <extensions>true</extensions>
+ </plugin>
+ </plugins>
+ </build>
+</project>
Added: sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/apache/sling/yamf/api/Filter.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/apache/sling/yamf/api/Filter.java?rev=1552355&view=auto
==============================================================================
--- sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/apache/sling/yamf/api/Filter.java (added)
+++ sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/apache/sling/yamf/api/Filter.java Thu Dec 19 17:01:20 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.api;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Provide a filter on an @Inject.
+ */
+@Target({ ElementType.FIELD, ElementType.METHOD })
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Filter {
+
+ public String value();
+
+}
Added: sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/apache/sling/yamf/api/Model.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/apache/sling/yamf/api/Model.java?rev=1552355&view=auto
==============================================================================
--- sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/apache/sling/yamf/api/Model.java (added)
+++ sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/apache/sling/yamf/api/Model.java Thu Dec 19 17:01:20 2013
@@ -0,0 +1,34 @@
+/*
+ * 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.api;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Mark a class as adaptable via YAMF.
+ *
+ */
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Model {
+
+ public Class<?>[] adaptables();
+
+}
Added: sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/apache/sling/yamf/api/Optional.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/apache/sling/yamf/api/Optional.java?rev=1552355&view=auto
==============================================================================
--- sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/apache/sling/yamf/api/Optional.java (added)
+++ sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/apache/sling/yamf/api/Optional.java Thu Dec 19 17:01:20 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.api;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Marker annotation for optional injections.
+ */
+@Target({ ElementType.FIELD, ElementType.METHOD })
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Optional {
+
+}
Added: sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/apache/sling/yamf/api/Projection.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/apache/sling/yamf/api/Projection.java?rev=1552355&view=auto
==============================================================================
--- sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/apache/sling/yamf/api/Projection.java (added)
+++ sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/apache/sling/yamf/api/Projection.java Thu Dec 19 17:01:20 2013
@@ -0,0 +1,34 @@
+/*
+ * 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.api;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Indicate that this injection point should be handled using a projected
+ * property of the adaptable.
+ */
+@Target({ ElementType.FIELD, ElementType.METHOD })
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Projection {
+
+ public String value();
+
+}
\ No newline at end of file
Added: sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/pom.xml
URL: http://svn.apache.org/viewvc/sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/pom.xml?rev=1552355&view=auto
==============================================================================
--- sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/pom.xml (added)
+++ sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/pom.xml Thu Dec 19 17:01:20 2013
@@ -0,0 +1,160 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+ 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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>sling</artifactId>
+ <version>18</version>
+ </parent>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.yamf.impl</artifactId>
+ <packaging>bundle</packaging>
+ <version>0.0.1-SNAPSHOT</version>
+ <name>org.apache.sling.yamf.impl</name>
+ <description>org.apache.sling - org.apache.sling.yamf.impl</description>
+ <properties>
+ <sling.java.version>6</sling.java.version>
+ </properties>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-scr-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <extensions>true</extensions>
+ <configuration>
+ <instructions>
+ <Embed-Dependency>*;scope=compile</Embed-Dependency>
+ </instructions>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.yamf.api</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.compendium</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.core</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.api</artifactId>
+ <version>2.2.0</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.scr.annotations</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>javax.inject</groupId>
+ <artifactId>javax.inject</artifactId>
+ <version>1</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-all</artifactId>
+ <version>1.9.5</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-simple</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.commons.osgi</artifactId>
+ <version>2.2.0</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>commons-lang</groupId>
+ <artifactId>commons-lang</artifactId>
+ <version>2.5</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>commons-beanutils</groupId>
+ <artifactId>commons-beanutils</artifactId>
+ <version>1.8.3</version>
+ <scope>compile</scope>
+ </dependency>
+ </dependencies>
+ <profiles>
+ <profile>
+ <id>autoInstallBundle</id>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>maven-sling-plugin</artifactId>
+ <version>2.0.4-incubator</version>
+ <executions>
+ <execution>
+ <id>install-bundle</id>
+ <goals>
+ <goal>install</goal>
+ </goals>
+ <configuration>
+ <slingUrl>http://localhost:8888/system/console</slingUrl>
+ <user>admin</user>
+ <password>admin</password>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+ </profile>
+ </profiles>
+</project>
Added: 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=1552355&view=auto
==============================================================================
--- sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/YamfAdapterFactory.java (added)
+++ sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/YamfAdapterFactory.java Thu Dec 19 17:01:20 2013
@@ -0,0 +1,481 @@
+/*
+ * 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.impl;
+
+import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.lang.reflect.Type;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+
+import javax.annotation.PostConstruct;
+import javax.inject.Inject;
+import javax.inject.Named;
+
+import org.apache.commons.beanutils.PropertyUtils;
+import org.apache.commons.lang.StringUtils;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+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.AdapterFactory;
+import org.apache.sling.commons.osgi.PropertiesUtil;
+import org.apache.sling.commons.osgi.ServiceUtil;
+import org.apache.sling.yamf.api.Model;
+import org.apache.sling.yamf.api.Optional;
+import org.apache.sling.yamf.api.Projection;
+import org.apache.sling.yamf.spi.Injector;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.BundleListener;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Component
+public class YamfAdapterFactory implements AdapterFactory, BundleListener {
+
+ public static class MapBackedInvocationHandler implements InvocationHandler {
+
+ private Map<Method, Object> methods;
+
+ public MapBackedInvocationHandler(Map<Method, Object> methods) {
+ this.methods = methods;
+ }
+
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+ return methods.get(method);
+ }
+
+ }
+
+ private static final Logger log = LoggerFactory.getLogger(YamfAdapterFactory.class);
+
+ private BundleContext bundleContext;
+
+ @Reference(name = "injector", referenceInterface = Injector.class,
+ cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE, policy = ReferencePolicy.DYNAMIC)
+ private final Map<Object, Injector> injectors = new TreeMap<Object, Injector>();
+
+ private Map<Bundle, List<ServiceRegistration>> registeredModelFactories;
+
+ private volatile Injector[] sortedInjectors = new Injector[0];
+
+ @Override
+ public synchronized void bundleChanged(BundleEvent event) {
+ if (event.getType() == BundleEvent.STOPPED) {
+ removeBundle(event.getBundle());
+ } else if (event.getType() == BundleEvent.STARTED) {
+ addBundle(event.getBundle());
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
+ Model modelAnnotation = type.getAnnotation(Model.class);
+ if (modelAnnotation == null) {
+ return null;
+ }
+ boolean isAdaptable = false;
+
+ Class<?>[] declaredAdaptable = modelAnnotation.adaptables();
+ for (Class<?> clazz : declaredAdaptable) {
+ if (clazz.isInstance(adaptable)) {
+ isAdaptable = true;
+ }
+ }
+ if (!isAdaptable) {
+ return null;
+ }
+
+ if (type.isInterface()) {
+ InvocationHandler handler = createInvocationHandler(adaptable, type);
+ if (handler != null) {
+ return (AdapterType) Proxy.newProxyInstance(type.getClassLoader(), new Class<?>[] { type },
+ handler);
+ } else {
+ return null;
+ }
+ } else {
+ try {
+ return createObject(adaptable, type);
+ } catch (Exception e) {
+ log.error("unable to create object", e);
+ return null;
+ }
+ }
+ }
+
+ private void addBundle(final Bundle bundle) {
+ Dictionary<?, ?> headers = bundle.getHeaders();
+ String packageList = PropertiesUtil.toString(headers.get("Sling-YAMF-Packages"), null);
+ if (packageList != null) {
+ List<ServiceRegistration> regs = new ArrayList<ServiceRegistration>();
+
+ packageList = StringUtils.deleteWhitespace(packageList);
+ String[] packages = packageList.split(",");
+ for (String singlePackage : packages) {
+ @SuppressWarnings("unchecked")
+ Enumeration<URL> classUrls = bundle.findEntries("/" + singlePackage.replace('.', '/'), "*.class",
+ true);
+ while (classUrls.hasMoreElements()) {
+ URL url = classUrls.nextElement();
+ String className = toClassName(url);
+ try {
+ Class<?> clazz = bundle.loadClass(className);
+ Model annotation = clazz.getAnnotation(Model.class);
+ if (annotation != null) {
+ Class<?>[] adaptables = annotation.adaptables();
+ String[] classNames = toStringArray(adaptables);
+ Dictionary<String, Object> registrationProps = new Hashtable<String, Object>();
+ registrationProps.put(AdapterFactory.ADAPTER_CLASSES, className);
+ registrationProps.put(AdapterFactory.ADAPTABLE_CLASSES, classNames);
+ ServiceRegistration reg = bundleContext.registerService(AdapterFactory.SERVICE_NAME, this, registrationProps);
+ regs.add(reg);
+ }
+ } catch (ClassNotFoundException e) {
+ log.warn("Unable to load class", e);
+ }
+
+ }
+ }
+
+ if (!regs.isEmpty()) {
+ synchronized (this) {
+ registeredModelFactories.put(bundle, regs);
+ }
+ }
+ }
+ }
+
+ private String[] toStringArray(Class<?>[] classes) {
+ String[] arr = new String[classes.length];
+ for (int i = 0; i < classes.length; i++) {
+ arr[i] = classes[i].getName();
+ }
+ return arr;
+ }
+
+ /** Convert class URL to class name */
+ private String toClassName(URL url) {
+ final String f = url.getFile();
+ final String cn = f.substring(1, f.length() - ".class".length());
+ return cn.replace('/', '.');
+ }
+
+ private Set<Field> collectInjectableFields(Class<?> type) {
+ Set<Field> result = new HashSet<Field>();
+ while (type != null) {
+ Field[] fields = type.getDeclaredFields();
+ for (Field field : fields) {
+ Inject injection = field.getAnnotation(Inject.class);
+ if (injection != null) {
+ result.add(field);
+ }
+ }
+ type = type.getSuperclass();
+ }
+ return result;
+ }
+
+ private Set<Method> collectInjectableMethods(Class<?> type) {
+ Set<Method> result = new HashSet<Method>();
+ while (type != null) {
+ Method[] methods = type.getDeclaredMethods();
+ for (Method method : methods) {
+ Inject injection = method.getAnnotation(Inject.class);
+ if (injection != null) {
+ result.add(method);
+ }
+ }
+ type = type.getSuperclass();
+ }
+ return result;
+ }
+
+ private InvocationHandler createInvocationHandler(final Object adaptable, final Class<?> type) {
+ Set<Method> injectableMethods = collectInjectableMethods(type);
+ Map<Method, Object> methods = new HashMap<Method, Object>();
+
+ for (Injector injector : sortedInjectors) {
+ Iterator<Method> it = injectableMethods.iterator();
+ while (it.hasNext()) {
+ Method method = it.next();
+ String name = getName(method);
+ Type returnType = mapPrimitiveClasses(method.getGenericReturnType());
+ Object injectionAdaptable = getAdaptable(adaptable, method);
+ if (injectionAdaptable != null) {
+ Object value = injector.getValue(injectionAdaptable, name, returnType, method);
+ if (setMethod(method, methods, value)) {
+ it.remove();
+ }
+ }
+ }
+ }
+
+ if (injectableMethods.isEmpty()) {
+ return new MapBackedInvocationHandler(methods);
+ } else {
+ Set<Method> requiredMethods = new HashSet<Method>();
+ for (Method method : injectableMethods) {
+ if (method.getAnnotation(Optional.class) == null) {
+ requiredMethods.add(method);
+ }
+ }
+
+ if (!requiredMethods.isEmpty()) {
+ log.warn("Required methods {} on model class {} were not able to be injected.", requiredMethods,
+ type);
+ return null;
+ } else {
+ return new MapBackedInvocationHandler(methods);
+ }
+ }
+ }
+
+ private <AdapterType> AdapterType createObject(Object adaptable, Class<AdapterType> type)
+ throws InstantiationException, IllegalAccessException {
+ Set<Field> injectableFields = collectInjectableFields(type);
+
+ AdapterType object = type.newInstance();
+
+ for (Injector injector : sortedInjectors) {
+ Iterator<Field> it = injectableFields.iterator();
+ while (it.hasNext()) {
+ Field field = it.next();
+ String name = getName(field);
+ Type fieldType = mapPrimitiveClasses(field.getGenericType());
+ Object injectionAdaptable = getAdaptable(adaptable, field);
+ if (injectionAdaptable != null) {
+ Object value = injector.getValue(injectionAdaptable, name, fieldType, field);
+ if (setField(field, object, value)) {
+ it.remove();
+ }
+ }
+ }
+ }
+
+ if (injectableFields.isEmpty()) {
+ try {
+ invokePostConstruct(object);
+ return object;
+ } catch (Exception e) {
+ log.error("Unable to invoke post construct method.", e);
+ return null;
+ }
+ } else {
+ Set<Field> requiredFields = new HashSet<Field>();
+ for (Field field : injectableFields) {
+ if (field.getAnnotation(Optional.class) == null) {
+ requiredFields.add(field);
+ }
+ }
+
+ if (!requiredFields.isEmpty()) {
+ log.warn("Required properties {} on model class {} were not able to be injected.", requiredFields,
+ type);
+ return null;
+ } else {
+ try {
+ invokePostConstruct(object);
+ return object;
+ } catch (Exception e) {
+ log.error("Unable to invoke post construct method.", e);
+ return null;
+ }
+ }
+ }
+ }
+
+ private Object getAdaptable(Object adaptable, AnnotatedElement point) {
+ Projection projection = point.getAnnotation(Projection.class);
+ if (projection == null) {
+ return adaptable;
+ }
+ String projectionPath = projection.value();
+ try {
+ return PropertyUtils.getProperty(adaptable, projectionPath);
+ } catch (Exception e) {
+ log.error("Unable to execution projection " + projectionPath, e);
+ return null;
+ }
+ }
+
+ private String getName(Field field) {
+ Named named = field.getAnnotation(Named.class);
+ if (named != null) {
+ return named.value();
+ }
+ return field.getName();
+ }
+
+ private String getName(Method method) {
+ Named named = method.getAnnotation(Named.class);
+ if (named != null) {
+ return named.value();
+ }
+ String methodName = method.getName();
+ if (methodName.startsWith("get")) {
+ return methodName.substring(3, 4).toLowerCase() + methodName.substring(4);
+ } else if (methodName.startsWith("is")) {
+ return methodName.substring(2, 3).toLowerCase() + methodName.substring(3);
+ } else {
+ return methodName;
+ }
+ }
+
+ private void invokePostConstruct(Object object) throws Exception {
+ Class<?> clazz = object.getClass();
+ List<Method> postConstructMethods = new ArrayList<Method>();
+ while (clazz != null) {
+ Method[] methods = clazz.getDeclaredMethods();
+ for (Method method : methods) {
+ if (method.isAnnotationPresent(PostConstruct.class)) {
+ postConstructMethods.add(method);
+ }
+ }
+ clazz = clazz.getSuperclass();
+ }
+ Collections.reverse(postConstructMethods);
+ for (Method method : postConstructMethods) {
+ boolean accessible = method.isAccessible();
+ try {
+ if (!accessible) {
+ method.setAccessible(true);
+ }
+ method.invoke(object);
+ } finally {
+ if (!accessible) {
+ method.setAccessible(false);
+ }
+ }
+ }
+ }
+
+ private Type mapPrimitiveClasses(Type type) {
+ if (type == Integer.TYPE) {
+ return Integer.class;
+ }
+ if (type == Long.TYPE) {
+ return Long.class;
+ }
+ if (type == Boolean.TYPE) {
+ return Boolean.class;
+ }
+ if (type == Double.TYPE) {
+ return Double.class;
+ }
+ if (type == Float.TYPE) {
+ return Float.class;
+ }
+ if (type == Short.TYPE) {
+ return Short.class;
+ }
+ if (type == Character.TYPE) {
+ return Character.class;
+ }
+
+ return type;
+ }
+
+ private void removeBundle(final Bundle bundle) {
+ if (registeredModelFactories.containsKey(bundle)) {
+ List<ServiceRegistration> regs = registeredModelFactories.get(bundle);
+ for (ServiceRegistration reg : regs) {
+ reg.unregister();
+ }
+ registeredModelFactories.remove(bundle);
+ }
+ }
+
+ private boolean setField(Field field, Object createdObject, Object value) {
+ if (value != null) {
+ boolean accessible = field.isAccessible();
+ try {
+ if (!accessible) {
+ field.setAccessible(true);
+ }
+ field.set(createdObject, value);
+ return true;
+ } catch (Exception e) {
+ log.error("unable to inject field", e);
+ return false;
+ } finally {
+ if (!accessible) {
+ field.setAccessible(false);
+ }
+ }
+ } else {
+ return false;
+ }
+ }
+
+ private boolean setMethod(Method method, Map<Method, Object> methods, Object value) {
+ if (value != null && method.getReturnType().isInstance(value)) {
+ methods.put(method, value);
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ @Activate
+ protected void activate(final ComponentContext ctx) throws InvalidSyntaxException {
+ this.bundleContext = ctx.getBundleContext();
+ this.registeredModelFactories = new HashMap<Bundle, List<ServiceRegistration>>();
+ for (final Bundle bundle : this.bundleContext.getBundles()) {
+ if (bundle.getState() == Bundle.ACTIVE) {
+ addBundle(bundle);
+ }
+ }
+
+ this.bundleContext.addBundleListener(this);
+ }
+
+ protected void bindInjector(final Injector injector, final Map<String, Object> props) {
+ synchronized (injectors) {
+ injectors.put(ServiceUtil.getComparableForServiceRanking(props), injector);
+ sortedInjectors = injectors.values().toArray(new Injector[injectors.size()]);
+ }
+ }
+
+ protected void unbindInjector(final Injector injector, final Map<String, Object> props) {
+ synchronized (injectors) {
+ injectors.remove(ServiceUtil.getComparableForServiceRanking(props));
+ sortedInjectors = injectors.values().toArray(new Injector[injectors.size()]);
+ }
+ }
+
+}
Added: sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/injectors/BindingsInjector.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/BindingsInjector.java?rev=1552355&view=auto
==============================================================================
--- sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/injectors/BindingsInjector.java (added)
+++ sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/injectors/BindingsInjector.java Thu Dec 19 17:01:20 2013
@@ -0,0 +1,69 @@
+/*
+ * 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.impl.injectors;
+
+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.Service;
+import org.apache.sling.api.scripting.SlingBindings;
+import org.apache.sling.yamf.spi.Injector;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Component
+@Service
+public class BindingsInjector implements Injector {
+
+ private static final Logger log = LoggerFactory.getLogger(BindingsInjector.class);
+
+ private static Object getValue(SlingBindings bindings, String name, Class<?> type) {
+ Object value = bindings.get(name);
+ if (type.isInstance(value)) {
+ return value;
+ } else {
+ return null;
+ }
+ }
+
+ public Object getValue(Object adaptable, String name, Type type, AnnotatedElement element) {
+ SlingBindings bindings = getBindings(adaptable);
+ if (bindings == null) {
+ return null;
+ }
+ if (type instanceof Class<?>) {
+ return getValue(bindings, name, (Class<?>) type);
+ } else {
+ log.warn("BindingsInjector doesn't support non-class types");
+ return null;
+ }
+
+ }
+
+ private SlingBindings getBindings(Object adaptable) {
+ if (adaptable instanceof ServletRequest) {
+ ServletRequest request = (ServletRequest) adaptable;
+ return (SlingBindings) request.getAttribute(SlingBindings.class.getName());
+ } else {
+ return null;
+ }
+ }
+
+}
Added: 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=1552355&view=auto
==============================================================================
--- sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/injectors/OSGiServiceInjector.java (added)
+++ sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/injectors/OSGiServiceInjector.java Thu Dec 19 17:01:20 2013
@@ -0,0 +1,132 @@
+/*
+ * 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.impl.injectors;
+
+import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Array;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+
+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.yamf.api.Filter;
+import org.apache.sling.yamf.spi.Injector;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Component
+@Service
+public class OSGiServiceInjector implements Injector {
+
+ private static final Logger log = LoggerFactory.getLogger(OSGiServiceInjector.class);
+
+ private BundleContext bundleContext;
+
+ @Activate
+ public void activate(ComponentContext ctx) {
+ this.bundleContext = ctx.getBundleContext();
+ }
+
+ public Object getValue(Object adaptable, String name, Type type, AnnotatedElement element) {
+ Filter filter = element.getAnnotation(Filter.class);
+ String filterString = null;
+ if (filter != null) {
+ filterString = filter.value();
+ }
+
+ return getValue(type, filterString);
+ }
+
+ private Object getService(Class<?> type, String filter) {
+ 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;
+ }
+ }
+
+ 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);
+ }
+ }
+ return services.toArray();
+ }
+ } catch (InvalidSyntaxException e) {
+ log.error("invalid filter expression", e);
+ return null;
+ }
+ }
+
+ private Object getValue(Type type, String filterString) {
+ if (type instanceof Class) {
+ Class<?> injectedClass = (Class<?>) type;
+ if (injectedClass == BundleContext.class) {
+ return bundleContext;
+ } else if (injectedClass.isArray()) {
+ Object[] services = getServices(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);
+ }
+ } else if (type instanceof ParameterizedType) {
+ ParameterizedType ptype = (ParameterizedType) type;
+ if (ptype.getActualTypeArguments().length != 1) {
+ return null;
+ }
+ Class<?> collectionType = (Class<?>) ptype.getRawType();
+ if (!(collectionType.equals(Collection.class) || collectionType.equals(List.class))) {
+ return null;
+ }
+
+ Class<?> serviceType = (Class<?>) ptype.getActualTypeArguments()[0];
+ Object[] services = getServices(serviceType, filterString);
+ return Arrays.asList(services);
+ } else {
+ log.warn("Cannot handle type {}", type);
+ return null;
+ }
+ }
+
+}
Added: 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=1552355&view=auto
==============================================================================
--- sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/injectors/RequestAttributeInjector.java (added)
+++ sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/injectors/RequestAttributeInjector.java Thu Dec 19 17:01:20 2013
@@ -0,0 +1,54 @@
+/*
+ * 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.impl.injectors;
+
+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.Service;
+import org.apache.sling.yamf.spi.Injector;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Component
+@Service
+public class RequestAttributeInjector implements Injector {
+
+ private static final Logger log = LoggerFactory.getLogger(RequestAttributeInjector.class);
+
+ @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;
+ }
+ } else {
+ log.warn("BindingsInjector doesn't support non-class types");
+ return null;
+ }
+ }
+
+}
Added: sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/injectors/ValueMapInjector.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/ValueMapInjector.java?rev=1552355&view=auto
==============================================================================
--- sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/injectors/ValueMapInjector.java (added)
+++ sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/injectors/ValueMapInjector.java Thu Dec 19 17:01:20 2013
@@ -0,0 +1,59 @@
+/*
+ * 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.impl.injectors;
+
+import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Type;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.adapter.Adaptable;
+import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.yamf.spi.Injector;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Component
+@Service
+public class ValueMapInjector implements Injector {
+
+ private static final Logger log = LoggerFactory.getLogger(ValueMapInjector.class);
+
+ public Object getValue(Object adaptable, String name, Type type, AnnotatedElement element) {
+ ValueMap map = getMap(adaptable);
+ if (map == null) {
+ return null;
+ } else if (type instanceof Class<?>) {
+ return map.get(name, (Class<?>) type);
+ } else {
+ log.warn("ValueMapInjector doesn't support non-class types");
+ return null;
+ }
+ }
+
+ private ValueMap getMap(Object adaptable) {
+ if (adaptable instanceof ValueMap) {
+ return (ValueMap) adaptable;
+ } else if (adaptable instanceof Adaptable) {
+ ValueMap map = ((Adaptable) adaptable).adaptTo(ValueMap.class);
+ return map;
+ } else {
+ return null;
+ }
+ }
+
+}
Added: sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/spi/Injector.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/spi/Injector.java?rev=1552355&view=auto
==============================================================================
--- sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/spi/Injector.java (added)
+++ sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/spi/Injector.java Thu Dec 19 17:01:20 2013
@@ -0,0 +1,25 @@
+/*
+ * 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.spi;
+
+import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Type;
+
+public interface Injector {
+
+ Object getValue(Object adaptable, String name, Type declaredType, AnnotatedElement element);
+}
\ No newline at end of file
Added: 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=1552355&view=auto
==============================================================================
--- sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/OSGiInjectionTest.java (added)
+++ sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/OSGiInjectionTest.java Thu Dec 19 17:01:20 2013
@@ -0,0 +1,172 @@
+/*
+ * 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.impl;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
+
+import java.util.Collections;
+
+import org.apache.sling.api.resource.Resource;
+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.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.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.component.ComponentContext;
+
+@RunWith(MockitoJUnitRunner.class)
+public class OSGiInjectionTest {
+ private YamfAdapterFactory factory;
+
+ @Mock
+ private ComponentContext componentCtx;
+
+ @Mock
+ private BundleContext bundleContext;
+
+ @Before
+ public void setup() {
+ factory = new YamfAdapterFactory();
+ when(componentCtx.getBundleContext()).thenReturn(bundleContext);
+
+ OSGiServiceInjector injectorFactory = new OSGiServiceInjector();
+ injectorFactory.activate(componentCtx);
+ factory.bindInjector(injectorFactory,
+ Collections.<String, Object> singletonMap(Constants.SERVICE_ID, 0L));
+ }
+
+ @Test
+ public void testSimpleOSGiModel() throws Exception {
+ ServiceReference ref = mock(ServiceReference.class);
+ ServiceInterface service = mock(ServiceInterface.class);
+ when(bundleContext.getServiceReferences(ServiceInterface.class.getName(), null)).thenReturn(
+ new ServiceReference[] { ref });
+ when(bundleContext.getService(ref)).thenReturn(service);
+
+ Resource res = mock(Resource.class);
+
+ SimpleOSGiModel model = factory.getAdapter(res, SimpleOSGiModel.class);
+ assertNotNull(model);
+ assertNotNull(model.getService());
+ assertEquals(service, model.getService());
+
+ verifyNoMoreInteractions(res);
+ }
+
+ @Test
+ public void testListOSGiModel() throws Exception {
+ ServiceReference ref1 = mock(ServiceReference.class);
+ ServiceInterface service1 = mock(ServiceInterface.class);
+ when(bundleContext.getService(ref1)).thenReturn(service1);
+ ServiceReference ref2 = mock(ServiceReference.class);
+ ServiceInterface service2 = mock(ServiceInterface.class);
+ when(bundleContext.getService(ref2)).thenReturn(service2);
+
+ when(bundleContext.getServiceReferences(ServiceInterface.class.getName(), null)).thenReturn(
+ new ServiceReference[] { ref1, ref2 });
+
+ Resource res = mock(Resource.class);
+
+ ListOSGiModel model = factory.getAdapter(res, ListOSGiModel.class);
+ assertNotNull(model);
+ assertNotNull(model.getServices());
+ assertEquals(2, model.getServices().size());
+ assertEquals(service1, model.getServices().get(0));
+ assertEquals(service2, model.getServices().get(1));
+
+ verifyNoMoreInteractions(res);
+ }
+
+ @Test
+ public void testArrayOSGiModel() throws Exception {
+ ServiceReference ref1 = mock(ServiceReference.class);
+ ServiceInterface service1 = mock(ServiceInterface.class);
+ when(bundleContext.getService(ref1)).thenReturn(service1);
+ ServiceReference ref2 = mock(ServiceReference.class);
+ ServiceInterface service2 = mock(ServiceInterface.class);
+ when(bundleContext.getService(ref2)).thenReturn(service2);
+
+ when(bundleContext.getServiceReferences(ServiceInterface.class.getName(), null)).thenReturn(
+ new ServiceReference[] { ref1, ref2 });
+
+ Resource res = mock(Resource.class);
+
+ ArrayOSGiModel model = factory.getAdapter(res, ArrayOSGiModel.class);
+ assertNotNull(model);
+ assertNotNull(model.getServices());
+ assertEquals(2, model.getServices().length);
+ assertEquals(service1, model.getServices()[0]);
+ assertEquals(service2, model.getServices()[1]);
+
+ verifyNoMoreInteractions(res);
+ }
+ @Test
+ public void testCollectionOSGiModel() throws Exception {
+ ServiceReference ref1 = mock(ServiceReference.class);
+ ServiceInterface service1 = mock(ServiceInterface.class);
+ when(bundleContext.getService(ref1)).thenReturn(service1);
+ ServiceReference ref2 = mock(ServiceReference.class);
+ ServiceInterface service2 = mock(ServiceInterface.class);
+ when(bundleContext.getService(ref2)).thenReturn(service2);
+
+ when(bundleContext.getServiceReferences(ServiceInterface.class.getName(), null)).thenReturn(
+ new ServiceReference[] { ref1, ref2 });
+
+ Resource res = mock(Resource.class);
+
+ CollectionOSGiModel model = factory.getAdapter(res, CollectionOSGiModel.class);
+ assertNotNull(model);
+ assertNotNull(model.getServices());
+ assertEquals(2, model.getServices().size());
+
+ assertTrue(model.getServices().contains(service1));
+ assertTrue(model.getServices().contains(service2));
+
+ verifyNoMoreInteractions(res);
+ }
+
+ @Test
+ public void testSetOSGiModel() throws Exception {
+ ServiceReference ref1 = mock(ServiceReference.class);
+ ServiceInterface service1 = mock(ServiceInterface.class);
+ when(bundleContext.getService(ref1)).thenReturn(service1);
+ ServiceReference ref2 = mock(ServiceReference.class);
+ ServiceInterface service2 = mock(ServiceInterface.class);
+ when(bundleContext.getService(ref2)).thenReturn(service2);
+
+ when(bundleContext.getServiceReferences(ServiceInterface.class.getName(), null)).thenReturn(
+ new ServiceReference[] { ref1, ref2 });
+
+ Resource res = mock(Resource.class);
+
+ SetOSGiModel model = factory.getAdapter(res, SetOSGiModel.class);
+ assertNull(model);
+
+ verifyNoMoreInteractions(res, bundleContext);
+ }
+}
Added: sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/PostConstructTest.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/PostConstructTest.java?rev=1552355&view=auto
==============================================================================
--- sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/PostConstructTest.java (added)
+++ sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/PostConstructTest.java Thu Dec 19 17:01:20 2013
@@ -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.yamf.impl;
+
+import static org.mockito.Mockito.*;
+import static org.junit.Assert.*;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.yamf.testmodels.classes.SubClass;
+import org.junit.Test;
+
+public class PostConstructTest {
+
+ @Test
+ public void testClassOrder() {
+ Resource r = mock(Resource.class);
+ YamfAdapterFactory factory = new YamfAdapterFactory();
+ // no injectors are necessary
+
+ SubClass sc = factory.getAdapter(r, SubClass.class);
+ assertTrue(sc.getPostConstructCalledTimestampInSub() > sc.getPostConstructCalledTimestampInSuper());
+ assertTrue(sc.getPostConstructCalledTimestampInSuper() > 0);
+ }
+}
Added: sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/ProjectionTest.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/ProjectionTest.java?rev=1552355&view=auto
==============================================================================
--- sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/ProjectionTest.java (added)
+++ sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/ProjectionTest.java Thu Dec 19 17:01:20 2013
@@ -0,0 +1,68 @@
+/*
+ * 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.impl;
+
+import static org.mockito.Mockito.*;
+import static org.junit.Assert.*;
+
+import java.util.Collections;
+
+import org.apache.commons.lang.RandomStringUtils;
+import org.apache.sling.api.SlingHttpServletRequest;
+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.ValueMapInjector;
+import org.apache.sling.yamf.testmodels.classes.ProjectionModel;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.osgi.framework.Constants;
+
+@RunWith(MockitoJUnitRunner.class)
+public class ProjectionTest {
+
+ @Mock
+ private Resource resource;
+
+ @Mock
+ private SlingHttpServletRequest request;
+
+ private YamfAdapterFactory factory;
+
+ @Before
+ public void setup() {
+ when(request.getResource()).thenReturn(resource);
+ factory = new YamfAdapterFactory();
+ factory.bindInjector(new ValueMapInjector(),
+ Collections.<String, Object> singletonMap(Constants.SERVICE_ID, 0L));
+ }
+
+ @Test
+ public void testProjectionToResource() {
+ String value = RandomStringUtils.randomAlphanumeric(10);
+ ValueMap map = new ValueMapDecorator(Collections.<String, Object> singletonMap("firstProperty", value));
+ when(resource.adaptTo(ValueMap.class)).thenReturn(map);
+
+ ProjectionModel model = factory.getAdapter(request, ProjectionModel.class);
+ assertNotNull(model);
+ assertEquals(value, model.getFirstProperty());
+ }
+
+}
Added: sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/RequestInjectionTest.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/RequestInjectionTest.java?rev=1552355&view=auto
==============================================================================
--- sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/RequestInjectionTest.java (added)
+++ sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/RequestInjectionTest.java Thu Dec 19 17:01:20 2013
@@ -0,0 +1,64 @@
+/*
+ * 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.impl;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
+
+import java.util.Collections;
+
+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.impl.injectors.BindingsInjector;
+import org.apache.sling.yamf.testmodels.classes.BindingsModel;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.osgi.framework.Constants;
+
+@RunWith(MockitoJUnitRunner.class)
+public class RequestInjectionTest {
+
+ private YamfAdapterFactory factory;
+
+ @Mock
+ private SlingHttpServletRequest request;
+
+ @Mock
+ private SlingScriptHelper sling;
+
+ @Before
+ public void setup() {
+ SlingBindings bindings = new SlingBindings();
+ bindings.setSling(sling);
+ when(request.getAttribute(SlingBindings.class.getName())).thenReturn(bindings);
+
+ factory = new YamfAdapterFactory();
+ factory.bindInjector(new BindingsInjector(), Collections.<String, Object> singletonMap(Constants.SERVICE_ID, 0L));
+ }
+
+ @Test
+ public void testNamedInjection() {
+ BindingsModel model = factory.getAdapter(request, BindingsModel.class);
+ assertNotNull(model.getSling());
+ assertEquals(sling, model.getSling());
+ }
+
+}
Added: 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=1552355&view=auto
==============================================================================
--- sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/ResourceModelClassesTest.java (added)
+++ sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/ResourceModelClassesTest.java Thu Dec 19 17:01:20 2013
@@ -0,0 +1,83 @@
+/*
+ * 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.impl;
+
+import static org.mockito.Mockito.*;
+import static org.junit.Assert.*;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+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.ValueMapInjector;
+import org.apache.sling.yamf.testmodels.classes.ResourceModelWithRequiredField;
+import org.apache.sling.yamf.testmodels.classes.SimplePropertyModel;
+import org.junit.Before;
+import org.junit.Test;
+import org.osgi.framework.Constants;
+
+public class ResourceModelClassesTest {
+
+ private YamfAdapterFactory factory;
+
+ @Before
+ public void setup() {
+ factory = new YamfAdapterFactory();
+ factory.bindInjector(new ValueMapInjector(),
+ Collections.<String, Object> singletonMap(Constants.SERVICE_ID, 0L));
+ }
+
+ @Test
+ public void testSimplePropertyModel() {
+ Map<String, Object> map = new HashMap<String, Object>();
+ map.put("first", "first-value");
+ map.put("third", "third-value");
+ map.put("intProperty", new Integer(3));
+ ValueMap vm = new ValueMapDecorator(map);
+
+ Resource res = mock(Resource.class);
+ when(res.adaptTo(ValueMap.class)).thenReturn(vm);
+
+ SimplePropertyModel model = factory.getAdapter(res, SimplePropertyModel.class);
+ assertNotNull(model);
+ assertEquals("first-value", model.getFirst());
+ assertNull(model.getSecond());
+ assertEquals("third-value", model.getThirdProperty());
+ assertEquals(3, model.getIntProperty());
+ assertTrue(model.isPostConstructCalled());
+ }
+
+ @Test
+ public void testRequiredPropertyModel() {
+ Map<String, Object> map = new HashMap<String, Object>();
+ map.put("first", "first-value");
+ map.put("third", "third-value");
+ ValueMap vm = spy(new ValueMapDecorator(map));
+
+ Resource res = mock(Resource.class);
+ when(res.adaptTo(ValueMap.class)).thenReturn(vm);
+
+ ResourceModelWithRequiredField model = factory.getAdapter(res, ResourceModelWithRequiredField.class);
+ assertNull(model);
+
+ verify(vm).get("required", String.class);
+ }
+
+}
Added: sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/ResourceModelInterfacesTest.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/ResourceModelInterfacesTest.java?rev=1552355&view=auto
==============================================================================
--- sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/ResourceModelInterfacesTest.java (added)
+++ sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/ResourceModelInterfacesTest.java Thu Dec 19 17:01:20 2013
@@ -0,0 +1,80 @@
+/*
+ * 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.impl;
+
+import static org.mockito.Mockito.*;
+import static org.junit.Assert.*;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+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.ValueMapInjector;
+import org.apache.sling.yamf.testmodels.classes.ResourceModelWithRequiredField;
+import org.apache.sling.yamf.testmodels.interfaces.SimplePropertyModel;
+import org.junit.Before;
+import org.junit.Test;
+import org.osgi.framework.Constants;
+
+public class ResourceModelInterfacesTest {
+
+ private YamfAdapterFactory factory;
+
+ @Before
+ public void setup() {
+ factory = new YamfAdapterFactory();
+ factory.bindInjector(new ValueMapInjector(),
+ Collections.<String, Object> singletonMap(Constants.SERVICE_ID, 0L));
+ }
+
+ @Test
+ public void testSimplePropertyModel() {
+ Map<String, Object> map = new HashMap<String, Object>();
+ map.put("first", "first-value");
+ map.put("third", "third-value");
+ ValueMap vm = new ValueMapDecorator(map);
+
+ Resource res = mock(Resource.class);
+ when(res.adaptTo(ValueMap.class)).thenReturn(vm);
+
+ SimplePropertyModel model = factory.getAdapter(res, SimplePropertyModel.class);
+ assertNotNull(model);
+ assertEquals("first-value", model.getFirst());
+ assertNull(model.getSecond());
+ assertEquals("third-value", model.getThirdProperty());
+ }
+
+ @Test
+ public void testRequiredPropertyModel() {
+ Map<String, Object> map = new HashMap<String, Object>();
+ map.put("first", "first-value");
+ map.put("third", "third-value");
+ ValueMap vm = spy(new ValueMapDecorator(map));
+
+ Resource res = mock(Resource.class);
+ when(res.adaptTo(ValueMap.class)).thenReturn(vm);
+
+ ResourceModelWithRequiredField model = factory.getAdapter(res, ResourceModelWithRequiredField.class);
+ assertNull(model);
+
+ verify(vm).get("required", String.class);
+ }
+
+}
Added: sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/ArrayOSGiModel.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/ArrayOSGiModel.java?rev=1552355&view=auto
==============================================================================
--- sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/ArrayOSGiModel.java (added)
+++ sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/ArrayOSGiModel.java Thu Dec 19 17:01:20 2013
@@ -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.resource.Resource;
+import org.apache.sling.yamf.api.Model;
+import org.apache.sling.yamf.testmodels.interfaces.ServiceInterface;
+
+@Model(adaptables = Resource.class)
+public class ArrayOSGiModel {
+
+ @Inject
+ private ServiceInterface[] services;
+
+ public ServiceInterface[] getServices() {
+ return services;
+ }
+
+}
Added: sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/BindingsModel.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/BindingsModel.java?rev=1552355&view=auto
==============================================================================
--- sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/BindingsModel.java (added)
+++ sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/BindingsModel.java Thu Dec 19 17:01:20 2013
@@ -0,0 +1,37 @@
+/*
+ * 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 javax.inject.Named;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.scripting.SlingScriptHelper;
+import org.apache.sling.yamf.api.Model;
+
+@Model(adaptables=SlingHttpServletRequest.class)
+public class BindingsModel {
+
+ @Inject
+ @Named("sling")
+ private SlingScriptHelper sling;
+
+ public SlingScriptHelper getSling() {
+ return sling;
+ }
+
+}