From 22fadc22a71a843652bcb94cd276710d99bc44e4 Mon Sep 17 00:00:00 2001 From: Feike Visser Date: Wed, 21 Jun 2017 11:56:59 +0200 Subject: [PATCH 1/4] SLING-6504 implementing custom adaptable in Use-api --- .../engine/extension/use/JavaUseProvider.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/JavaUseProvider.java b/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/JavaUseProvider.java index 65c34a8cba..4053434adc 100644 --- a/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/JavaUseProvider.java +++ b/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/JavaUseProvider.java @@ -24,6 +24,8 @@ import javax.script.Bindings; import javax.servlet.ServletRequest; +import org.apache.sling.api.adapter.*; + import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.scripting.SlingScriptHelper; @@ -96,6 +98,27 @@ public ProviderOutcome provide(String identifier, RenderContext renderContext, B } else { LOG.debug("Attempting to load class {} from the classloader cache.", identifier); Class cls = classLoaderWriter.getClassLoader().loadClass(identifier); + + if (arguments.containsKey("adaptable")) { + Object adaptable = arguments.get("adaptable"); + LOG.debug("adaptable {}", adaptable); + + if (adaptable instanceof Adaptable ) { + LOG.debug("Object is of type Adaptable"); + try { + Adaptable a = (Adaptable)adaptable; + result = a.adaptTo(cls); + LOG.info("Adapted object {}", result); + } catch (Exception e) { + LOG.error("error during adapting...", e); + } + } + } + + if (result != null) { + return ProviderOutcome.success(result); + } + // attempt OSGi service load result = sling.getService(cls); if (result != null) { From e76ee71fe65613e1e40fc4aebcbdb5ae93ba7f55 Mon Sep 17 00:00:00 2001 From: Feike Visser Date: Wed, 21 Jun 2017 12:05:25 +0200 Subject: [PATCH 2/4] SLING-6504 implementing custom adaptable in Use-api --- .../impl/engine/extension/use/JavaUseProvider.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/JavaUseProvider.java b/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/JavaUseProvider.java index 4053434adc..da0c7883ce 100644 --- a/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/JavaUseProvider.java +++ b/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/JavaUseProvider.java @@ -24,8 +24,7 @@ import javax.script.Bindings; import javax.servlet.ServletRequest; -import org.apache.sling.api.adapter.*; - +import org.apache.sling.api.adapter.Adaptable; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.scripting.SlingScriptHelper; @@ -99,7 +98,7 @@ public ProviderOutcome provide(String identifier, RenderContext renderContext, B LOG.debug("Attempting to load class {} from the classloader cache.", identifier); Class cls = classLoaderWriter.getClassLoader().loadClass(identifier); - if (arguments.containsKey("adaptable")) { + if (arguments.containsKey("adaptable")) { Object adaptable = arguments.get("adaptable"); LOG.debug("adaptable {}", adaptable); @@ -113,11 +112,11 @@ public ProviderOutcome provide(String identifier, RenderContext renderContext, B LOG.error("error during adapting...", e); } } - } + } - if (result != null) { - return ProviderOutcome.success(result); - } + if (result != null) { + return ProviderOutcome.success(result); + } // attempt OSGi service load result = sling.getService(cls); From bc709cf794c31c8bb462ebc13845c2609df15692 Mon Sep 17 00:00:00 2001 From: Feike Visser Date: Wed, 28 Jun 2017 10:26:16 +0200 Subject: [PATCH 3/4] Adding tests --- .../ResourceResolverAdapterUseObject.java | 25 +++++++++ .../impl/ResourceResolverAdapterFactory.java | 56 +++++++++++++++++++ .../ResourceResolverAdapterUseObjectImpl.java | 35 ++++++++++++ .../testing/adaptable/package-info.java | 2 +- 4 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/ResourceResolverAdapterUseObject.java create mode 100644 bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/ResourceResolverAdapterFactory.java create mode 100644 bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/ResourceResolverAdapterUseObjectImpl.java diff --git a/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/ResourceResolverAdapterUseObject.java b/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/ResourceResolverAdapterUseObject.java new file mode 100644 index 0000000000..ec53c07b8b --- /dev/null +++ b/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/ResourceResolverAdapterUseObject.java @@ -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 ResourceResolverAdapterUseObject { + + public String getTitle(); + +} diff --git a/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/ResourceResolverAdapterFactory.java b/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/ResourceResolverAdapterFactory.java new file mode 100644 index 0000000000..a373f9d0c8 --- /dev/null +++ b/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/ResourceResolverAdapterFactory.java @@ -0,0 +1,56 @@ +/******************************************************************************* + * 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.ResourceResolver; +import org.apache.sling.api.resource.ValueMap; +import org.apache.sling.scripting.sightly.testing.adaptable.ResourceResolverAdapterUseObject; + +@Component +@Service +@Properties({ + @Property( + name = AdapterFactory.ADAPTABLE_CLASSES, + value = { + "org.apache.sling.api.resource.ResourceResolver" + } + ), + @Property( + name = AdapterFactory.ADAPTER_CLASSES, + value = { + "org.apache.sling.scripting.sightly.testing.adaptable.ResourceResolverAdapterUseObject" + } + ) +}) +public class ResourceResolverAdapterFactory implements AdapterFactory { + + @Override + public AdapterType getAdapter(Object adaptable, Class type) { + if (type == ResourceResolverAdapterUseObject.class && adaptable instanceof ResourceResolver) { + String title = "test"; + return (AdapterType) new ResourceResolverAdapterUseObjectImpl(title); + } + return null; + } +} diff --git a/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/ResourceResolverAdapterUseObjectImpl.java b/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/ResourceResolverAdapterUseObjectImpl.java new file mode 100644 index 0000000000..e28ddeba5d --- /dev/null +++ b/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/ResourceResolverAdapterUseObjectImpl.java @@ -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 ResourceResolverAdapterUseObjectImpl implements ResourceAdapterUseObject { + + private String title; + + public ResourceResolverAdapterUseObjectImpl(String title) { + this.title = title; + } + + @Override + public String getTitle() { + return title != null ? title : "FAILED"; + } +} diff --git a/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/package-info.java b/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/package-info.java index 1647401e5f..23488f7d30 100644 --- a/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/package-info.java +++ b/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/package-info.java @@ -15,7 +15,7 @@ * limitations under the License. ******************************************************************************/ -@Version("1.0.4") +@Version("1.1.0") package org.apache.sling.scripting.sightly.testing.adaptable; import org.osgi.annotation.versioning.Version; From 1293d332d4ebeff5679cd5709b394d5de5ed037c Mon Sep 17 00:00:00 2001 From: Feike Visser Date: Wed, 28 Jun 2017 11:01:52 +0200 Subject: [PATCH 4/4] Adding tests for SLING-6504 --- .../adaptable/impl/ResourceResolverAdapterFactory.java | 4 ++-- .../adaptable/impl/ResourceResolverAdapterUseObjectImpl.java | 4 ++-- .../resources/SLING-INF/apps/sightly/scripts/use/use.html | 2 ++ .../sling/scripting/sightly/it/SlingSpecificsSightlyIT.java | 1 + 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/ResourceResolverAdapterFactory.java b/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/ResourceResolverAdapterFactory.java index a373f9d0c8..55d354d1bb 100644 --- a/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/ResourceResolverAdapterFactory.java +++ b/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/ResourceResolverAdapterFactory.java @@ -48,8 +48,8 @@ public class ResourceResolverAdapterFactory implements AdapterFactory { @Override public AdapterType getAdapter(Object adaptable, Class type) { if (type == ResourceResolverAdapterUseObject.class && adaptable instanceof ResourceResolver) { - String title = "test"; - return (AdapterType) new ResourceResolverAdapterUseObjectImpl(title); + String title = "SUCCESS"; + return (AdapterType) new ResourceResolverAdapterUseObjectImpl(title); } return null; } diff --git a/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/ResourceResolverAdapterUseObjectImpl.java b/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/ResourceResolverAdapterUseObjectImpl.java index e28ddeba5d..cdc6170acd 100644 --- a/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/ResourceResolverAdapterUseObjectImpl.java +++ b/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/adaptable/impl/ResourceResolverAdapterUseObjectImpl.java @@ -18,9 +18,9 @@ ******************************************************************************/ package org.apache.sling.scripting.sightly.testing.adaptable.impl; -import org.apache.sling.scripting.sightly.testing.adaptable.ResourceAdapterUseObject; +import org.apache.sling.scripting.sightly.testing.adaptable.ResourceResolverAdapterUseObject; -public class ResourceResolverAdapterUseObjectImpl implements ResourceAdapterUseObject { +public class ResourceResolverAdapterUseObjectImpl implements ResourceResolverAdapterUseObject { private String title; diff --git a/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/use/use.html b/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/use/use.html index a64f9b61ca..255c846806 100644 --- a/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/use/use.html +++ b/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/use/use.html @@ -25,6 +25,7 @@ data-sly-use.resmodel="org.apache.sling.scripting.sightly.testing.models.ResourceModel" data-sly-use.reqadapt="org.apache.sling.scripting.sightly.testing.adaptable.RequestAdapterUseObject" data-sly-use.resadapt="org.apache.sling.scripting.sightly.testing.adaptable.ResourceAdapterUseObject" + data-sly-use.rradapt="${'org.apache.sling.scripting.sightly.testing.adaptable.ResourceResolverAdapterUseObject' @ adaptable = request.resourceResolver}" data-sly-use.osgi="org.apache.sling.scripting.sightly.testing.use.TestService" data-sly-use.repoNoPkg="RepoPojoNoPkg" data-sly-use.testEnum="TestEnumHolder"> @@ -37,5 +38,6 @@
${osgi.sayHello}
${repoNoPkg.FIELD}
${testEnum.constantValue}
+
${rradapt.title}
diff --git a/bundles/scripting/sightly/testing/src/test/java/org/apache/sling/scripting/sightly/it/SlingSpecificsSightlyIT.java b/bundles/scripting/sightly/testing/src/test/java/org/apache/sling/scripting/sightly/it/SlingSpecificsSightlyIT.java index a906b05871..f7d49fd67f 100644 --- a/bundles/scripting/sightly/testing/src/test/java/org/apache/sling/scripting/sightly/it/SlingSpecificsSightlyIT.java +++ b/bundles/scripting/sightly/testing/src/test/java/org/apache/sling/scripting/sightly/it/SlingSpecificsSightlyIT.java @@ -88,6 +88,7 @@ public void testAdaptablesUseAPI() { String pageContent = client.getStringContent(url, 200); assertEquals("SUCCESS", HTMLExtractor.innerHTML(url, pageContent, "#resadapt")); assertEquals("SUCCESS", HTMLExtractor.innerHTML(url, pageContent, "#reqadapt")); + assertEquals("SUCCESS", HTMLExtractor.innerHTML(url, pageContent, "#rradapt")); } @Test