Save This Page
Home » org.apache.sling.launchpad.base-2.2.0-source-release » org.apache.sling.launchpad.base.impl » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.sling.launchpad.base.impl;
   18   
   19   import java.io.IOException;
   20   import java.net.JarURLConnection;
   21   import java.net.URL;
   22   import java.net.URLConnection;
   23   import java.util.ArrayList;
   24   import java.util.Collections;
   25   import java.util.Enumeration;
   26   import java.util.Iterator;
   27   import java.util.List;
   28   import java.util.jar.JarEntry;
   29   import java.util.jar.JarFile;
   30   import java.util.regex.Pattern;
   31   
   32   
   33   /**
   34    * The <code>ClassLoaderResourceProvider</code> TODO
   35    */
   36   public class ClassLoaderResourceProvider extends ResourceProvider {
   37   
   38       private final ClassLoader classLoader;
   39   
   40       public ClassLoaderResourceProvider(ClassLoader classLoader) {
   41           this.classLoader = (classLoader != null)
   42                   ? classLoader
   43                   : this.getClass().getClassLoader();
   44       }
   45   
   46       @Override
   47       public Iterator<String> getChildren(String path) {
   48           List<String> children;
   49   
   50           URL url = this.classLoader.getResource(path);
   51           if (url != null) {
   52               Pattern pathPattern = Pattern.compile("^" + path + "/[^/]+/?$");
   53   
   54               children = new ArrayList<String>();
   55               try {
   56                   URLConnection conn = url.openConnection();
   57                   if (conn instanceof JarURLConnection) {
   58                       JarFile jar = ((JarURLConnection) conn).getJarFile();
   59                       Enumeration<JarEntry> entries = jar.entries();
   60                       while (entries.hasMoreElements()) {
   61                           String entry = entries.nextElement().getName();
   62                           if (pathPattern.matcher(entry).matches()) {
   63                               children.add(entry);
   64                           }
   65                       }
   66                   }
   67               } catch (IOException ioe) {
   68                   // ignore for now
   69               }
   70           } else {
   71               children = Collections.emptyList();
   72           }
   73   
   74           return children.iterator();
   75       }
   76   
   77       /**
   78        * @see org.apache.sling.launchpad.base.impl.ResourceProvider#getResource(java.lang.String)
   79        */
   80       public URL getResource(String path) {
   81           // ensure path
   82           if (path == null || path.length() == 0) {
   83               return null;
   84           }
   85   
   86           // remove leading slash
   87           if (path.charAt(0) == '/') {
   88               path = path.substring(1);
   89           }
   90   
   91           return (this.classLoader != null) ? this.classLoader.getResource(path) : null;
   92       }
   93   
   94   }

Save This Page
Home » org.apache.sling.launchpad.base-2.2.0-source-release » org.apache.sling.launchpad.base.impl » [javadoc | source]