1 | /* |
2 | |
3 | Derby - Class org.apache.derby.impl.io.JarStorageFactory |
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.iapi.services.sanity.SanityManager; |
24 | |
25 | import org.apache.derby.io.StorageFactory; |
26 | import org.apache.derby.io.StorageFile; |
27 | |
28 | import java.io.File; |
29 | import java.io.FileNotFoundException; |
30 | import java.io.InputStream; |
31 | import java.io.OutputStream; |
32 | import java.io.IOException; |
33 | |
34 | import java.util.Properties; |
35 | import java.util.zip.ZipEntry; |
36 | import java.util.zip.ZipFile; |
37 | |
38 | /** |
39 | * This class provides a Jar file based implementation of the StorageFactory interface. It is used by the |
40 | * database engine to access persistent data and transaction logs under the jar subsubprotocol. |
41 | */ |
42 | |
43 | public class JarStorageFactory extends BaseStorageFactory |
44 | { |
45 | ZipFile zipData; |
46 | |
47 | /** |
48 | * Construct a persistent StorageFile from a path name. |
49 | * |
50 | * @param path The path name of the file |
51 | * |
52 | * @return A corresponding StorageFile object |
53 | */ |
54 | StorageFile newPersistentFile( String path) |
55 | { |
56 | return new JarDBFile( this, path); |
57 | } |
58 | |
59 | /** |
60 | * Construct a StorageFile from a directory and file name. |
61 | * |
62 | * @param directoryName The directory part of the path name. Must not be null, nor may it be in the temp dir. |
63 | * @param fileName The name of the file within the directory. |
64 | * |
65 | * @return A corresponding StorageFile object |
66 | */ |
67 | StorageFile newPersistentFile( String directoryName, String fileName) |
68 | { |
69 | if( directoryName == null || directoryName.length() == 0) |
70 | return newPersistentFile( fileName); |
71 | return new JarDBFile( this, directoryName, fileName); |
72 | } |
73 | |
74 | /** |
75 | * Construct a StorageFile from a directory and file name. |
76 | * |
77 | * @param directoryName The directory part of the path name. |
78 | * @param fileName The name of the file within the directory. |
79 | * |
80 | * @return A corresponding StorageFile object |
81 | */ |
82 | StorageFile newPersistentFile( StorageFile directoryName, String fileName) |
83 | { |
84 | if( directoryName == null) |
85 | return newPersistentFile( fileName); |
86 | return new JarDBFile( (JarDBFile) directoryName, fileName); |
87 | } |
88 | |
89 | void doInit() throws IOException |
90 | { |
91 | if( dataDirectory == null) |
92 | return; |
93 | // Parse the dataDirectory name. It should be of the form "(jar-file)directory" or "jar-file" |
94 | int offset = 0; |
95 | while( offset < dataDirectory.length() & Character.isSpaceChar( dataDirectory.charAt( offset))) |
96 | offset ++; |
97 | int leftParen = -1; |
98 | int rightParen = -1; |
99 | if( offset < dataDirectory.length()) |
100 | { |
101 | leftParen = dataDirectory.indexOf( '(', offset); |
102 | if( leftParen >= 0) |
103 | rightParen = dataDirectory.indexOf( ')', leftParen + 1); |
104 | } |
105 | File jarFile = null; |
106 | if( rightParen > 0) |
107 | { |
108 | jarFile = getJarFile( dataDirectory.substring( leftParen + 1, rightParen)); |
109 | offset = rightParen + 1; |
110 | while( offset < dataDirectory.length() & Character.isSpaceChar( dataDirectory.charAt( offset))) |
111 | offset ++; |
112 | dataDirectory = dataDirectory.substring( offset, dataDirectory.length()); |
113 | } |
114 | else |
115 | { |
116 | jarFile = getJarFile( dataDirectory); |
117 | dataDirectory = ""; |
118 | } |
119 | zipData = new ZipFile( jarFile); |
120 | canonicalName = "(" + jarFile.getCanonicalPath() + ")" + dataDirectory; |
121 | separatedDataDirectory = dataDirectory + '/'; // Zip files use '/' as a separator |
122 | createTempDir(); |
123 | } // end of doInit |
124 | |
125 | private File getJarFile( String name) |
126 | { |
127 | File jarFile = new File( name); |
128 | if( home != null && !jarFile.isAbsolute()) |
129 | jarFile = new File( home, name); |
130 | return jarFile; |
131 | } // end of getJarFile |
132 | } |