You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by pa...@apache.org on 2017/05/29 22:18:26 UTC

svn commit: r1796709 - in /sling/whiteboard/pauls/gush/servlet/src/main/java/org/apache/sling/whiteboard/pauls/gush/servlet: GushResource.java GushResourceProvider.java

Author: pauls
Date: Mon May 29 22:18:26 2017
New Revision: 1796709

URL: http://svn.apache.org/viewvc?rev=1796709&view=rev
Log:
Add some scaffolding for a resource provider.

Added:
    sling/whiteboard/pauls/gush/servlet/src/main/java/org/apache/sling/whiteboard/pauls/gush/servlet/GushResource.java
    sling/whiteboard/pauls/gush/servlet/src/main/java/org/apache/sling/whiteboard/pauls/gush/servlet/GushResourceProvider.java

Added: sling/whiteboard/pauls/gush/servlet/src/main/java/org/apache/sling/whiteboard/pauls/gush/servlet/GushResource.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/pauls/gush/servlet/src/main/java/org/apache/sling/whiteboard/pauls/gush/servlet/GushResource.java?rev=1796709&view=auto
==============================================================================
--- sling/whiteboard/pauls/gush/servlet/src/main/java/org/apache/sling/whiteboard/pauls/gush/servlet/GushResource.java (added)
+++ sling/whiteboard/pauls/gush/servlet/src/main/java/org/apache/sling/whiteboard/pauls/gush/servlet/GushResource.java Mon May 29 22:18:26 2017
@@ -0,0 +1,76 @@
+/*
+ * 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.whiteboard.pauls.gush.servlet;
+
+
+import org.apache.sling.api.resource.AbstractResource;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceMetadata;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ValueMap;
+
+public class GushResource extends AbstractResource implements Resource {
+
+    private final ResourceResolver m_resolver;
+    private final String m_path;
+    private final ValueMap m_data;
+    private final ResourceMetadata m_metadata;
+    
+    public GushResource(ResourceResolver resourceResolver, String path, ValueMap data) {
+        m_resolver = resourceResolver;
+        m_path = path;
+        m_data = data;
+        m_metadata = new ResourceMetadata();
+        m_metadata.setResolutionPath(m_path);
+    }
+
+    @Override
+    public String getPath() {
+        return m_path;
+    }
+
+    @Override
+    public String getResourceType() {
+        return "gush/connections/connection";
+    }
+
+    @Override
+    public String getResourceSuperType() {
+        return null;
+    }
+
+    @Override
+    public ResourceMetadata getResourceMetadata() {
+        return m_metadata;
+    }
+
+    @Override
+    public ResourceResolver getResourceResolver() {
+        return m_resolver;
+    }
+    
+    @Override
+    public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
+        if(type == ValueMap.class) {
+            return type.cast(m_data);
+        }
+        return super.adaptTo(type);
+    }
+
+}

Added: sling/whiteboard/pauls/gush/servlet/src/main/java/org/apache/sling/whiteboard/pauls/gush/servlet/GushResourceProvider.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/pauls/gush/servlet/src/main/java/org/apache/sling/whiteboard/pauls/gush/servlet/GushResourceProvider.java?rev=1796709&view=auto
==============================================================================
--- sling/whiteboard/pauls/gush/servlet/src/main/java/org/apache/sling/whiteboard/pauls/gush/servlet/GushResourceProvider.java (added)
+++ sling/whiteboard/pauls/gush/servlet/src/main/java/org/apache/sling/whiteboard/pauls/gush/servlet/GushResourceProvider.java Mon May 29 22:18:26 2017
@@ -0,0 +1,71 @@
+/*
+ * 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.whiteboard.pauls.gush.servlet;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.SyntheticResource;
+import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.api.resource.observation.ResourceChange;
+import org.apache.sling.api.resource.observation.ResourceChangeListener;
+import org.apache.sling.spi.resource.provider.ResolveContext;
+import org.apache.sling.spi.resource.provider.ResourceContext;
+import org.apache.sling.spi.resource.provider.ResourceProvider;
+
+
+public class GushResourceProvider extends ResourceProvider<Void> implements ResourceChangeListener {
+    private static final String ROOT = "/var/gush/connections";
+    
+    private final ConcurrentHashMap<String, ValueMap> m_resources = new ConcurrentHashMap<>();
+    
+    
+    @Override
+    public Resource getResource(ResolveContext<Void> ctx, String path, ResourceContext resourceContext,
+            Resource parent) {
+        if (ROOT.equals(path)) {
+            return new SyntheticResource(ctx.getResourceResolver(), path, "gush/connections");
+        }
+        final ValueMap data = m_resources.get(path);
+        return data == null ? null : new GushResource(ctx.getResourceResolver(), path, data);
+    }
+
+    @Override
+    public Iterator<Resource> listChildren(ResolveContext<Void> ctx, Resource parent) {
+        if(parent.getPath().startsWith(ROOT)) {
+            return m_resources.entrySet().stream().filter(entry -> parent.getPath().equals(parentPath(entry.getKey())))
+                .map(entry -> (Resource) new GushResource(parent.getResourceResolver(), entry.getKey(), entry.getValue()))
+                .iterator();
+        } else {
+            return null;
+        }
+    }
+
+    @Override
+    public void onChange(List<ResourceChange> changes) {
+        
+    }
+    
+    private static String parentPath(String path) {
+        final int lastSlash = path.lastIndexOf("/");
+        return lastSlash > 0 ? path.substring(0, lastSlash) : "";
+    }
+}