KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > resource > ClasspathResource


1 /*
2  * $Id: ClasspathResource.java,v 1.5 2005/05/13 13:16:26 neurolabs Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.resource;
15
16 import org.wings.Resource;
17 import org.wings.StaticResource;
18 import org.wings.externalizer.ExternalizeManager;
19
20 import java.io.InputStream JavaDoc;
21
22 /**
23  * A Classpath Resource is a static resource whose content is
24  * read from a classloader.
25  *
26  * @author <a HREF="mailto:haaf@mercatis.de">Armin Haaf</a>
27  * @author <a HREF="mailto:H.Zeller@acm.org">Henner Zeller</a>
28  * @version $Revision: 1.5 $
29  */

30 public class ClasspathResource
31         extends StaticResource {
32     /**
33      * The class loader from which the resource is loaded
34      */

35     protected final ClassLoader JavaDoc classLoader;
36
37     /**
38      * The name that identifies the resource in the classpath
39      */

40     protected final String JavaDoc resourceFileName;
41
42     /**
43      * A static resource that is obtained from the default classpath.
44      */

45     public ClasspathResource(String JavaDoc resourceFileName) {
46         this(Resource.class.getClassLoader(), resourceFileName, "unkonwn");
47     }
48
49     /**
50      * A static resource that is obtained from the default classpath.
51      */

52     public ClasspathResource(String JavaDoc resourceFileName, String JavaDoc mimeType) {
53         this(Resource.class.getClassLoader(), resourceFileName, mimeType);
54     }
55
56     /**
57      * A static resource that is obtained from the specified class loader
58      *
59      * @param classLoader the classLoader from which the resource is obtained
60      * @param resourceFileName the resource relative to the baseClass
61      */

62     public ClasspathResource(ClassLoader JavaDoc classLoader, String JavaDoc resourceFileName) {
63         this(classLoader, resourceFileName, "unknown");
64     }
65
66     /**
67      * A static resource that is obtained from the specified class loader
68      *
69      * @param classLoader the classLoader from which the resource is obtained
70      * @param resourceFileName the resource relative to the baseClass
71      */

72     public ClasspathResource(ClassLoader JavaDoc classLoader, String JavaDoc resourceFileName, String JavaDoc mimeType) {
73         super(null, mimeType);
74         this.classLoader = classLoader;
75         this.resourceFileName = resourceFileName;
76         int dotIndex = resourceFileName.lastIndexOf('.');
77         if (dotIndex > -1) {
78             extension = resourceFileName.substring(dotIndex + 1);
79         }
80         externalizerFlags = ExternalizeManager.GLOBAL | ExternalizeManager.FINAL;
81     }
82
83
84     public String JavaDoc toString() {
85         return getId() + " " + resourceFileName;
86     }
87
88     protected InputStream JavaDoc getResourceStream() {
89         return classLoader.getResourceAsStream(resourceFileName);
90     }
91
92     /*
93      * the equal() and hashCode() method make sure, that the same resources
94      * get the same name in the SystemExternalizer.
95      */

96
97     /**
98      * resources using the same classloader and are denoting the same
99      * name, do have the same hashCode(). Thus the same resources get the
100      * same ID in the System externalizer.
101      *
102      * @return a hashcode, comprised from the hashcodes of the classloader
103      * and from the file name of the resource.
104      */

105     public int hashCode() {
106         return classLoader.hashCode() ^ resourceFileName.hashCode();
107     }
108
109     /**
110      * Two ClasspathResouces are equal if both of them use the same
111      * classloader and point to a resource with the same name.
112      *
113      * @return true if classloader and resource name are equal.
114      */

115     public boolean equals(Object JavaDoc o) {
116         if (o instanceof ClasspathResource) {
117             ClasspathResource other = (ClasspathResource) o;
118             return ((this == other)
119                     || (classLoader.equals(other.classLoader)
120                     && resourceFileName.equals(other.resourceFileName)));
121         }
122         return false;
123     }
124 }
125
126
127
Popular Tags