KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.io.DirStorageFactory
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.WritableStorageFactory;
27 import org.apache.derby.io.StorageFile;
28 import org.apache.derby.io.StorageRandomAccessFile;
29
30 import java.io.File JavaDoc;
31 import java.io.FileNotFoundException JavaDoc;
32 import java.io.FileOutputStream JavaDoc;
33 import java.io.FileInputStream JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.io.OutputStream JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.SyncFailedException JavaDoc;
38
39 import java.util.Properties JavaDoc;
40
41 /**
42  * This class provides a disk based implementation of the StorageFactory interface. It is used by the
43  * database engine to access persistent data and transaction logs under the directory (default) subsubprotocol.
44  */

45
46 public class DirStorageFactory extends BaseStorageFactory
47     implements WritableStorageFactory
48 {
49     /**
50      * Construct a StorageFile from a path name.
51      *
52      * @param path The path name of the file
53      *
54      * @return A corresponding StorageFile object
55      */

56     public final StorageFile newStorageFile( String JavaDoc path)
57     {
58         return newPersistentFile( path);
59     }
60     
61     /**
62      * Construct a StorageFile from a directory and file name.
63      *
64      * @param directoryName The directory part of the path name.
65      * @param fileName The name of the file within the directory.
66      *
67      * @return A corresponding StorageFile object
68      */

69     public final StorageFile newStorageFile( String JavaDoc directoryName, String JavaDoc fileName)
70     {
71        return newPersistentFile( 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     public final StorageFile newStorageFile( StorageFile directoryName, String JavaDoc fileName)
83     {
84         return newPersistentFile( directoryName, fileName);
85     }
86     /**
87      * Construct a persistent StorageFile from a path name.
88      *
89      * @param path The path name of the file. Guaranteed not to be in the temporary file directory. If null
90      * then the database directory should be returned.
91      *
92      * @return A corresponding StorageFile object
93      */

94     StorageFile newPersistentFile( String JavaDoc path)
95     {
96         if( path == null)
97             return new DirFile( dataDirectory);
98         return new DirFile(dataDirectory, path);
99     }
100
101     /**
102      * Construct a persistent StorageFile from a directory and path name.
103      *
104      * @param directoryName The path name of the directory. Guaranteed not to be in the temporary file directory.
105      * Guaranteed not to be null
106      * @param fileName The name of the file within the directory. Guaranteed not to be null.
107      *
108      * @return A corresponding StorageFile object
109      */

110     StorageFile newPersistentFile( String JavaDoc directoryName, String JavaDoc fileName)
111     {
112         return new DirFile( separatedDataDirectory + directoryName, fileName);
113     }
114
115     /**
116      * Construct a persistent StorageFile from a directory and path name.
117      *
118      * @param directoryName The path name of the directory. Guaranteed not to be to be null. Guaranteed to be
119      * created by a call to one of the newPersistentFile methods.
120      * @param fileName The name of the file within the directory. Guaranteed not to be null.
121      *
122      * @return A corresponding StorageFile object
123      */

124     StorageFile newPersistentFile( StorageFile directoryName, String JavaDoc fileName)
125     {
126         return new DirFile( (DirFile) directoryName, fileName);
127     }
128
129     /**
130      * Force the data of an output stream out to the underlying storage. That is, ensure that
131      * it has been made persistent. If the database is to be transient, that is, if the database
132      * does not survive a restart, then the sync method implementation need not do anything.
133      *
134      * @param stream The stream to be synchronized.
135      * @param metaData If true then this method must force both changes to the file's
136      * contents and metadata to be written to storage; if false, it need only force file content changes
137      * to be written. The implementation is allowed to ignore this parameter and always force out
138      * metadata changes.
139      *
140      * @exception IOException if an I/O error occurs.
141      * @exception SyncFailedException Thrown when the buffers cannot be flushed,
142      * or because the system cannot guarantee that all the buffers have been
143      * synchronized with physical media.
144      */

145     public void sync( OutputStream JavaDoc stream, boolean metaData) throws IOException JavaDoc, SyncFailedException JavaDoc
146     {
147         ((FileOutputStream JavaDoc) stream).getFD().sync();
148     }
149
150     /**
151      * This method tests whether the "rws" and "rwd" modes are implemented. If the "rws" method is supported
152      * then the database engine will conclude that the write methods of "rws" mode StorageRandomAccessFiles are
153      * slow but the sync method is fast and optimize accordingly.
154      *
155      * @return <b>true</b> if an StIRandomAccess file opened with "rws" or "rwd" modes immediately writes data to the
156      * underlying storage, <b>false</b> if not.
157      */

158     public boolean supportsRws()
159     {
160         return false;
161     }
162
163     public boolean isReadOnlyDatabase()
164     {
165         return false;
166     }
167
168     /**
169      * Determine whether the storage supports random access. If random access is not supported then
170      * it will only be accessed using InputStreams and OutputStreams (if the database is writable).
171      *
172      * @return <b>true</b> if the storage supports random access, <b>false</b> if it is writable.
173      */

174     public boolean supportsRandomAccess()
175     {
176         return true;
177     }
178
179     void doInit() throws IOException JavaDoc
180     {
181         if( dataDirectory != null)
182         {
183             File dataDirectoryFile = new File( dataDirectory);
184             File databaseRoot = null;
185             if( dataDirectoryFile.isAbsolute())
186                 databaseRoot = dataDirectoryFile;
187             else if( home != null && dataDirectory.startsWith( home))
188                 databaseRoot = dataDirectoryFile;
189             else
190             {
191                 databaseRoot = new File( home, dataDirectory);
192                 if (home != null)
193                     dataDirectory = home + getSeparator() + dataDirectory;
194             }
195             canonicalName = databaseRoot.getCanonicalPath();
196             createTempDir();
197             separatedDataDirectory = dataDirectory + getSeparator();
198         }
199         else if( home != null)
200         {
201             File root = new File( home);
202             dataDirectory = root.getCanonicalPath();
203             separatedDataDirectory = dataDirectory + getSeparator();
204         }
205     } // end of doInit
206
}
207
Popular Tags