001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: ResourceFinderClasspath.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.resources;
009:
010: import com.uwyn.rife.resources.exceptions.CantOpenResourceStreamException;
011: import com.uwyn.rife.resources.exceptions.CantRetrieveResourceContentException;
012: import com.uwyn.rife.resources.exceptions.ResourceFinderErrorException;
013: import com.uwyn.rife.tools.FileUtils;
014: import com.uwyn.rife.tools.InnerClassException;
015: import com.uwyn.rife.tools.InputStreamUser;
016: import com.uwyn.rife.tools.exceptions.FileUtilsErrorException;
017: import java.io.IOException;
018: import java.io.InputStream;
019: import java.net.URL;
020: import java.net.URLConnection;
021:
022: /**
023: * This class offers <code>ResourceFinder</code> capabilities for resources that
024: * are available through the classloader. This is done for directories as well
025: * as for jar files. Basically, this corresponds to the resources that are
026: * available through the classpath.
027: * <p>
028: * Since the application's classloader isn't supposed to change in a global way,
029: * the <code>ResourceFinderClasspath</code> class can only be instantiated
030: * through the static <code>getInstance()</code> method that always returns
031: * the same instance as a singleton.
032: *
033: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
034: * @version $Revision: 3634 $
035: * @see com.uwyn.rife.resources.ResourceFinder
036: * @since 1.0
037: */
038: public class ResourceFinderClasspath extends AbstractResourceFinder {
039: protected ResourceFinderClasspath() {
040: }
041:
042: /**
043: * Returns the shared singleton instance of the
044: * <code>ResourceFinderClasspath</code> class.
045: *
046: * @return the singleton <code>ResourceFinderClasspath</code> instance
047: *
048: * @since 1.0
049: */
050: public static ResourceFinderClasspath getInstance() {
051: return ResourceFinderClasspathSingleton.INSTANCE;
052: }
053:
054: public URL getResource(String name) {
055: URL resource = null;
056:
057: if (this .getClass().getClassLoader() != null) {
058: // Try the class loader that loaded this class.
059: resource = this .getClass().getClassLoader().getResource(
060: name);
061: } else {
062: // Try the system class loader.
063: resource = ClassLoader.getSystemClassLoader().getResource(
064: name);
065: }
066:
067: if (null == resource) {
068: // if not found in classpath fall back to default
069: resource = this .getClass().getResource(name);
070: }
071:
072: return resource;
073: }
074:
075: public <ResultType> ResultType useStream(URL resource,
076: InputStreamUser user) throws ResourceFinderErrorException,
077: InnerClassException {
078: if (null == resource || null == user) {
079: return null;
080: }
081:
082: InputStream stream = null;
083: try {
084: URLConnection connection = resource.openConnection();
085: connection.setUseCaches(false);
086: stream = connection.getInputStream();
087: return (ResultType) user.useInputStream(stream);
088: } catch (IOException e) {
089: throw new CantOpenResourceStreamException(resource, e);
090: } finally {
091: if (stream != null) {
092: try {
093: stream.close();
094: } catch (IOException e) {
095: // couldn't close stream since it probably already has been
096: // closed after an exception
097: // proceed without reporting an error message.
098: }
099: }
100: }
101: }
102:
103: public String getContent(URL resource, String encoding)
104: throws ResourceFinderErrorException {
105: if (null == resource) {
106: return null;
107: }
108:
109: try {
110: return FileUtils.readString(resource, encoding);
111: } catch (FileUtilsErrorException e) {
112: throw new CantRetrieveResourceContentException(resource,
113: encoding, e);
114: }
115: }
116:
117: public long getModificationTime(URL resource)
118: throws ResourceFinderErrorException {
119: return ModificationTimeClasspath.getModificationTime(resource);
120: }
121: }
|