KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > util > corruptio > CorruptBaseStorageFactory


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 package org.apache.derbyTesting.functionTests.util.corruptio;
22 import org.apache.derby.io.WritableStorageFactory;
23 import org.apache.derby.io.StorageFile;
24
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.SyncFailedException JavaDoc;
30
31 /**
32  * This class provides a proxy base implementation of the
33  * WritableStorageFactory interface to instrument I/O operations for testing
34  * purposes.
35  * Some methods in this class adds support for corrupting the I/O operation
36  * sent by the engine before invoking the real storage factory underneath.
37  * By deault all the calls will go to the real storage factory defined by the
38  * concrete class, unless corruption is enabled through CorruptibleIo instance.
39  *
40  * @see CorruptibleIo
41  * @see WritableStorageFactory
42  * @see StorageFactory
43  *
44  */

45
46 abstract class CorruptBaseStorageFactory implements WritableStorageFactory
47 {
48
49     protected WritableStorageFactory realStorageFactory;
50
51     /**
52      * Most of the initialization is done in the init method.
53      */

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

89     public void init( String JavaDoc home, String JavaDoc databaseName, String JavaDoc tempDirName, String JavaDoc uniqueName)
90         throws IOException JavaDoc
91     {
92         realStorageFactory = getRealStorageFactory();
93         realStorageFactory.init(home, databaseName, tempDirName, uniqueName);
94     } // end of init
95

96     
97     public void shutdown()
98     {
99         realStorageFactory.shutdown();
100     }
101     
102
103     /**
104      * Get the canonical name of the database.
105      *
106      * This is a name that uniquely identifies it. It is system dependent.
107      *
108      * The normal, disk based implementation uses method
109      * java.io.File.getCanonicalPath on the directory holding the
110      * database to construct the canonical name.
111      *
112      * @return the canonical name
113      *
114      * @exception IOException if an IO error occurred during the construction
115      * of the name.
116      */

117     public String JavaDoc getCanonicalName() throws IOException JavaDoc
118     {
119         return realStorageFactory.getCanonicalName();
120     }
121     
122     /**
123      * Construct a StorageFile from a path name.
124      *
125      * @param path The path name of the file
126      *
127      * @return A corresponding StorageFile object
128      */

129     public StorageFile newStorageFile( String JavaDoc path)
130     {
131         return new CorruptFile(realStorageFactory.newStorageFile(path));
132     }
133     
134     /**
135      * Construct a StorageFile from a directory and file name.
136      *
137      * @param directoryName The directory part of the path name.
138      * @param fileName The name of the file within the directory.
139      *
140      * @return A corresponding StorageFile object
141      */

142     public StorageFile newStorageFile( String JavaDoc directoryName, String JavaDoc fileName)
143     {
144         return new CorruptFile(realStorageFactory.newStorageFile(directoryName, fileName));
145     }
146     
147     /**
148      * Construct a StorageFile from a directory and file name.
149      *
150      * @param directoryName The directory part of the path name.
151      * @param fileName The name of the file within the directory.
152      *
153      * @return A corresponding StorageFile object
154      */

155     public StorageFile newStorageFile( StorageFile directoryName, String JavaDoc fileName)
156     {
157         StorageFile realDirFile = ((CorruptFile) directoryName).getRealFileInstance();
158         return new CorruptFile(realStorageFactory.newStorageFile(realDirFile, fileName));
159     }
160     
161     /**
162      * Get the pathname separator character used by the StorageFile
163      * implementation.
164      *
165      * @return the pathname separator character. (Normally '/' or '\').
166      */

167     public char getSeparator()
168     {
169         return realStorageFactory.getSeparator();
170     }
171
172     /**
173      * Get the abstract name of the directory that holds temporary files.
174      *
175      * @return a directory name
176      */

177     public StorageFile getTempDir()
178     {
179         return new CorruptFile(realStorageFactory.getTempDir());
180     }
181
182     /**
183      * This method is used to determine whether the storage is fast
184      * (RAM based) or slow (disk based).
185      *
186      * It may be used by the database engine to determine the default size of
187      * the page cache.
188      *
189      * @return <b>true</b> if the storage is fast, <b>false</b> if it is slow.
190      */

191     public boolean isFast()
192     {
193         return realStorageFactory.isFast();
194     }
195
196     public boolean isReadOnlyDatabase()
197     {
198         return realStorageFactory.isReadOnlyDatabase();
199     }
200
201     /**
202      * Determine whether the storage supports random access.
203      * If random access is not supported then it will only be accessed using
204      * InputStreams and OutputStreams (if the database is writable).
205      *
206      * @return <b>true</b> if the storage supports random access, <b>false</b> if it is writable.
207      */

208     public boolean supportsRandomAccess()
209     {
210         return realStorageFactory.supportsRandomAccess();
211     }
212
213     public int getStorageFactoryVersion()
214     {
215         return realStorageFactory.getStorageFactoryVersion();
216     }
217
218
219     
220     /**
221      * Force the data of an output stream out to the underlying storage.
222      *
223      * That is, ensure that it has been made persistent. If the database is to
224      * be transient, that is, if the database does not survive a restart, then
225      * the sync method implementation need not do anything.
226      *
227      * @param stream The stream to be synchronized.
228      * @param metaData If true then this method must force both changes to the
229      * file's contents and metadata to be written to storage;
230      * if false, it need only force file content changes to be
231      * written. The implementation is allowed to ignore this
232      * parameter and always force out metadata changes.
233      *
234      * @exception IOException if an I/O error occurs.
235      * @exception SyncFailedException Thrown when the buffers cannot be flushed,
236      * or because the system cannot guarantee that all the buffers
237      * have been synchronized with physical media.
238      */

239     public void sync( OutputStream JavaDoc stream, boolean metaData) throws IOException JavaDoc, SyncFailedException JavaDoc
240     {
241         realStorageFactory.sync(stream, metaData);
242     }
243
244     /**
245      * This method tests whether the "rws" and "rwd" modes are implemented.
246      *
247      * If the "rws" method is supported then the database engine will conclude
248      * that the write methods of "rws" mode StorageRandomAccessFiles are
249      * slow but the sync method is fast and optimize accordingly.
250      *
251      * @return <b>true</b> if an StIRandomAccess file opened with "rws" or "rwd" modes immediately writes data to the
252      * underlying storage, <b>false</b> if not.
253      */

254     public boolean supportsRws()
255     {
256         return realStorageFactory.supportsRws();
257     }
258
259     
260     /**
261      * get the real storage factory
262      *
263      */

264     abstract WritableStorageFactory getRealStorageFactory();
265
266 }
267
Popular Tags