001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings.resource;
014:
015: import org.wings.StaticResource;
016: import org.wings.externalizer.ExternalizeManager;
017: import java.io.InputStream;
018:
019: /**
020: * A Classpath Resource is a static resource whose content is
021: * read from a classloader.
022: *
023: * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a>
024: * @author <a href="mailto:H.Zeller@acm.org">Henner Zeller</a>
025: */
026: public class ClassPathResource extends StaticResource implements
027: NamedResource {
028: /**
029: * The class loader from which the resource is loaded
030: */
031: private final transient ClassLoader classLoader;
032:
033: /**
034: * The name that identifies the resource in the classpath
035: */
036: protected final String resourceFileName;
037:
038: /**
039: * A static resource that is obtained from the default classpath.
040: */
041: public ClassPathResource(String resourceFileName) {
042: this (null, resourceFileName, "unkonwn");
043: }
044:
045: /**
046: * A static resource that is obtained from the default classpath.
047: */
048: public ClassPathResource(String resourceFileName, String mimeType) {
049: this (null, resourceFileName, mimeType);
050: }
051:
052: /**
053: * A static resource that is obtained from the specified class loader
054: *
055: * @param classLoader the classLoader from which the resource is obtained
056: * @param resourceFileName the resource relative to the baseClass
057: */
058: public ClassPathResource(ClassLoader classLoader,
059: String resourceFileName) {
060: this (classLoader, resourceFileName, "unknown");
061: }
062:
063: /**
064: * A static resource that is obtained from the specified class loader
065: *
066: * @param classLoader the classLoader from which the resource is obtained
067: * @param resourceFileName the resource relative to the baseClass
068: */
069: public ClassPathResource(ClassLoader classLoader,
070: String resourceFileName, String mimeType) {
071: super (null, mimeType);
072: this .classLoader = classLoader;
073: this .resourceFileName = resourceFileName;
074: int dotIndex = resourceFileName.lastIndexOf('.');
075: if (dotIndex > -1) {
076: extension = resourceFileName.substring(dotIndex + 1);
077: }
078: externalizerFlags = ExternalizeManager.GLOBAL
079: | ExternalizeManager.FINAL;
080: }
081:
082: @Override
083: protected InputStream getResourceStream()
084: throws ResourceNotFoundException {
085: InputStream stream = getClassLoader().getResourceAsStream(
086: resourceFileName);
087: if (stream == null)
088: throw new ResourceNotFoundException(
089: "Classpath resource not found: " + resourceFileName);
090: return stream;
091: }
092:
093: /*
094: * the equal() and hashCode() method make sure, that the same resources
095: * get the same name in the SystemExternalizer.
096: */
097:
098: /**
099: * resources using the same classloader and are denoting the same
100: * name, do have the same hashCode(). Thus the same resources get the
101: * same ID in the System externalizer.
102: *
103: * @return a hashcode, comprised from the hashcodes of the classloader
104: * and from the file name of the resource.
105: */
106: @Override
107: public int hashCode() {
108: return (classLoader != null ? classLoader.hashCode() : 0)
109: ^ resourceFileName.hashCode();
110: }
111:
112: /**
113: * Two ClasspathResouces are equal if both of them use the same
114: * classloader and point to a resource with the same name.
115: *
116: * @return true if classloader and resource name are equal.
117: */
118: @Override
119: public boolean equals(Object o) {
120: if (o instanceof ClassPathResource) {
121: ClassPathResource other = (ClassPathResource) o;
122: return ((this == other) || ((classLoader == other.classLoader || (classLoader != null && classLoader
123: .equals(other.classLoader))) && resourceFileName
124: .equals(other.resourceFileName)));
125: }
126: return false;
127: }
128:
129: /**
130: * @return The stored classloader or the current context classloader if none fixed passed (prefer this case due to session serialization!)
131: */
132: protected ClassLoader getClassLoader() {
133: return this .classLoader == null ? Thread.currentThread()
134: .getContextClassLoader() : this .classLoader;
135: }
136:
137: public String getResourceName() {
138: return resourceFileName;
139: }
140: }
|