001: /*
002:
003: Derby - Class org.apache.derby.impl.io.JarStorageFactory
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.iapi.services.sanity.SanityManager;
025:
026: import org.apache.derby.io.StorageFactory;
027: import org.apache.derby.io.StorageFile;
028:
029: import java.io.File;
030: import java.io.FileNotFoundException;
031: import java.io.InputStream;
032: import java.io.OutputStream;
033: import java.io.IOException;
034:
035: import java.util.Properties;
036: import java.util.zip.ZipEntry;
037: import java.util.zip.ZipFile;
038:
039: /**
040: * This class provides a Jar file based implementation of the StorageFactory interface. It is used by the
041: * database engine to access persistent data and transaction logs under the jar subsubprotocol.
042: */
043:
044: public class JarStorageFactory extends BaseStorageFactory {
045: ZipFile zipData;
046:
047: /**
048: * Construct a persistent StorageFile from a path name.
049: *
050: * @param path The path name of the file
051: *
052: * @return A corresponding StorageFile object
053: */
054: StorageFile newPersistentFile(String path) {
055: return new JarDBFile(this , path);
056: }
057:
058: /**
059: * Construct a StorageFile from a directory and file name.
060: *
061: * @param directoryName The directory part of the path name. Must not be null, nor may it be in the temp dir.
062: * @param fileName The name of the file within the directory.
063: *
064: * @return A corresponding StorageFile object
065: */
066: StorageFile newPersistentFile(String directoryName, String fileName) {
067: if (directoryName == null || directoryName.length() == 0)
068: return newPersistentFile(fileName);
069: return new JarDBFile(this , directoryName, fileName);
070: }
071:
072: /**
073: * Construct a StorageFile from a directory and file name.
074: *
075: * @param directoryName The directory part of the path name.
076: * @param fileName The name of the file within the directory.
077: *
078: * @return A corresponding StorageFile object
079: */
080: StorageFile newPersistentFile(StorageFile directoryName,
081: String fileName) {
082: if (directoryName == null)
083: return newPersistentFile(fileName);
084: return new JarDBFile((JarDBFile) directoryName, fileName);
085: }
086:
087: void doInit() throws IOException {
088: if (dataDirectory == null)
089: return;
090: // Parse the dataDirectory name. It should be of the form "(jar-file)directory" or "jar-file"
091: int offset = 0;
092: while (offset < dataDirectory.length()
093: & Character.isSpaceChar(dataDirectory.charAt(offset)))
094: offset++;
095: int leftParen = -1;
096: int rightParen = -1;
097: if (offset < dataDirectory.length()) {
098: leftParen = dataDirectory.indexOf('(', offset);
099: if (leftParen >= 0)
100: rightParen = dataDirectory.indexOf(')', leftParen + 1);
101: }
102: File jarFile = null;
103: if (rightParen > 0) {
104: jarFile = getJarFile(dataDirectory.substring(leftParen + 1,
105: rightParen));
106: offset = rightParen + 1;
107: while (offset < dataDirectory.length()
108: & Character.isSpaceChar(dataDirectory
109: .charAt(offset)))
110: offset++;
111: dataDirectory = dataDirectory.substring(offset,
112: dataDirectory.length());
113: } else {
114: jarFile = getJarFile(dataDirectory);
115: dataDirectory = "";
116: }
117: zipData = new ZipFile(jarFile);
118: canonicalName = "(" + jarFile.getCanonicalPath() + ")"
119: + dataDirectory;
120: separatedDataDirectory = dataDirectory + '/'; // Zip files use '/' as a separator
121: createTempDir();
122: } // end of doInit
123:
124: private File getJarFile(String name) {
125: File jarFile = new File(name);
126: if (home != null && !jarFile.isAbsolute())
127: jarFile = new File(home, name);
128: return jarFile;
129: } // end of getJarFile
130: }
|