This is an automated email from the ASF dual-hosted git repository. andysch pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git
commit b1e3a748485acd56fc918eac05cc0fe6008eec8c Author: Andreas Schaefer <[email protected]> AuthorDate: Sun Mar 28 21:27:12 2021 -0700 Fixed an issue with the Event Listener and added another sample (Sample 2) that can be installed after DDR is instaleld to see how it is working when a DDR is configured and provided afterwards. --- .../DeclarativeDynamicResourceManagerService.java | 26 ++++--- org.apache.sling.ddr/sample/Readme.md | 2 +- org.apache.sling.ddr/sample2/Readme.md | 24 ++++++ org.apache.sling.ddr/sample2/pom.xml | 85 ++++++++++++++++++++++ .../resources/SLING-CONTENT/apps/ddr-after.json | 3 + .../SLING-CONTENT/apps/ddr-after/components.json | 5 ++ .../resources/SLING-CONTENT/apps/ddr-static2.json | 3 + .../SLING-CONTENT/apps/ddr-static2/button.json | 3 + .../apps/ddr-static2/button/button.html | 19 +++++ .../SLING-CONTENT/apps/ddr-static2/text.json | 3 + .../SLING-CONTENT/apps/ddr-static2/text/text.html | 19 +++++ .../resources/SLING-CONTENT/conf/ddr-after.json | 4 + .../SLING-CONTENT/conf/ddr-after/settings.json | 23 ++++++ .../resources/SLING-CONTENT/content/ddr-after.json | 3 + .../SLING-CONTENT/content/ddr-after/button.json | 4 + .../SLING-CONTENT/content/ddr-after/text.json | 4 + 16 files changed, 219 insertions(+), 11 deletions(-) diff --git a/org.apache.sling.ddr/core/src/main/java/org/apache/sling/ddr/core/DeclarativeDynamicResourceManagerService.java b/org.apache.sling.ddr/core/src/main/java/org/apache/sling/ddr/core/DeclarativeDynamicResourceManagerService.java index fb7b447..b88c33b 100644 --- a/org.apache.sling.ddr/core/src/main/java/org/apache/sling/ddr/core/DeclarativeDynamicResourceManagerService.java +++ b/org.apache.sling.ddr/core/src/main/java/org/apache/sling/ddr/core/DeclarativeDynamicResourceManagerService.java @@ -37,7 +37,6 @@ import org.slf4j.LoggerFactory; import javax.jcr.RepositoryException; import javax.jcr.Session; -import javax.jcr.UnsupportedRepositoryOperationException; import javax.jcr.observation.Event; import javax.jcr.observation.EventIterator; import javax.jcr.observation.EventListener; @@ -85,22 +84,24 @@ public class DeclarativeDynamicResourceManagerService private Map<String, DeclarativeDynamicResourceProvider> registeredServices = new HashMap<>(); private BundleContext bundleContext; -// private String dynamicTargetPath; + // Keep the Resource Resolver around otherwise the Event Listener will not work anymore + private ResourceResolver resourceResolver; @Activate private void activate(BundleContext bundleContext) { this.bundleContext = bundleContext; log.info("Activate Started, bundle context: '{}'", bundleContext); - try (ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver( - new HashMap<String, Object>() {{ put(ResourceResolverFactory.SUBSERVICE, DYNAMIC_COMPONENTS_SERVICE_USER); }} - )) { + try { + resourceResolver = resourceResolverFactory.getServiceResourceResolver( + new HashMap<String, Object>() {{ put(ResourceResolverFactory.SUBSERVICE, DYNAMIC_COMPONENTS_SERVICE_USER); }} + ); // Register an Event Listener to get informed when Session session = resourceResolver.adaptTo(Session.class); if (session != null) { log.info("Register Event Listener on Path: '{}'", CONFIGURATION_ROOT_PATH); session.getWorkspace().getObservationManager().addEventListener( this, EVENT_TYPES, CONFIGURATION_ROOT_PATH, - true, null, null, true + true, null, null, false ); } else { log.warn("Resource Resolver could not be adapted to Session"); @@ -120,11 +121,9 @@ public class DeclarativeDynamicResourceManagerService } } } catch (LoginException e) { - log.error("Failed to Activation Resource", e); - } catch (UnsupportedRepositoryOperationException e) { - log.error("Failed to Activation Resource", e); + log.error("Unable to obtain our Service Resource Resolver --> DDR disabled", e); } catch (RepositoryException e) { - log.error("Failed to Activation Resource", e); + log.error("Failed to Obtain the Observation Manager to register for Resource Events --> DDR disabled", e); } } @@ -181,16 +180,21 @@ public class DeclarativeDynamicResourceManagerService service.unregisterService(); log.info("After UnRegistering Tenant RP, service: '{}'", service); } + if(resourceResolver != null) { + resourceResolver.close(); + } } @Override public void onEvent(EventIterator events) { + log.info("Handle Events: '{}'", events); try (ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver( new HashMap<String, Object>() {{ put(ResourceResolverFactory.SUBSERVICE, DYNAMIC_COMPONENTS_SERVICE_USER); }} )) { while (events.hasNext()) { Event event = events.nextEvent(); String path = event.getPath(); + log.info("Handle Event: '{}', path: '{}', type: '{}'", event, path, event.getType()); switch (event.getType()) { case Event.PROPERTY_ADDED: case Event.PROPERTY_CHANGED: @@ -198,8 +202,10 @@ public class DeclarativeDynamicResourceManagerService if(index > 0) { path = path.substring(0, index -1); } + log.info("Property Added or Changed, path: '{}'", path); case Event.NODE_ADDED: Resource source = resourceResolver.getResource(path); + log.info("Source Resource found: '{}'", source); if(source != null) { handleDDRResource(source); } diff --git a/org.apache.sling.ddr/sample/Readme.md b/org.apache.sling.ddr/sample/Readme.md index fb66989..80f8cf8 100644 --- a/org.apache.sling.ddr/sample/Readme.md +++ b/org.apache.sling.ddr/sample/Readme.md @@ -32,6 +32,6 @@ Try this by going to the System Console **Bundles** and stop the **org.apache.sl Refershing the **/apps/ddr-dynamic** folder in composum will not display any child resources. Restart the bundle now again and make sure the child resources are there again. Now we want to see these components but open the content on **/ddr-sample/button.html**. This will -show *Hello Button: * followed by the path of the resource. +show *Hello Button* or *Hello Text*. Andreas Schaefer: 3/27/2021 \ No newline at end of file diff --git a/org.apache.sling.ddr/sample2/Readme.md b/org.apache.sling.ddr/sample2/Readme.md new file mode 100644 index 0000000..ce94b7e --- /dev/null +++ b/org.apache.sling.ddr/sample2/Readme.md @@ -0,0 +1,24 @@ +# Declarative Dynamic Resource Sample 2 + +This is more or less the same as **Sample** but it is not added to the project so it will +not be built and installed during the build of DDR. +The Goal of this project is to showcase the dynamic nature of the DDR registration so that a +user can register new DDRs even when Sling is started and DDR project is installed. + +## Design + +These are the components of the Sample 2: + +* **/apps/ddr-after/components**: the DDR target folder which is empty +* **/apps/ddr-static2**: the base component **button** and **text** +* **/conf/ddr-after/settings/dynamic**: the source DDR components that points to the DDR target of **/apps/ddr-after/components and contains an updated version of Button and Text +* **/content/ddr-after**: content that show the usage of enhanced Button and Text + +## Review + +This is the same as Sample2. After the installation you will find DDRs in /apps/ddr-after/components +as **button2** and **text2**. +Also opening the component under **/ddr-after/button.html** or **/ddr-after/text.html** you will +a page that will show the actual target found in **/ddr-static2**. + +Andreas Schaefer: 3/28/2021 \ No newline at end of file diff --git a/org.apache.sling.ddr/sample2/pom.xml b/org.apache.sling.ddr/sample2/pom.xml new file mode 100644 index 0000000..7edaecd --- /dev/null +++ b/org.apache.sling.ddr/sample2/pom.xml @@ -0,0 +1,85 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <parent> + <artifactId>org.apache.sling.ddr</artifactId> + <groupId>org.apache.sling</groupId> + <version>1.0.0-SNAPSHOT</version> + <relativePath>../pom.xml</relativePath> + </parent> + + <artifactId>org.apache.sling.ddr.sample2</artifactId> + <packaging>bundle</packaging> + + <name>Declarative Dynamic Resources - Sample 2</name> + <description> + Sample Project to showcase how DDRs are used after the original + deployment of the DDR code. Deploy this manually after the DDR + Code and see that the components are also available on /apps/ddr-after. + </description> + + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <extensions>true</extensions> + <configuration> + <instructions> + <Embed-Dependency>*;scope=compile;inline=true</Embed-Dependency> + <_dsannotations>*</_dsannotations> + <_metatypeannotations>*</_metatypeannotations> + <_removeheaders> + Embed-Dependency, + Private-Package, + Include-Resource + </_removeheaders> + <Sling-Initial-Content> + SLING-CONTENT/apps/ddr-after;path:=/apps/ddr-after;overwrite:=true;overwriteProperties:=true, + SLING-CONTENT/apps/ddr-static2;path:=/apps/ddr-static2;overwrite:=true;overwriteProperties:=true, + SLING-CONTENT/conf/ddr-after;path:=/conf/ddr-after;overwrite:=true;overwriteProperties:=true, + SLING-CONTENT/content/ddr-after;path:=/content/ddr-after;overwrite:=true;overwriteProperties:=true + </Sling-Initial-Content> + </instructions> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.sling</groupId> + <artifactId>maven-sling-plugin</artifactId> + <configuration> + <slingUrl>http://${sling.host}:${sling.port}/system/console</slingUrl> + <user>${sling.user}</user> + <password>${sling.password}</password> + </configuration> + </plugin> + </plugins> + </build> + <dependencies> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>org.apache.sling.ddr.core</artifactId> + <version>${project.version}</version> + <scope>provided</scope> + </dependency> + </dependencies> +</project> diff --git a/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/apps/ddr-after.json b/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/apps/ddr-after.json new file mode 100644 index 0000000..68baa9c --- /dev/null +++ b/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/apps/ddr-after.json @@ -0,0 +1,3 @@ +{ + "jcr:primaryType": "sling:Folder" +} \ No newline at end of file diff --git a/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/apps/ddr-after/components.json b/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/apps/ddr-after/components.json new file mode 100644 index 0000000..b287772 --- /dev/null +++ b/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/apps/ddr-after/components.json @@ -0,0 +1,5 @@ + { + "jcr:description": "Root Folder of the Declarative Dynamic Resources After Installation (Target)", + "jcr:primaryType": "sling:Folder", + "jcr:title": "Declarative Dynamic Resource Folder after Installation" +} \ No newline at end of file diff --git a/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/apps/ddr-static2.json b/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/apps/ddr-static2.json new file mode 100644 index 0000000..68baa9c --- /dev/null +++ b/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/apps/ddr-static2.json @@ -0,0 +1,3 @@ +{ + "jcr:primaryType": "sling:Folder" +} \ No newline at end of file diff --git a/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/apps/ddr-static2/button.json b/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/apps/ddr-static2/button.json new file mode 100644 index 0000000..68baa9c --- /dev/null +++ b/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/apps/ddr-static2/button.json @@ -0,0 +1,3 @@ +{ + "jcr:primaryType": "sling:Folder" +} \ No newline at end of file diff --git a/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/apps/ddr-static2/button/button.html b/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/apps/ddr-static2/button/button.html new file mode 100644 index 0000000..e1364e2 --- /dev/null +++ b/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/apps/ddr-static2/button/button.html @@ -0,0 +1,19 @@ +<!-- + 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. +--> +<p>After Hello Button</p> \ No newline at end of file diff --git a/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/apps/ddr-static2/text.json b/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/apps/ddr-static2/text.json new file mode 100644 index 0000000..68baa9c --- /dev/null +++ b/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/apps/ddr-static2/text.json @@ -0,0 +1,3 @@ +{ + "jcr:primaryType": "sling:Folder" +} \ No newline at end of file diff --git a/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/apps/ddr-static2/text/text.html b/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/apps/ddr-static2/text/text.html new file mode 100644 index 0000000..f58e318 --- /dev/null +++ b/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/apps/ddr-static2/text/text.html @@ -0,0 +1,19 @@ +<!-- + 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. +--> +<p>After Hello Text</p> \ No newline at end of file diff --git a/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/conf/ddr-after.json b/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/conf/ddr-after.json new file mode 100644 index 0000000..17bebfc --- /dev/null +++ b/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/conf/ddr-after.json @@ -0,0 +1,4 @@ +{ + "jcr:primaryType": "sling:Folder", + "jcr:title": "DDR Sample Configuration Folder After Installation" +} \ No newline at end of file diff --git a/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/conf/ddr-after/settings.json b/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/conf/ddr-after/settings.json new file mode 100644 index 0000000..7a3d594 --- /dev/null +++ b/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/conf/ddr-after/settings.json @@ -0,0 +1,23 @@ +{ + "jcr:primaryType": "sling:Folder", + "dynamic": { + "jcr:mixinTypes": [ + "rep:AccessControllable" + ], + "jcr:primaryType": "slingddr:Folder", + "slingddr:target": "/apps/ddr-after/components", + "button2": { + "componentGroup": "Dynamic.Content", + "jcr:description": "Dynamic Test Button", + "jcr:primaryType": "sling:Folder", + "jcr:title": "Button-2", + "sling:resourceSuperType": "ddr-static2/button" + }, + "text2": { + "componentGroup": "Dynamic.Content", + "jcr:primaryType": "sling:Folder", + "jcr:title": "Text-2", + "sling:resourceSuperType": "ddr-static2/text" + } + } +} \ No newline at end of file diff --git a/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/content/ddr-after.json b/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/content/ddr-after.json new file mode 100644 index 0000000..68baa9c --- /dev/null +++ b/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/content/ddr-after.json @@ -0,0 +1,3 @@ +{ + "jcr:primaryType": "sling:Folder" +} \ No newline at end of file diff --git a/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/content/ddr-after/button.json b/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/content/ddr-after/button.json new file mode 100644 index 0000000..78ec080 --- /dev/null +++ b/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/content/ddr-after/button.json @@ -0,0 +1,4 @@ +{ + "jcr:primaryType": "sling:Folder", + "sling:resourceSuperType": "ddr-after/components/button2" +} diff --git a/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/content/ddr-after/text.json b/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/content/ddr-after/text.json new file mode 100644 index 0000000..830c94b --- /dev/null +++ b/org.apache.sling.ddr/sample2/src/main/resources/SLING-CONTENT/content/ddr-after/text.json @@ -0,0 +1,4 @@ +{ + "jcr:primaryType": "sling:Folder", + "sling:resourceSuperType": "ddr-after/components/text2" +}
