KickJava   Java API By Example, From Geeks To Geeks.

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


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

44 class DirFile extends File implements StorageFile
45 {
46
47     /**
48      * Construct a DirFile from a path name.
49      *
50      * @param path The path name.
51      */

52     DirFile( String JavaDoc path)
53     {
54         super( path);
55     }
56
57     /**
58      * Construct a DirFile from a directory name and a file name.
59      *
60      * @param directoryName The directory part of the path name.
61      * @param fileName The name of the file within the directory.
62      */

63     DirFile( String JavaDoc directoryName, String JavaDoc fileName)
64     {
65         super( directoryName, fileName);
66     }
67
68     /**
69      * Construct a DirFile from a directory name and a file name.
70      *
71      * @param directoryName The directory part of the path name.
72      * @param fileName The name of the file within the directory.
73      */

74     DirFile( DirFile directoryName, String JavaDoc fileName)
75     {
76         super( (File) directoryName, fileName);
77     }
78
79     /**
80      * Get the name of the parent directory if this name includes a parent.
81      *
82      * @return An StorageFile denoting the parent directory of this StorageFile, if it has a parent, null if
83      * it does not have a parent.
84      */

85     public StorageFile getParentDir()
86     {
87         String JavaDoc parent = getParent();
88         if( parent == null)
89             return null;
90         return new DirFile( parent);
91     }
92     
93     /**
94      * Get the name of the directory of temporary files.
95      *
96      * @return The abstract name of the temp directory;
97      */

98     static StorageFile getTempDir() throws IOException JavaDoc
99     {
100         File temp = File.createTempFile("derby", "tmp");
101         StorageFile parent = new DirFile( temp.getParent());
102         temp.delete();
103
104         return parent;
105     } // End of getTempDir
106

107     /**
108      * Creates an output stream from a file name.
109      *
110      * @return an output stream suitable for writing to the file.
111      *
112      * @exception FileNotFoundException if the file exists but is a directory
113      * rather than a regular file, does not exist but cannot be created, or
114      * cannot be opened for any other reason.
115      */

116     public OutputStream JavaDoc getOutputStream( ) throws FileNotFoundException JavaDoc
117     {
118         return new FileOutputStream JavaDoc( (File) this);
119     }
120     
121     /**
122      * Creates an output stream from a file name.
123      *
124      * @param append If true then data will be appended to the end of the file, if it already exists.
125      * If false and a normal file already exists with this name the file will first be truncated
126      * to zero length.
127      *
128      * @return an output stream suitable for writing to the file.
129      *
130      * @exception FileNotFoundException if the file exists but is a directory
131      * rather than a regular file, does not exist but cannot be created, or
132      * cannot be opened for any other reason.
133      */

134     public OutputStream JavaDoc getOutputStream( final boolean append) throws FileNotFoundException JavaDoc
135     {
136         return new FileOutputStream JavaDoc( getPath(), append);
137     }
138
139     /**
140      * Creates an input stream from a file name.
141      *
142      * @return an input stream suitable for reading from the file.
143      *
144      * @exception FileNotFoundException if the file is not found.
145      */

146     public InputStream JavaDoc getInputStream( ) throws FileNotFoundException JavaDoc
147     {
148         return new FileInputStream JavaDoc( (File) this);
149     }
150
151     /**
152      * Get an exclusive lock. This is used to ensure that two or more JVMs do not open the same database
153      * at the same time.
154      *
155      *
156      * @return EXCLUSIVE_FILE_LOCK_NOT_AVAILABLE if the lock cannot be acquired because it is already held.<br>
157      * EXCLUSIVE_FILE_LOCK if the lock was successfully acquired.<br>
158      * NO_FILE_LOCK_SUPPORT if the system does not support exclusive locks.<br>
159      */

160     public synchronized int getExclusiveFileLock()
161     {
162         if (exists())
163         {
164             delete();
165         }
166         try
167         {
168             //Just create an empty file
169
RandomAccessFile lockFileOpen = new RandomAccessFile( (File) this, "rw");
170             lockFileOpen.getFD().sync( );
171             lockFileOpen.close();
172         }catch(IOException JavaDoc ioe)
173         {
174             // do nothing - it may be read only medium, who knows what the
175
// problem is
176
if (SanityManager.DEBUG)
177             {
178                 SanityManager.THROWASSERT("Unable to create Exclusive Lock File " + getPath());
179             }
180         }
181         
182         return NO_FILE_LOCK_SUPPORT;
183     } // end of getExclusiveFileLock
184

185     /**
186      * Release the resource associated with an earlier acquired exclusive lock
187      *
188      * @see #getExclusiveFileLock
189      */

190     public synchronized void releaseExclusiveFileLock()
191     {
192         if( exists())
193         {
194             delete();
195         }
196     } // End of releaseExclusiveFileLock
197

198     /**
199      * Get a random access (read/write) file.
200      *
201      * @param mode "r", "rw", "rws", or "rwd". The "rws" and "rwd" modes specify
202      * that the data is to be written to persistent store, consistent with the
203      * java.io.RandomAccessFile class ("synchronized" with the persistent
204      * storage, in the file system meaning of the word "synchronized"). However
205      * the implementation is not required to implement the "rws" or "rwd"
206      * modes. The implementation may treat "rws" and "rwd" as "rw". It is up to
207      * the user of this interface to call the StorageRandomAccessFile.sync
208      * method. If the "rws" or "rwd" modes are supported and the
209      * RandomAccessFile was opened in "rws" or "rwd" mode then the
210      * implementation of StorageRandomAccessFile.sync need not do anything.
211      *
212      * @return an object that can be used for random access to the file.
213      *
214      * @exception IllegalArgumentException if the mode argument is not equal to one of "r", "rw".
215      * @exception FileNotFoundException if the file exists but is a directory rather than a regular
216      * file, or cannot be opened or created for any other reason .
217      */

218     public StorageRandomAccessFile getRandomAccessFile( String JavaDoc mode) throws FileNotFoundException JavaDoc
219     {
220         // Assume that modes "rws" and "rwd" are not supported.
221
if( "rws".equals( mode) || "rwd".equals( mode))
222             mode = "rw";
223         return new DirRandomAccessFile( (File) this, mode);
224     } // end of getRandomAccessFile
225

226     /**
227      * Rename the file denoted by this name. Note that StorageFile objects are immutable. This method
228      * renames the underlying file, it does not change this StorageFile object. The StorageFile object denotes the
229      * same name as before, however the exists() method will return false after the renameTo method
230      * executes successfully.
231      *
232      *<p>It is not specified whether this method will succeed if a file already exists under the new name.
233      *
234      * @param newName the new name.
235      *
236      * @return <b>true</b> if the rename succeeded, <b>false</b> if not.
237      */

238     public boolean renameTo( StorageFile newName)
239     {
240         return super.renameTo( (File) newName);
241     }
242
243     /**
244      * Deletes the named file and, if it is a directory, all the files and directories it contains.
245      *
246      * @return <b>true</b> if the named file or directory is successfully deleted, <b>false</b> if not
247      */

248     public boolean deleteAll()
249     {
250         if( !exists())
251             return false;
252         if( isDirectory())
253         {
254             String JavaDoc[] childList = super.list();
255             String JavaDoc parentName = getPath();
256             for( int i = 0; i < childList.length; i++)
257             {
258                 if( childList[i].equals( ".") || childList[i].equals( ".."))
259                     continue;
260                 DirFile child = new DirFile( parentName, childList[i]);
261                 if( ! child.deleteAll())
262                     return false;
263             }
264         }
265         return delete();
266     } // end of deleteAll
267

268     /**
269      * @see org.apache.derby.io.StorageFile#getURL()
270      */

271     public URL JavaDoc getURL() throws MalformedURLException JavaDoc {
272         
273         return toURL();
274     }
275 }
276
Popular Tags