1 | /* |
2 | |
3 | Derby - Class org.apache.derby.impl.io.CPFile |
4 | |
5 | Copyright 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.impl.io; |
22 | |
23 | import org.apache.derby.io.StorageFile; |
24 | |
25 | import java.io.InputStream; |
26 | |
27 | import java.io.FileNotFoundException; |
28 | import java.net.MalformedURLException; |
29 | import java.net.URL; |
30 | |
31 | /** |
32 | * This class provides a class path based implementation of the StorageFile interface. It is used by the |
33 | * database engine to access persistent data and transaction logs under the classpath subsubprotocol. |
34 | */ |
35 | class CPFile extends InputStreamFile |
36 | { |
37 | |
38 | private final CPStorageFactory storageFactory; |
39 | |
40 | CPFile( CPStorageFactory storageFactory, String path) |
41 | { |
42 | super( storageFactory, path); |
43 | this.storageFactory = storageFactory; |
44 | } |
45 | |
46 | CPFile( CPStorageFactory storageFactory, String parent, String name) |
47 | { |
48 | super( storageFactory, parent, name); |
49 | this.storageFactory = storageFactory; |
50 | } |
51 | |
52 | CPFile( CPFile dir, String name) |
53 | { |
54 | super( dir,name); |
55 | this.storageFactory = dir.storageFactory; |
56 | } |
57 | |
58 | private CPFile( CPStorageFactory storageFactory, String child, int pathLen) |
59 | { |
60 | super( storageFactory, child, pathLen); |
61 | this.storageFactory = storageFactory; |
62 | } |
63 | |
64 | /** |
65 | * Tests whether the named file exists. |
66 | * |
67 | * @return <b>true</b> if the named file exists, <b>false</b> if not. |
68 | */ |
69 | public boolean exists() |
70 | { |
71 | return getURL() != null; |
72 | } // end of exists |
73 | |
74 | /** |
75 | * Get the parent of this file. |
76 | * |
77 | * @param pathLen the length of the parent's path name. |
78 | */ |
79 | StorageFile getParentDir( int pathLen) |
80 | { |
81 | return new CPFile( storageFactory, path, pathLen); |
82 | } |
83 | |
84 | /** |
85 | * Creates an input stream from a file name. |
86 | * |
87 | * @return an input stream suitable for reading from the file. |
88 | * |
89 | * @exception FileNotFoundException if the file is not found. |
90 | */ |
91 | public InputStream getInputStream( ) throws FileNotFoundException |
92 | { |
93 | //System.out.println("HERE FOR " + toString()); |
94 | InputStream is = null; |
95 | ClassLoader cl = Thread.currentThread().getContextClassLoader(); |
96 | if (cl != null) |
97 | is = cl.getResourceAsStream(path); |
98 | |
99 | // don't assume the context class loader is tied |
100 | // into the class loader that loaded this class. |
101 | if (is == null) |
102 | { |
103 | cl = getClass().getClassLoader(); |
104 | // Javadoc indicates implementations can use |
105 | // null as a return from Class.getClassLoader() |
106 | // to indicate the system/bootstrap classloader. |
107 | if (cl != null) |
108 | is = cl.getResourceAsStream(path); |
109 | else |
110 | is = ClassLoader.getSystemResourceAsStream(path); |
111 | } |
112 | |
113 | if (is == null) |
114 | throw new FileNotFoundException(toString()); |
115 | return is; |
116 | |
117 | } // end of getInputStream |
118 | |
119 | /** |
120 | * Return a URL for this file (resource). |
121 | * |
122 | * @see org.apache.derby.io.StorageFile#getURL() |
123 | */ |
124 | public URL getURL() { |
125 | |
126 | ClassLoader cl = Thread.currentThread().getContextClassLoader(); |
127 | URL myURL; |
128 | if (cl != null) { |
129 | myURL = cl.getResource(path); |
130 | if (myURL != null) |
131 | return myURL; |
132 | } |
133 | |
134 | // don't assume the context class loader is tied |
135 | // into the class loader that loaded this class. |
136 | cl = getClass().getClassLoader(); |
137 | // Javadoc indicates implementations can use |
138 | // null as a return from Class.getClassLoader() |
139 | // to indicate the system/bootstrap classloader. |
140 | if (cl != null) { |
141 | return cl.getResource(path); |
142 | } else { |
143 | return ClassLoader.getSystemResource(path); |
144 | } |
145 | } |
146 | } |