KickJava   Java API By Example, From Geeks To Geeks.

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


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: LocalDirResource.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, 24 sep 2002
33
import java.io.File JavaDoc;
34 import java.io.FileInputStream JavaDoc;
35 import java.io.FileNotFoundException JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.InputStream JavaDoc;
38
39 import com.lutris.logging.LogChannel;
40 import com.lutris.util.FatalExceptionError;
41
42 /**
43  * <P>A <CODE>Resource</CODE> that is a file on the local machine in
44  * a specified directory. The directory is represented by a
45  * <CODE>ClassPathEntry</CODE>, and the filename is specified by a String.
46  *
47  * @author Kristen Pol, Lutris Technologies
48  * @version $Revision : 1.1 $
49  * @see com.lutris.classloader.ClassPathEntry
50  * @see com.lutris.classloader.Resource
51  * @see java.io.File
52  */

53 public class LocalDirResource extends Resource {
54
55     // private data members
56

57     /** The file that represents this resource. */
58     private File JavaDoc file = null;
59
60     // constructors
61

62     /**
63      * Constructs local directory resource with specified name and location.
64      *
65      * @param name The file name of the resource.
66      * @param location The location of the resource.
67      * @param loadLogChannel The log channel for logging.
68      * @exception FileNotFoundException
69      * thrown if the desired file does not exist, does not have
70      * read permission or if the desired file exists above the
71      * relative root of the <code>LocalDirResource</code>.
72      * @see Resource
73      * @see ClassPathEntry
74      */

75     protected LocalDirResource(String JavaDoc name,
76                    ClassPathEntry location,
77 // v. strahinja, 24 sep 2002
78
LogChannel loadLogChannel)
79 // Logger loadLogger)
80
throws FileNotFoundException JavaDoc {
81 // v. strahinja, 24 sep 2002
82
super(name, location, loadLogChannel);
83 // super(name, location, loadLogger);
84
String JavaDoc locName = location.getName();
85     if (locName == null) {
86         throw new FileNotFoundException JavaDoc("The name for location, "
87                                             + location + ", is null");
88     }
89     file = new File JavaDoc(locName, name);
90     if (!file.exists() || !file.canRead()) {
91         File JavaDoc tmpFile = file;
92         file = null;
93         throw new FileNotFoundException JavaDoc("File, " +
94                         tmpFile.getAbsolutePath() +
95                                             ", does not exist or does not " +
96                                             "have read permission");
97     }
98
99         String JavaDoc path = null;
100         String JavaDoc parentPath = null;
101
102         try {
103             parentPath = new File JavaDoc(locName).getCanonicalPath();
104         } catch (IOException JavaDoc e) {
105             file = null;
106             throw new FileNotFoundException JavaDoc("Classpath Directory " + locName +
107                                             " cannot be resolved: " +
108                         e.toString());
109         }
110
111         try {
112             path = file.getCanonicalPath();
113         } catch (IOException JavaDoc e) {
114             File JavaDoc tmpFile = file;
115             file = null;
116             throw new FileNotFoundException JavaDoc("File " +
117                                             tmpFile.getAbsolutePath() +
118                                             " cannot be resolved: " +
119                         e.toString());
120         }
121
122         if (path.startsWith(parentPath) == false) {
123             File JavaDoc tmpFile = file;
124             file = null;
125             throw new FileNotFoundException JavaDoc("File, " + tmpFile +
126                                             " does not live under " + locName);
127         }
128
129     size = file.length();
130     lastModifiedTime = file.lastModified();
131     }
132
133     // public methods
134

135     /**
136      * Gets input stream representing resource.
137      *
138      * @return the input stream that represents the resource.
139      * @exception IOException if the input stream can not be constructed.
140      * @see InputStream
141      */

142     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
143     try {
144         return new FileInputStream JavaDoc(file);
145     } catch (FileNotFoundException JavaDoc e) {
146         throw new FatalExceptionError(e);
147     }
148     }
149
150     /**
151      * Get current last-modification time of resource. This is the
152      * time on the disk file the resource is associated with.
153      *
154      * @return the last-modified time of the permanent copy of the resource
155      * in milliseconds.
156      */

157     public long getCurrentLastModifiedTime() throws FileNotFoundException JavaDoc {
158         return file.lastModified();
159     }
160
161     /**
162      * Get the file associate with this resource.
163      */

164     public File JavaDoc getFile() {
165         return file;
166     }
167 }
168
Popular Tags