KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > io > CPFile


1 /*
2
3    Derby - Class org.apache.derby.impl.io.CPFile
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.io;
23
24 import org.apache.derby.io.StorageFile;
25
26 import java.io.InputStream JavaDoc;
27
28 import java.io.FileNotFoundException JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30 import java.net.URL JavaDoc;
31
32 /**
33  * This class provides a class path based implementation of the StorageFile interface. It is used by the
34  * database engine to access persistent data and transaction logs under the classpath subsubprotocol.
35  */

36 class CPFile extends InputStreamFile
37 {
38
39     private final CPStorageFactory storageFactory;
40  
41     CPFile( CPStorageFactory storageFactory, String JavaDoc path)
42     {
43         super( storageFactory, path);
44         this.storageFactory = storageFactory;
45     }
46
47     CPFile( CPStorageFactory storageFactory, String JavaDoc parent, String JavaDoc name)
48     {
49         super( storageFactory, parent, name);
50         this.storageFactory = storageFactory;
51     }
52
53     CPFile( CPFile dir, String JavaDoc name)
54     {
55         super( dir,name);
56         this.storageFactory = dir.storageFactory;
57     }
58
59     private CPFile( CPStorageFactory storageFactory, String JavaDoc child, int pathLen)
60     {
61         super( storageFactory, child, pathLen);
62         this.storageFactory = storageFactory;
63     }
64
65     /**
66      * Tests whether the named file exists.
67      *
68      * @return <b>true</b> if the named file exists, <b>false</b> if not.
69      */

70     public boolean exists()
71     {
72         return getURL() != null;
73     } // end of exists
74

75     /**
76      * Get the parent of this file.
77      *
78      * @param pathLen the length of the parent's path name.
79      */

80     StorageFile getParentDir( int pathLen)
81     {
82         return new CPFile( storageFactory, path, pathLen);
83     }
84     
85     /**
86      * Creates an input stream from a file name.
87      *
88      * @return an input stream suitable for reading from the file.
89      *
90      * @exception FileNotFoundException if the file is not found.
91      */

92     public InputStream JavaDoc getInputStream( ) throws FileNotFoundException JavaDoc
93     {
94         //System.out.println("HERE FOR " + toString());
95
InputStream JavaDoc is = null;
96         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
97         if (cl != null)
98             is = cl.getResourceAsStream(path);
99         
100         // don't assume the context class loader is tied
101
// into the class loader that loaded this class.
102
if (is == null)
103         {
104             cl = getClass().getClassLoader();
105             // Javadoc indicates implementations can use
106
// null as a return from Class.getClassLoader()
107
// to indicate the system/bootstrap classloader.
108
if (cl != null)
109                 is = cl.getResourceAsStream(path);
110             else
111                 is = ClassLoader.getSystemResourceAsStream(path);
112         }
113         
114         if (is == null)
115             throw new FileNotFoundException JavaDoc(toString());
116         return is;
117         
118     } // end of getInputStream
119

120     /**
121      * Return a URL for this file (resource).
122      *
123      * @see org.apache.derby.io.StorageFile#getURL()
124      */

125     public URL JavaDoc getURL() {
126
127         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
128         URL JavaDoc myURL;
129         if (cl != null) {
130             myURL = cl.getResource(path);
131             if (myURL != null)
132                 return myURL;
133         }
134
135         // don't assume the context class loader is tied
136
// into the class loader that loaded this class.
137
cl = getClass().getClassLoader();
138         // Javadoc indicates implementations can use
139
// null as a return from Class.getClassLoader()
140
// to indicate the system/bootstrap classloader.
141
if (cl != null) {
142             return cl.getResource(path);
143         } else {
144             return ClassLoader.getSystemResource(path);
145         }
146     }
147 }
148
Popular Tags