|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ClasspathFileSystemProvider.java | 62.5% | 81.8% | 100% | 79.4% |
|
||||||||||||||
| 1 | // | |
| 2 | //Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. | |
| 3 | //All rights reserved. | |
| 4 | // | |
| 5 | //Redistribution and use in source and binary forms, with or without modification, | |
| 6 | //are permitted provided that the following conditions are met: | |
| 7 | // | |
| 8 | //* Redistributions of source code must retain the above copyright notice, | |
| 9 | //this list of conditions and the following disclaimer. | |
| 10 | //* Redistributions in binary form must reproduce the above copyright notice, | |
| 11 | //this list of conditions and the following disclaimer in the documentation | |
| 12 | //and/or other materials provided with the distribution. | |
| 13 | //* Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. | |
| 14 | //nor the names of its contributors may be used to endorse or promote products | |
| 15 | //derived from this software without specific prior written permission. | |
| 16 | // | |
| 17 | //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 18 | //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 19 | //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
| 20 | //IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
| 21 | //INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
| 22 | //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, | |
| 23 | //OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
| 24 | //WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 25 | //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 26 | //POSSIBILITY OF SUCH DAMAGE. | |
| 27 | // | |
| 28 | ||
| 29 | package org.objectledge.filesystem; | |
| 30 | ||
| 31 | import java.io.IOException; | |
| 32 | import java.io.InputStream; | |
| 33 | import java.net.URL; | |
| 34 | import java.util.Enumeration; | |
| 35 | ||
| 36 | import org.objectledge.ComponentInitializationError; | |
| 37 | import org.objectledge.filesystem.impl.ReadOnlyFileSystemProvider; | |
| 38 | ||
| 39 | /** | |
| 40 | * An implementation of the FileSystemProvider that reads resources from the classpath. | |
| 41 | * | |
| 42 | * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a> | |
| 43 | * @version $Id: ClasspathFileSystemProvider.java,v 1.4 2005/08/03 10:22:22 pablo Exp $ | |
| 44 | */ | |
| 45 | public class ClasspathFileSystemProvider | |
| 46 | extends ReadOnlyFileSystemProvider | |
| 47 | { | |
| 48 | // instance variables ///////////////////////////////////////////////////////////////////////// | |
| 49 | ||
| 50 | /** the classloader this provider loads data from. */ | |
| 51 | private ClassLoader classLoader; | |
| 52 | ||
| 53 | // initialization ///////////////////////////////////////////////////////////////////////////// | |
| 54 | ||
| 55 | /** | |
| 56 | * Creates an new instance of the provider. | |
| 57 | * | |
| 58 | * @param name the name of the provider. | |
| 59 | * @param classLoader the class loader to load resources from. | |
| 60 | */ | |
| 61 | 10212 | public ClasspathFileSystemProvider(String name, ClassLoader classLoader) |
| 62 | { | |
| 63 | 10212 | super(name); |
| 64 | 10212 | this.classLoader = classLoader; |
| 65 | 10212 | String location = null; |
| 66 | 10212 | for(int i=0; i < LISTING_LOCATION.length; i++) |
| 67 | { | |
| 68 | 30636 | location = LISTING_LOCATION[i]; |
| 69 | 30636 | if(location.charAt(0) == '/') |
| 70 | { | |
| 71 | 30636 | location = location.substring(1); |
| 72 | } | |
| 73 | 30636 | URL listing = null; |
| 74 | 30636 | try |
| 75 | { | |
| 76 | ||
| 77 | 30636 | Enumeration listings = classLoader.getResources(location); |
| 78 | 30636 | while(listings.hasMoreElements()) |
| 79 | { | |
| 80 | 0 | listing = (URL)listings.nextElement(); |
| 81 | 0 | InputStream is = listing.openStream(); |
| 82 | 0 | processListing(listing.toString(), is); |
| 83 | } | |
| 84 | } | |
| 85 | catch(IOException e) | |
| 86 | { | |
| 87 | 0 | throw new ComponentInitializationError("failed to load listing "+location+" from "+ |
| 88 | listing, e); | |
| 89 | } | |
| 90 | } | |
| 91 | } | |
| 92 | ||
| 93 | // public interface /////////////////////////////////////////////////////////////////////////// | |
| 94 | ||
| 95 | /** | |
| 96 | * {@inheritDoc} | |
| 97 | */ | |
| 98 | 8142 | public InputStream getInputStream(String path) |
| 99 | { | |
| 100 | 8142 | path = stripLeadingSlash(FileSystem.normalizedPath(path)); |
| 101 | 8142 | return classLoader.getResourceAsStream(path); |
| 102 | } | |
| 103 | ||
| 104 | /** | |
| 105 | * {@inheritDoc} | |
| 106 | */ | |
| 107 | 6808 | public URL getResource(String path) |
| 108 | { | |
| 109 | 6808 | path = stripLeadingSlash(FileSystem.normalizedPath(path)); |
| 110 | 6808 | return classLoader.getResource(path); |
| 111 | } | |
| 112 | ||
| 113 | /////////////////////////////////////////////////////////////////////////////////////////////// | |
| 114 | ||
| 115 | 14950 | private String stripLeadingSlash(String path) |
| 116 | { | |
| 117 | 14950 | if(path.length() > 0 && path.charAt(0) == '/') |
| 118 | { | |
| 119 | 14950 | path = path.substring(1); |
| 120 | } | |
| 121 | 14950 | return path; |
| 122 | } | |
| 123 | } |
|
||||||||||