KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.io.JarStorageFactory
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.iapi.services.sanity.SanityManager;
25
26 import org.apache.derby.io.StorageFactory;
27 import org.apache.derby.io.StorageFile;
28
29 import java.io.File JavaDoc;
30 import java.io.FileNotFoundException JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34
35 import java.util.Properties JavaDoc;
36 import java.util.zip.ZipEntry JavaDoc;
37 import java.util.zip.ZipFile JavaDoc;
38
39 /**
40  * This class provides a Jar file based implementation of the StorageFactory interface. It is used by the
41  * database engine to access persistent data and transaction logs under the jar subsubprotocol.
42  */

43
44 public class JarStorageFactory extends BaseStorageFactory
45 {
46     ZipFile JavaDoc zipData;
47     
48     /**
49      * Construct a persistent StorageFile from a path name.
50      *
51      * @param path The path name of the file
52      *
53      * @return A corresponding StorageFile object
54      */

55     StorageFile newPersistentFile( String JavaDoc path)
56     {
57         return new JarDBFile( this, path);
58     }
59
60     /**
61      * Construct a StorageFile from a directory and file name.
62      *
63      * @param directoryName The directory part of the path name. Must not be null, nor may it be in the temp dir.
64      * @param fileName The name of the file within the directory.
65      *
66      * @return A corresponding StorageFile object
67      */

68     StorageFile newPersistentFile( String JavaDoc directoryName, String JavaDoc fileName)
69     {
70         if( directoryName == null || directoryName.length() == 0)
71             return newPersistentFile( fileName);
72         return new JarDBFile( this, directoryName, fileName);
73     }
74
75     /**
76      * Construct a StorageFile from a directory and file name.
77      *
78      * @param directoryName The directory part of the path name.
79      * @param fileName The name of the file within the directory.
80      *
81      * @return A corresponding StorageFile object
82      */

83     StorageFile newPersistentFile( StorageFile directoryName, String JavaDoc fileName)
84     {
85         if( directoryName == null)
86             return newPersistentFile( fileName);
87         return new JarDBFile( (JarDBFile) directoryName, fileName);
88     }
89
90     void doInit() throws IOException JavaDoc
91     {
92         if( dataDirectory == null)
93             return;
94         // Parse the dataDirectory name. It should be of the form "(jar-file)directory" or "jar-file"
95
int offset = 0;
96         while( offset < dataDirectory.length() & Character.isSpaceChar( dataDirectory.charAt( offset)))
97             offset ++;
98         int leftParen = -1;
99         int rightParen = -1;
100         if( offset < dataDirectory.length())
101         {
102             leftParen = dataDirectory.indexOf( '(', offset);
103             if( leftParen >= 0)
104                 rightParen = dataDirectory.indexOf( ')', leftParen + 1);
105         }
106         File jarFile = null;
107         if( rightParen > 0)
108         {
109             jarFile = getJarFile( dataDirectory.substring( leftParen + 1, rightParen));
110             offset = rightParen + 1;
111             while( offset < dataDirectory.length() & Character.isSpaceChar( dataDirectory.charAt( offset)))
112                 offset ++;
113             dataDirectory = dataDirectory.substring( offset, dataDirectory.length());
114         }
115         else
116         {
117             jarFile = getJarFile( dataDirectory);
118             dataDirectory = "";
119         }
120         zipData = new ZipFile JavaDoc( jarFile);
121         canonicalName = "(" + jarFile.getCanonicalPath() + ")" + dataDirectory;
122         separatedDataDirectory = dataDirectory + '/'; // Zip files use '/' as a separator
123
createTempDir();
124     } // end of doInit
125

126     private File getJarFile( String JavaDoc name)
127     {
128         File jarFile = new File( name);
129         if( home != null && !jarFile.isAbsolute())
130             jarFile = new File( home, name);
131         return jarFile;
132     } // end of getJarFile
133
}
134
Popular Tags