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 public ClasspathFileSystemProvider(String name, ClassLoader classLoader)
62 {
63 super(name);
64 this.classLoader = classLoader;
65 String location = null;
66 for(int i=0; i < LISTING_LOCATION.length; i++)
67 {
68 location = LISTING_LOCATION[i];
69 if(location.charAt(0) == '/')
70 {
71 location = location.substring(1);
72 }
73 URL listing = null;
74 try
75 {
76
77 Enumeration listings = classLoader.getResources(location);
78 while(listings.hasMoreElements())
79 {
80 listing = (URL)listings.nextElement();
81 InputStream is = listing.openStream();
82 processListing(listing.toString(), is);
83 }
84 }
85 catch(IOException e)
86 {
87 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 public InputStream getInputStream(String path)
99 {
100 path = stripLeadingSlash(FileSystem.normalizedPath(path));
101 return classLoader.getResourceAsStream(path);
102 }
103
104 /***
105 * {@inheritDoc}
106 */
107 public URL getResource(String path)
108 {
109 path = stripLeadingSlash(FileSystem.normalizedPath(path));
110 return classLoader.getResource(path);
111 }
112
113 ///////////////////////////////////////////////////////////////////////////////////////////////
114
115 private String stripLeadingSlash(String path)
116 {
117 if(path.length() > 0 && path.charAt(0) == '/')
118 {
119 path = path.substring(1);
120 }
121 return path;
122 }
123 }