KickJava   Java API By Example, From Geeks To Geeks.

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


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: RemoteZipResource.java,v 1.2 2005/03/24 10:51:25 slobodan Exp $
23  */

24
25
26
27
28
29 package com.lutris.classloader;
30
31 // lutris packages
32
// v. strahinja, 27 sep 2002
33
import java.io.FileNotFoundException JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import java.net.URL JavaDoc;
37 import java.util.zip.ZipEntry JavaDoc;
38 import java.util.zip.ZipInputStream JavaDoc;
39
40 import com.lutris.logging.LogChannel;
41 import com.lutris.util.FatalExceptionError;
42
43 /**
44  * <P>A <CODE>Resource</CODE> that is an entry in
45  * a specified zip file on a remote machine. The zip file is represented by a
46  * <CODE>ClassPathEntry</CODE>, and the filename is specified by a String.
47  *
48  * @author Kristen Pol, Lutris Technologies
49  * @version $Revision : 1.1 $
50  * @see com.lutris.classloader.MultiClassLoader
51  * @see com.lutris.classloader.ClassPathEntry
52  * @see com.lutris.classloader.Resource
53  * @see java.io.File
54  */

55 public class RemoteZipResource extends Resource {
56
57     // data members
58

59     URL JavaDoc zipFileURL = null;
60
61     // constructors
62

63     // FIXME: Test and change to protected and add javadoc. (kp)
64
private RemoteZipResource(String JavaDoc name, ClassPathEntry location,
65 // v. strahinja, 27 sep 2002
66
LogChannel loadLogChannel)
67 // Logger loadLogger)
68
throws FileNotFoundException JavaDoc {
69 // v. strahinja, 27 sep 2002
70
super(name, location, loadLogChannel);
71 // super(name, location, loadLogger);
72

73         // Get location's URL
74
zipFileURL = location.getURL();
75         if (zipFileURL == null) {
76         String JavaDoc error = "The URL for location, " + location + ", is null";
77 // v. strahinja, 27 sep 2002
78
logChannel.write(logLevel, error);
79 // logger.log(logLevel, error);
80
throw new FileNotFoundException JavaDoc(error);
81         }
82
83     // Make sure the ZipEntry exists in the remote zip file
84
// FileNotFoundException will be thrown if it is not found
85
ZipInputStream JavaDoc zipStream = null;
86     try {
87         zipStream = getZipInputStream();
88         if (zipStream == null) {
89         String JavaDoc error = "URL \"" + zipFileURL + "\" does not exist, " +
90             "can not be reached, or is not a zip file; or the " +
91             "resource \"" + name + "\" is not in the zip file";
92 // v. strahinja, 27 sep 2002
93
logChannel.write(logLevel, error);
94 // logger.log(logLevel, error);
95
throw new FileNotFoundException JavaDoc(error);
96         }
97     } finally {
98         if (zipStream != null) {
99         try {
100             zipStream.close();
101         } catch (IOException JavaDoc ioe) {
102             // This should not happen because it was already checked
103
String JavaDoc error = ioe.toString() + ": URL zip file \"" +
104             zipFileURL + "\" is corrupt or the connection died";
105 // v. strahinja, 27 sep 2002
106
logChannel.write(logLevel, error);
107 // logger.log(logLevel, error);
108
throw new FatalExceptionError(new IOException JavaDoc(error));
109         }
110         }
111     }
112     }
113
114     // public methods
115

116     /**
117      * Gets the specified resource as an input stream.
118      *
119      * @return an <CODE>InputStream</CODE> representing the specified resource.
120      * @exception an <CODE>IOException</CODE>
121      */

122     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
123     try {
124         return getZipInputStream();
125     } catch (FileNotFoundException JavaDoc fnfe) {
126         // This should not happen because it was already checked
127
String JavaDoc error = "URL zip file \"" + zipFileURL +
128         "\" has disappeared or can no longer be reached; or the " +
129         "resource \"" + name + "\" is no longer in the zip file";
130 // v. strahinja, 27 sep 2002
131
logChannel.write(logLevel, error);
132 // logger.log(logLevel, error);
133
throw new FatalExceptionError(new FileNotFoundException JavaDoc(error));
134     }
135     }
136
137     // Get the input stream for the resource to if it exists.
138
private ZipInputStream JavaDoc getZipInputStream() throws FileNotFoundException JavaDoc {
139         ZipInputStream JavaDoc zipStream;
140         try {
141             zipStream = new ZipInputStream JavaDoc(zipFileURL.openStream());
142             while (zipStream.available() > 0) {
143                 ZipEntry JavaDoc entry = zipStream.getNextEntry();
144                 if (entry == null) {
145 // v. strahinja, 27 sep 2002
146
logChannel.write(logLevel, "Null zip entry.");
147 // logger.log(logLevel, "Null zip entry.");
148
throw new IOException JavaDoc();
149                 }
150                 if (entry.getName().equals(name)) {
151             size = entry.getSize();
152             lastModifiedTime = entry.getTime();
153                     return zipStream;
154                 }
155                 zipStream.closeEntry();
156             }
157         } catch (IOException JavaDoc e) {
158             String JavaDoc error = "URL, " + zipFileURL + ", does not exist, " +
159         "can not be reached, is not a zip file, or the resource, " +
160         name + ", is not in the zip file";
161 // v. strahinja, 27 sep 2002
162
logChannel.write(logLevel, error);
163 // logger.log(logLevel, error);
164
throw new FileNotFoundException JavaDoc(error);
165         }
166     return null;
167     }
168
169     /**
170      * Get current last-modification time of resource. This is the
171      * time on the disk file the resource is associated with.
172      *
173      * @return the last-modified time of the permanent copy of the resource
174      * in milliseconds.
175      */

176     public long getCurrentLastModifiedTime() {
177         return -1; //FIXME: implement
178
}
179 }
180
Popular Tags