You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by dk...@apache.org on 2013/11/06 15:29:49 UTC
svn commit: r1539345 [1/2] - in /sling/whiteboard/dklco: dynamic-proxy/
dynamic-proxy/src/main/java/org/apache/sling/commons/
dynamic-proxy/src/main/java/org/apache/sling/extensions/
dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/...
Author: dklco
Date: Wed Nov 6 14:29:48 2013
New Revision: 1539345
URL: http://svn.apache.org/r1539345
Log:
Updating the structure of the Sling Dynamic Proxy to reflect it likely being an extension instead of part of commons
Added:
sling/whiteboard/dklco/dynamic-proxy/
- copied from r1534883, sling/whiteboard/dklco/sling-proxy/
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/AbstractProxyAdapterFactory.java
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/SlingProxy.java
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/SlingProxyService.java
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/annotations/
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/annotations/SlingChildren.java
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/annotations/SlingProperty.java
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/annotations/SlingReference.java
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/annotations/SlingServiceInvocation.java
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/annotations/package-info.java
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/DefaultSlingProxyServiceImpl.java
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/DeferredIterator.java
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/SlingInvocationHandler.java
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/lang/
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/lang/JDPEqualsImpl.java
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/lang/JDPHashCodeImpl.java
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/lang/JDPToStringImpl.java
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/lang/MethodType.java
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/lang/package-info.java
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/package-info.java
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/reflection/
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/reflection/Annotations.java
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/reflection/Classes.java
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/reflection/package-info.java
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/to/
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/to/InvokedChildrenTO.java
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/to/InvokedPropertyTO.java
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/to/InvokedTO.java
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/to/PathBasedInvokedTO.java
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/to/SlingServiceInvocationTO.java
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/to/package-info.java
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/package-info.java
sling/whiteboard/dklco/dynamic-proxy/src/test/java/org/apache/sling/extensions/
sling/whiteboard/dklco/dynamic-proxy/src/test/java/org/apache/sling/extensions/dynamicproxy/
sling/whiteboard/dklco/dynamic-proxy/src/test/java/org/apache/sling/extensions/dynamicproxy/BaseSlingProxyTest.java
sling/whiteboard/dklco/dynamic-proxy/src/test/java/org/apache/sling/extensions/dynamicproxy/TestSlingDefaultProxy.java
sling/whiteboard/dklco/dynamic-proxy/src/test/java/org/apache/sling/extensions/dynamicproxy/TestSlingPropertyProxy.java
sling/whiteboard/dklco/dynamic-proxy/src/test/java/org/apache/sling/extensions/dynamicproxy/lang/
sling/whiteboard/dklco/dynamic-proxy/src/test/java/org/apache/sling/extensions/dynamicproxy/lang/TestJDPEquals.java
sling/whiteboard/dklco/dynamic-proxy/src/test/java/org/apache/sling/extensions/dynamicproxy/lang/TestJDPHashCode.java
sling/whiteboard/dklco/dynamic-proxy/src/test/java/org/apache/sling/extensions/dynamicproxy/lang/TestJDPToString.java
sling/whiteboard/dklco/dynamic-proxy/src/test/java/org/apache/sling/extensions/dynamicproxy/samples/
sling/whiteboard/dklco/dynamic-proxy/src/test/java/org/apache/sling/extensions/dynamicproxy/samples/DuplicateSlingPropertyProxy.java
sling/whiteboard/dklco/dynamic-proxy/src/test/java/org/apache/sling/extensions/dynamicproxy/samples/NoAnnotationSlingProxy.java
sling/whiteboard/dklco/dynamic-proxy/src/test/java/org/apache/sling/extensions/dynamicproxy/samples/SlingDefaultsProxy.java
sling/whiteboard/dklco/dynamic-proxy/src/test/java/org/apache/sling/extensions/dynamicproxy/samples/SlingPropertyProxy.java
Removed:
sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/commons/
sling/whiteboard/dklco/dynamic-proxy/src/test/java/org/apache/sling/commons/
sling/whiteboard/dklco/sling-proxy/
Modified:
sling/whiteboard/dklco/dynamic-proxy/pom.xml
sling/whiteboard/dklco/dynamic-proxy/src/site/markdown/annotations.md
sling/whiteboard/dklco/dynamic-proxy/src/site/markdown/index.md
Modified: sling/whiteboard/dklco/dynamic-proxy/pom.xml
URL: http://svn.apache.org/viewvc/sling/whiteboard/dklco/dynamic-proxy/pom.xml?rev=1539345&r1=1534883&r2=1539345&view=diff
==============================================================================
--- sling/whiteboard/dklco/dynamic-proxy/pom.xml (original)
+++ sling/whiteboard/dklco/dynamic-proxy/pom.xml Wed Nov 6 14:29:48 2013
@@ -1,116 +1,101 @@
-<!-- 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>15</version>
- </parent>
-
- <artifactId>org.apache.sling.commons.proxy</artifactId>
- <packaging>bundle</packaging>
- <version>0.0.1-SNAPSHOT</version>
-
- <name>Apache Sling Proxy</name>
- <description>Java Dynamic Proxy implementation for Apache Sling</description>
- <scm>
- <connection>scm:svn:http://svn.apache.org/repos/asf/sling/trunk/bundles/commons/proxy</connection>
- <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/proxy</developerConnection>
- <url>http://svn.apache.org/viewvc/sling/trunk/bundles/commons/proxy</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>
- </plugin>
-
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-site-plugin</artifactId>
- <version>3.0</version>
- <dependencies>
- <dependency>
- <groupId>org.apache.maven.doxia</groupId>
- <artifactId>doxia-module-markdown</artifactId>
- <version>1.3</version>
- </dependency>
- </dependencies>
- </plugin>
-
- </plugins>
-
- </build>
-
- <dependencies>
- <dependency>
- <groupId>biz.aQute</groupId>
- <artifactId>bndlib</artifactId>
- </dependency>
- <dependency>
- <groupId>commons-lang</groupId>
- <artifactId>commons-lang</artifactId>
- <version>2.4</version>
- </dependency>
- <dependency>
- <groupId>org.apache.felix</groupId>
- <artifactId>org.apache.felix.scr.annotations</artifactId>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- </dependency>
- <dependency>
- <groupId>org.apache.sling</groupId>
- <artifactId>org.apache.sling.api</artifactId>
- <version>2.0.6</version>
- </dependency>
- <dependency>
- <groupId>org.apache.sling</groupId>
- <artifactId>org.apache.sling.commons.classloader</artifactId>
- <version>1.2.4</version>
- </dependency>
- <dependency>
- <groupId>org.osgi</groupId>
- <artifactId>org.osgi.compendium</artifactId>
- </dependency>
- <dependency>
- <groupId>org.osgi</groupId>
- <artifactId>org.osgi.core</artifactId>
- </dependency>
-
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <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.testing</artifactId>
- <version>2.0.12</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
+<!-- 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>15</version>
+ </parent>
+
+ <artifactId>org.apache.sling.extensions.dynamic-proxy</artifactId>
+ <packaging>bundle</packaging>
+ <version>0.0.1-SNAPSHOT</version>
+
+ <name>Apache Sling Dynamic Proxy</name>
+ <description>Java Dynamic Proxy implementation for Apache Sling</description>
+ <scm>
+ <connection>scm:svn:http://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/dynamic-proxy</connection>
+ <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/dynamic-proxy</developerConnection>
+ <url>http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/dynamic-proxy</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>
+ </plugin>
+ </plugins>
+ </build>
+
+ <dependencies>
+ <dependency>
+ <groupId>biz.aQute</groupId>
+ <artifactId>bndlib</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>commons-lang</groupId>
+ <artifactId>commons-lang</artifactId>
+ <version>2.4</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.scr.annotations</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.api</artifactId>
+ <version>2.0.6</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.commons.classloader</artifactId>
+ <version>0.9.0</version>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.compendium</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.core</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <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.testing</artifactId>
+ <version>2.0.12</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
</project>
\ No newline at end of file
Added: sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/AbstractProxyAdapterFactory.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/AbstractProxyAdapterFactory.java?rev=1539345&view=auto
==============================================================================
--- sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/AbstractProxyAdapterFactory.java (added)
+++ sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/AbstractProxyAdapterFactory.java Wed Nov 6 14:29:48 2013
@@ -0,0 +1,70 @@
+/*
+ * 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.extensions.dynamicproxy;
+
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.sling.api.adapter.AdapterFactory;
+import org.apache.sling.api.resource.Resource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Abstract Adaptor Factory for adapting Sling Resources to Sling Dynamic
+ * Proxies. This adaptor utilizes Java Dynamic Proxies to allow adapting
+ * resources into objects which proxy calls against interface methods into the
+ * resource properties.
+ *
+ * Implementors of the SlingProxy interface should register an AdapterFactory
+ * Service with the services
+ *
+ * @see org.apache.sling.api.adapter.AdapterFactory
+ */
+public class AbstractProxyAdapterFactory implements AdapterFactory {
+
+ /**
+ * The SLF4J Logger.
+ */
+ private static final Logger log = LoggerFactory
+ .getLogger(AbstractProxyAdapterFactory.class);
+
+ /**
+ * Reference to the Sling Proxy Service.
+ */
+ @Reference
+ private SlingProxyService slingProxyService;
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.sling.api.adapter.AdapterFactory#getAdapter(java.lang.Object, java.lang.Class)
+ */
+ public <AdapterType> AdapterType getAdapter(Object adaptable,
+ Class<AdapterType> type) {
+ log.trace("getAdapter");
+
+ if (adaptable instanceof Resource) {
+ Resource resource = ((Resource) adaptable);
+ return slingProxyService.getProxy(resource, type);
+ } else {
+ log.warn("Unable to adapt object of type: {}", adaptable.getClass()
+ .getName());
+ }
+ return null;
+ }
+
+}
Added: sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/SlingProxy.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/SlingProxy.java?rev=1539345&view=auto
==============================================================================
--- sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/SlingProxy.java (added)
+++ sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/SlingProxy.java Wed Nov 6 14:29:48 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.extensions.dynamicproxy;
+
+import org.apache.sling.api.resource.Resource;
+
+/**
+ * An interface allowing developers to create interfaces who's method calls are
+ * proxies from the Sling repository.
+ */
+public interface SlingProxy {
+
+ /**
+ * Retrieves the backing resource for this Sling Proxy.
+ *
+ * @return the resource backing the Sling Proxy
+ */
+ Resource getBackingResource();
+}
Added: sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/SlingProxyService.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/SlingProxyService.java?rev=1539345&view=auto
==============================================================================
--- sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/SlingProxyService.java (added)
+++ sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/SlingProxyService.java Wed Nov 6 14:29:48 2013
@@ -0,0 +1,60 @@
+/*
+ * 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.extensions.dynamicproxy;
+
+import org.apache.sling.api.resource.Resource;
+
+/**
+ * Service for retrieving SlingProxy instances from Sling Resources. This
+ * services utilizes Java's <a
+ * href="http://docs.oracle.com/javase/1.3/docs/guide/reflection/proxy.html"
+ * >Dynamic Proxy API</a> to create dynamically backed instances of Interfaces. <br/>
+ * The Interface methods should be decorated with Annotations to control the
+ * result of executing the method. The Annotations will be read in runtime by
+ * the service and will guide the service in executing the method.
+ */
+public interface SlingProxyService {
+
+ /**
+ * Creates new SlingProxy instances of the provided type <code>type</code>
+ * using the <code>resource</code> as the backing Sling Resource for the
+ * JavaBean accessor methods on <code>type</code>
+ *
+ * The following criteria must be met when invoking this method:
+ * <ol>
+ * <li><code>resource</code> must not be null</li>
+ * <li><code>type</code> must not be null</li>
+ * <li><code>type</code> must be an Interface</li>
+ * <li>Interface <code>type</code> must have at least one
+ * <code>@SlingProperty</code>, <code>@SlingReference</code>,
+ * <code>@SlingChildren</code> or <code>@SlingServiceInvocation</code>
+ * Annotation</li>
+ * </ol>
+ *
+ * @param <T>
+ * Interface for the proxy, extends SlingProxy
+ * @param resource
+ * Resource - the backing Resource
+ * @param type
+ * Class - the interface that extends SlingProxy that is the
+ * Interface we are to create a new Proxy instance of
+ * @return the new proxy instance of type <code>type</code>
+ */
+ <T> T getProxy(Resource resource, Class<T> type);
+}
Added: sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/annotations/SlingChildren.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/annotations/SlingChildren.java?rev=1539345&view=auto
==============================================================================
--- sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/annotations/SlingChildren.java (added)
+++ sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/annotations/SlingChildren.java Wed Nov 6 14:29:48 2013
@@ -0,0 +1,79 @@
+/*
+ * 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.extensions.dynamicproxy.annotations;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import org.apache.sling.api.resource.Resource;
+
+/**
+ * Annotation used to mark a Method as retrieving the children of a resource.
+ * The path is not required. If you do not specify a path the children of the
+ * proxied resource will be retrieved. Paths starting with '/' are absolute
+ * references, and do not have to be contained beneath the current Resource.
+ * Paths not starting with '/' are assumed to be relative to the current
+ * Resource.
+ *
+ * The child Resources at the path will be returned as an Iterator of either a
+ * resource or the type specified in the returnType property, according to the
+ * following rules (in order):
+ *
+ * <ol>
+ * <li>If the return type is an instance of a Resource, the resource at the path
+ * will be returned</li>
+ * <li>If adapting the resource into the return class returns a non-null, this
+ * object will be returned</li>
+ * <li>If using the ProxyService to retrieve a proxy instance does not throw and
+ * error and does not return null, the resulting object will returned</li>
+ * <li>Otherwise, null will be returned</li>
+ * </ol>
+ *
+ * Here are 3 examples, showing valid usages:
+ *
+ * <code><br/>
+ * @SlingChildren(path = "jcr:content")<br/>
+ * Iterator<Resource> getChildren();<br/><br/>
+ *
+ * @SlingReference(returnType = ValueMap.class)<br/>
+ * Iterator<ValueMap> getSubnodeProperties();<br/><br/>
+ *
+ * @SlingReference(path = "/content/page", returnType=IPageProxy.class)<br/>
+ * Iterator<IPageProxy> getSubPageProxes();<br/><br/>
+ * </code>
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ ElementType.METHOD })
+@Documented
+public @interface SlingChildren {
+
+ /**
+ * The path of the child resources to retrieve.
+ */
+ String path() default "";
+
+ /**
+ * The type to be returned as the generic type in the iterator.
+ */
+ Class<?> returnType() default Resource.class;
+
+}
Added: sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/annotations/SlingProperty.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/annotations/SlingProperty.java?rev=1539345&view=auto
==============================================================================
--- sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/annotations/SlingProperty.java (added)
+++ sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/annotations/SlingProperty.java Wed Nov 6 14:29:48 2013
@@ -0,0 +1,146 @@
+/*
+ * 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.extensions.dynamicproxy.annotations;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Annotation used to mark a Method as being a JCR backed property.
+ *
+ * Paths starting with '/' are absolute references, and do not have to be
+ * contained beneath the current Resource. Paths not starting with '/' are
+ * assumed to be relative to the current Resource.
+ *
+ * Here are 3 examples, one of each style:
+ *
+ * <code><br/>
+ * @SlingProperty(name = "cq:lastReplicated")<br/>
+ * Date getLastReplicated();
+ * <br/><br/>
+ * @SlingProperty(path = "par/image", name = "fileReference")<br/>
+ * String getImagePath();
+ * <br/><br/>
+ * @SlingProperty(path="/content/dam/geometrixx/documents/GeoCube_Datasheet.pdf/jcr:content/renditions/original/jcr:content", name="jcr:data")<br/>
+ * InputStream getGeoCubePDF();<br/>
+ * </code>
+ *
+ * This annotation supports the following return types and uses the default as indicated for each if no value is specified:
+ *
+ * <ul>
+ * <li>java.lang.Boolean or boolean - defaultBoolean</li>
+ * <li>java.lang.Byte[], byte[], java.io.InputStream - defaultBytes</li>
+ * <li>java.lang.Calendar, java.util.Date - defaultDate</li>
+ * <li>java.lang.Integer, int - defaultInt</li>
+ * <li>java.lang.Double, double - defaultDouble</li>
+ * <li>java.lang.Long, long - defaultLong</li>
+ * <li>java.lang.String - defaultString</li>
+ * <li>java.lang.String[] - defaultStrings</li>
+ * </ul>
+ *
+ * Other values will result with the value be retrieved without the default and null may be returned.
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ ElementType.METHOD })
+@Documented
+public @interface SlingProperty {
+
+ /**
+ * The default boolean, used to provide a default for booleans.
+ *
+ * @return the default boolean
+ */
+ boolean defaultBoolean() default false;
+
+ /**
+ * The default byte array, used to provide a default when the method returns
+ * an InputStream or a byte array.
+ *
+ * @return the default bytes
+ */
+ byte[] defaultBytes() default {};
+
+ /**
+ * The default date, used to set the default value for Calendars and Dates.
+ *
+ * @return the default date
+ */
+ long defaultDate() default -1;
+
+ /**
+ * The default double, used to set the default values for doubles.
+ *
+ * @return the default double
+ */
+ double defaultDouble() default 0.0;
+
+ /**
+ * Sets the default integer value, used to set the value for integers.
+ *
+ * @return the default integer
+ */
+ int defaultInt() default 0;
+
+ /**
+ * The default long, used to set the default values for long.
+ *
+ * @return the default long
+ */
+ long defaultLong() default 0L;
+
+ /**
+ * The default String, used to set the default String value.
+ *
+ * @return the default String
+ */
+ String defaultString() default "";
+
+ /**
+ * The default String array, used to set the default String array value.
+ *
+ * @return the default String array
+ */
+ String[] defaultStrings() default {};
+
+ /**
+ * The name of the property to retrieve from Sling.
+ *
+ * @return the name of the property to retrieve
+ */
+ String name() default "";
+
+ /**
+ * The path to the property to retrieve, if it begins with '/' it will be
+ * treated as an absolute path, otherwise, it will be treated as a relative
+ * path.
+ *
+ * @return the path to the property to retrieve
+ */
+ String path() default "";
+
+ /**
+ * Use a default value instead of just casting.
+ *
+ * @return the use default flag
+ */
+ boolean useDefault() default false;
+}
Added: sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/annotations/SlingReference.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/annotations/SlingReference.java?rev=1539345&view=auto
==============================================================================
--- sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/annotations/SlingReference.java (added)
+++ sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/annotations/SlingReference.java Wed Nov 6 14:29:48 2013
@@ -0,0 +1,73 @@
+/*
+ * 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.extensions.dynamicproxy.annotations;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Annotation used to mark a Method as referencing another resource. The path is
+ * required paths starting with '/' are absolute references, and do not have to
+ * be contained beneath the current Resource. Paths not starting with '/' are
+ * assumed to be relative to the current Resource.
+ *
+ * The Resource at the path will be returned depending on the return type of the
+ * method, according to the following rules (in order):
+ *
+ * <ol>
+ * <li>If the return type is an instance of a Resource, the resource at the path
+ * will be returned</li>
+ * <li>If adapting the resource into the return class returns a non-null, this
+ * object will be returned</li>
+ * <li>If using the ProxyService to retrieve a proxy instance does not throw and
+ * error and does not return null, the resulting object will returned</li>
+ * <li>Otherwise, null will be returned</li>
+ * </ol>
+ *
+ * Here are 3 examples, showing valid return types:
+ *
+ * <code><br/>
+ * @SlingReference(path = "/content/page")<br/>
+ * Resource getPage();<br/><br/>
+ *
+ * @SlingReference(path = "/content/page")<br/>
+ * ValueMap getPageProperties();<br/><br/>
+ *
+ * @SlingReference(path = "/content/page")
+ * IPageProxy getPageProxy();
+ * </code>
+ *
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ ElementType.METHOD })
+@Documented
+public @interface SlingReference {
+
+ /**
+ * The path to the resource being referenced, if it begins with '/' it will
+ * be treated as an absolute path, otherwise, it will be treated as a
+ * relative path.
+ *
+ * @return the path to the resource being referenced
+ */
+ String path();
+}
Added: sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/annotations/SlingServiceInvocation.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/annotations/SlingServiceInvocation.java?rev=1539345&view=auto
==============================================================================
--- sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/annotations/SlingServiceInvocation.java (added)
+++ sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/annotations/SlingServiceInvocation.java Wed Nov 6 14:29:48 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.extensions.dynamicproxy.annotations;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Annotation used to mark a Method as invoking another method on a Sling
+ * Service. When the method on the proxy interface is invoked, it will retrieve
+ * an instance of the Service and attempt to find a method matching the
+ * signature of the proxy method. If the first parameter of the method is the
+ * proxy interface, the proxy will inject an instance of itself.
+ *
+ * Below are two examples, showing configurations, they assume you have a
+ * service with the interface:
+ *
+ * <code><br/>
+ * public interface MyCustomService {<br/>
+ * Resource doStuff();<br/>
+ * Resource doOtherStuff(MyProxy proxy);<br/>
+ * Resource doEvenMoreStuff(MyProxy proxy, String path);<br/>
+ * }
+ * </code>
+ *
+ * <code><br/>
+ * @SlingServiceInvocation(service = MyCustomService.class)<br/>
+ * Resource doStuff();<br/><br/>
+ *
+ * @SlingServiceInvocation(service = MyCustomService.class)<br/>
+ * Resource doOtherStuff();<br/><br/>
+ *
+ * @SlingServiceInvocation(service = MyCustomService.class)<br/>
+ * Resource doEvenMoreStuff(String path);
+ * </code>
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ ElementType.METHOD })
+@Documented
+public @interface SlingServiceInvocation {
+
+ /**
+ * The service interface to reference. This service should have a single
+ * implementation and should have a method matching the signature of the
+ * method upon which this annotation is added.
+ *
+ * @return the service interface
+ */
+ Class<?> service();
+}
Added: sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/annotations/package-info.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/annotations/package-info.java?rev=1539345&view=auto
==============================================================================
--- sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/annotations/package-info.java (added)
+++ sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/annotations/package-info.java Wed Nov 6 14:29:48 2013
@@ -0,0 +1,29 @@
+/*
+ * 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.
+ */
+
+/**
+ * Provides the Annotations to configure SlingProxy interfaces.
+ *
+ * @version 1.0
+ */
+@Version("1.0")
+package org.apache.sling.extensions.dynamicproxy.annotations;
+
+import aQute.bnd.annotation.Version;
+
Added: sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/DefaultSlingProxyServiceImpl.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/DefaultSlingProxyServiceImpl.java?rev=1539345&view=auto
==============================================================================
--- sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/DefaultSlingProxyServiceImpl.java (added)
+++ sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/DefaultSlingProxyServiceImpl.java Wed Nov 6 14:29:48 2013
@@ -0,0 +1,112 @@
+/*
+ * 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.extensions.dynamicproxy.impl;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Proxy;
+
+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.resource.Resource;
+import org.apache.sling.extensions.dynamicproxy.SlingProxyService;
+import org.apache.sling.extensions.dynamicproxy.impl.reflection.Annotations;
+import org.osgi.service.component.ComponentContext;
+
+/**
+ * Default implementation of the
+ * {@link org.apache.sling.extensions.dynamicproxy.SlingProxyService}
+ */
+@Service(value = SlingProxyService.class)
+@Component(description = "Creates SlingProxy instances", immediate = true)
+public final class DefaultSlingProxyServiceImpl implements SlingProxyService {
+
+ /**
+ * The sevice's context in OSGi
+ */
+ private ComponentContext context;
+
+ /**
+ * Called by the OSGi Container when this service is activated.
+ *
+ * @param context
+ * the context for this service
+ */
+ @Activate
+ protected void activate(ComponentContext context) {
+ this.context = context;
+ }
+
+ /**
+ * Checks to see if an instance of the specified <code>type</code> can be
+ * instantiated from the <code>resource</code>.
+ *
+ * @param resource
+ * the resource to check
+ * @param type
+ * the type to check
+ */
+ private <T> void validateIsInstantiable(Resource resource, Class<T> type) {
+ if (resource == null) {
+ String msg = "The backing Resource cannot be NULL.";
+ throw new NullPointerException(msg);
+ }
+ if (type == null) {
+ String msg = "The provided SlingProxy Interface cannot be NULL.";
+ throw new NullPointerException(msg);
+ }
+ if (!type.isInterface()) {
+ String msg = "Proxy class " + type.getName()
+ + " must be an Interface.";
+ throw new UnsupportedOperationException(msg);
+ }
+ if (!Annotations.hasMethodProxyAnnotation(type)) {
+ String msg = "Proxy interface "
+ + type.getName()
+ + " must have at least "
+ + "one Method with a @SlingProperty, @SlingReference, @SlingServiceInvocation or @SlingChildren annotation.";
+ throw new UnsupportedOperationException(msg);
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.sling.commons.proxy.SlingProxyService#getProxy(org.apache
+ * .sling.api.resource.Resource, java.lang.Class)
+ */
+ public <T> T getProxy(Resource resource,
+ Class<T> type) {
+ validateIsInstantiable(resource, type);
+
+ InvocationHandler ih = new SlingInvocationHandler(resource, this);
+ T rtn = type.cast(Proxy.newProxyInstance(
+ type.getClassLoader(), new Class[] { type }, ih));
+ return rtn;
+ }
+
+ /**
+ * Gets the component context for this service.
+ *
+ * @return the component context
+ */
+ public ComponentContext getContext() {
+ return context;
+ }
+}
Added: sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/DeferredIterator.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/DeferredIterator.java?rev=1539345&view=auto
==============================================================================
--- sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/DeferredIterator.java (added)
+++ sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/DeferredIterator.java Wed Nov 6 14:29:48 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.extensions.dynamicproxy.impl;
+
+import java.util.Iterator;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.extensions.dynamicproxy.SlingProxyService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * An iterator which defers the loading of the elements from a backing of
+ * Iterator of resources.
+ *
+ * @param <E>
+ * the object type for the interator contents, set in the
+ * SlingChildren annotation
+ */
+final class DeferredIterator<E> implements Iterator<E> {
+
+ /**
+ * The SLF4j Logger
+ */
+ private static final Logger log = LoggerFactory
+ .getLogger(DeferredIterator.class);
+
+ /**
+ * The iterator of Resources which backs this deferred iterator
+ */
+ private final Iterator<Resource> backingResources;
+
+ /**
+ * They type to be returned.
+ */
+ private final Class<E> returnType;
+
+ /**
+ * A reference to the Sling Proxy service, used to load items which are
+ * SlingProxies
+ */
+ private final SlingProxyService slingProxyService;
+
+ /**
+ * Instantiates a Deferred Iterator.
+ *
+ * @param backingResources
+ * the Iterator of resources with which to back this iterator
+ * @param returnType
+ * the class which should be returned by next
+ * @param slingProxyService
+ * a reference to the Sling Proxy service
+ */
+ public DeferredIterator(final Iterator<Resource> backingResources,
+ final Class<E> returnType,
+ final SlingProxyService slingProxyService) {
+ this.backingResources = backingResources;
+ this.returnType = returnType;
+ this.slingProxyService = slingProxyService;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.util.Iterator#hasNext()
+ */
+ public boolean hasNext() {
+ return this.backingResources.hasNext();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.util.Iterator#next()
+ */
+ public E next() {
+ log.trace("next");
+ final Resource resource = this.backingResources.next();
+
+ Object toReturn = null;
+ if (resource != null) {
+ if (Resource.class.equals(this.returnType)) {
+ log.debug("Returning resource as child");
+ toReturn = resource;
+ }
+
+ final Object adapted = resource.adaptTo(this.returnType);
+ if (adapted != null) {
+ log.debug("Returning adapted object as child");
+ toReturn = adapted;
+ }
+
+ try {
+ final Object proxy = this.slingProxyService.getProxy(resource,
+ this.returnType);
+ log.debug("Returning proxy as reference");
+ toReturn = proxy;
+ } catch (final Exception e) {
+ log.warn("Exception getting proxy, null reference will be returned");
+ }
+ } else {
+ log.debug("Referenced resource is null");
+ }
+ return this.returnType.cast(toReturn);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.util.Iterator#remove()
+ */
+ public void remove() {
+ this.backingResources.remove();
+ }
+
+}
Added: sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/SlingInvocationHandler.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/SlingInvocationHandler.java?rev=1539345&view=auto
==============================================================================
--- sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/SlingInvocationHandler.java (added)
+++ sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/SlingInvocationHandler.java Wed Nov 6 14:29:48 2013
@@ -0,0 +1,403 @@
+/*
+ * 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.extensions.dynamicproxy.impl;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Collections;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceNotFoundException;
+import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.extensions.dynamicproxy.SlingProxyService;
+import org.apache.sling.extensions.dynamicproxy.annotations.SlingChildren;
+import org.apache.sling.extensions.dynamicproxy.annotations.SlingProperty;
+import org.apache.sling.extensions.dynamicproxy.annotations.SlingReference;
+import org.apache.sling.extensions.dynamicproxy.annotations.SlingServiceInvocation;
+import org.apache.sling.extensions.dynamicproxy.impl.lang.JDPEqualsImpl;
+import org.apache.sling.extensions.dynamicproxy.impl.lang.JDPHashCodeImpl;
+import org.apache.sling.extensions.dynamicproxy.impl.lang.JDPToStringImpl;
+import org.apache.sling.extensions.dynamicproxy.impl.lang.MethodType;
+import org.apache.sling.extensions.dynamicproxy.impl.reflection.Annotations;
+import org.apache.sling.extensions.dynamicproxy.impl.to.InvokedChildrenTO;
+import org.apache.sling.extensions.dynamicproxy.impl.to.InvokedPropertyTO;
+import org.apache.sling.extensions.dynamicproxy.impl.to.PathBasedInvokedTO;
+import org.apache.sling.extensions.dynamicproxy.impl.to.SlingServiceInvocationTO;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Invocation handler for proxies backed by a Sling Resource.
+ */
+public class SlingInvocationHandler implements InvocationHandler {
+
+ /**
+ * SLF4J Logger
+ */
+ private static final Logger log = LoggerFactory
+ .getLogger(SlingInvocationHandler.class);
+
+ /**
+ * The backing Sling Resource
+ */
+ private final Resource r;
+
+ /**
+ * This caches all 'get' or 'is' method's return values
+ */
+ private final Map<String, Object> cache;
+
+ /**
+ * The SlingProxyService instance, used to retrieve references and children.
+ */
+ private final SlingProxyService slingProxyService;
+
+ /**
+ * Create a new ResourceInvocationHandler allowing invocation of all Methods
+ * that this InvocationHandler represents
+ *
+ * @param r
+ * the resource
+ * @param defaultSlingProxyServiceImpl
+ */
+ SlingInvocationHandler(final Resource r,
+ final SlingProxyService slingProxyService) {
+ this.r = r;
+ this.slingProxyService = slingProxyService;
+ this.cache = new java.util.HashMap<String, Object>();
+ }
+
+ /**
+ * Get the path of the resource backing this invocation handler.
+ *
+ * @return the resource path
+ */
+ public final String getResourcePath() {
+ return this.r.getPath();
+ }
+
+ /**
+ * Handle get children invocations.
+ *
+ * @param to
+ * the method invocation transfer object
+ * @return the returned object
+ */
+ @SuppressWarnings("unchecked")
+ private Object handleGetChildren(final InvokedChildrenTO to) {
+ log.trace("handleGetChildren");
+
+ Resource resource = this.r;
+ if (!StringUtils.isEmpty(to.getPath())) {
+ log.debug("Loading child resources from: {}", to.getPath());
+ if (to.isAbsolute()) {
+ resource = this.r.getResourceResolver().getResource(
+ to.getPath());
+ } else {
+ resource = this.r.getResourceResolver().getResource(this.r,
+ to.getPath());
+ }
+ }
+
+ Class<?> returnType = Resource.class;
+ if (to.getReturnType() != null) {
+ returnType = to.getReturnType();
+ }
+
+ Iterator<?> toReturn = Collections.EMPTY_LIST.iterator();
+ if (resource != null) {
+ toReturn = new DeferredIterator<Object>(resource
+ .getResourceResolver().listChildren(resource),
+ (Class<Object>) returnType, this.slingProxyService);
+ }
+ return toReturn;
+ }
+
+ /**
+ * Handles get requests against a proxy to a Sling Resource.
+ *
+ * @param to
+ * the DTO for the invocation
+ * @return the result of the get access
+ * @throws Throwable
+ */
+ private Object handleGetProperty(final InvokedPropertyTO to)
+ throws Throwable {
+ Object objReturn;
+
+ // handle properties
+ if (this.cache.containsKey(to.getPropertyName())) {
+ objReturn = this.cache.get(to.getPropertyName());
+ } else {
+ // TODO: refactor to also cache the ValueMap for a given path maybe?
+ ValueMap vm;
+ if (to.getPath() == null) {
+ vm = this.r.adaptTo(ValueMap.class);
+ } else {
+ Resource rsrc;
+ if (StringUtils.isEmpty(to.getPath())) {
+ rsrc = this.r;
+ } else if (to.isAbsolute()) {
+ rsrc = this.r.getResourceResolver().getResource(
+ to.getPath());
+ } else {
+ rsrc = this.r.getResourceResolver().getResource(this.r,
+ to.getPath());
+ }
+ if (rsrc == null) {
+ throw new ResourceNotFoundException(
+ "Unable to load resource at path: " + to.getPath());
+ }
+ vm = rsrc.adaptTo(ValueMap.class);
+ }
+
+ if (to.isUseDefault()) {
+ Class<?> rt = to.getMethod().getReturnType();
+ if (Boolean.class.equals(rt) || boolean.class.equals(rt)) {
+ objReturn = vm.get(to.getName(), to.getDefaultBoolean());
+ } else if (Byte[].class.equals(rt) || byte[].class.equals(rt)) {
+ objReturn = vm.get(to.getName(), to.getDefaultBytes());
+ } else if (InputStream.class.equals(rt)) {
+ final InputStream defaultIs = new ByteArrayInputStream(
+ to.getDefaultBytes());
+ objReturn = vm.get(to.getName(), defaultIs);
+ } else if (Calendar.class.equals(rt)) {
+ final Calendar c = Calendar.getInstance();
+ c.setTimeInMillis(to.getDefaultDate());
+ objReturn = vm.get(to.getName(), c);
+ } else if (Date.class.equals(rt)) {
+ objReturn = vm.get(to.getName(),
+ new Date(to.getDefaultDate()));
+ } else if (Integer.class.equals(rt) || int.class.equals(rt)) {
+ objReturn = vm.get(to.getName(), to.getDefaultInt());
+ } else if (Double.class.equals(rt) || double.class.equals(rt)) {
+ objReturn = vm.get(to.getName(), to.getDefaultDouble());
+ } else if (Long.class.equals(rt) || long.class.equals(rt)) {
+ objReturn = vm.get(to.getName(), to.getDefaultLong());
+ } else if (String.class.equals(rt)) {
+ objReturn = vm.get(to.getName(), to.getDefaultString());
+ } else if (String[].class.equals(rt)) {
+ objReturn = vm.get(to.getName(), to.getDefaultStrings());
+ } else {
+ log.warn(
+ "Unmappable return type {} specified and default requested",
+ to.getMethod().getReturnType());
+ objReturn = vm.get(to.getName(), to.getMethod()
+ .getReturnType());
+ }
+ } else {
+ objReturn = vm
+ .get(to.getName(), to.getMethod().getReturnType());
+ }
+ this.cache.put(to.getPropertyName(), objReturn);
+ }
+ return objReturn;
+ }
+
+ /**
+ * Handles a call to get a reference to another resource.
+ *
+ * @param to
+ * the method invocation transfer object
+ * @return the resulting object
+ */
+ private Object handleGetReference(final PathBasedInvokedTO to) {
+ log.trace("handleGetReference");
+
+ Object value = null;
+ log.debug("Referencing resource at path: {}", to.getPath());
+ Resource reference = null;
+ if (to.getPath().startsWith("/")) {
+ reference = this.r.getResourceResolver().getResource(to.getPath());
+ } else {
+ reference = this.r.getResourceResolver().getResource(this.r,
+ to.getPath());
+ }
+ log.debug("Loaded resource: {}", reference);
+
+ if (reference != null) {
+ final Class<?> returnType = to.getMethod().getReturnType();
+
+ log.debug("Attempting to get instance of {}",
+ returnType.getCanonicalName());
+ if (Resource.class.equals(returnType)) {
+ log.debug("Returning resource as reference");
+ value = reference;
+ }
+
+ if (value == null) {
+ value = reference.adaptTo(returnType);
+ if (value != null) {
+ log.debug("Returning adapted object as reference");
+ }
+ }
+
+ if (value == null) {
+ try {
+ value = this.slingProxyService.getProxy(reference,
+ returnType);
+ if (value != null) {
+ log.debug("Returning adapted object as reference");
+ }
+ } catch (final Exception e) {
+ log.warn("Exception getting proxy, null reference will be returned");
+ }
+ }
+ } else {
+ log.debug("Referenced resource is null");
+ }
+ log.debug("Returning value: {}", value);
+
+ return value;
+ }
+
+ /**
+ * Handles the invocation of Sling Service methods.
+ *
+ * @param to
+ * the transfer object
+ * @return the value returned by the Sling Service if anything
+ */
+ private Object handleServiceInvocation(SlingServiceInvocationTO to) {
+ Object value = null;
+
+ BundleContext context = ((DefaultSlingProxyServiceImpl) slingProxyService)
+ .getContext().getBundleContext();
+ ServiceReference reference = context.getServiceReference(to
+ .getService().getCanonicalName());
+ if (reference != null) {
+ Method toInvoke = null;
+ Object service = context.getService(reference);
+ for (Method m : service.getClass().getDeclaredMethods()) {
+ if (m.getName().equals(to.getMethod().getName())) {
+ final Class<?>[] params = to.getMethod()
+ .getParameterTypes();
+ if (m.getParameterTypes().equals(params)
+ && m.getReturnType().equals(
+ to.getMethod().getReturnType())) {
+ toInvoke = m;
+ break;
+ }
+ Class<?>[] newParams = new ArrayList<Class<?>>() {
+ private static final long serialVersionUID = 1L;
+ {
+ for (Class<?> clazz : params) {
+ add(clazz);
+ }
+ }
+ }.toArray(new Class<?>[params.length]);
+ if (m.getParameterTypes().equals(newParams)
+ && m.getReturnType().equals(
+ to.getMethod().getReturnType())) {
+ toInvoke = m;
+ break;
+ }
+ }
+ }
+ if (toInvoke != null) {
+ try {
+ value = toInvoke.invoke(service, to.getArgs());
+ } catch (IllegalArgumentException e) {
+ log.warn("Exception calling service method "
+ + service.getClass().getCanonicalName() + "."
+ + to.getMethod().getName() + " invalid paramters",
+ e);
+ } catch (IllegalAccessException e) {
+ log.warn("Exception calling service method "
+ + service.getClass().getCanonicalName() + "."
+ + to.getMethod().getName() + " illegal access", e);
+ } catch (InvocationTargetException e) {
+ log.warn("Exception calling service method "
+ + service.getClass().getCanonicalName() + "."
+ + to.getMethod().getName(), e);
+ }
+ } else {
+ log.warn("Unable to find method {} on service {}", to
+ .getMethod().getName(), service.getClass()
+ .getCanonicalName());
+ }
+ } else {
+ log.warn("Unable to find reference to service {}", to.getService()
+ .getCanonicalName());
+ }
+ return value;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#hashCode()
+ */
+ @Override
+ public int hashCode() {
+ return SlingInvocationHandler.class.hashCode() * 37
+ + this.r.getPath().hashCode();
+ }
+
+ /*
+ * (non-Javadoc) @see
+ * java.lang.reflect.InvocationHandler#invoke(java.lang.Object,
+ * java.lang.reflect.Method, java.lang.Object[])
+ */
+ public Object invoke(final Object proxy, final Method method,
+ final Object[] args) throws Throwable {
+
+ MethodType mt = MethodType.getMethodType(method);
+ if (mt == MethodType.ToString) {
+ return new JDPToStringImpl().toString(proxy);
+ } else if (mt == MethodType.HashCode) {
+ return new JDPHashCodeImpl().hashCode(proxy);
+ } else if (mt == MethodType.Equals) {
+ if ((args == null) || (args.length != 1)) {
+ final String msg = "Method 'equals' requires exactly 1 argument.";
+ throw new IllegalArgumentException(msg);
+ }
+ return new JDPEqualsImpl().equals(proxy, args[0]);
+ } else if (mt == MethodType.BackingResource) {
+ return this.r;
+ } else if (Annotations.methodHasAnnotation(method, SlingProperty.class)) {
+ return handleGetProperty(InvokedPropertyTO.newInstance(method));
+ } else if (Annotations
+ .methodHasAnnotation(method, SlingReference.class)) {
+ SlingReference sr = method.getAnnotation(SlingReference.class);
+ PathBasedInvokedTO to = new PathBasedInvokedTO(method, sr.path());
+ return handleGetReference(to);
+ } else if (Annotations.methodHasAnnotation(method, SlingChildren.class)) {
+ return handleGetChildren(InvokedChildrenTO.newInstance(method));
+ } else if (Annotations.methodHasAnnotation(method,
+ SlingServiceInvocation.class)) {
+ return handleServiceInvocation(SlingServiceInvocationTO
+ .newInstance(method, args));
+ }
+
+ throw new UnsupportedOperationException("Method " + method.getName()
+ + " does not have valid annotation");
+ }
+}
Added: sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/lang/JDPEqualsImpl.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/lang/JDPEqualsImpl.java?rev=1539345&view=auto
==============================================================================
--- sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/lang/JDPEqualsImpl.java (added)
+++ sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/lang/JDPEqualsImpl.java Wed Nov 6 14:29:48 2013
@@ -0,0 +1,84 @@
+/*
+ * 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.extensions.dynamicproxy.impl.lang;
+
+import java.lang.reflect.Proxy;
+
+import org.apache.sling.extensions.dynamicproxy.impl.SlingInvocationHandler;
+import org.apache.sling.extensions.dynamicproxy.impl.reflection.Classes;
+
+/**
+ * Allows for testing the equality of the Sling Proxy instance.
+ */
+public class JDPEqualsImpl {
+
+ /**
+ * Test if the Sling Proxy object is equal to another object.
+ *
+ * @param o1
+ * the first object
+ * @param o2
+ * the second object
+ * @return whether or not the objects are equal
+ */
+ public boolean equals(Object o1, Object o2) {
+ if (o1 == o2) {
+ return true;
+ }
+ if (o1 == null || o2 == null) {
+ return false;
+ }
+ if (!Proxy.isProxyClass(o2.getClass())) {
+ return false;
+ }
+
+ try {
+ SlingInvocationHandler rih1 = (SlingInvocationHandler) Proxy
+ .getInvocationHandler(o1);
+ SlingInvocationHandler rih2 = (SlingInvocationHandler) Proxy
+ .getInvocationHandler(o2);
+ if (Classes.haveSameInterfaces(o1, o2)) {
+ return stringEquals(rih1.getResourcePath(),
+ rih2.getResourcePath());
+ }
+ } catch (ClassCastException ex) {
+ }
+ return false;
+ }
+
+ /**
+ * Compares the two strings for equality, either string being null results
+ * in inequality.
+ *
+ * @param s1
+ * the first string
+ * @param s2
+ * the second string
+ * @return whether or not the two strings are equal
+ */
+ private static boolean stringEquals(String s1, String s2) {
+ if (s1 == s2) {
+ return true;
+ }
+ if (s1 == null || s2 == null) {
+ return false;
+ }
+ return s1.equals(s2);
+ }
+}
Added: sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/lang/JDPHashCodeImpl.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/lang/JDPHashCodeImpl.java?rev=1539345&view=auto
==============================================================================
--- sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/lang/JDPHashCodeImpl.java (added)
+++ sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/lang/JDPHashCodeImpl.java Wed Nov 6 14:29:48 2013
@@ -0,0 +1,63 @@
+/*
+ * 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.extensions.dynamicproxy.impl.lang;
+
+import java.lang.reflect.Proxy;
+
+import org.apache.sling.extensions.dynamicproxy.impl.SlingInvocationHandler;
+
+/**
+ * Generates a HashCode for the provided Object based on:
+ *
+ * <ul>
+ * <li>The hash codes of all of the interfaces for the object</li>
+ * <li>The hash code of the resource path</li>
+ * </ul>
+ *
+ * The result of the hash code call is added to 37 * the original result.
+ */
+public final class JDPHashCodeImpl {
+
+ /**
+ * Generates a hash code for the specified object.
+ *
+ * @param obj
+ * the object for which to generate the hash code
+ * @return the hash code for the object
+ * @throws IllegalStateException
+ */
+ public int hashCode(Object obj) throws IllegalStateException {
+
+ int result = 7;
+
+ Class<?>[] interfaces = obj.getClass().getInterfaces();
+ for (Class<?> intf : interfaces) {
+ result = 37 * result + intf.hashCode();
+ }
+
+ SlingInvocationHandler sih = (SlingInvocationHandler) Proxy
+ .getInvocationHandler(obj);
+ result = 37
+ * result
+ + (sih.getResourcePath() != null ? sih.getResourcePath()
+ .hashCode() : 0);
+
+ return result;
+ }
+}
Added: sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/lang/JDPToStringImpl.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/lang/JDPToStringImpl.java?rev=1539345&view=auto
==============================================================================
--- sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/lang/JDPToStringImpl.java (added)
+++ sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/lang/JDPToStringImpl.java Wed Nov 6 14:29:48 2013
@@ -0,0 +1,79 @@
+/*
+ * 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.extensions.dynamicproxy.impl.lang;
+
+import java.lang.reflect.Proxy;
+
+import org.apache.sling.extensions.dynamicproxy.impl.SlingInvocationHandler;
+
+/**
+ * Generates a String representation based on the return values for all of the
+ * 'get' methods on the provided Object.
+ *
+ */
+public final class JDPToStringImpl {
+
+ /**
+ * Adding the interface information to the string representation.
+ *
+ * @param obj
+ * the object from which to cull the interfaces
+ * @param sb
+ * the string builder to hold the string representation
+ */
+ private void addInterfaceInfo(final Object obj, final StringBuilder sb) {
+ final Class<?>[] cla = obj.getClass().getInterfaces();
+ sb.append(obj.getClass().getName());
+ sb.append(", Proxy Of: [");
+
+ final int max = (cla != null ? cla.length : -1);
+ for (int ndx = 0; ndx < max; ndx++) {
+ sb.append(cla[ndx].getName());
+ if ((ndx + 1) < max) {
+ sb.append(" , ");
+ }
+ }
+
+ sb.append("]");
+ }
+
+ /**
+ * Generates a String representation of an object.
+ *
+ * @param obj
+ * the object to generate a string of
+ * @return the string representation
+ */
+ public String toString(final Object obj) {
+ final StringBuilder sb = new StringBuilder();
+
+ if (obj != null) {
+ this.addInterfaceInfo(obj, sb);
+ sb.append(", Backing Resource: [");
+ SlingInvocationHandler sih = (SlingInvocationHandler) Proxy
+ .getInvocationHandler(obj);
+ sb.append(sih.getResourcePath());
+ sb.append("]");
+ } else {
+ sb.append("null");
+ }
+
+ return sb.toString();
+ }
+}
Added: sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/lang/MethodType.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/lang/MethodType.java?rev=1539345&view=auto
==============================================================================
--- sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/lang/MethodType.java (added)
+++ sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/lang/MethodType.java Wed Nov 6 14:29:48 2013
@@ -0,0 +1,119 @@
+/*
+ * 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.extensions.dynamicproxy.impl.lang;
+
+import java.lang.reflect.Method;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Types of Method's determined solely by the Method name.
+ */
+public enum MethodType {
+
+ ToString("^(toString)$"), HashCode("^(hashCode)$"), Equals("^(equals)$"), BackingResource(
+ "^(getBackingResource)$"), JavaBeanGet("^get([A-Z])(\\w+)$"), JavaBeanIs(
+ "^is([A-Z])(\\w+)$"), JavaBeanSet("^set([A-Z])(\\w+)$"), Unknown(
+ "^$");
+
+ private final String regex;
+
+ MethodType(String type) {
+ regex = type;
+ }
+
+ /**
+ * Get the method pattern.
+ *
+ * @return the method pattern
+ */
+ public Pattern getMethodPattern() {
+ return Pattern.compile(regex);
+ }
+
+ /**
+ * Checks to see if the method matches the current type.
+ *
+ * @param m
+ * the method to check
+ * @return true if the method matches, false otherwise
+ */
+ public boolean isType(Method m) {
+ return Pattern.matches(regex, m.getName());
+ }
+
+ /**
+ * Determine if the given MethodType enum is contained within the Array of
+ * MethodType's
+ *
+ * @param mta
+ * MethodType[] - the MethodType Array to search in
+ * @param mt
+ * MethodType - the MethodType to search for
+ * @return TRUE if 'mt' is found within 'mta' FALSE otherwise
+ */
+ public static final boolean contains(MethodType[] mta, MethodType mt) {
+ for (MethodType _mt : mta) {
+ if (mt == _mt)
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Get the JavaBean name of the corresponding Method
+ *
+ * @param mt
+ * MethodType - The Method type of Method 'm'
+ * @param m
+ * Method
+ * @return String - If the method is of the given the JavaBean name for the
+ * provided Method, if it is of type 'mt'
+ */
+ public static final String getBeanName(MethodType mt, Method m) {
+ Matcher match = mt.getMethodPattern().matcher(m.getName());
+ String name = null;
+ if (match.find()) {
+ if (match.groupCount() == 1) {
+ name = match.group(1);
+ } else {
+ String pfx = match.group(1).toLowerCase();
+ name = pfx + match.group(2);
+ }
+ }
+ return name;
+ }
+
+ /**
+ * Checks the method against all of the default method types to determine
+ * which type the method is.
+ *
+ * @param m
+ * the method to check
+ * @return the type the method is
+ */
+ public static final MethodType getMethodType(Method m) {
+ for (MethodType mt : values()) {
+ if (mt.isType(m)) {
+ return mt;
+ }
+ }
+ return Unknown;
+ }
+}
Added: sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/lang/package-info.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/lang/package-info.java?rev=1539345&view=auto
==============================================================================
--- sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/lang/package-info.java (added)
+++ sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/lang/package-info.java Wed Nov 6 14:29:48 2013
@@ -0,0 +1,29 @@
+/*
+ * 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.
+ */
+
+/**
+ * Provides helper classes for handling language default methods.
+ *
+ * @version 1.0
+ */
+@Version("1.0")
+package org.apache.sling.extensions.dynamicproxy.impl.lang;
+
+import aQute.bnd.annotation.Version;
+
Added: sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/package-info.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/package-info.java?rev=1539345&view=auto
==============================================================================
--- sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/package-info.java (added)
+++ sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/package-info.java Wed Nov 6 14:29:48 2013
@@ -0,0 +1,29 @@
+/*
+ * 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.
+ */
+
+/**
+ * Provides the default implementation of the Sling Proxy API.
+ *
+ * @version 1.0
+ */
+@Version("1.0")
+package org.apache.sling.extensions.dynamicproxy.impl;
+
+import aQute.bnd.annotation.Version;
+
Added: sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/reflection/Annotations.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/reflection/Annotations.java?rev=1539345&view=auto
==============================================================================
--- sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/reflection/Annotations.java (added)
+++ sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/reflection/Annotations.java Wed Nov 6 14:29:48 2013
@@ -0,0 +1,119 @@
+/*
+ * 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.extensions.dynamicproxy.impl.reflection;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+
+import org.apache.sling.extensions.dynamicproxy.annotations.SlingChildren;
+import org.apache.sling.extensions.dynamicproxy.annotations.SlingProperty;
+import org.apache.sling.extensions.dynamicproxy.annotations.SlingReference;
+import org.apache.sling.extensions.dynamicproxy.annotations.SlingServiceInvocation;
+
+/**
+ * Methods for simplifying reflection annotation calls.
+ */
+public final class Annotations {
+
+ /**
+ * For the given Class'clazz' determine if it has at least one method
+ * containing an Annotation of type or subclass of the proxy annotations
+ *
+ * @param <T>
+ * - <T extends Annotation> - Defines generic type T to be a
+ * subtype of Annotation
+ * @return TRUE if it does, FALSE otherwise
+ */
+ public static <T extends Annotation> boolean hasMethodProxyAnnotation(
+ Class<?> clazz) {
+ Method[] ma = clazz.getMethods();
+ if (ma == null) {
+ return false;
+ }
+
+ for (Method m : ma) {
+ if (methodHasProxyAnnotation(m)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * For the given Method 'method' determine if it has an Annotation of type
+ * or subclass of the proxy annotations
+ *
+ * @param <T>
+ * - <T extends Annotation> - Defines generic type T to be a
+ * subtype of Annotation
+ * @param method
+ * Method - the Method to get annotations from
+ * @return TRUE if it does, FALSE otherwise
+ */
+ @SuppressWarnings("unchecked")
+ public static <T extends Annotation> boolean methodHasProxyAnnotation(
+ Method method) {
+
+ Annotation[] ana = method.getAnnotations();
+ if (ana == null) {
+ return false;
+ }
+ Class<?>[] annotations = new Class<?>[] { SlingChildren.class,
+ SlingProperty.class, SlingReference.class,
+ SlingServiceInvocation.class };
+ for (Class<?> annType : annotations) {
+ if(methodHasAnnotation(method, (Class<T>)annType)){
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * For the given Method 'method' determine if it has an Annotation of type
+ * or subclass of type 'annType'.
+ *
+ * @param <T>
+ * - <T extends Annotation> - Defines generic type T to be a
+ * subtype of Annotation
+ * @param method
+ * Method - the Method to get annotations from
+ * @param annType
+ * Class<T> - the annotation types to get from Class 'clazz'
+ * @return TRUE if it does, FALSE otherwise
+ */
+ public static <T extends Annotation> boolean methodHasAnnotation(
+ Method method, Class<T> annType) {
+
+ Annotation[] ana = method.getAnnotations();
+ if (ana == null) {
+ return false;
+ }
+
+ for (Annotation a : ana) {
+ if (annType.isAssignableFrom(a.getClass())) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+}
Added: sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/reflection/Classes.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/reflection/Classes.java?rev=1539345&view=auto
==============================================================================
--- sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/reflection/Classes.java (added)
+++ sling/whiteboard/dklco/dynamic-proxy/src/main/java/org/apache/sling/extensions/dynamicproxy/impl/reflection/Classes.java Wed Nov 6 14:29:48 2013
@@ -0,0 +1,82 @@
+/*
+ * 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.extensions.dynamicproxy.impl.reflection;
+
+import java.lang.reflect.Proxy;
+import java.util.Set;
+
+/**
+ * Methods for simplifying reflection class calls.
+ */
+public final class Classes {
+
+
+ /**
+ * To have the same set of interfaces the two Objects must be one of the
+ * following: > be equal in identity to one another, or both NULL > have 0
+ * interfaces and either be Proxy Classes or be the same Type > must have
+ * the same number of interfaces, and the same interfaces
+ *
+ * @param o1
+ * @param o2
+ * @return TRUE if the objects have the same interfaces, FALSE otherwise
+ */
+ public static boolean haveSameInterfaces(Object o1, Object o2) {
+ if (o1 == o2) {
+ return true;
+ }
+
+ Class<?>[] ca1 = o1.getClass().getInterfaces();
+ Class<?>[] ca2 = o2.getClass().getInterfaces();
+ int size1 = size(ca1);
+ if (size1 != size(ca2)) {
+ return false;
+ }
+ if (size1 < 1) {
+ if (Proxy.isProxyClass(o1.getClass())
+ && Proxy.isProxyClass(o2.getClass())) {
+ return true;
+ } else if (o1.getClass() == o2.getClass()) {
+ return true;
+ }
+ return false;
+ }
+
+ Set<Class<?>> set = new java.util.HashSet<Class<?>>(size1);
+ for (Class<?> c : ca1) {
+ set.add(c);
+ }
+ for (Class<?> c : ca2) {
+ set.remove(c);
+ }
+ return set.size() < 1;
+ }
+
+ /**
+ * Checks the size of the class arraym return the size or -1 if the array is
+ * null.
+ *
+ * @param ca
+ * the array to check
+ * @return the size of the array
+ */
+ private static final int size(Class<?>[] ca) {
+ return (ca != null ? ca.length : -1);
+ }
+}