001: /*
002:
003: Derby - Class org.apache.derby.impl.io.CPFile
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.io;
023:
024: import org.apache.derby.io.StorageFile;
025:
026: import java.io.InputStream;
027:
028: import java.io.FileNotFoundException;
029: import java.net.MalformedURLException;
030: import java.net.URL;
031:
032: /**
033: * This class provides a class path based implementation of the StorageFile interface. It is used by the
034: * database engine to access persistent data and transaction logs under the classpath subsubprotocol.
035: */
036: class CPFile extends InputStreamFile {
037:
038: private final CPStorageFactory storageFactory;
039:
040: CPFile(CPStorageFactory storageFactory, String path) {
041: super (storageFactory, path);
042: this .storageFactory = storageFactory;
043: }
044:
045: CPFile(CPStorageFactory storageFactory, String parent, String name) {
046: super (storageFactory, parent, name);
047: this .storageFactory = storageFactory;
048: }
049:
050: CPFile(CPFile dir, String name) {
051: super (dir, name);
052: this .storageFactory = dir.storageFactory;
053: }
054:
055: private CPFile(CPStorageFactory storageFactory, String child,
056: int pathLen) {
057: super (storageFactory, child, pathLen);
058: this .storageFactory = storageFactory;
059: }
060:
061: /**
062: * Tests whether the named file exists.
063: *
064: * @return <b>true</b> if the named file exists, <b>false</b> if not.
065: */
066: public boolean exists() {
067: return getURL() != null;
068: } // end of exists
069:
070: /**
071: * Get the parent of this file.
072: *
073: * @param pathLen the length of the parent's path name.
074: */
075: StorageFile getParentDir(int pathLen) {
076: return new CPFile(storageFactory, path, pathLen);
077: }
078:
079: /**
080: * Creates an input stream from a file name.
081: *
082: * @return an input stream suitable for reading from the file.
083: *
084: * @exception FileNotFoundException if the file is not found.
085: */
086: public InputStream getInputStream() throws FileNotFoundException {
087: //System.out.println("HERE FOR " + toString());
088: InputStream is = null;
089: ClassLoader cl = Thread.currentThread().getContextClassLoader();
090: if (cl != null)
091: is = cl.getResourceAsStream(path);
092:
093: // don't assume the context class loader is tied
094: // into the class loader that loaded this class.
095: if (is == null) {
096: cl = getClass().getClassLoader();
097: // Javadoc indicates implementations can use
098: // null as a return from Class.getClassLoader()
099: // to indicate the system/bootstrap classloader.
100: if (cl != null)
101: is = cl.getResourceAsStream(path);
102: else
103: is = ClassLoader.getSystemResourceAsStream(path);
104: }
105:
106: if (is == null)
107: throw new FileNotFoundException(toString());
108: return is;
109:
110: } // end of getInputStream
111:
112: /**
113: * Return a URL for this file (resource).
114: *
115: * @see org.apache.derby.io.StorageFile#getURL()
116: */
117: public URL getURL() {
118:
119: ClassLoader cl = Thread.currentThread().getContextClassLoader();
120: URL myURL;
121: if (cl != null) {
122: myURL = cl.getResource(path);
123: if (myURL != null)
124: return myURL;
125: }
126:
127: // don't assume the context class loader is tied
128: // into the class loader that loaded this class.
129: cl = getClass().getClassLoader();
130: // Javadoc indicates implementations can use
131: // null as a return from Class.getClassLoader()
132: // to indicate the system/bootstrap classloader.
133: if (cl != null) {
134: return cl.getResource(path);
135: } else {
136: return ClassLoader.getSystemResource(path);
137: }
138: }
139: }
|