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 2014/01/22 18:36:42 UTC
svn commit: r1560437 [1/3] - in /sling/trunk: ./ bundles/extensions/models/
bundles/extensions/models/api/ bundles/extensions/models/api/src/
bundles/extensions/models/api/src/main/
bundles/extensions/models/api/src/main/java/ bundles/extensions/models...
Author: justin
Date: Wed Jan 22 17:36:40 2014
New Revision: 1560437
URL: http://svn.apache.org/r1560437
Log:
SLING-3313 - adding initial version of Sling Models (nee YAMF)
Added:
sling/trunk/bundles/extensions/models/
sling/trunk/bundles/extensions/models/api/
sling/trunk/bundles/extensions/models/api/README.txt
sling/trunk/bundles/extensions/models/api/pom.xml
sling/trunk/bundles/extensions/models/api/src/
sling/trunk/bundles/extensions/models/api/src/main/
sling/trunk/bundles/extensions/models/api/src/main/java/
sling/trunk/bundles/extensions/models/api/src/main/java/org/
sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/
sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/
sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/
sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/
sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Default.java
sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Filter.java
sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Model.java
sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Optional.java
sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Source.java
sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Via.java
sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/package-info.java
sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/
sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/DisposalCallback.java
sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/DisposalCallbackRegistry.java
sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/Injector.java
sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/package-info.java
sling/trunk/bundles/extensions/models/impl/
sling/trunk/bundles/extensions/models/impl/README.txt
sling/trunk/bundles/extensions/models/impl/pom.xml
sling/trunk/bundles/extensions/models/impl/src/
sling/trunk/bundles/extensions/models/impl/src/main/
sling/trunk/bundles/extensions/models/impl/src/main/java/
sling/trunk/bundles/extensions/models/impl/src/main/java/org/
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelPackageBundleListener.java
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/BindingsInjector.java
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/ChildResourceInjector.java
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/OSGiServiceInjector.java
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/RequestAttributeInjector.java
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/ValueMapInjector.java
sling/trunk/bundles/extensions/models/impl/src/test/
sling/trunk/bundles/extensions/models/impl/src/test/java/
sling/trunk/bundles/extensions/models/impl/src/test/java/org/
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/DefaultTest.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/MultipleInjectorTest.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/OSGiInjectionTest.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/PostConstructTest.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/RequestInjectionTest.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/ResourceModelClassesTest.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/ResourceModelInterfacesTest.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/ViaTest.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/ArrayOSGiModel.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/BindingsModel.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/ChildModel.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/CollectionOSGiModel.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/DefaultStringModel.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/ListOSGiModel.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/RequestOSGiModel.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/ResourceModelWithRequiredField.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/SetOSGiModel.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/SimpleOSGiModel.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/SimplePropertyModel.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/SubClass.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/SuperClass.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/ViaModel.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/interfaces/
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/interfaces/ChildResourceModel.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/interfaces/ChildValueMapModel.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/interfaces/ParentModel.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/interfaces/ResourceModelWithRequiredField.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/interfaces/ServiceInterface.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/interfaces/SimplePropertyModel.java
sling/trunk/bundles/extensions/models/integration-tests/
sling/trunk/bundles/extensions/models/integration-tests/pom.xml
sling/trunk/bundles/extensions/models/integration-tests/src/
sling/trunk/bundles/extensions/models/integration-tests/src/test/
sling/trunk/bundles/extensions/models/integration-tests/src/test/java/
sling/trunk/bundles/extensions/models/integration-tests/src/test/java/org/
sling/trunk/bundles/extensions/models/integration-tests/src/test/java/org/apache/
sling/trunk/bundles/extensions/models/integration-tests/src/test/java/org/apache/sling/
sling/trunk/bundles/extensions/models/integration-tests/src/test/java/org/apache/sling/models/
sling/trunk/bundles/extensions/models/integration-tests/src/test/java/org/apache/sling/models/it/
sling/trunk/bundles/extensions/models/integration-tests/src/test/java/org/apache/sling/models/it/RemoteTest.java
sling/trunk/bundles/extensions/models/server-side-tests/
sling/trunk/bundles/extensions/models/server-side-tests/pom.xml
sling/trunk/bundles/extensions/models/server-side-tests/src/
sling/trunk/bundles/extensions/models/server-side-tests/src/main/
sling/trunk/bundles/extensions/models/server-side-tests/src/main/java/
sling/trunk/bundles/extensions/models/server-side-tests/src/main/java/org/
sling/trunk/bundles/extensions/models/server-side-tests/src/main/java/org/apache/
sling/trunk/bundles/extensions/models/server-side-tests/src/main/java/org/apache/sling/
sling/trunk/bundles/extensions/models/server-side-tests/src/main/java/org/apache/sling/models/
sling/trunk/bundles/extensions/models/server-side-tests/src/main/java/org/apache/sling/models/it/
sling/trunk/bundles/extensions/models/server-side-tests/src/main/java/org/apache/sling/models/it/SimpleTest.java
sling/trunk/bundles/extensions/models/server-side-tests/src/main/java/org/apache/sling/models/it/models/
sling/trunk/bundles/extensions/models/server-side-tests/src/main/java/org/apache/sling/models/it/models/TestModel.java
Modified:
sling/trunk/.gitignore
sling/trunk/pom.xml
Modified: sling/trunk/.gitignore
URL: http://svn.apache.org/viewvc/sling/trunk/.gitignore?rev=1560437&r1=1560436&r2=1560437&view=diff
==============================================================================
--- sling/trunk/.gitignore (original)
+++ sling/trunk/.gitignore Wed Jan 22 17:36:40 2014
@@ -12,3 +12,4 @@ coverage.ec
coverage/
.DS_Store
.idea/
+bundles/extensions/models/integration-tests/sling
Added: sling/trunk/bundles/extensions/models/api/README.txt
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/api/README.txt?rev=1560437&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/api/README.txt (added)
+++ sling/trunk/bundles/extensions/models/api/README.txt Wed Jan 22 17:36:40 2014
@@ -0,0 +1,24 @@
+Apache Sling Models API
+
+Getting Started
+===============
+
+This component uses a Maven 2 (http://maven.apache.org/) build
+environment. It requires a Java 5 JDK (or higher) and Maven (http://maven.apache.org/)
+2.0.7 or later. We recommend to use the latest Maven version.
+
+If you have Maven 2 installed, you can compile and
+package the jar using the following command:
+
+ mvn package
+
+See the Maven 2 documentation for other build features.
+
+The latest source code for this component is available in the
+Subversion (http://subversion.tigris.org/) source repository of
+the Apache Software Foundation. If you have Subversion installed,
+you can checkout the latest source using the following command:
+
+ svn checkout http://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/settings
+
+See the Subversion documentation for other source control features.
Added: sling/trunk/bundles/extensions/models/api/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/api/pom.xml?rev=1560437&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/api/pom.xml (added)
+++ sling/trunk/bundles/extensions/models/api/pom.xml Wed Jan 22 17:36:40 2014
@@ -0,0 +1,65 @@
+<?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>
+ <relativePath>../../../../parent/pom.xml</relativePath>
+ </parent>
+
+ <artifactId>org.apache.sling.models.api</artifactId>
+ <packaging>bundle</packaging>
+ <version>0.0.1-SNAPSHOT</version>
+ <name>Sling Models API</name>
+ <description>Sling Models API</description>
+ <scm>
+ <connection>scm:svn:http://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models/api</connection>
+ <developerConnection> scm:svn:https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models/api</developerConnection>
+ <url>http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/api</url>
+ </scm>
+ <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>
+ <Export-Package>org.apache.sling.models.*,javax.inject;version=0</Export-Package>
+ </instructions>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ <dependencies>
+ <dependency>
+ <groupId>javax.inject</groupId>
+ <artifactId>javax.inject</artifactId>
+ <version>1</version>
+ <scope>provided</scope>
+ </dependency>
+ </dependencies>
+</project>
Added: sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Default.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Default.java?rev=1560437&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Default.java (added)
+++ sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Default.java Wed Jan 22 17:36:40 2014
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.models.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Default value for an injection.
+ */
+@Target({ ElementType.FIELD, ElementType.METHOD })
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Default {
+
+ boolean[] booleanValues() default false;
+
+ double[] doubleValues() default 0;
+
+ float[] floatValues() default 0;
+
+ int[] intValues() default 0;
+
+ long[] longValues() default 0L;
+
+ short[] shortValues() default 0;
+
+ String[] values() default "";
+
+}
Added: sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Filter.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Filter.java?rev=1560437&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Filter.java (added)
+++ sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Filter.java Wed Jan 22 17:36:40 2014
@@ -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.models.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import javax.inject.Qualifier;
+
+/**
+ * Provide a filter on an @Inject.
+ */
+@Target({ ElementType.FIELD, ElementType.METHOD })
+@Retention(RetentionPolicy.RUNTIME)
+@Source("osgi-services")
+@Qualifier
+public @interface Filter {
+
+ public String value();
+
+}
Added: sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Model.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Model.java?rev=1560437&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Model.java (added)
+++ sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Model.java Wed Jan 22 17:36:40 2014
@@ -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.models.annotations;
+
+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/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Optional.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Optional.java?rev=1560437&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Optional.java (added)
+++ sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Optional.java Wed Jan 22 17:36:40 2014
@@ -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.models.annotations;
+
+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/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Source.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Source.java?rev=1560437&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Source.java (added)
+++ sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Source.java Wed Jan 22 17:36:40 2014
@@ -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.models.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import javax.inject.Qualifier;
+
+/**
+ * Annotation which indicates the specific injector which will populate this injection.
+ * Can also be used as a meta-annotation to declare that some other annotation implies
+ * a source.
+ */
+@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE })
+@Retention(RetentionPolicy.RUNTIME)
+@Qualifier
+public @interface Source {
+
+ String value();
+}
Added: sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Via.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Via.java?rev=1560437&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Via.java (added)
+++ sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/Via.java Wed Jan 22 17:36:40 2014
@@ -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.models.annotations;
+
+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 Via {
+
+ public String value();
+
+}
\ No newline at end of file
Added: sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/package-info.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/package-info.java?rev=1560437&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/package-info.java (added)
+++ sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/package-info.java Wed Jan 22 17:36:40 2014
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+@Version("1.0.0")
+package org.apache.sling.models.annotations;
+
+import aQute.bnd.annotation.Version;
\ No newline at end of file
Added: sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/DisposalCallback.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/DisposalCallback.java?rev=1560437&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/DisposalCallback.java (added)
+++ sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/DisposalCallback.java Wed Jan 22 17:36:40 2014
@@ -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.models.spi;
+
+/**
+ * Callback interface which can be implemented by Injectors to perform some action
+ * when a created object is destroyed.
+ */
+public interface DisposalCallback {
+
+ /**
+ * Callback method for when an object is destroyed.
+ */
+ void onDisposed();
+
+}
Added: sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/DisposalCallbackRegistry.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/DisposalCallbackRegistry.java?rev=1560437&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/DisposalCallbackRegistry.java (added)
+++ sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/DisposalCallbackRegistry.java Wed Jan 22 17:36:40 2014
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.models.spi;
+
+/**
+ * Interface which allows for the registration of DisposalCallback listeners.
+ * This interface should not be implemnted by clients.
+ */
+public interface DisposalCallbackRegistry {
+
+ /**
+ * Register a callback.
+ *
+ * @param callback the callback to register
+ */
+ void addDisposalCallback(DisposalCallback callback);
+
+}
Added: sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/Injector.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/Injector.java?rev=1560437&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/Injector.java (added)
+++ sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/Injector.java Wed Jan 22 17:36:40 2014
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.models.spi;
+
+import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Type;
+
+/**
+ * Service interface for pluggable value injectors.
+ */
+public interface Injector {
+
+ /**
+ * Return a logical name for the injector. Used in resolving <pre>@Source</pre> annotations.
+ *
+ * @return the injector's name
+ */
+ String getName();
+
+ /**
+ * Produce a value for an injection point.
+ *
+ * @param adaptable the object which should be used as the basis for value resolution.
+ * @param name the injection point name
+ * @param declaredType the declared type of the injection point
+ * @param element the injection point itself
+ * @param callbackRegistry a registry object to register a callback object which will be
+ * invoked when the adapted object is disposed.
+ *
+ * @return the value to be injected or null if no value could be resolved
+ */
+ Object getValue(Object adaptable, String name, Type declaredType, AnnotatedElement element, DisposalCallbackRegistry callbackRegistry);
+}
\ No newline at end of file
Added: sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/package-info.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/package-info.java?rev=1560437&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/package-info.java (added)
+++ sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/package-info.java Wed Jan 22 17:36:40 2014
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+@Version("1.0.0")
+package org.apache.sling.models.spi;
+
+import aQute.bnd.annotation.Version;
\ No newline at end of file
Added: sling/trunk/bundles/extensions/models/impl/README.txt
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/README.txt?rev=1560437&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/README.txt (added)
+++ sling/trunk/bundles/extensions/models/impl/README.txt Wed Jan 22 17:36:40 2014
@@ -0,0 +1,24 @@
+Apache Sling Models Implementation
+
+Getting Started
+===============
+
+This component uses a Maven 2 (http://maven.apache.org/) build
+environment. It requires a Java 5 JDK (or higher) and Maven (http://maven.apache.org/)
+2.0.7 or later. We recommend to use the latest Maven version.
+
+If you have Maven 2 installed, you can compile and
+package the jar using the following command:
+
+ mvn package
+
+See the Maven 2 documentation for other build features.
+
+The latest source code for this component is available in the
+Subversion (http://subversion.tigris.org/) source repository of
+the Apache Software Foundation. If you have Subversion installed,
+you can checkout the latest source using the following command:
+
+ svn checkout http://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/settings
+
+See the Subversion documentation for other source control features.
Added: sling/trunk/bundles/extensions/models/impl/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/pom.xml?rev=1560437&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/pom.xml (added)
+++ sling/trunk/bundles/extensions/models/impl/pom.xml Wed Jan 22 17:36:40 2014
@@ -0,0 +1,142 @@
+<?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>
+ <relativePath>../../../../parent/pom.xml</relativePath>
+ </parent>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.models.impl</artifactId>
+ <packaging>bundle</packaging>
+ <version>0.0.1-SNAPSHOT</version>
+ <name>Sling Models Implementation</name>
+ <description>Sling Models Implementation</description>
+ <properties>
+ <sling.java.version>6</sling.java.version>
+ </properties>
+ <scm>
+ <connection>scm:svn:http://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models/impl</connection>
+ <developerConnection> scm:svn:https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models/impl</developerConnection>
+ <url>http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl</url>
+ </scm>
+ <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,
+ org.osgi.compendium;inline="org/osgi/util/tracker/*"</Embed-Dependency>
+ </instructions>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.models.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>
+ <version>4.2.0</version>
+ <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>
+</project>
Added: sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java?rev=1560437&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java (added)
+++ sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java Wed Jan 22 17:36:40 2014
@@ -0,0 +1,615 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.models.impl;
+
+import java.lang.annotation.Annotation;
+import java.lang.ref.PhantomReference;
+import java.lang.ref.ReferenceQueue;
+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.util.ArrayList;
+import java.util.Collections;
+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 java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+import javax.annotation.PostConstruct;
+import javax.inject.Inject;
+import javax.inject.Named;
+
+import org.apache.commons.beanutils.PropertyUtils;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+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.models.annotations.Default;
+import org.apache.sling.models.annotations.Model;
+import org.apache.sling.models.annotations.Optional;
+import org.apache.sling.models.annotations.Via;
+import org.apache.sling.models.annotations.Source;
+import org.apache.sling.models.spi.DisposalCallback;
+import org.apache.sling.models.spi.DisposalCallbackRegistry;
+import org.apache.sling.models.spi.Injector;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Component
+public class ModelAdapterFactory implements AdapterFactory, Runnable {
+
+ public static class DisposalCallbackRegistryImpl implements DisposalCallbackRegistry {
+
+ private List<DisposalCallback> callbacks = new ArrayList<DisposalCallback>();
+
+ @Override
+ public void addDisposalCallback(DisposalCallback callback) {
+ callbacks.add(callback);
+ }
+
+ private void lock() {
+ callbacks = Collections.unmodifiableList(callbacks);
+ }
+
+ private void onDisposed() {
+ for (DisposalCallback callback : callbacks) {
+ callback.onDisposed();
+ }
+ }
+
+ }
+
+ private ReferenceQueue<Object> queue;
+
+ private ConcurrentMap<java.lang.ref.Reference<Object>, DisposalCallbackRegistryImpl> disposalCallbacks;
+
+ 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);
+ }
+
+ }
+
+ @Override
+ public void run() {
+ java.lang.ref.Reference<? extends Object> ref = queue.poll();
+ if (ref != null) {
+ log.info("calling disposal for " + ref.toString());
+ DisposalCallbackRegistryImpl registry = disposalCallbacks.remove(ref);
+ registry.onDisposed();
+ }
+ }
+
+ private static final Logger log = LoggerFactory.getLogger(ModelAdapterFactory.class);
+
+ @Reference(name = "injector", referenceInterface = Injector.class,
+ cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE, policy = ReferencePolicy.DYNAMIC)
+ private final Map<Object, Injector> injectors = new TreeMap<Object, Injector>();
+
+ private volatile Injector[] sortedInjectors = new Injector[0];
+
+ private ModelPackageBundleListener listener;
+
+ private ServiceRegistration jobRegistration;
+
+ @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 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>();
+ MapBackedInvocationHandler handler = new MapBackedInvocationHandler(methods);
+
+ DisposalCallbackRegistryImpl registry = createAndRegisterCallbackRegistry(handler);
+
+ for (Injector injector : sortedInjectors) {
+ Iterator<Method> it = injectableMethods.iterator();
+ while (it.hasNext()) {
+ Method method = it.next();
+ String source = getSource(method);
+ if (source == null || source.equals(injector.getName())) {
+ 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, registry);
+ if (setMethod(method, methods, value)) {
+ it.remove();
+ }
+ }
+ }
+ }
+ }
+
+ Iterator<Method> it = injectableMethods.iterator();
+ while (it.hasNext()) {
+ Method method = it.next();
+ Default defaultAnnotation = method.getAnnotation(Default.class);
+ if (defaultAnnotation != null) {
+ Type returnType = mapPrimitiveClasses(method.getGenericReturnType());
+ Object value = getDefaultValue(defaultAnnotation, returnType);
+ if (setMethod(method, methods, value)) {
+ it.remove();
+ }
+ }
+ }
+
+ if (injectableMethods.isEmpty()) {
+ return handler;
+ } 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 handler;
+ }
+ }
+ }
+
+ private DisposalCallbackRegistryImpl createAndRegisterCallbackRegistry(Object object) {
+ PhantomReference<Object> reference = new PhantomReference<Object>(object, queue);
+ DisposalCallbackRegistryImpl registry = new DisposalCallbackRegistryImpl();
+ disposalCallbacks.put(reference, registry);
+ return registry;
+ }
+
+ private String getSource(AnnotatedElement element) {
+ Source source = element.getAnnotation(Source.class);
+ if (source != null) {
+ return source.value();
+ } else {
+ for (Annotation ann : element.getAnnotations()) {
+ source = ann.annotationType().getAnnotation(Source.class);
+ if (source != null) {
+ return source.value();
+ }
+ }
+ }
+ return null;
+ }
+
+ private <AdapterType> AdapterType createObject(Object adaptable, Class<AdapterType> type)
+ throws InstantiationException, IllegalAccessException {
+ Set<Field> injectableFields = collectInjectableFields(type);
+
+ AdapterType object = type.newInstance();
+
+ DisposalCallbackRegistryImpl registry = createAndRegisterCallbackRegistry(object);
+
+ for (Injector injector : sortedInjectors) {
+ Iterator<Field> it = injectableFields.iterator();
+ while (it.hasNext()) {
+ Field field = it.next();
+ String source = getSource(field);
+ if (source == null || source.equals(injector.getName())) {
+ 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, registry);
+ if (setField(field, object, value)) {
+ it.remove();
+ }
+ }
+ }
+ }
+ }
+
+ Iterator<Field> it = injectableFields.iterator();
+ while (it.hasNext()) {
+ Field field = it.next();
+ Default defaultAnnotation = field.getAnnotation(Default.class);
+ if (defaultAnnotation != null) {
+ Type fieldType = mapPrimitiveClasses(field.getGenericType());
+ Object value = getDefaultValue(defaultAnnotation, fieldType);
+ 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 getDefaultValue(Default defaultAnnotation, Type type) {
+ if (type instanceof Class) {
+ Class<?> injectedClass = (Class<?>) type;
+ if (injectedClass.isArray()) {
+ Class<?> componentType = injectedClass.getComponentType();
+ if (componentType == String.class) {
+ return defaultAnnotation.values();
+ }
+ if (componentType == Integer.TYPE) {
+ return defaultAnnotation.intValues();
+ }
+ if (componentType == Long.TYPE) {
+ return defaultAnnotation.longValues();
+ }
+ if (componentType == Boolean.TYPE) {
+ return defaultAnnotation.booleanValues();
+ }
+ if (componentType == Short.TYPE) {
+ return defaultAnnotation.shortValues();
+ }
+ if (componentType == Float.TYPE) {
+ return defaultAnnotation.floatValues();
+ }
+ if (componentType == Double.TYPE) {
+ return defaultAnnotation.doubleValues();
+ }
+
+ log.warn("Default values for {} are not supported", componentType);
+ return null;
+ } else {
+ if (injectedClass == String.class) {
+ return defaultAnnotation.values()[0];
+ }
+ if (injectedClass == Integer.TYPE) {
+ return defaultAnnotation.intValues()[0];
+ }
+ if (injectedClass == Long.TYPE) {
+ return defaultAnnotation.longValues()[0];
+ }
+ if (injectedClass == Boolean.TYPE) {
+ return defaultAnnotation.booleanValues()[0];
+ }
+ if (injectedClass == Short.TYPE) {
+ return defaultAnnotation.shortValues()[0];
+ }
+ if (injectedClass == Float.TYPE) {
+ return defaultAnnotation.floatValues()[0];
+ }
+ if (injectedClass == Double.TYPE) {
+ return defaultAnnotation.doubleValues()[0];
+ }
+
+ log.warn("Default values for {} are not supported", injectedClass);
+ return null;
+ }
+ } else {
+ log.warn("Cannot provide default for {}", type);
+ return null;
+ }
+ }
+
+ private Object getAdaptable(Object adaptable, AnnotatedElement point) {
+ Via viaAnnotation = point.getAnnotation(Via.class);
+ if (viaAnnotation == null) {
+ return adaptable;
+ }
+ String viaPropertyName = viaAnnotation.value();
+ try {
+ return PropertyUtils.getProperty(adaptable, viaPropertyName);
+ } catch (Exception e) {
+ log.error("Unable to execution projection " + viaPropertyName, 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 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) {
+ 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) {
+ 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 {
+ return false;
+ }
+ }
+
+ private boolean isAcceptableType(Class<?> type, Object value) {
+ if (type.isInstance(value)) {
+ return true;
+ }
+
+ if (type == Integer.TYPE) {
+ return Integer.class.isInstance(value);
+ }
+ if (type == Long.TYPE) {
+ return Long.class.isInstance(value);
+ }
+ if (type == Boolean.TYPE) {
+ return Boolean.class.isInstance(value);
+ }
+ if (type == Double.TYPE) {
+ return Double.class.isInstance(value);
+ }
+ if (type == Float.TYPE) {
+ return Float.class.isInstance(value);
+ }
+ if (type == Short.TYPE) {
+ return Short.class.isInstance(value);
+ }
+ if (type == Character.TYPE) {
+ return Character.class.isInstance(value);
+ }
+
+ return false;
+ }
+
+ @Activate
+ protected void activate(final ComponentContext ctx) {
+ this.queue = new ReferenceQueue<Object>();
+ this.disposalCallbacks = new ConcurrentHashMap<java.lang.ref.Reference<Object>, DisposalCallbackRegistryImpl>();
+ Hashtable<Object, Object> properties = new Hashtable<Object, Object>();
+ properties.put("scheduler.concurrent", false);
+ properties.put("scheduler.period", Long.valueOf(30));
+
+ this.jobRegistration = ctx.getBundleContext().registerService(Runnable.class.getName(), this,
+ properties);
+
+ this.listener = new ModelPackageBundleListener(ctx.getBundleContext(), this);
+ }
+
+ @Deactivate
+ protected void deactivate() {
+ this.listener.unregisterAll();
+ if (jobRegistration != null) {
+ jobRegistration.unregister();
+ jobRegistration = null;
+ }
+ }
+
+ 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/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelPackageBundleListener.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelPackageBundleListener.java?rev=1560437&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelPackageBundleListener.java (added)
+++ sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelPackageBundleListener.java Wed Jan 22 17:36:40 2014
@@ -0,0 +1,130 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.models.impl;
+
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.Hashtable;
+import java.util.List;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.sling.api.adapter.AdapterFactory;
+import org.apache.sling.commons.osgi.PropertiesUtil;
+import org.apache.sling.models.annotations.Model;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.util.tracker.BundleTracker;
+import org.osgi.util.tracker.BundleTrackerCustomizer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ModelPackageBundleListener implements BundleTrackerCustomizer {
+
+ private static final String HEADER = "Sling-Model-Packages";
+
+ private static final Logger log = LoggerFactory.getLogger(ModelPackageBundleListener.class);
+
+ private final BundleContext bundleContext;
+
+ private final BundleTracker bundleTracker;
+
+ private final AdapterFactory factory;
+
+ public ModelPackageBundleListener(BundleContext bundleContext, AdapterFactory factory) {
+ this.bundleContext = bundleContext;
+ this.factory = factory;
+ this.bundleTracker = new BundleTracker(bundleContext, Bundle.ACTIVE, this);
+ this.bundleTracker.open();
+ }
+
+ @Override
+ public Object addingBundle(Bundle bundle, BundleEvent event) {
+List<ServiceRegistration> regs = new ArrayList<ServiceRegistration>();
+
+ Dictionary<?, ?> headers = bundle.getHeaders();
+ String packageList = PropertiesUtil.toString(headers.get(HEADER), null);
+ if (packageList != null) {
+
+ 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,
+ factory, registrationProps);
+ regs.add(reg);
+ }
+ } catch (ClassNotFoundException e) {
+ log.warn("Unable to load class", e);
+ }
+
+ }
+ }
+ }
+ return regs.toArray(new ServiceRegistration[0]);
+ }
+
+ @Override
+ public void modifiedBundle(Bundle bundle, BundleEvent event, Object object) {
+ }
+
+ @Override
+ public void removedBundle(Bundle bundle, BundleEvent event, Object object) {
+ if (object instanceof ServiceRegistration[]) {
+ for (ServiceRegistration reg : (ServiceRegistration[]) object) {
+ reg.unregister();
+ }
+ }
+ }
+
+ public synchronized void unregisterAll() {
+ this.bundleTracker.close();
+ }
+
+ /** 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 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;
+ }
+
+}
Added: sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/BindingsInjector.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/BindingsInjector.java?rev=1560437&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/BindingsInjector.java (added)
+++ sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/BindingsInjector.java Wed Jan 22 17:36:40 2014
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.models.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.models.spi.DisposalCallbackRegistry;
+import org.apache.sling.models.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);
+
+ @Override
+ public String getName() {
+ return "script-bindings";
+ }
+
+ 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, DisposalCallbackRegistry callbackRegistry) {
+ SlingBindings bindings = getBindings(adaptable);
+ if (bindings == null) {
+ return null;
+ }
+ if (type instanceof Class<?>) {
+ return getValue(bindings, name, (Class<?>) type);
+ } else {
+ log.debug("BindingsInjector doesn't support non-class type {}", type);
+ 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/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/ChildResourceInjector.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/ChildResourceInjector.java?rev=1560437&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/ChildResourceInjector.java (added)
+++ sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/ChildResourceInjector.java Wed Jan 22 17:36:40 2014
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.models.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.resource.Resource;
+import org.apache.sling.models.spi.DisposalCallbackRegistry;
+import org.apache.sling.models.spi.Injector;
+
+@Component
+@Service
+public class ChildResourceInjector implements Injector {
+
+ @Override
+ public String getName() {
+ return "child-resources";
+ }
+
+ @Override
+ public Object getValue(Object adaptable, String name, Type declaredType, AnnotatedElement element, DisposalCallbackRegistry callbackRegistry) {
+ if (adaptable instanceof Resource) {
+ return ((Resource) adaptable).getChild(name);
+ } else {
+ return null;
+ }
+ }
+
+}
Added: sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/OSGiServiceInjector.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/OSGiServiceInjector.java?rev=1560437&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/OSGiServiceInjector.java (added)
+++ sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/OSGiServiceInjector.java Wed Jan 22 17:36:40 2014
@@ -0,0 +1,194 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.models.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 javax.servlet.ServletRequest;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.scripting.SlingBindings;
+import org.apache.sling.api.scripting.SlingScriptHelper;
+import org.apache.sling.models.annotations.Filter;
+import org.apache.sling.models.spi.DisposalCallback;
+import org.apache.sling.models.spi.DisposalCallbackRegistry;
+import org.apache.sling.models.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;
+
+ @Override
+ public String getName() {
+ return "osgi-services";
+ }
+
+ @Activate
+ public void activate(ComponentContext ctx) {
+ this.bundleContext = ctx.getBundleContext();
+ }
+
+ public Object getValue(Object adaptable, String name, Type type, AnnotatedElement element, DisposalCallbackRegistry callbackRegistry) {
+ Filter filter = element.getAnnotation(Filter.class);
+ String filterString = null;
+ if (filter != null) {
+ filterString = filter.value();
+ }
+
+ return getValue(adaptable, type, filterString, callbackRegistry);
+ }
+
+ private <T> Object getService(Object adaptable, Class<T> type, String filter, DisposalCallbackRegistry callbackRegistry) {
+ SlingScriptHelper helper = getScriptHelper(adaptable);
+
+ if (helper != null) {
+ T[] services = helper.getServices(type, filter);
+ if (services == null || services.length == 0) {
+ return null;
+ } else {
+ return services[0];
+ }
+ } else {
+ try {
+ ServiceReference[] refs = bundleContext.getServiceReferences(type.getName(), filter);
+ if (refs == null || refs.length == 0) {
+ return null;
+ } else {
+ callbackRegistry.addDisposalCallback(new Callback(refs, bundleContext));
+ return bundleContext.getService(refs[0]);
+ }
+ } catch (InvalidSyntaxException e) {
+ log.error("invalid filter expression", e);
+ return null;
+ }
+ }
+ }
+
+ private <T> Object[] getServices(Object adaptable, Class<T> type, String filter, DisposalCallbackRegistry callbackRegistry) {
+ SlingScriptHelper helper = getScriptHelper(adaptable);
+
+ if (helper != null) {
+ T[] services = helper.getServices(type, filter);
+ return services;
+ } else {
+ try {
+ ServiceReference[] refs = bundleContext.getServiceReferences(type.getName(), filter);
+ if (refs == null || refs.length == 0) {
+ return null;
+ } else {
+ callbackRegistry.addDisposalCallback(new Callback(refs, bundleContext));
+ 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 SlingScriptHelper getScriptHelper(Object adaptable) {
+ if (adaptable instanceof ServletRequest) {
+ ServletRequest request = (ServletRequest) adaptable;
+ SlingBindings bindings = (SlingBindings) request.getAttribute(SlingBindings.class.getName());
+ if (bindings != null) {
+ return bindings.getSling();
+ } else {
+ return null;
+ }
+ } else {
+ return null;
+ }
+ }
+
+ private Object getValue(Object adaptable, Type type, String filterString, DisposalCallbackRegistry callbackRegistry) {
+ if (type instanceof Class) {
+ Class<?> injectedClass = (Class<?>) type;
+ if (injectedClass.isArray()) {
+ Object[] services = getServices(adaptable, injectedClass.getComponentType(), filterString, callbackRegistry);
+ 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(adaptable, injectedClass, filterString, callbackRegistry);
+ }
+ } 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(adaptable, serviceType, filterString, callbackRegistry);
+ return Arrays.asList(services);
+ } else {
+ log.warn("Cannot handle type {}", type);
+ return null;
+ }
+ }
+
+ private static class Callback implements DisposalCallback {
+ private final ServiceReference[] refs;
+ private final BundleContext context;
+
+ public Callback(ServiceReference[] refs, BundleContext context) {
+ this.refs = refs;
+ this.context = context;
+ }
+
+ @Override
+ public void onDisposed() {
+ if (refs != null) {
+ for (ServiceReference ref : refs) {
+ context.ungetService(ref);
+ }
+ }
+ }
+ }
+
+}