incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1569223 - in /sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger: api/ impl/
Date Tue, 18 Feb 2014 07:49:05 GMT
Author: cziegeler
Date: Tue Feb 18 07:49:05 2014
New Revision: 1569223

URL: http://svn.apache.org/r1569223
Log:
SLING-3397 : Provide a way for path operations. Apply modified patch from Gilles Knobloch

Added:
    sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/api/
    sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/api/ResourceMergerService.java
  (with props)
    sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/api/package-info.java
  (with props)
Modified:
    sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResource.java
    sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceConstants.java
    sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProviderFactory.java

Added: sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/api/ResourceMergerService.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/api/ResourceMergerService.java?rev=1569223&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/api/ResourceMergerService.java
(added)
+++ sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/api/ResourceMergerService.java
Tue Feb 18 07:49:05 2014
@@ -0,0 +1,57 @@
+/*
+ * 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.resourcemerger.api;
+
+import org.apache.sling.api.resource.Resource;
+
+import aQute.bnd.annotation.ProviderType;
+
+/**
+ * Service to handle merged resources.
+ */
+@ProviderType
+public interface ResourceMergerService {
+
+    /**
+     * Returns the absolute path for the provided relative path.
+     *
+     * @param relativePath The relative path
+     * @return Returns the merged resource path
+     * @throw IllegalArgumentException if relativePath is <code>null</code> or
absolute
+     */
+    String getMergedResourcePath(String relativePath);
+
+    /**
+     * Returns a merged resource if the provided resource is from one of the search paths
+     * and the resource is not hidden.
+     * If the resource is <code>null</code>, <code>null</code> is
returned.
+     *
+     * @param resource The resource
+     * @return Returns the merged resource or <code>null</code>
+     */
+    Resource getMergedResource(Resource resource);
+
+    /**
+     * Returns <code>true</code> if the provided {@link Resource} is a merged
resource.
+     * If the resource is <code>null</code>, <code>false</code> is
returned.
+     * @param resource The resource
+     * @return Returns <code>true</code> if the provided {@link Resource} is
a merged resource.
+     */
+    boolean isMergedResource(Resource resource);
+}

Propchange: sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/api/ResourceMergerService.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/api/ResourceMergerService.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/api/ResourceMergerService.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/api/package-info.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/api/package-info.java?rev=1569223&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/api/package-info.java
(added)
+++ sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/api/package-info.java
Tue Feb 18 07:49:05 2014
@@ -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.
+ */
+
+/**
+ * Provides a service to merge multiple physical resources into a single one
+ */
+@Version("0.0.1")
+package org.apache.sling.resourcemerger.api;
+
+import aQute.bnd.annotation.Version;
\ No newline at end of file

Propchange: sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/api/package-info.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/api/package-info.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/api/package-info.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResource.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResource.java?rev=1569223&r1=1569222&r2=1569223&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResource.java
(original)
+++ sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResource.java
Tue Feb 18 07:49:05 2014
@@ -31,9 +31,6 @@ import org.apache.sling.api.resource.Val
  */
 public class MergedResource extends AbstractResource {
 
-    private static final String MD_FLAG = "sling.mappedResource";
-    private static final String MD_RESOURCES = "sling.mappedResources";
-
     /** The resource resolver. */
     private final ResourceResolver resolver;
 
@@ -43,7 +40,7 @@ public class MergedResource extends Abst
     /** Resource type. */
     private final String resourceType;
 
-    /** Resource metadata. */
+    /** Resource meta data. */
     private final ResourceMetadata metadata = new ResourceMetadata();
 
     /** Cache value map. */
@@ -66,14 +63,14 @@ public class MergedResource extends Abst
         this.path = (relativePath.length() == 0 ? mergeRootPath : mergeRootPath + "/" + relativePath);
         this.properties = new MergedValueMap(valueMaps);
         this.resourceType = this.properties.get(ResourceResolver.PROPERTY_RESOURCE_TYPE,
(relativePath.length() == 0 ? "/" : relativePath));
-        metadata.put(MD_FLAG, true);
+        metadata.put(MergedResourceConstants.METADATA_FLAG, true);
         final String[] resourcePaths = new String[mappedResources.size()];
         int i = 0;
         for(final Resource rsrc : mappedResources) {
             resourcePaths[i] = rsrc.getPath();
             i++;
         }
-        metadata.put(MD_RESOURCES, resourcePaths);
+        metadata.put(MergedResourceConstants.METADATA_RESOURCES, resourcePaths);
     }
 
     /**
@@ -161,6 +158,6 @@ public class MergedResource extends Abst
     @Override
     public String toString() {
         return "MergedResource [path=" + this.path +
-               ", resources=" + this.metadata.get(MD_RESOURCES) + "]";
+               ", resources=" + this.metadata.get(MergedResourceConstants.METADATA_RESOURCES)
+ "]";
     }
 }

Modified: sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceConstants.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceConstants.java?rev=1569223&r1=1569222&r2=1569223&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceConstants.java
(original)
+++ sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceConstants.java
Tue Feb 18 07:49:05 2014
@@ -48,4 +48,14 @@ public class MergedResourceConstants {
      */
     public static final String PN_ORDER_BEFORE = "sling:orderBefore";
 
+    /**
+     * Resource meta data entry indicating that the resource is a merged resource
+     */
+    public static final String METADATA_FLAG = "sling.mergedResource";
+
+    /**
+     * Resource meta data entry containing the paths of the merged resources.
+     */
+    public static final String METADATA_RESOURCES = "sling.mergedResources";
+
 }

Modified: sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProviderFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProviderFactory.java?rev=1569223&r1=1569222&r2=1569223&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProviderFactory.java
(original)
+++ sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProviderFactory.java
Tue Feb 18 07:49:05 2014
@@ -26,14 +26,17 @@ import org.apache.felix.scr.annotations.
 import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Service;
 import org.apache.sling.api.resource.LoginException;
+import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceProvider;
 import org.apache.sling.api.resource.ResourceProviderFactory;
+import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.commons.osgi.PropertiesUtil;
+import org.apache.sling.resourcemerger.api.ResourceMergerService;
 
 @Component(name = "Apache Sling Merged Resource Provider Factory",
            description = "This resource provider delivers merged resources based on the search
paths.",
            metatype=true)
-@Service(value = ResourceProviderFactory.class)
+@Service(value = {ResourceProviderFactory.class, ResourceMergerService.class})
 @Properties({
     @Property(name = ResourceProvider.ROOTS, value=MergedResourceProviderFactory.DEFAULT_ROOT,
             label="Root",
@@ -42,9 +45,9 @@ import org.apache.sling.commons.osgi.Pro
 })
 /**
  * The <code>MergedResourceProviderFactory</code> creates merged resource
- * providers.
+ * providers and implements the <code>ResourceMergerService</code>.
  */
-public class MergedResourceProviderFactory implements ResourceProviderFactory {
+public class MergedResourceProviderFactory implements ResourceProviderFactory, ResourceMergerService
{
 
     public static final String DEFAULT_ROOT = "/mnt/overlay";
 
@@ -66,11 +69,54 @@ public class MergedResourceProviderFacto
         return new MergedResourceProvider(mergeRootPath);
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    public String getMergedResourcePath(final String relativePath) {
+        if (relativePath == null) {
+            throw new IllegalArgumentException("Provided relative path is null");
+        }
+
+        if (relativePath.startsWith("/")) {
+            throw new IllegalArgumentException("Provided path is not a relative path");
+        }
+
+        return mergeRootPath + "/" + relativePath;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Resource getMergedResource(final Resource resource) {
+        if (resource != null) {
+            final ResourceResolver resolver = resource.getResourceResolver();
+            final String[] searchPaths = resolver.getSearchPath();
+            for (final String searchPathPrefix : searchPaths) {
+                if (resource.getPath().startsWith(searchPathPrefix)) {
+                    final String searchPath = searchPathPrefix.substring(0, searchPathPrefix.length()
- 1);
+                    return resolver.getResource(resource.getPath().replaceFirst(searchPath,
mergeRootPath));
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean isMergedResource(final Resource resource) {
+        if (resource == null) {
+            return false;
+        }
+
+        return Boolean.TRUE.equals(resource.getResourceMetadata().get(MergedResourceConstants.METADATA_FLAG));
+    }
+
     @Activate
     protected void configure(final Map<String, Object> properties) {
         mergeRootPath = PropertiesUtil.toString(properties.get(ResourceProvider.ROOTS), DEFAULT_ROOT);
-        if ( mergeRootPath.endsWith("/") ) {
-            mergeRootPath = mergeRootPath.substring(mergeRootPath.length() - 1);
+        if (mergeRootPath.endsWith("/")) {
+            mergeRootPath = mergeRootPath.substring(0, mergeRootPath.length() - 1);
         }
     }
 }



Mime
View raw message