KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > classloader > Resource


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: Resource.java,v 1.1 2005/07/13 11:09:06 slobodan Exp $
23  */

24
25
26
27
28
29 package com.lutris.classloader;
30
31 // lutris packages
32
// v. strahinja, 24 sep 2002
33
import java.io.ByteArrayOutputStream JavaDoc;
34 import java.io.FileNotFoundException JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.InputStream JavaDoc;
37
38 import com.lutris.logging.LogChannel;
39
40 /**
41  * <P>A resource that is a file existing on the local machine or a remote
42  * machine. The properties of a resource include file name, location, size,
43  * and last-modified time. The location of the resource is represented by a
44  * <CODE>ClassPathEntry</CODE> and is either a directory or a zip file.
45  * The file name, described by a String, is relative to the specified location.
46  * The resource size is the file size in bytes and the resource time is the
47  * last-modified time of the file in milliseconds.
48  *
49  * <P>This is an abstract class. All subclasses must determine the size
50  * and last-modified time for the resource, and must implement the
51  * <CODE>getInputStream</CODE> method.
52  *
53  * <P>
54  * Resources can be ASCII or binary files, such as text files, HTML files,
55  * Java source-code files, Java byte-code files, Java archive (JAR) files, and
56  * gif files. Some examples resource file names are:
57  *
58  * <PRE>
59  * /users/kristen/text/schedule.txt
60  * /webserver/htdocs/index.html
61  * /jdk1.1.5/src/java/util/Enumeration.java
62  * /jdk1.1.5/classes/java/util/Enumeration.class
63  * /users/kristen/lutris/lutris.jar
64  * /webserver/htdocs/banner.gif
65  * </PRE>
66  *
67  * Since the file names are relative to the location, the beginning slash ("/")
68  * does NOT mean that the file name is given as the absolute path on its
69  * host machine.
70  *
71  * @author Kristen Pol, Lutris Technologies
72  * @version $Revision : 1.1 $
73  * @see com.lutris.classloader.ClassPathEntry
74  */

75 public abstract class Resource {
76
77     // protected data members
78

79     /** The file name of the resource relative to the location. */
80     protected String JavaDoc name = null;
81
82     /** The location of the resource, which can be local or remote. */
83     protected ClassPathEntry location = null;
84
85     /** The size of the resource in bytes. */
86     protected long size = -1;
87
88     /** The last-modified time of the resource when this object was created. */
89     protected long lastModifiedTime = -1;
90
91     /** Is logging enabled? */
92     protected boolean loggingEnabled = false;
93
94     /** Log channel to write messages to */
95 // v. strahinja, 24 sep 2002
96
protected LogChannel logChannel;
97 // protected static Logger logger;
98

99     /** Numeric log level number for LOGLEVEL string */
100 // v. strahinja, 24 sep 2002
101
protected int logLevel;
102 // protected Level logLevel;
103

104     // constructors
105

106     /**
107      * Constructs resource with specified name, location and log channel.
108      * The file name must be described relative to the location. The location,
109      * described by the <CODE>ClassPathEntry</CODE>, can be a directory
110      * or a zip file on the local or a remote machine.
111      *
112      * <P>Assumes that all slashes in the <CODE>name</CODE> parameter are
113      * forward slashes ("/");
114      *
115      * @param name The file name of the resource.
116      * @param location The location of the resource.
117      * @param loadLogChannel The log channel for logging.
118      * @exception NullPointerException if either the <CODE>name</CODE> or
119      * <CODE>location</CODE> parameters are null.
120      * @see ClassPathEntry
121      */

122     protected Resource(String JavaDoc name, ClassPathEntry location,
123 // v. strahinja, 24 sep 2002
124
LogChannel loadLogChannel)
125 // Logger loadLogger)
126
throws NullPointerException JavaDoc {
127 // v. strahinja, 24 sep 2002
128
this.logChannel = loadLogChannel;
129 // v. strahinja, 24 sep 2002
130
if (logChannel != null) {
131 // v. strahinja, 24 sep 2002
132
this.logLevel = logChannel.getLevel(MultiClassLoader.LOG_LEVEL);
133 // v. strahinja, 24 sep 2002
134
loggingEnabled = logChannel.isEnabled(logLevel);
135 // this.logger = loadLogger;
136
// if (logger != null) {
137
// this.logLevel = MultiClassLoader.LOG_LEVEL;
138
// loggingEnabled = logger.isEnabledFor(logLevel);
139
}
140     if (name == null || location == null) {
141         throw new NullPointerException JavaDoc();
142     }
143     this.name = name;
144     this.location = location;
145     }
146
147     // public methods
148

149     /**
150      * Gets file name of resource set previously by constructor.
151      *
152      * @return the file name of the resource represented by a
153      * <CODE>String</CODE>.
154      */

155     public String JavaDoc getName() {
156     return name;
157     }
158
159     /**
160      * Gets location of resource set previously by constructor.
161      *
162      * @return the location of the resource represented by a
163      * <CODE>ClassPathEntry</CODE>.
164      */

165     public ClassPathEntry getLocation() {
166     return location;
167     }
168
169     /**
170      * Stringifies resource described previously by constructor. The
171      * the file name and location are concatenated together to represent
172      * the resource.
173      *
174      * @return the resource represented by a <CODE>String</CODE> composed of
175      * the file name and location.
176      */

177     public String JavaDoc toString() {
178     return "[" + location + "][" + name + "]";
179     }
180
181     /**
182      * Gets size of resource in bytes.
183      *
184      * <P>All subclasses are expected to determine this size prior to
185      * this method being called.
186      *
187      * @return the size of the resource in bytes.
188      */

189     public long getSize() {
190     return size;
191     }
192
193     /**
194      * Gets last-modification time of resource at the time this
195      * Resource object was created.
196      *
197      * <P>All subclasses are expected to determine this time prior to
198      * this method being called.
199      *
200      * @return the last-modified time of the resource in milliseconds.
201      */

202     public long getLastModifiedTime() {
203     return lastModifiedTime;
204     }
205
206     /**
207      * Gets last-modified time of resource in milliseconds.
208      *
209      * @deprectated
210      * @see getlastModifiedTime
211      */

212     public long getTime() {
213     return lastModifiedTime;
214     }
215
216     /**
217      * Get current last-modification time of resource. This is the
218      * time on the disk file the resource is associated with.
219      *
220      * @return the last-modified time of the permanent copy of the resource
221      * in milliseconds.
222      */

223     public abstract long getCurrentLastModifiedTime() throws FileNotFoundException JavaDoc;
224
225     /**
226      * Determine if the resource has been modified since it was loaded.
227      *
228      * @return <CODE>true</CODE> if the resource has been modified,
229      * <CODE>false</CODE> if not.
230      */

231     public boolean hasBeenModified() throws FileNotFoundException JavaDoc {
232         long currentTime = getCurrentLastModifiedTime();
233         if (loggingEnabled) {
234 // v. strahinja, 24 sep 2002
235
logChannel.write(logLevel, "hasBeenModified: " + getName()
236 // logger.log(logLevel, "hasBeenModified: " + getName()
237
+ ": current time=" + currentTime
238                              + ", last time=" + lastModifiedTime);
239         }
240         return (currentTime > lastModifiedTime);
241     }
242
243     /**
244      * Get the resouce as a byte array when the size is known.
245      *
246      * @param inputStream Resource input stream
247      * @return an array of bytes representing the resource.
248      * @exception IOException if the byte array can not be constructed.
249      */

250     private byte[] getBytesKnowSize(InputStream JavaDoc inputStream)
251         throws IOException JavaDoc {
252         byte[] bytes = new byte[(int)size];
253         int bytesRead;
254
255         // Even though the total size to read is known, there is
256
// no guarantee that it's returned all in one read.
257
int idx = 0;
258         while (idx < size) {
259             if (inputStream.available() == 0) {
260                 break;
261             }
262             if ((bytesRead = inputStream.read(bytes, idx, ((int)size)-idx)) == -1) {
263                 break;
264             }
265             idx += bytesRead;
266         }
267
268         if (idx != size) {
269             throw new IOException JavaDoc("Read of resource expected " + size
270                                   + " bytes, got " + idx + " bytes");
271         }
272         return bytes;
273     }
274
275     /**
276      * Get the resouce as a byte array when the size is not known.
277      *
278      * @param inputStream Resource input stream
279      * @return an array of bytes representing the resource.
280      * @exception IOException if the byte array can not be constructed.
281      */

282     private byte[] getBytesUnknowSize(InputStream JavaDoc inputStream)
283         throws IOException JavaDoc {
284         int bytesRead;
285         byte[] byteBuffer = new byte[10240];
286         ByteArrayOutputStream JavaDoc byteStream = new ByteArrayOutputStream JavaDoc();
287         while (true) {
288             if (inputStream.available() == 0) {
289                 break;
290             }
291             if ((bytesRead = inputStream.read(byteBuffer)) == -1) {
292                 break;
293             }
294             byteStream.write(byteBuffer, 0, bytesRead);
295         }
296         return byteStream.toByteArray();
297     }
298
299     /**
300      * Gets byte array representing resource.
301      *
302      * <P>This method relies on the implementation of
303      * <CODE>getInputStream</CODE>.
304      *
305      * @return an array of bytes representing the resource.
306      * @exception IOException if the byte array can not be constructed.
307      */

308     public byte[] getBytes() throws IOException JavaDoc {
309     InputStream JavaDoc inputStream = getInputStream();
310     if (inputStream == null) {
311             throw new IOException JavaDoc("Null InputStream for resource, " + this);
312         }
313         try {
314             if (size < 0) {
315                 return getBytesUnknowSize(inputStream);
316             } else {
317                 return getBytesKnowSize(inputStream);
318             }
319         } finally {
320             inputStream.close();
321         }
322     }
323
324     /**
325      * Gets input stream representing resource.
326      *
327      * <P>This method is abstract and must be implemented by all subclasses.
328      * The <CODE>getBytes</CODE> method also depends upon this implementation.
329      *
330      * @return the input stream that represents the resource.
331      * @exception IOException if the input stream can not be constructed.
332      */

333     public abstract InputStream JavaDoc getInputStream() throws IOException JavaDoc;
334
335     /**
336      * Determines if the specified resource is equal to this resource.
337      * Resources are considered equal if the file names, locations, sizes,
338      * and last-modified times are all the same.
339      *
340      * @return true if the resources are equal, false if not.
341      */

342     public boolean equals(Resource resource) {
343     if (name.equals(resource.getName())
344             && location.equals(resource.getLocation())
345             && (size == resource.getSize())
346             && (lastModifiedTime == resource.getLastModifiedTime())) {
347         return true;
348     }
349     return false;
350     }
351 }
352
Popular Tags