KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.io.BaseStorageFactory
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.store.raw.data.DataFactory;
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.IOException JavaDoc;
31
32 /**
33  * This class provides a base for implementations of the StorageFactory interface. It is used by the
34  * database engine to access persistent data and transaction logs under the directory (default) subsubprotocol.
35  */

36
37 abstract class BaseStorageFactory implements StorageFactory
38 {
39
40     String JavaDoc home;
41     protected StorageFile tempDir;
42     protected String JavaDoc tempDirPath;
43     protected String JavaDoc dataDirectory;
44     protected String JavaDoc separatedDataDirectory; // dataDirectory + separator
45
protected String JavaDoc uniqueName;
46     protected String JavaDoc canonicalName;
47     private static final String JavaDoc TEMP_DIR_PREFIX = "derbytmp_";
48
49     /**
50      * Most of the initialization is done in the init method.
51      */

52     BaseStorageFactory()
53     {}
54
55     /**
56      * Classes implementing the StorageFactory interface must have a null
57      * constructor. This method is called when the database is booted up to
58      * initialize the class. It should perform all actions necessary to start the
59      * basic storage, such as creating a temporary file directory.
60      *
61      * The init method will be called once, before any other method is called, and will not
62      * be called again.
63      *
64      * @param home The name of the directory containing the database. It comes from the system.home system property.
65      * It may be null. A storage factory may decide to ignore this parameter. (For instance the classpath
66      * storage factory ignores it.
67      * @param databaseName The name of the database (directory). All relative pathnames are relative to this directory.
68      * If null then the storage factory will only be used to deal with the directory containing
69      * the databases.
70      * @param tempDirName The name of the temporary file directory set in properties. If null then a default
71      * directory should be used. Each database should get a separate temporary file
72      * directory within this one to avoid collisions.
73      * @param uniqueName A unique name that can be used to create the temporary file directory for this database.
74      *
75      * @exception IOException on an error (unexpected).
76      */

77     public void init( String JavaDoc home, String JavaDoc databaseName, String JavaDoc tempDirName, String JavaDoc uniqueName)
78         throws IOException JavaDoc
79     {
80         if( databaseName != null)
81         {
82             dataDirectory = databaseName;
83             separatedDataDirectory = databaseName + getSeparator();
84         }
85         this.home = home;
86         this.uniqueName = uniqueName;
87         tempDirPath = tempDirName;
88         doInit();
89     } // end of init
90

91     abstract void doInit() throws IOException JavaDoc;
92     
93     public void shutdown()
94     {
95     }
96     
97
98     /**
99      * Get the canonical name of the database. This is a name that uniquely identifies it. It is system dependent.
100      *
101      * The normal, disk based implementation uses method java.io.File.getCanonicalPath on the directory holding the
102      * database to construct the canonical name.
103      *
104      * @return the canonical name
105      *
106      * @exception IOException if an IO error occurred during the construction of the name.
107      */

108     public String JavaDoc getCanonicalName() throws IOException JavaDoc
109     {
110         return canonicalName;
111     }
112     
113     /**
114      * Construct a StorageFile from a path name.
115      *
116      * @param path The path name of the file
117      *
118      * @return A corresponding StorageFile object
119      */

120     public StorageFile newStorageFile( String JavaDoc path)
121     {
122         if( path != null && tempDirPath != null && path.startsWith( tempDirPath))
123             return new DirFile( path);
124         return newPersistentFile( path);
125     }
126     
127     /**
128      * Construct a StorageFile from a directory and file name.
129      *
130      * @param directoryName The directory part of the path name.
131      * @param fileName The name of the file within the directory.
132      *
133      * @return A corresponding StorageFile object
134      */

135     public StorageFile newStorageFile( String JavaDoc directoryName, String JavaDoc fileName)
136     {
137         if( directoryName == null)
138             return newStorageFile( fileName);
139         else if( tempDirPath != null && directoryName.startsWith( tempDirPath))
140             return new DirFile(directoryName, fileName);
141         else
142             return newPersistentFile( directoryName, fileName);
143     }
144     
145     /**
146      * Construct a StorageFile from a directory and file name.
147      *
148      * @param directoryName The directory part of the path name.
149      * @param fileName The name of the file within the directory.
150      *
151      * @return A corresponding StorageFile object
152      */

153     public StorageFile newStorageFile( StorageFile directoryName, String JavaDoc fileName)
154     {
155         if( directoryName == null)
156             return newStorageFile( fileName);
157         if( fileName == null)
158             return directoryName;
159         else if (tempDirPath != null && directoryName.getPath().startsWith(tempDirPath))
160             return new DirFile( (DirFile) directoryName, fileName);
161         return newPersistentFile( directoryName, fileName);
162     }
163     
164     /**
165      * Construct a persistent StorageFile from a path name.
166      *
167      * @param path The path name of the file. Guaranteed not to be in the temporary file directory. If null
168      * then the database directory should be returned.
169      *
170      * @return A corresponding StorageFile object
171      */

172     abstract StorageFile newPersistentFile( String JavaDoc path);
173
174     /**
175      * Construct a persistent StorageFile from a directory and path name.
176      *
177      * @param directoryName The path name of the directory. Guaranteed not to be in the temporary file directory.
178      * Guaranteed not to be null
179      * @param fileName The name of the file within the directory. Guaranteed not to be null.
180      *
181      * @return A corresponding StorageFile object
182      */

183     abstract StorageFile newPersistentFile( String JavaDoc directoryName, String JavaDoc fileName);
184
185     /**
186      * Construct a persistent StorageFile from a directory and path name.
187      *
188      * @param directoryName The path name of the directory. Guaranteed not to be to be null. Guaranteed to be
189      * created by a call to one of the newPersistentFile methods.
190      * @param fileName The name of the file within the directory. Guaranteed not to be null.
191      *
192      * @return A corresponding StorageFile object
193      */

194     abstract StorageFile newPersistentFile( StorageFile directoryName, String JavaDoc fileName);
195     
196     /**
197      * Get the pathname separator character used by the StorageFile implementation.
198      *
199      * @return the pathname separator character. (Normally '/' or '\').
200      */

201     public char getSeparator()
202     {
203         // Temp files are always java.io.File's and use its separator.
204
return File.separatorChar;
205     }
206
207     /**
208      * Get the abstract name of the directory that holds temporary files.
209      *
210      * @return a directory name
211      */

212     public StorageFile getTempDir()
213     {
214         return tempDir;
215     }
216
217     /**
218      * This method is used to determine whether the storage is fast (RAM based) or slow (disk based).
219      * It may be used by the database engine to determine the default size of the page cache.
220      *
221      * @return <b>true</b> if the storage is fast, <b>false</b> if it is slow.
222      */

223     public boolean isFast()
224     {
225         return false;
226     }
227
228     public boolean isReadOnlyDatabase()
229     {
230         return true;
231     }
232
233     /**
234      * Determine whether the storage supports random access. If random access is not supported then
235      * it will only be accessed using InputStreams and OutputStreams (if the database is writable).
236      *
237      * @return <b>true</b> if the storage supports random access, <b>false</b> if it is writable.
238      */

239     public boolean supportsRandomAccess()
240     {
241         return false;
242     }
243
244     void createTempDir() throws java.io.IOException JavaDoc
245     {
246         if( uniqueName == null)
247             return;
248
249         if( tempDirPath != null)
250             tempDir = new DirFile( tempDirPath, TEMP_DIR_PREFIX.concat(uniqueName));
251         else if( isReadOnlyDatabase())
252             tempDir = new DirFile( readOnlyTempRoot(), TEMP_DIR_PREFIX.concat(uniqueName));
253         else
254             tempDir = new DirFile( canonicalName, DataFactory.TEMP_SEGMENT_NAME);
255             
256         // blow away any temporary directory
257
tempDir.deleteAll();
258
259         tempDir.mkdirs();
260         tempDirPath = tempDir.getPath();
261     } // end of createTempDir
262

263     private String JavaDoc readOnlyTempRoot() throws java.io.IOException JavaDoc
264     {
265         // return the system temp dir by creating a temp file
266
// and finding its parent.
267
File temp = File.createTempFile("derby", "tmp");
268         String JavaDoc parent = temp.getParent();
269         temp.delete();
270
271         return parent;
272     }
273
274     public int getStorageFactoryVersion()
275     {
276         return StorageFactory.VERSION_NUMBER;
277     }
278 }
279
Popular Tags