incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r644887 - in /incubator/sling/trunk/sling/bundleresource: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/sling/ src/main/java/org/apache/sling/bundleresource/ src/main/java/org/apache/...
Date Fri, 04 Apr 2008 20:27:46 GMT
Author: fmeschbe
Date: Fri Apr  4 13:27:41 2008
New Revision: 644887

URL: http://svn.apache.org/viewvc?rev=644887&view=rev
Log:
SLING-356 Add separate bundle for the BundleResourceProvider

Added:
    incubator/sling/trunk/sling/bundleresource/pom.xml   (with props)
    incubator/sling/trunk/sling/bundleresource/src/
    incubator/sling/trunk/sling/bundleresource/src/main/
    incubator/sling/trunk/sling/bundleresource/src/main/java/
    incubator/sling/trunk/sling/bundleresource/src/main/java/org/
    incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/
    incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/
    incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/
    incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/
    incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/Activator.java   (with props)
    incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResource.java
      - copied, changed from r644816, incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/bundle/BundleResource.java
    incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceIterator.java
      - copied, changed from r644816, incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/bundle/BundleResourceIterator.java
    incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceProvider.java
      - copied, changed from r644816, incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/bundle/BundleResourceProvider.java
    incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceURLConnection.java
      - copied, changed from r644816, incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/bundle/BundleResourceURLConnection.java
    incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceURLStreamHandler.java
      - copied, changed from r644816, incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/bundle/BundleResourceURLStreamHandler.java
    incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/MappedPath.java   (with props)
    incubator/sling/trunk/sling/bundleresource/src/main/resources/
    incubator/sling/trunk/sling/bundleresource/src/main/resources/META-INF/
    incubator/sling/trunk/sling/bundleresource/src/main/resources/META-INF/LICENSE
    incubator/sling/trunk/sling/bundleresource/src/main/resources/META-INF/NOTICE

Added: incubator/sling/trunk/sling/bundleresource/pom.xml
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/bundleresource/pom.xml?rev=644887&view=auto
==============================================================================
--- incubator/sling/trunk/sling/bundleresource/pom.xml (added)
+++ incubator/sling/trunk/sling/bundleresource/pom.xml Fri Apr  4 13:27:41 2008
@@ -0,0 +1,111 @@
+<?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>
+        <groupId>org.apache.sling</groupId>
+        <artifactId>sling</artifactId>
+        <version>1-incubator-SNAPSHOT</version>
+        <relativePath>../../parent/pom.xml</relativePath>
+    </parent>
+
+    <artifactId>org.apache.sling.bundleresource.impl</artifactId>
+    <packaging>bundle</packaging>
+    <version>2.0.0-incubator-SNAPSHOT</version>
+
+    <name>Sling Bundle Resource Provider</name>
+    <description>
+        Provides a ResourceProvider implementation supporting bundle
+        based resources.
+    </description>
+
+    <scm>
+        <connection>
+            scm:svn:http://svn.apache.org/repos/asf/incubator/sling/trunk/sling/bundleresource
+        </connection>
+        <developerConnection>
+            scm:svn:https://svn.apache.org/repos/asf/incubator/sling/trunk/sling/bundleresource
+        </developerConnection>
+        <url>
+            http://svn.apache.org/viewvc/incubator/sling/trunk/sling/bundleresource
+        </url>
+    </scm>
+
+    <build>
+        <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>
+                        <Bundle-Activator>
+                            org.apache.sling.bundleresource.impl.Activator
+                        </Bundle-Activator>
+                        <Private-Package>
+                            org.apache.sling.bundleresource.*
+                        </Private-Package>
+                    </instructions>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+    <dependencies>
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>servlet-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.api</artifactId>
+            <version>2.0.0-incubator-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.jcr.resource</artifactId>
+            <version>2.0.0-incubator-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.commons.json</artifactId>
+            <version>2.0.0-incubator-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.osgi.core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-simple</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+        </dependency>
+    </dependencies>
+</project>

Propchange: incubator/sling/trunk/sling/bundleresource/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/Activator.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/Activator.java?rev=644887&view=auto
==============================================================================
--- incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/Activator.java (added)
+++ incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/Activator.java Fri Apr  4 13:27:41 2008
@@ -0,0 +1,111 @@
+/*
+ * 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.bundleresource.impl;
+
+import static org.apache.sling.jcr.resource.JcrResourceConstants.BUNDLE_RESOURCE_ROOTS;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.BundleListener;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class Activator implements BundleActivator, BundleListener {
+
+    /** default log */
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    
+    private Map<Long, BundleResourceProvider> bundleResourceProviderMap = new HashMap<Long, BundleResourceProvider>();
+    
+    private BundleContext bundleContext;
+
+    public void start(BundleContext context) throws Exception {
+
+        this.bundleContext = context;
+        
+        context.addBundleListener(this);
+
+        try {
+            Bundle[] bundles = context.getBundles();
+            for (Bundle bundle : bundles) {
+                if (bundle.getState() == Bundle.ACTIVE) {
+                    // add bundle resource provider for active bundles
+                    addBundleResourceProvider(bundle);
+                }
+            }
+        } catch (Throwable t) {
+            log.error("activate: Problem while loading initial content and"
+                + " registering mappings for existing bundles", t);
+        }
+    }
+
+    public void stop(BundleContext context) throws Exception {
+        context.removeBundleListener(this);
+        this.bundleContext = null;
+    }
+
+    /**
+     * Loads and unloads any components provided by the bundle whose state
+     * changed. If the bundle has been started, the components are loaded. If
+     * the bundle is about to stop, the components are unloaded.
+     *
+     * @param event The <code>BundleEvent</code> representing the bundle state
+     *            change.
+     */
+    public void bundleChanged(BundleEvent event) {
+        switch (event.getType()) {
+            case BundleEvent.STARTED:
+                // register resource provider for the started bundle
+                addBundleResourceProvider(event.getBundle());
+                break;
+
+            case BundleEvent.STOPPED:
+                // remove resource provider after the bundle has stopped
+                removeBundleResourceProvider(event.getBundle());
+                break;
+        }
+    }
+    
+    // ---------- Bundle provided resources -----------------------------------
+
+    private void addBundleResourceProvider(Bundle bundle) {
+        String prefixes = (String) bundle.getHeaders().get(
+            BUNDLE_RESOURCE_ROOTS);
+        if (prefixes != null) {
+            BundleResourceProvider brp = new BundleResourceProvider(bundle,
+                prefixes);
+            brp.registerService(bundleContext);
+            bundleResourceProviderMap.put(bundle.getBundleId(), brp);
+        }
+    }
+
+    private void removeBundleResourceProvider(Bundle bundle) {
+        BundleResourceProvider brp = bundleResourceProviderMap.remove(bundle.getBundleId());
+        if (brp != null) {
+            brp.unregisterService();
+        }
+    }
+
+
+}

Propchange: incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/Activator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Copied: incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResource.java (from r644816, incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/bundle/BundleResource.java)
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResource.java?p2=incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResource.java&p1=incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/bundle/BundleResource.java&r1=644816&r2=644887&rev=644887&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/bundle/BundleResource.java (original)
+++ incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResource.java Fri Apr  4 13:27:41 2008
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.sling.jcr.resource.internal.helper.bundle;
+package org.apache.sling.bundleresource.impl;
 
 import static org.apache.jackrabbit.JcrConstants.NT_FILE;
 import static org.apache.jackrabbit.JcrConstants.NT_FOLDER;
@@ -46,6 +46,8 @@
 
     private final Bundle bundle;
 
+    private final MappedPath mappedPath;
+
     private final String path;
 
     private URL url;
@@ -54,67 +56,74 @@
 
     private final ResourceMetadata metadata;
 
-    public static BundleResource getResource(ResourceResolver resourceResolver, Bundle bundle, String path) {
+    public static BundleResource getResource(ResourceResolver resourceResolver,
+            Bundle bundle, MappedPath mappedPath, String resourcePath) {
+
+        String entryPath = mappedPath.getEntryPath(resourcePath);
 
         // first try, whether the bundle has an entry with a trailing slash
         // which would be a folder. In this case we check whether the
         // repository contains an item with the same path. If so, we
         // don't create a BundleResource but instead return null to be
         // able to return an item-based resource
-        URL entry = bundle.getEntry(path.concat("/"));
+        URL entry = bundle.getEntry(entryPath.concat("/"));
         if (entry != null) {
             Session session = resourceResolver.adaptTo(Session.class);
             if (session != null) {
                 try {
-                    if (session.itemExists(path)) {
+                    if (session.itemExists(resourcePath)) {
                         return null;
                     }
                 } catch (RepositoryException re) {
                     // don't care
                 }
             }
-            
+
             // append the slash to path for next steps
-            path = path.concat("/");
+            resourcePath = resourcePath.concat("/");
         }
 
         // if there is no entry with a trailing slash, try plain name
         // which would then of course be a file
         if (entry == null) {
-            entry = bundle.getEntry(path);
+            entry = bundle.getEntry(entryPath);
         }
 
         // here we either have a folder for which no same-named item exists
         // or a bundle file
         if (entry != null) {
-            return new BundleResource(resourceResolver, bundle, path);
+            return new BundleResource(resourceResolver, bundle, mappedPath,
+                resourcePath);
         }
 
         // the bundle does not contain the path
         return null;
     }
 
-    public BundleResource(ResourceResolver resourceResolver, Bundle bundle, String path) {
+    public BundleResource(ResourceResolver resourceResolver, Bundle bundle,
+            MappedPath mappedPath, String resourcePath) {
+
         this.resourceResolver = resourceResolver;
         this.bundle = bundle;
+        this.mappedPath = mappedPath;
 
         metadata = new ResourceMetadata();
-        metadata.setResolutionPath(path);
+        metadata.setResolutionPath(resourcePath);
         metadata.setCreationTime(bundle.getLastModified());
         metadata.setModificationTime(bundle.getLastModified());
 
-        if (path.endsWith("/")) {
+        if (resourcePath.endsWith("/")) {
 
-            this.path = path.substring(0, path.length() - 1);
+            this.path = resourcePath.substring(0, resourcePath.length() - 1);
             this.resourceType = NT_FOLDER;
 
         } else {
 
-            this.path = path;
+            this.path = resourcePath;
             this.resourceType = NT_FILE;
 
             try {
-                URL url = bundle.getEntry(path);
+                URL url = bundle.getEntry(mappedPath.getEntryPath(resourcePath));
                 metadata.setContentLength(url.openConnection().getContentLength());
             } catch (Exception e) {
                 // don't care, we just have no content length
@@ -188,7 +197,8 @@
         if (url == null) {
             try {
                 url = new URL(BundleResourceURLStreamHandler.PROTOCOL, null,
-                    -1, path, new BundleResourceURLStreamHandler(bundle));
+                    -1, path, new BundleResourceURLStreamHandler(bundle,
+                        mappedPath.getEntryPath(path)));
             } catch (MalformedURLException mue) {
                 log.error("getURL: Cannot get URL for " + this, mue);
             }
@@ -205,6 +215,10 @@
         return bundle;
     }
 
+    MappedPath getMappedPath() {
+        return mappedPath;
+    }
+    
     boolean isFile() {
         return NT_FILE.equals(getResourceType());
     }

Copied: incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceIterator.java (from r644816, incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/bundle/BundleResourceIterator.java)
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceIterator.java?p2=incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceIterator.java&p1=incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/bundle/BundleResourceIterator.java&r1=644816&r2=644887&rev=644887&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/bundle/BundleResourceIterator.java (original)
+++ incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceIterator.java Fri Apr  4 13:27:41 2008
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.sling.jcr.resource.internal.helper.bundle;
+package org.apache.sling.bundleresource.impl;
 
 import java.util.Enumeration;
 import java.util.Iterator;
@@ -39,16 +39,18 @@
     private final Logger log = LoggerFactory.getLogger(getClass());
 
     /** The bundle resource resolver */
-    private ResourceResolver resourceResolver;
-    
+    private final ResourceResolver resourceResolver;
+
     /** Bundle providing the entry resources */
-    private Bundle bundle;
+    private final Bundle bundle;
 
+    private final MappedPath mappedPath;
+    
     /** Underlying bundle entry path enumeration */
-    private Enumeration<String> entries;
+    private final Enumeration<String> entries;
 
     /** The length of the parent entry path, see seek() */
-    private int prefixLength;
+    private final int prefixLength;
 
     /** The prefetched next iterator entry, null at the end of iterating */
     private Resource nextResult;
@@ -66,6 +68,7 @@
             this.bundle = null;
             this.entries = null;
             this.prefixLength = 0;
+            this.mappedPath = null;
             this.nextResult = null;
 
         } else {
@@ -74,15 +77,20 @@
 
             this.resourceResolver = parent.getResourceResolver();
             this.bundle = parent.getBundle();
+            this.mappedPath = parent.getMappedPath();
+            
             // unchecked cast
             this.entries = parent.getBundle().getEntryPaths(parentPath);
             this.prefixLength = parentPath.length();
+            
             this.nextResult = seek();
         }
     }
-    
+
     @SuppressWarnings("unchecked")
-    BundleResourceIterator(ResourceResolver resourceResolver, Bundle bundle, String parentPath) {
+    BundleResourceIterator(ResourceResolver resourceResolver, Bundle bundle,
+            MappedPath mappedPath, String parentPath) {
+        
         // trailing slash to enumerate children
         if (!parentPath.endsWith("/")) {
             parentPath = parentPath.concat("/");
@@ -90,11 +98,13 @@
 
         this.resourceResolver = resourceResolver;
         this.bundle = bundle;
+        this.mappedPath = mappedPath;
+        
         // unchecked cast
         this.entries = bundle.getEntryPaths(parentPath);
         this.prefixLength = parentPath.length();
+
         this.nextResult = (entries != null) ? seek() : null;
-        
     }
 
     /** Returns true if there is another Resource available */
@@ -137,7 +147,8 @@
             int slash = entry.indexOf('/', prefixLength);
             if (slash < 0 || slash == entry.length() - 1) {
                 log.debug("seek: Using entry {}", entry);
-                return new BundleResource(resourceResolver, bundle, entry);
+                return new BundleResource(resourceResolver, bundle, mappedPath,
+                    entry);
             }
 
             log.debug("seek: Ignoring entry {}", entry);

Copied: incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceProvider.java (from r644816, incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/bundle/BundleResourceProvider.java)
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceProvider.java?p2=incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceProvider.java&p1=incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/bundle/BundleResourceProvider.java&r1=644816&r2=644887&rev=644887&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/bundle/BundleResourceProvider.java (original)
+++ incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceProvider.java Fri Apr  4 13:27:41 2008
@@ -16,10 +16,12 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.sling.jcr.resource.internal.helper.bundle;
+package org.apache.sling.bundleresource.impl;
 
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Dictionary;
+import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.List;
 import java.util.StringTokenizer;
@@ -31,6 +33,9 @@
 import org.apache.sling.api.resource.ResourceProvider;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceRegistration;
 
 public class BundleResourceProvider implements ResourceProvider {
 
@@ -38,7 +43,9 @@
     private final Bundle bundle;
 
     /** The root paths */
-    private final String[] roots;
+    private final MappedPath[] roots;
+
+    private ServiceRegistration serviceRegistration;
 
     /**
      * Creates Bundle resource provider accessing entries in the given Bundle an
@@ -49,21 +56,50 @@
         this.bundle = bundle;
 
         StringTokenizer pt = new StringTokenizer(rootList, ", \t\n\r\f");
-        List<String> prefixList = new ArrayList<String>();
+        List<MappedPath> prefixList = new ArrayList<MappedPath>();
         while (pt.hasMoreTokens()) {
-            String prefix = pt.nextToken().trim();
-            if (prefix.length() > 0) {
-                prefixList.add(prefix);
+            String resourceRoot = pt.nextToken();
+            if (resourceRoot.length() > 0) {
+                prefixList.add(MappedPath.create(resourceRoot));
             }
         }
-        this.roots = prefixList.toArray(new String[prefixList.size()]);
+        this.roots = prefixList.toArray(new MappedPath[prefixList.size()]);
     }
 
+    void registerService(BundleContext context) {
+        Dictionary<String, Object> props = new Hashtable<String, Object>();
+        props.put(Constants.SERVICE_DESCRIPTION, "Provider of Bundle based Resources");
+        props.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
+        props.put(ROOTS, getRoots());
+        
+        serviceRegistration = context.registerService(SERVICE_NAME, this, props);
+    }
+    
+    void unregisterService() {
+        if (serviceRegistration != null) {
+            serviceRegistration.unregister();
+        }
+    }
+    
     /** Returns the root paths */
-    public String[] getRoots() {
-        return roots;
+    private String[] getRoots() {
+        String[] rootPaths = new String[roots.length];
+        for (int i=0; i < roots.length; i++) {
+            rootPaths[i] = roots[i].getResourceRoot();
+        }
+        return rootPaths;
     }
-
+    
+    private MappedPath getMappedPath(String resourcePath) {
+        for (MappedPath mappedPath: roots) {
+            if (mappedPath.isChild(resourcePath)) {
+                return mappedPath;
+            }
+        }
+        
+        return null;
+    }
+    
     public Resource getResource(ResourceResolver resourceResolver,
             HttpServletRequest request, String path) {
         return getResource(resourceResolver, path);
@@ -75,7 +111,12 @@
      * implementation.
      */
     public Resource getResource(ResourceResolver resourceResolver, String path) {
-        return BundleResource.getResource(resourceResolver, bundle, path);
+        MappedPath mappedPath = getMappedPath(path);
+        if (mappedPath != null) {
+            return BundleResource.getResource(resourceResolver, bundle, mappedPath, path);
+        }
+        
+        return null;
     }
 
     public Iterator<Resource> listChildren(final Resource parent)
@@ -88,12 +129,10 @@
 
         // ensure this provider may have children of the parent
         String parentPath = parent.getPath();
-        for (String rootPath : getRoots()) {
-            if (parentPath.equals(rootPath)
-                || parentPath.startsWith(rootPath.concat("/"))) {
-                return new BundleResourceIterator(parent.getResourceResolver(),
-                    bundle, parent.getPath());
-            }
+        MappedPath mappedPath = getMappedPath(parentPath);
+        if (mappedPath != null) {
+            return new BundleResourceIterator(parent.getResourceResolver(),
+                bundle, mappedPath, parentPath);
         }
 
         // the parent resource cannot have children in this provider,

Copied: incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceURLConnection.java (from r644816, incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/bundle/BundleResourceURLConnection.java)
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceURLConnection.java?p2=incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceURLConnection.java&p1=incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/bundle/BundleResourceURLConnection.java&r1=644816&r2=644887&rev=644887&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/bundle/BundleResourceURLConnection.java (original)
+++ incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceURLConnection.java Fri Apr  4 13:27:41 2008
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.sling.jcr.resource.internal.helper.bundle;
+package org.apache.sling.bundleresource.impl;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -34,15 +34,18 @@
 public class BundleResourceURLConnection extends URLConnection {
 
     /** The bundle owning the resource underlying the URLConnection */
-    private Bundle bundle;
+    private final Bundle bundle;
 
+    private final String bundlePath;
+    
     /** The original URLConnection */
-    URLConnection delegatee;
+    private URLConnection delegatee;
 
-    protected BundleResourceURLConnection(Bundle bundle, URL url) {
+    protected BundleResourceURLConnection(Bundle bundle, String bundlePath, URL url) {
         super(url);
 
         this.bundle = bundle;
+        this.bundlePath = bundlePath;
     }
 
     /**
@@ -51,11 +54,10 @@
      */
     public synchronized void connect() throws IOException {
         if (!connected) {
-            String path = url.getPath();
-            URL url = bundle.getEntry(getURL().getPath());
+            URL url = bundle.getEntry(bundlePath);
             if (url == null) {
-                throw new IOException("Cannot find entry " + path
-                    + " in bundle " + bundle);
+                throw new IOException("Cannot find entry " + bundlePath
+                    + " in bundle " + bundle + " for URL " + url);
             }
 
             delegatee = url.openConnection();

Copied: incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceURLStreamHandler.java (from r644816, incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/bundle/BundleResourceURLStreamHandler.java)
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceURLStreamHandler.java?p2=incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceURLStreamHandler.java&p1=incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/bundle/BundleResourceURLStreamHandler.java&r1=644816&r2=644887&rev=644887&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/bundle/BundleResourceURLStreamHandler.java (original)
+++ incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceURLStreamHandler.java Fri Apr  4 13:27:41 2008
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.sling.jcr.resource.internal.helper.bundle;
+package org.apache.sling.bundleresource.impl;
 
 import java.io.IOException;
 import java.net.URL;
@@ -30,9 +30,12 @@
     static final String PROTOCOL = "bundle";
 
     private final Bundle bundle;
+    
+    private final String bundlePath;
 
-    BundleResourceURLStreamHandler(Bundle bundle) {
+    BundleResourceURLStreamHandler(Bundle bundle, String bundlePath) {
         this.bundle = bundle;
+        this.bundlePath = bundlePath;
     }
 
     @Override
@@ -42,7 +45,7 @@
                 + ", wrong protocol");
         }
 
-        return new BundleResourceURLConnection(bundle, u);
+        return new BundleResourceURLConnection(bundle, bundlePath, u);
     }
 
 }

Added: incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/MappedPath.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/MappedPath.java?rev=644887&view=auto
==============================================================================
--- incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/MappedPath.java (added)
+++ incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/MappedPath.java Fri Apr  4 13:27:41 2008
@@ -0,0 +1,100 @@
+/*
+ * 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.bundleresource.impl;
+
+class MappedPath {
+
+    private final String resourceRoot;
+    private final String resourceRootPrefix;
+    private final String entryRoot;
+    private final String entryRootPrefix;
+
+    static MappedPath create(String configPath) {
+        String resourceRoot;
+        String entryRoot;
+        int pipe = configPath.indexOf('¦');
+        if (pipe >= 0) {
+            entryRoot = configPath.substring(pipe + 1);
+            resourceRoot = configPath.substring(0, pipe).concat(entryRoot);
+        } else {
+            resourceRoot = configPath;
+            entryRoot = null;
+        }
+        return new MappedPath(resourceRoot, entryRoot);
+    }
+    
+    MappedPath(String resourceRoot, String entryRoot) {
+        this.resourceRoot = resourceRoot;
+        this.resourceRootPrefix = ensureTrailingSlash(resourceRoot);
+        this.entryRoot = entryRoot;
+        this.entryRootPrefix = ensureTrailingSlash(entryRoot);
+    }
+    
+    boolean isChild(String resourcePath) {
+        return resourcePath.startsWith(resourceRootPrefix)
+            || resourcePath.equals(resourceRoot);
+    }
+    
+    String getEntryPath(String resourcePath) {
+        if (entryRootPrefix == null) {
+            return resourcePath;
+        }
+        
+        if (resourcePath.startsWith(resourceRootPrefix)) {
+            return entryRootPrefix.concat(resourcePath.substring(resourceRootPrefix.length()));
+        } else if (resourcePath.equals(resourceRoot)) {
+            return entryRoot;
+        }
+        
+        return null;
+    }
+    
+    String getResourceRoot() {
+        return resourceRoot;
+    }
+    
+    String getResourceRootPrefix() {
+        return resourceRootPrefix;
+    }
+    
+    String getEntryRoot() {
+        return entryRoot;
+    }
+    
+    String getEntryRootPrefix() {
+        return entryRootPrefix;
+    }
+    
+    private static String ensureTrailingSlash(String path) {
+        if (path == null || path.length() == 0) {
+            return null;
+        }
+        
+        if (!path.endsWith("/")) {
+            return path.concat("/");
+        }
+        
+        return path;
+    }
+    
+    @Override
+    public String toString() {
+        return "MappedPath: " + getResourceRoot() + " -> " + getEntryRoot();
+    }
+}

Propchange: incubator/sling/trunk/sling/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/MappedPath.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/sling/trunk/sling/bundleresource/src/main/resources/META-INF/LICENSE
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/bundleresource/src/main/resources/META-INF/LICENSE?rev=644887&view=auto
==============================================================================
--- incubator/sling/trunk/sling/bundleresource/src/main/resources/META-INF/LICENSE (added)
+++ incubator/sling/trunk/sling/bundleresource/src/main/resources/META-INF/LICENSE Fri Apr  4 13:27:41 2008
@@ -0,0 +1,202 @@
+
+                                 Apache License
+                           Version 2.0, January 2004
+                        http://www.apache.org/licenses/
+
+   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+   1. Definitions.
+
+      "License" shall mean the terms and conditions for use, reproduction,
+      and distribution as defined by Sections 1 through 9 of this document.
+
+      "Licensor" shall mean the copyright owner or entity authorized by
+      the copyright owner that is granting the License.
+
+      "Legal Entity" shall mean the union of the acting entity and all
+      other entities that control, are controlled by, or are under common
+      control with that entity. For the purposes of this definition,
+      "control" means (i) the power, direct or indirect, to cause the
+      direction or management of such entity, whether by contract or
+      otherwise, or (ii) ownership of fifty percent (50%) or more of the
+      outstanding shares, or (iii) beneficial ownership of such entity.
+
+      "You" (or "Your") shall mean an individual or Legal Entity
+      exercising permissions granted by this License.
+
+      "Source" form shall mean the preferred form for making modifications,
+      including but not limited to software source code, documentation
+      source, and configuration files.
+
+      "Object" form shall mean any form resulting from mechanical
+      transformation or translation of a Source form, including but
+      not limited to compiled object code, generated documentation,
+      and conversions to other media types.
+
+      "Work" shall mean the work of authorship, whether in Source or
+      Object form, made available under the License, as indicated by a
+      copyright notice that is included in or attached to the work
+      (an example is provided in the Appendix below).
+
+      "Derivative Works" shall mean any work, whether in Source or Object
+      form, that is based on (or derived from) the Work and for which the
+      editorial revisions, annotations, elaborations, or other modifications
+      represent, as a whole, an original work of authorship. For the purposes
+      of this License, Derivative Works shall not include works that remain
+      separable from, or merely link (or bind by name) to the interfaces of,
+      the Work and Derivative Works thereof.
+
+      "Contribution" shall mean any work of authorship, including
+      the original version of the Work and any modifications or additions
+      to that Work or Derivative Works thereof, that is intentionally
+      submitted to Licensor for inclusion in the Work by the copyright owner
+      or by an individual or Legal Entity authorized to submit on behalf of
+      the copyright owner. For the purposes of this definition, "submitted"
+      means any form of electronic, verbal, or written communication sent
+      to the Licensor or its representatives, including but not limited to
+      communication on electronic mailing lists, source code control systems,
+      and issue tracking systems that are managed by, or on behalf of, the
+      Licensor for the purpose of discussing and improving the Work, but
+      excluding communication that is conspicuously marked or otherwise
+      designated in writing by the copyright owner as "Not a Contribution."
+
+      "Contributor" shall mean Licensor and any individual or Legal Entity
+      on behalf of whom a Contribution has been received by Licensor and
+      subsequently incorporated within the Work.
+
+   2. Grant of Copyright License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      copyright license to reproduce, prepare Derivative Works of,
+      publicly display, publicly perform, sublicense, and distribute the
+      Work and such Derivative Works in Source or Object form.
+
+   3. Grant of Patent License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      (except as stated in this section) patent license to make, have made,
+      use, offer to sell, sell, import, and otherwise transfer the Work,
+      where such license applies only to those patent claims licensable
+      by such Contributor that are necessarily infringed by their
+      Contribution(s) alone or by combination of their Contribution(s)
+      with the Work to which such Contribution(s) was submitted. If You
+      institute patent litigation against any entity (including a
+      cross-claim or counterclaim in a lawsuit) alleging that the Work
+      or a Contribution incorporated within the Work constitutes direct
+      or contributory patent infringement, then any patent licenses
+      granted to You under this License for that Work shall terminate
+      as of the date such litigation is filed.
+
+   4. Redistribution. You may reproduce and distribute copies of the
+      Work or Derivative Works thereof in any medium, with or without
+      modifications, and in Source or Object form, provided that You
+      meet the following conditions:
+
+      (a) You must give any other recipients of the Work or
+          Derivative Works a copy of this License; and
+
+      (b) You must cause any modified files to carry prominent notices
+          stating that You changed the files; and
+
+      (c) You must retain, in the Source form of any Derivative Works
+          that You distribute, all copyright, patent, trademark, and
+          attribution notices from the Source form of the Work,
+          excluding those notices that do not pertain to any part of
+          the Derivative Works; and
+
+      (d) If the Work includes a "NOTICE" text file as part of its
+          distribution, then any Derivative Works that You distribute must
+          include a readable copy of the attribution notices contained
+          within such NOTICE file, excluding those notices that do not
+          pertain to any part of the Derivative Works, in at least one
+          of the following places: within a NOTICE text file distributed
+          as part of the Derivative Works; within the Source form or
+          documentation, if provided along with the Derivative Works; or,
+          within a display generated by the Derivative Works, if and
+          wherever such third-party notices normally appear. The contents
+          of the NOTICE file are for informational purposes only and
+          do not modify the License. You may add Your own attribution
+          notices within Derivative Works that You distribute, alongside
+          or as an addendum to the NOTICE text from the Work, provided
+          that such additional attribution notices cannot be construed
+          as modifying the License.
+
+      You may add Your own copyright statement to Your modifications and
+      may provide additional or different license terms and conditions
+      for use, reproduction, or distribution of Your modifications, or
+      for any such Derivative Works as a whole, provided Your use,
+      reproduction, and distribution of the Work otherwise complies with
+      the conditions stated in this License.
+
+   5. Submission of Contributions. Unless You explicitly state otherwise,
+      any Contribution intentionally submitted for inclusion in the Work
+      by You to the Licensor shall be under the terms and conditions of
+      this License, without any additional terms or conditions.
+      Notwithstanding the above, nothing herein shall supersede or modify
+      the terms of any separate license agreement you may have executed
+      with Licensor regarding such Contributions.
+
+   6. Trademarks. This License does not grant permission to use the trade
+      names, trademarks, service marks, or product names of the Licensor,
+      except as required for reasonable and customary use in describing the
+      origin of the Work and reproducing the content of the NOTICE file.
+
+   7. Disclaimer of Warranty. Unless required by applicable law or
+      agreed to in writing, Licensor provides the Work (and each
+      Contributor provides its Contributions) on an "AS IS" BASIS,
+      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+      implied, including, without limitation, any warranties or conditions
+      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+      PARTICULAR PURPOSE. You are solely responsible for determining the
+      appropriateness of using or redistributing the Work and assume any
+      risks associated with Your exercise of permissions under this License.
+
+   8. Limitation of Liability. In no event and under no legal theory,
+      whether in tort (including negligence), contract, or otherwise,
+      unless required by applicable law (such as deliberate and grossly
+      negligent acts) or agreed to in writing, shall any Contributor be
+      liable to You for damages, including any direct, indirect, special,
+      incidental, or consequential damages of any character arising as a
+      result of this License or out of the use or inability to use the
+      Work (including but not limited to damages for loss of goodwill,
+      work stoppage, computer failure or malfunction, or any and all
+      other commercial damages or losses), even if such Contributor
+      has been advised of the possibility of such damages.
+
+   9. Accepting Warranty or Additional Liability. While redistributing
+      the Work or Derivative Works thereof, You may choose to offer,
+      and charge a fee for, acceptance of support, warranty, indemnity,
+      or other liability obligations and/or rights consistent with this
+      License. However, in accepting such obligations, You may act only
+      on Your own behalf and on Your sole responsibility, not on behalf
+      of any other Contributor, and only if You agree to indemnify,
+      defend, and hold each Contributor harmless for any liability
+      incurred by, or claims asserted against, such Contributor by reason
+      of your accepting any such warranty or additional liability.
+
+   END OF TERMS AND CONDITIONS
+
+   APPENDIX: How to apply the Apache License to your work.
+
+      To apply the Apache License to your work, attach the following
+      boilerplate notice, with the fields enclosed by brackets "[]"
+      replaced with your own identifying information. (Don't include
+      the brackets!)  The text should be enclosed in the appropriate
+      comment syntax for the file format. We also recommend that a
+      file or class name and description of purpose be included on the
+      same "printed page" as the copyright notice for easier
+      identification within third-party archives.
+
+   Copyright [yyyy] [name of copyright owner]
+
+   Licensed 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.

Added: incubator/sling/trunk/sling/bundleresource/src/main/resources/META-INF/NOTICE
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/bundleresource/src/main/resources/META-INF/NOTICE?rev=644887&view=auto
==============================================================================
--- incubator/sling/trunk/sling/bundleresource/src/main/resources/META-INF/NOTICE (added)
+++ incubator/sling/trunk/sling/bundleresource/src/main/resources/META-INF/NOTICE Fri Apr  4 13:27:41 2008
@@ -0,0 +1,5 @@
+Apache Sling Bundle Resource Provider
+Copyright 2007 The Apache Software Foundation
+
+This product includes software developed at
+The Apache Software Foundation (http://www.apache.org/).



Mime
View raw message