Author: radu
Date: Fri Feb 13 11:40:30 2015
New Revision: 1659520

URL: http://svn.apache.org/r1659520
Log:
SLING-4419 - [Regression] Sling Models cannot be used with Sightly Java Use-API

* merged PojoUseProvider with ClassUseProvider into JavaUseProvider which first 
tries to adapt the object to
Resource / SlingHttpServletRequest, then tries to load the object as a POJO 
from the repository
* provided IT for the Sling specific Use-API implementation

Added:
    
sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/JavaUseProvider.java
      - copied, changed from r1659489, 
sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/ClassUseProvider.java
    sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/
    sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/
    
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/
    
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/
    
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/
    
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/
    
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/
    
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/
    
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/RequestAdapterUseObject.java
    
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/ResourceAdapterUseObject.java
    
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/
    
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/RequestAdapterFactory.java
    
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/RequestAdapterUseObjectImpl.java
    
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/ResourceAdapterFactory.java
    
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/ResourceAdapterUseObjectImpl.java
    
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/models/
    
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/models/RequestModel.java
    
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/models/ResourceModel.java
    
sling/trunk/contrib/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/
    
sling/trunk/contrib/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/
    
sling/trunk/contrib/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/use/
    
sling/trunk/contrib/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/use/use.html
    
sling/trunk/contrib/scripting/sightly/testing-content/src/main/resources/SLING-INF/sightly.json
    
sling/trunk/contrib/scripting/sightly/testing/src/test/java/org/apache/sling/scripting/sightly/it/SlingSpecificsSightlyIT.java
Removed:
    
sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/ClassUseProvider.java
    
sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/PojoUseProvider.java
Modified:
    
sling/trunk/contrib/scripting/sightly/js-use-provider/src/main/java/org/apache/sling/scripting/sightly/js/impl/JsUseProvider.java
    sling/trunk/contrib/scripting/sightly/testing-content/pom.xml
    sling/trunk/contrib/scripting/sightly/testing/pom.xml

Copied: 
sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/JavaUseProvider.java
 (from r1659489, 
sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/ClassUseProvider.java)
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/JavaUseProvider.java?p2=sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/JavaUseProvider.java&p1=sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/ClassUseProvider.java&r1=1659489&r2=1659520&rev=1659520&view=diff
==============================================================================
--- 
sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/ClassUseProvider.java
 (original)
+++ 
sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/JavaUseProvider.java
 Fri Feb 13 11:40:30 2015
@@ -16,25 +16,29 @@
  * specific language governing permissions and limitations
  * under the License.
  
******************************************************************************/
-
 package org.apache.sling.scripting.sightly.impl.engine.extension.use;
 
 import java.util.HashMap;
 import java.util.Map;
 import java.util.regex.Pattern;
-
 import javax.script.Bindings;
 import javax.servlet.ServletRequest;
 
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Properties;
 import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.Service;
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.scripting.SlingBindings;
 import org.apache.sling.api.scripting.SlingScriptHelper;
 import org.apache.sling.commons.classloader.DynamicClassLoaderManager;
+import org.apache.sling.scripting.sightly.ResourceResolution;
+import org.apache.sling.scripting.sightly.impl.compiler.CompilerException;
+import 
org.apache.sling.scripting.sightly.impl.compiler.SightlyJavaCompilerService;
+import org.apache.sling.scripting.sightly.pojo.Use;
 import org.apache.sling.scripting.sightly.render.RenderContext;
 import org.apache.sling.scripting.sightly.use.ProviderOutcome;
 import org.apache.sling.scripting.sightly.use.UseProvider;
@@ -42,15 +46,10 @@ import org.osgi.framework.Constants;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-/**
- * Interprets the identifier as a class name and tries to load that class
- * using a dynamic class loader
- */
 @Component(
         metatype = true,
-        label = "Apache Sling Scripting Sightly Class Use Provider",
-        description = "The Class Use Provider is responsible for instantiating 
Use-API objects that are adaptable from Resource or " +
-                "SlingHttpServletRequest."
+        label = "Apache Sling Scripting Sightly Java Use Provider",
+        description = "The Java Use Provider is responsible for instantiating 
Java Use-API objects."
 )
 @Service(UseProvider.class)
 @Properties({
@@ -59,44 +58,84 @@ import org.slf4j.LoggerFactory;
                 label = "Service Ranking",
                 description = "The Service Ranking value acts as the priority 
with which this Use Provider is queried to return an " +
                         "Use-object. A higher value represents a higher 
priority.",
-                intValue = 80,
+                intValue = 90,
                 propertyPrivate = false
         )
 })
-public class ClassUseProvider implements UseProvider {
+public class JavaUseProvider implements UseProvider {
 
-    private static final Logger LOG = 
LoggerFactory.getLogger(ClassUseProvider.class);
+    private static final Logger LOG = 
LoggerFactory.getLogger(JavaUseProvider.class);
     private static final Pattern JAVA_PATTERN = 
Pattern.compile("([[\\p{L}&&[^\\p{Lu}]]_$][\\p{L}\\p{N}_$]*\\.)*[\\p{Lu}_$][\\p{L}\\p{N}_$]*");
 
+    @Reference
+    private SightlyJavaCompilerService sightlyJavaCompilerService = null;
+
+    @Reference
+    private DynamicClassLoaderManager dynamicClassLoaderManager = null;
+
     @Override
     public ProviderOutcome provide(String identifier, RenderContext 
renderContext, Bindings arguments) {
         if (!JAVA_PATTERN.matcher(identifier).matches()) {
             LOG.debug("Identifier {} does not match a Java class name 
pattern.", identifier);
             return ProviderOutcome.failure();
         }
+
         Bindings globalBindings = renderContext.getBindings();
         Bindings bindings = UseProviderUtils.merge(globalBindings, arguments);
-        SlingScriptHelper sling = (SlingScriptHelper) 
bindings.get(SlingBindings.SLING);
+        SlingScriptHelper sling = UseProviderUtils.getHelper(bindings);
         Resource resource = (Resource) bindings.get(SlingBindings.RESOURCE);
         final SlingHttpServletRequest request = (SlingHttpServletRequest) 
bindings.get(SlingBindings.REQUEST);
         Map<String, Object> overrides = setRequestAttributes(request, 
arguments);
-        
-        DynamicClassLoaderManager classLoaderManager = 
sling.getService(DynamicClassLoaderManager.class);
-        Object obj;
+
+        Object result;
         try {
-            Class<?> cls = 
classLoaderManager.getDynamicClassLoader().loadClass(identifier);
-            obj = resource.adaptTo(cls);
-            if (obj == null) {
-                obj = request.adaptTo(cls);
+            Class<?> cls = 
dynamicClassLoaderManager.getDynamicClassLoader().loadClass(identifier);
+            result = resource.adaptTo(cls);
+            if (result == null) {
+                result = request.adaptTo(cls);
+            }
+            if (result != null) {
+                return ProviderOutcome.success(result);
+            } else {
+                /**
+                 * the object was cached by the classloader but it's not 
adaptable from {@link Resource} or {@link
+                 * SlingHttpServletRequest}; attempt to load it like a regular 
POJO that optionally could implement {@link Use}
+                 */
+                result = cls.newInstance();
+                if (result instanceof Use) {
+                    ((Use) result).init(bindings);
+                }
+                return ProviderOutcome.notNullOrFailure(result);
             }
-            return ProviderOutcome.success(obj);
         } catch (ClassNotFoundException e) {
+            // this object might not be exported from a bundle; let's try to 
load it from the repository
+            return getPOJOFromRepository(renderContext, sling, identifier, 
bindings);
+        } catch (Exception e) {
+            // any other exception is an error
             return ProviderOutcome.failure(e);
         } finally {
             resetRequestAttribute(request, overrides);
         }
     }
 
+    private ProviderOutcome getPOJOFromRepository(RenderContext renderContext, 
SlingScriptHelper sling, String identifier, Bindings bindings) {
+        try {
+            ResourceResolver adminResolver = 
renderContext.getScriptResourceResolver();
+            Resource resource = 
ResourceResolution.getResourceForRequest(adminResolver, sling.getRequest());
+            Object result = 
sightlyJavaCompilerService.getInstance(adminResolver, resource, identifier);
+            if (result instanceof Use) {
+                ((Use) result).init(bindings);
+            }
+            return ProviderOutcome.success(result);
+        } catch (Exception e) {
+            if (e instanceof CompilerException) {
+                return ProviderOutcome.failure(e);
+            } else {
+                return ProviderOutcome.failure();
+            }
+        }
+    }
+
     private Map<String, Object> setRequestAttributes(ServletRequest request, 
Bindings arguments) {
         Map<String, Object> overrides = new HashMap<String, Object>();
         for (Map.Entry<String, Object> entry : arguments.entrySet()) {
@@ -126,5 +165,4 @@ public class ClassUseProvider implements
     }
 
     private static final Object NULL = new Object();
-
 }

Modified: 
sling/trunk/contrib/scripting/sightly/js-use-provider/src/main/java/org/apache/sling/scripting/sightly/js/impl/JsUseProvider.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/js-use-provider/src/main/java/org/apache/sling/scripting/sightly/js/impl/JsUseProvider.java?rev=1659520&r1=1659519&r2=1659520&view=diff
==============================================================================
--- 
sling/trunk/contrib/scripting/sightly/js-use-provider/src/main/java/org/apache/sling/scripting/sightly/js/impl/JsUseProvider.java
 (original)
+++ 
sling/trunk/contrib/scripting/sightly/js-use-provider/src/main/java/org/apache/sling/scripting/sightly/js/impl/JsUseProvider.java
 Fri Feb 13 11:40:30 2015
@@ -61,7 +61,7 @@ import org.slf4j.LoggerFactory;
                 label = "Service Ranking",
                 description = "The Service Ranking value acts as the priority 
with which this Use Provider is queried to return an " +
                         "Use-object. A higher value represents a higher 
priority.",
-                intValue = 90,
+                intValue = 80,
                 propertyPrivate = false
         )
 })

Modified: sling/trunk/contrib/scripting/sightly/testing-content/pom.xml
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/testing-content/pom.xml?rev=1659520&r1=1659519&r2=1659520&view=diff
==============================================================================
--- sling/trunk/contrib/scripting/sightly/testing-content/pom.xml (original)
+++ sling/trunk/contrib/scripting/sightly/testing-content/pom.xml Fri Feb 13 
11:40:30 2015
@@ -123,6 +123,7 @@
                 <configuration>
                     <instructions>
                         
<Sling-Initial-Content>SLING-INF;overwrite=true</Sling-Initial-Content>
+                        
<Sling-Model-Packages>org.apache.sling.scripting.sightly.testing.models</Sling-Model-Packages>
                     </instructions>
                 </configuration>
             </plugin>
@@ -152,7 +153,38 @@
                     </execution>
                 </executions>
             </plugin>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-scr-plugin</artifactId>
+                <dependencies>
+                    <dependency>
+                        <groupId>javax.jcr</groupId>
+                        <artifactId>jcr</artifactId>
+                        <version>2.0</version>
+                    </dependency>
+                </dependencies>
+            </plugin>
         </plugins>
     </build>
 
+    <dependencies>
+        <!-- Test bundle dependencies -->
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>servlet-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.models.api</artifactId>
+            <version>1.1.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.api</artifactId>
+            <version>2.1.0</version>
+            <scope>provided</scope>
+        </dependency>
+    </dependencies>
+
 </project>

Added: 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/RequestAdapterUseObject.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/RequestAdapterUseObject.java?rev=1659520&view=auto
==============================================================================
--- 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/RequestAdapterUseObject.java
 (added)
+++ 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/RequestAdapterUseObject.java
 Fri Feb 13 11:40:30 2015
@@ -0,0 +1,25 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ 
******************************************************************************/
+package org.apache.sling.scripting.sightly.testing.adaptable;
+
+public interface RequestAdapterUseObject {
+
+    public String getTitle();
+
+}

Added: 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/ResourceAdapterUseObject.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/ResourceAdapterUseObject.java?rev=1659520&view=auto
==============================================================================
--- 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/ResourceAdapterUseObject.java
 (added)
+++ 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/ResourceAdapterUseObject.java
 Fri Feb 13 11:40:30 2015
@@ -0,0 +1,25 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ 
******************************************************************************/
+package org.apache.sling.scripting.sightly.testing.adaptable;
+
+public interface ResourceAdapterUseObject {
+
+    public String getTitle();
+
+}

Added: 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/RequestAdapterFactory.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/RequestAdapterFactory.java?rev=1659520&view=auto
==============================================================================
--- 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/RequestAdapterFactory.java
 (added)
+++ 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/RequestAdapterFactory.java
 Fri Feb 13 11:40:30 2015
@@ -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.scripting.sightly.testing.adaptable.impl;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Properties;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.adapter.AdapterFactory;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ValueMap;
+import 
org.apache.sling.scripting.sightly.testing.adaptable.RequestAdapterUseObject;
+
+@Component
+@Service
+@Properties({
+        @Property(
+                name = AdapterFactory.ADAPTABLE_CLASSES,
+                value = {
+                        "org.apache.sling.api.SlingHttpServletRequest"
+                }
+        ),
+        @Property(
+                name = AdapterFactory.ADAPTER_CLASSES,
+                value = {
+                        
"org.apache.sling.scripting.sightly.testing.adaptable.RequestAdapterUseObject"
+                }
+        )
+})
+public class RequestAdapterFactory implements AdapterFactory {
+
+    @Override
+    public <AdapterType> AdapterType getAdapter(Object adaptable, 
Class<AdapterType> type) {
+        if (type == RequestAdapterUseObject.class && adaptable instanceof 
SlingHttpServletRequest) {
+            SlingHttpServletRequest request = (SlingHttpServletRequest) 
adaptable;
+            Resource resource = request.getResource();
+            ValueMap properties = resource.adaptTo(ValueMap.class);
+            String title = (String) properties.get("jcr:title");
+            return (AdapterType) new RequestAdapterUseObjectImpl(title);
+        }
+        return null;
+    }
+}

Added: 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/RequestAdapterUseObjectImpl.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/RequestAdapterUseObjectImpl.java?rev=1659520&view=auto
==============================================================================
--- 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/RequestAdapterUseObjectImpl.java
 (added)
+++ 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/RequestAdapterUseObjectImpl.java
 Fri Feb 13 11:40:30 2015
@@ -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.scripting.sightly.testing.adaptable.impl;
+
+import 
org.apache.sling.scripting.sightly.testing.adaptable.RequestAdapterUseObject;
+
+public class RequestAdapterUseObjectImpl implements RequestAdapterUseObject {
+
+    private String title;
+
+    public RequestAdapterUseObjectImpl(String title) {
+        this.title = title;
+    }
+
+    @Override
+    public String getTitle() {
+        return title != null ? title : "FAILED";
+    }
+}

Added: 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/ResourceAdapterFactory.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/ResourceAdapterFactory.java?rev=1659520&view=auto
==============================================================================
--- 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/ResourceAdapterFactory.java
 (added)
+++ 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/ResourceAdapterFactory.java
 Fri Feb 13 11:40:30 2015
@@ -0,0 +1,58 @@
+/*******************************************************************************
+ * 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.scripting.sightly.testing.adaptable.impl;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Properties;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.adapter.AdapterFactory;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ValueMap;
+import 
org.apache.sling.scripting.sightly.testing.adaptable.ResourceAdapterUseObject;
+
+@Component
+@Service
+@Properties({
+        @Property(
+                name = AdapterFactory.ADAPTABLE_CLASSES,
+                value = {
+                        "org.apache.sling.api.resource.Resource"
+                }
+        ),
+        @Property(
+                name = AdapterFactory.ADAPTER_CLASSES,
+                value = {
+                        
"org.apache.sling.scripting.sightly.testing.adaptable.ResourceAdapterUseObject"
+                }
+        )
+})
+public class ResourceAdapterFactory implements AdapterFactory {
+
+    @Override
+    public <AdapterType> AdapterType getAdapter(Object adaptable, 
Class<AdapterType> type) {
+        if (type == ResourceAdapterUseObject.class && adaptable instanceof 
Resource) {
+            Resource resource = (Resource) adaptable;
+            ValueMap properties = resource.adaptTo(ValueMap.class);
+            String title = (String) properties.get("jcr:title");
+            return (AdapterType) new ResourceAdapterUseObjectImpl(title);
+        }
+        return null;
+    }
+}

Added: 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/ResourceAdapterUseObjectImpl.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/ResourceAdapterUseObjectImpl.java?rev=1659520&view=auto
==============================================================================
--- 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/ResourceAdapterUseObjectImpl.java
 (added)
+++ 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/ResourceAdapterUseObjectImpl.java
 Fri Feb 13 11:40:30 2015
@@ -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.scripting.sightly.testing.adaptable.impl;
+
+import 
org.apache.sling.scripting.sightly.testing.adaptable.ResourceAdapterUseObject;
+
+public class ResourceAdapterUseObjectImpl implements ResourceAdapterUseObject {
+
+    private String title;
+
+    public ResourceAdapterUseObjectImpl(String title) {
+        this.title = title;
+    }
+
+    @Override
+    public String getTitle() {
+        return title != null ? title : "FAILED";
+    }
+}

Added: 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/models/RequestModel.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/models/RequestModel.java?rev=1659520&view=auto
==============================================================================
--- 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/models/RequestModel.java
 (added)
+++ 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/models/RequestModel.java
 Fri Feb 13 11:40:30 2015
@@ -0,0 +1,40 @@
+/*******************************************************************************
+ * 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.scripting.sightly.testing.models;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.models.annotations.Model;
+import org.apache.sling.models.annotations.Via;
+
+@Model(adaptables = SlingHttpServletRequest.class)
+public class RequestModel {
+
+    @Inject
+    @Via("resource")
+    @Named("jcr:title")
+    private String title;
+
+    public String getTitle() {
+        return title != null ? title : "FAILED";
+    }
+
+}

Added: 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/models/ResourceModel.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/models/ResourceModel.java?rev=1659520&view=auto
==============================================================================
--- 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/models/ResourceModel.java
 (added)
+++ 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/models/ResourceModel.java
 Fri Feb 13 11:40:30 2015
@@ -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.scripting.sightly.testing.models;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.models.annotations.Model;
+
+@Model(adaptables = Resource.class)
+public class ResourceModel {
+
+    @Inject
+    @Named("jcr:title")
+    private String title;
+
+    public String getTitle() {
+        return title != null ? title : "FAILED";
+    }
+}

Added: 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/use/use.html
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/use/use.html?rev=1659520&view=auto
==============================================================================
--- 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/use/use.html
 (added)
+++ 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/use/use.html
 Fri Feb 13 11:40:30 2015
@@ -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.
+  
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
+<!DOCTYPE html>
+<html>
+    <head>
+        <title>Sightly Use-API - Sling implementation</title>
+    </head>
+    <body>
+        <div id="reqmodel" 
data-sly-use.reqmodel="org.apache.sling.scripting.sightly.testing.models.RequestModel">${reqmodel.title}</div>
+        <div id="resmodel" 
data-sly-use.resmodel="org.apache.sling.scripting.sightly.testing.models.ResourceModel">${resmodel.title}</div>
+        <div id="reqadapt"
+             
data-sly-use.reqadapt="org.apache.sling.scripting.sightly.testing.adaptable.RequestAdapterUseObject">${reqadapt.title}</div>
+        <div id="resadapt"
+             
data-sly-use.resadapt="org.apache.sling.scripting.sightly.testing.adaptable.ResourceAdapterUseObject">${resadapt.title}</div>
+    </body>
+</html>
\ No newline at end of file

Added: 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/resources/SLING-INF/sightly.json
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/testing-content/src/main/resources/SLING-INF/sightly.json?rev=1659520&view=auto
==============================================================================
--- 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/resources/SLING-INF/sightly.json
 (added)
+++ 
sling/trunk/contrib/scripting/sightly/testing-content/src/main/resources/SLING-INF/sightly.json
 Fri Feb 13 11:40:30 2015
@@ -0,0 +1,8 @@
+{
+    "jcr:primaryType": "sling:Folder",
+    "use": {
+        "jcr:primaryType": "nt:unstructured",
+        "sling:resourceType": "/apps/sightly/scripts/use",
+        "jcr:title": "SUCCESS"
+    }
+}

Modified: sling/trunk/contrib/scripting/sightly/testing/pom.xml
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/testing/pom.xml?rev=1659520&r1=1659519&r2=1659520&view=diff
==============================================================================
--- sling/trunk/contrib/scripting/sightly/testing/pom.xml (original)
+++ sling/trunk/contrib/scripting/sightly/testing/pom.xml Fri Feb 13 11:40:30 
2015
@@ -118,6 +118,7 @@
                 <configuration>
                     <includes>
                         <include>%regex[.*LaunchpadReadyIT.*]</include>
+                        <include>%regex[.*SlingSpecificsSightlyIT.*]</include>
                         <include>%regex[.*TestsRunner.*]</include>
                     </includes>
                     <dependenciesToScan>

Added: 
sling/trunk/contrib/scripting/sightly/testing/src/test/java/org/apache/sling/scripting/sightly/it/SlingSpecificsSightlyIT.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/testing/src/test/java/org/apache/sling/scripting/sightly/it/SlingSpecificsSightlyIT.java?rev=1659520&view=auto
==============================================================================
--- 
sling/trunk/contrib/scripting/sightly/testing/src/test/java/org/apache/sling/scripting/sightly/it/SlingSpecificsSightlyIT.java
 (added)
+++ 
sling/trunk/contrib/scripting/sightly/testing/src/test/java/org/apache/sling/scripting/sightly/it/SlingSpecificsSightlyIT.java
 Fri Feb 13 11:40:30 2015
@@ -0,0 +1,58 @@
+/*******************************************************************************
+ * 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.scripting.sightly.it;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import io.sightly.tck.http.Client;
+import io.sightly.tck.html.HTMLExtractor;
+
+import static org.junit.Assert.assertEquals;
+
+public class SlingSpecificsSightlyIT {
+
+    private static Client client;
+    private static String launchpadURL;
+    private static final String SLING_USE = "/sightly/use.html";
+
+    @BeforeClass
+    public static void init() {
+        client = new Client();
+        launchpadURL = System.getProperty("launchpad.http.server.url");
+    }
+
+    @Test
+    public void testSlingModelsUseAPI() {
+        String url = launchpadURL + SLING_USE;
+        String pageContent = client.getStringContent(url, 200);
+        assertEquals("SUCCESS", HTMLExtractor.innerHTML(url, pageContent, 
"#reqmodel"));
+        assertEquals("SUCCESS", HTMLExtractor.innerHTML(url, pageContent, 
"#resmodel"));
+        
+    }
+    
+    @Test
+    public void testAdaptablesUseAPI() {
+        String url = launchpadURL + SLING_USE;
+        String pageContent = client.getStringContent(url, 200);
+        assertEquals("SUCCESS", HTMLExtractor.innerHTML(url, pageContent, 
"#resadapt"));
+        assertEquals("SUCCESS", HTMLExtractor.innerHTML(url, pageContent, 
"#reqadapt"));
+    }
+
+}


Reply via email to