Source code: com/lutris/classloader/LocalDirResource.java
1 /*
2 * Enhydra Java Application Server Project
3 *
4 * The contents of this file are subject to the Enhydra Public License
5 * Version 1.1 (the "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy of the License on
7 * the Enhydra web site ( http://www.enhydra.org/ ).
8 *
9 * Software distributed under the License is distributed on an "AS IS"
10 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11 * the License for the specific terms governing rights and limitations
12 * under the License.
13 *
14 * The Initial Developer of the Enhydra Application Server is Lutris
15 * Technologies, Inc. The Enhydra Application Server and portions created
16 * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17 * All Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 * $Id: LocalDirResource.java,v 1.16.12.2 2000/10/19 17:58:53 jasona Exp $
22 */
23
24
25
26
27
28 package com.lutris.classloader;
29
30 // lutris packages
31 import com.lutris.logging.LogChannel;
32 import com.lutris.util.FatalExceptionError;
33
34 // io packages
35 import java.io.File;
36 import java.io.InputStream;
37 import java.io.FileInputStream;
38
39 // io exceptions
40 import java.io.IOException;
41 import java.io.FileNotFoundException;
42
43 /**
44 * <P>A <CODE>Resource</CODE> that is a file on the local machine in
45 * a specified directory. The directory 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.ClassPathEntry
51 * @see com.lutris.classloader.Resource
52 * @see java.io.File
53 */
54 public class LocalDirResource extends Resource {
55
56 // private data members
57
58 /** The file that represents this resource. */
59 private File file = null;
60
61 // constructors
62
63 /**
64 * Constructs local directory resource with specified name and location.
65 *
66 * @param name The file name of the resource.
67 * @param location The location of the resource.
68 * @param loadLogChannel The log channel for logging.
69 * @exception FileNotFoundException
70 * thrown if the desired file does not exist, does not have
71 * read permission or if the desired file exists above the
72 * relative root of the <code>LocalDirResource</code>.
73 * @see Resource
74 * @see ClassPathEntry
75 */
76 protected LocalDirResource(String name,
77 ClassPathEntry location,
78 LogChannel loadLogChannel)
79 throws FileNotFoundException {
80 super(name, location, loadLogChannel);
81 String locName = location.getName();
82 if (locName == null) {
83 throw new FileNotFoundException("The name for location, "
84 + location + ", is null");
85 }
86 file = new File(locName, name);
87 if (!file.exists() || !file.canRead()) {
88 File tmpFile = file;
89 file = null;
90 throw new FileNotFoundException("File, " +
91 tmpFile.getAbsolutePath() +
92 ", does not exist or does not " +
93 "have read permission");
94 }
95
96 String path = null;
97 String parentPath = null;
98
99 try {
100 parentPath = new File(locName).getCanonicalPath();
101 } catch (IOException e) {
102 file = null;
103 throw new FileNotFoundException("Classpath Directory " + locName +
104 " cannot be resolved: " +
105 e.toString());
106 }
107
108 try {
109 path = file.getCanonicalPath();
110 } catch (IOException e) {
111 File tmpFile = file;
112 file = null;
113 throw new FileNotFoundException("File " +
114 tmpFile.getAbsolutePath() +
115 " cannot be resolved: " +
116 e.toString());
117 }
118
119 if (path.startsWith(parentPath) == false) {
120 File tmpFile = file;
121 file = null;
122 throw new FileNotFoundException("File, " + tmpFile +
123 " does not live under " + locName);
124 }
125
126 size = file.length();
127 lastModifiedTime = file.lastModified();
128 }
129
130 // public methods
131
132 /**
133 * Gets input stream representing resource.
134 *
135 * @return the input stream that represents the resource.
136 * @exception IOException if the input stream can not be constructed.
137 * @see InputStream
138 */
139 public InputStream getInputStream() throws IOException {
140 try {
141 return new FileInputStream(file);
142 } catch (FileNotFoundException e) {
143 throw new FatalExceptionError(e);
144 }
145 }
146
147 /**
148 * Get current last-modification time of resource. This is the
149 * time on the disk file the resource is associated with.
150 *
151 * @return the last-modified time of the permanent copy of the resource
152 * in milliseconds.
153 */
154 public long getCurrentLastModifiedTime() throws FileNotFoundException {
155 return file.lastModified();
156 }
157
158 /**
159 * Get the file associate with this resource.
160 */
161 public File getFile() {
162 return file;
163 }
164 }