You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 10:24:44 UTC

[sling-org-apache-sling-validation-test-services] 19/33: use OSGi R6 annotations and add resource presence (wip)

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.validation.test-services-1.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-validation-test-services.git

commit 7fa5eef75af9bb79a8dacceb0ece5fc13204fa77
Author: Oliver Lietz <ol...@apache.org>
AuthorDate: Sun Feb 26 18:52:27 2017 +0000

    use OSGi R6 annotations and add resource presence (wip)
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/validation/test-services@1784469 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  25 +---
 .../validation/testservices/ResourcePresence.java  |  26 ++++
 .../testservices/internal/ResourcePresenter.java   | 131 +++++++++++++++++++++
 .../{ => internal}/ValidationPostOperation.java    |  23 ++--
 .../{ => internal}/ValidationPostResponse.java     |   2 +-
 .../ValidationPostResponseCreator.java             |  19 ++-
 6 files changed, 182 insertions(+), 44 deletions(-)

diff --git a/pom.xml b/pom.xml
index 9102cc5..d386827 100644
--- a/pom.xml
+++ b/pom.xml
@@ -44,21 +44,11 @@
         <plugins>
             <plugin>
                 <groupId>org.apache.felix</groupId>
-                <artifactId>maven-scr-plugin</artifactId>
-            </plugin>
-            <plugin>
-                <groupId>org.apache.felix</groupId>
                 <artifactId>maven-bundle-plugin</artifactId>
                 <extensions>true</extensions>
                 <configuration>
                     <instructions>
                         <Sling-Initial-Content>SLING-CONTENT;overwrite:=true</Sling-Initial-Content>
-                        <Export-Package>
-                            !org.apache.sling.validation.testservices
-                        </Export-Package>
-                        <Private-Package>
-                            org.apache.sling.validation.testservices
-                        </Private-Package>
                     </instructions>
                 </configuration>
             </plugin>
@@ -82,11 +72,6 @@
             <scope>provided</scope>
         </dependency>
         <dependency>
-            <groupId>org.apache.felix</groupId>
-            <artifactId>org.apache.felix.scr.annotations</artifactId>
-            <scope>provided</scope>
-        </dependency>
-        <dependency>
             <groupId>com.google.code.findbugs</groupId>
             <artifactId>jsr305</artifactId>
             <scope>provided</scope>
@@ -94,7 +79,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.api</artifactId>
-            <version>2.4.2</version>
+            <version>2.11.0</version>
             <scope>provided</scope>
         </dependency>
         <dependency>
@@ -108,6 +93,7 @@
             <groupId>javax.jcr</groupId>
             <artifactId>jcr</artifactId>
             <version>2.0</version>
+            <scope>provided</scope>
         </dependency>
         <dependency>
             <groupId>org.apache.sling</groupId>
@@ -126,12 +112,5 @@
             <artifactId>slf4j-api</artifactId>
             <scope>provided</scope>
         </dependency>
-        <!-- test dependencies -->
-        <dependency>
-            <groupId>org.apache.sling</groupId>
-            <artifactId>org.apache.sling.commons.testing</artifactId>
-            <version>2.0.16</version>
-            <scope>test</scope>
-        </dependency>
     </dependencies>
 </project>
diff --git a/src/main/java/org/apache/sling/validation/testservices/ResourcePresence.java b/src/main/java/org/apache/sling/validation/testservices/ResourcePresence.java
new file mode 100644
index 0000000..6a414a1
--- /dev/null
+++ b/src/main/java/org/apache/sling/validation/testservices/ResourcePresence.java
@@ -0,0 +1,26 @@
+/*
+ * 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.validation.testservices;
+
+import org.osgi.annotation.versioning.ProviderType;
+
+@ProviderType
+public interface ResourcePresence {
+
+}
diff --git a/src/main/java/org/apache/sling/validation/testservices/internal/ResourcePresenter.java b/src/main/java/org/apache/sling/validation/testservices/internal/ResourcePresenter.java
new file mode 100644
index 0000000..7043488
--- /dev/null
+++ b/src/main/java/org/apache/sling/validation/testservices/internal/ResourcePresenter.java
@@ -0,0 +1,131 @@
+/*
+ * 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.validation.testservices.internal;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.List;
+
+import javax.annotation.Nonnull;
+
+import org.apache.sling.api.resource.LoginException;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.api.resource.observation.ResourceChange;
+import org.apache.sling.api.resource.observation.ResourceChangeListener;
+import org.apache.sling.validation.testservices.ResourcePresence;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Component(
+    service = ResourceChangeListener.class,
+    immediate = true,
+    property = {
+        "resource.paths=/apps/sling/validation"
+    }
+)
+public class ResourcePresenter implements ResourceChangeListener {
+
+    @Reference
+    private ResourceResolverFactory resourceResolverFactory;
+
+    private ServiceRegistration<ResourcePresence> model;
+
+    private DefaultResourcePresence resourcePresence = new DefaultResourcePresence("/apps/sling/validation/models/model1");
+
+    private BundleContext bundleContext;
+
+    private final Logger logger = LoggerFactory.getLogger(ResourcePresenter.class);
+
+    @Activate
+    public void activate(final BundleContext bundleContext) {
+        this.bundleContext = bundleContext;
+        try (final ResourceResolver resourceResolver = getServiceResourceResolver()) {
+            final Resource validation = resourceResolver.getResource("/apps/sling/validation/models/model1");
+            if (validation != null) {
+                registerResourcePresence();
+            }
+        } catch (LoginException e) {
+            logger.error(e.getMessage(), e);
+        }
+    }
+
+    @Deactivate
+    public void deactivate(final BundleContext bundleContext) {
+        unregisterResourcePresence();
+    }
+
+    @Override
+    public void onChange(@Nonnull List<ResourceChange> list) {
+        for (ResourceChange resourceChange : list) {
+            logger.info("resource change at {}: {}", resourceChange.getPath(), resourceChange.getType());
+            if (resourcePresence.getPath().equals(resourceChange.getPath())) {
+                switch (resourceChange.getType()) {
+                    case ADDED:
+                        registerResourcePresence();
+                        break;
+                    case REMOVED:
+                        unregisterResourcePresence();
+                        break;
+                    default:
+                        break;
+                }
+            }
+        }
+    }
+
+    private ResourceResolver getServiceResourceResolver() throws LoginException {
+        return resourceResolverFactory.getServiceResourceResolver(null);
+    }
+
+    private void registerResourcePresence() {
+        final Dictionary<String, Object> properties = new Hashtable<>();
+        properties.put("path", resourcePresence.getPath());
+        this.model = bundleContext.registerService(ResourcePresence.class, resourcePresence, properties);
+        logger.info("resource presence for {} registered", resourcePresence.getPath());
+    }
+
+    private void unregisterResourcePresence() {
+        if (model != null) {
+            model.unregister();
+            logger.info("resource presence for {} unregistered", resourcePresence.getPath());
+        }
+    }
+
+    private class DefaultResourcePresence implements ResourcePresence {
+
+        private final String path;
+
+        DefaultResourcePresence(final String path) {
+            this.path = path;
+        }
+
+        public String getPath() {
+            return path;
+        }
+    }
+
+}
diff --git a/src/main/java/org/apache/sling/validation/testservices/ValidationPostOperation.java b/src/main/java/org/apache/sling/validation/testservices/internal/ValidationPostOperation.java
similarity index 87%
rename from src/main/java/org/apache/sling/validation/testservices/ValidationPostOperation.java
rename to src/main/java/org/apache/sling/validation/testservices/internal/ValidationPostOperation.java
index a05008e..a829340 100644
--- a/src/main/java/org/apache/sling/validation/testservices/ValidationPostOperation.java
+++ b/src/main/java/org/apache/sling/validation/testservices/internal/ValidationPostOperation.java
@@ -16,18 +16,13 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.sling.validation.testservices;
+package org.apache.sling.validation.testservices.internal;
 
 import java.util.Enumeration;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
-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.wrappers.ValueMapDecorator;
 import org.apache.sling.servlets.post.AbstractPostOperation;
@@ -37,17 +32,17 @@ import org.apache.sling.servlets.post.PostResponse;
 import org.apache.sling.validation.ValidationResult;
 import org.apache.sling.validation.ValidationService;
 import org.apache.sling.validation.model.ValidationModel;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-@Component()
-@Service(PostOperation.class)
-@Properties({
-    @Property(
-        name = PostOperation.PROP_OPERATION_NAME,
-        value = "validation"
-    )
-})
+@Component(
+    service = PostOperation.class,
+    property = {
+        PostOperation.PROP_OPERATION_NAME + "=validation"
+    }
+)
 public class ValidationPostOperation extends AbstractPostOperation {
 
     private final Logger logger = LoggerFactory.getLogger(ValidationPostOperation.class);
diff --git a/src/main/java/org/apache/sling/validation/testservices/ValidationPostResponse.java b/src/main/java/org/apache/sling/validation/testservices/internal/ValidationPostResponse.java
similarity index 98%
rename from src/main/java/org/apache/sling/validation/testservices/ValidationPostResponse.java
rename to src/main/java/org/apache/sling/validation/testservices/internal/ValidationPostResponse.java
index c023f0e..ed04681 100644
--- a/src/main/java/org/apache/sling/validation/testservices/ValidationPostResponse.java
+++ b/src/main/java/org/apache/sling/validation/testservices/internal/ValidationPostResponse.java
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.sling.validation.testservices;
+package org.apache.sling.validation.testservices.internal;
 
 import java.io.IOException;
 import java.io.PrintWriter;
diff --git a/src/main/java/org/apache/sling/validation/testservices/ValidationPostResponseCreator.java b/src/main/java/org/apache/sling/validation/testservices/internal/ValidationPostResponseCreator.java
similarity index 71%
rename from src/main/java/org/apache/sling/validation/testservices/ValidationPostResponseCreator.java
rename to src/main/java/org/apache/sling/validation/testservices/internal/ValidationPostResponseCreator.java
index a884749..7446b42 100644
--- a/src/main/java/org/apache/sling/validation/testservices/ValidationPostResponseCreator.java
+++ b/src/main/java/org/apache/sling/validation/testservices/internal/ValidationPostResponseCreator.java
@@ -16,26 +16,33 @@
  * specific language governing permissions and limitations
  * under the License.
  */
- package org.apache.sling.validation.testservices;
+ package org.apache.sling.validation.testservices.internal;
 
 import java.util.Locale;
+import java.util.ResourceBundle;
 
-import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Service;
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.servlets.post.PostResponse;
 import org.apache.sling.servlets.post.PostResponseCreator;
 import org.apache.sling.servlets.post.SlingPostConstants;
+import org.osgi.service.component.annotations.Component;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-@Component()
-@Service(PostResponseCreator.class)
+@Component(
+    service = PostResponseCreator.class
+)
 public class ValidationPostResponseCreator implements PostResponseCreator {
 
+    private final Logger logger = LoggerFactory.getLogger(ValidationPostResponseCreator.class);
+
     @Override
     public PostResponse createPostResponse(SlingHttpServletRequest request) {
         String operation = request.getParameter(SlingPostConstants.RP_OPERATION);
         if (operation != null && "validation".equals(operation)) {
-            return new ValidationPostResponse(request.getResourceBundle(Locale.US));
+            final ResourceBundle resourceBundle = request.getResourceBundle(Locale.US);
+            logger.debug("resource bundle: {}", resourceBundle);
+            return new ValidationPostResponse(resourceBundle);
         }
         return null;
     }

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.