Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/derby/impl/io/DirFile.java


1   /*
2   
3      Derby - Class org.apache.derby.impl.io.DirFile
4   
5      Copyright 2004 The Apache Software Foundation or its licensors, as applicable.
6   
7      Licensed under the Apache License, Version 2.0 (the "License");
8      you may not use this file except in compliance with the License.
9      You may obtain a copy of the License at
10  
11        http://www.apache.org/licenses/LICENSE-2.0
12  
13     Unless required by applicable law or agreed to in writing, software
14     distributed under the License is distributed on an "AS IS" BASIS,
15     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16     See the License for the specific language governing permissions and
17     limitations under the License.
18  
19   */
20  
21  package org.apache.derby.impl.io;
22  
23  import org.apache.derby.io.StorageFile;
24  import org.apache.derby.io.StorageRandomAccessFile;
25  
26  import org.apache.derby.iapi.services.sanity.SanityManager;
27  
28  import java.io.File;
29  import java.io.InputStream;
30  import java.io.OutputStream;
31  import java.io.FileOutputStream;
32  import java.io.FileInputStream;
33  import java.io.IOException;
34  import java.io.FileNotFoundException;
35  import java.io.RandomAccessFile;
36  
37  /**
38   * This class provides a disk based implementation of the StorageFile interface. It is used by the
39   * database engine to access persistent data and transaction logs under the directory (default) subsubprotocol.
40   */
41  class DirFile extends File implements StorageFile
42  {
43  
44      /**
45       * Construct a DirFile from a path name.
46       *
47       * @param path The path name.
48       */
49      DirFile( String path)
50      {
51          super( path);
52      }
53  
54      /**
55       * Construct a DirFile from a directory name and a file name.
56       *
57       * @param directory The directory part of the path name.
58       * @param fileName The name of the file within the directory.
59       */
60      DirFile( String directoryName, String fileName)
61      {
62          super( directoryName, fileName);
63      }
64  
65      /**
66       * Construct a DirFile from a directory name and a file name.
67       *
68       * @param directory The directory part of the path name.
69       * @param fileName The name of the file within the directory.
70       */
71      DirFile( DirFile directoryName, String fileName)
72      {
73          super( (File) directoryName, fileName);
74      }
75  
76      /**
77       * Get the name of the parent directory if this name includes a parent.
78       *
79       * @return An StorageFile denoting the parent directory of this StorageFile, if it has a parent, null if
80       *         it does not have a parent.
81       */
82      public StorageFile getParentDir()
83      {
84          String parent = getParent();
85          if( parent == null)
86              return null;
87          return new DirFile( parent);
88      }
89      
90      /**
91       * Get the name of the directory of temporary files.
92       *
93       * @return The abstract name of the temp directory;
94       */
95      static StorageFile getTempDir() throws IOException
96      {
97          File temp = File.createTempFile("derby", "tmp");
98          StorageFile parent = new DirFile( temp.getParent());
99          temp.delete();
100 
101     return parent;
102   } // End of getTempDir
103 
104     /**
105      * Creates an output stream from a file name.
106      *
107      * @param name The file name.
108      *
109      * @return an output stream suitable for writing to the file.
110      *
111      * @exception FileNotFoundException if the file exists but is a directory
112      *            rather than a regular file, does not exist but cannot be created, or
113      *            cannot be opened for any other reason.
114      */
115     public OutputStream getOutputStream( ) throws FileNotFoundException
116     {
117         return new FileOutputStream( (File) this);
118     }
119     
120     /**
121      * Creates an output stream from a file name.
122      *
123      * @param append If true then data will be appended to the end of the file, if it already exists.
124      *               If false and a normal file already exists with this name the file will first be truncated
125      *               to zero length.
126      *
127      * @return an output stream suitable for writing to the file.
128      *
129      * @exception FileNotFoundException if the file exists but is a directory
130      *            rather than a regular file, does not exist but cannot be created, or
131      *            cannot be opened for any other reason.
132      */
133     public OutputStream getOutputStream( final boolean append) throws FileNotFoundException
134     {
135         return new FileOutputStream( getPath(), append);
136     }
137 
138     /**
139      * Creates an input stream from a file name.
140      *
141      * @param name The file name.
142      *
143      * @return an input stream suitable for reading from the file.
144      *
145      * @exception FileNotFoundException if the file is not found.
146      */
147     public InputStream getInputStream( ) throws FileNotFoundException
148     {
149         return new FileInputStream( (File) this);
150     }
151 
152     /**
153      * Get an exclusive lock. This is used to ensure that two or more JVMs do not open the same database
154      * at the same time.
155      *
156      * @param lockFile The name of the lock. In a file system implementation it will be the name of a
157      *                 lock file.
158      *
159      * @return EXCLUSIVE_FILE_LOCK_NOT_AVAILABLE if the lock cannot be acquired because it is already held.<br>
160      *    EXCLUSIVE_FILE_LOCK if the lock was successfully acquired.<br>
161      *    NO_FILE_LOCK_SUPPORT if the system does not support exclusive locks.<br>
162      */
163     public synchronized int getExclusiveFileLock()
164   {
165     if (exists())
166     {
167       delete();
168     }
169     try
170         {
171       //Just create an empty file
172       RandomAccessFile lockFileOpen = new RandomAccessFile( (File) this, "rw");
173       lockFileOpen.getFD().sync( );
174       lockFileOpen.close();
175     }catch(IOException ioe)
176     {
177       // do nothing - it may be read only medium, who knows what the
178       // problem is
179       if (SanityManager.DEBUG)
180       {
181         SanityManager.THROWASSERT("Unable to create Exclusive Lock File " + getPath());
182       }
183     }
184     
185     return NO_FILE_LOCK_SUPPORT;
186   } // end of getExclusiveFileLock
187 
188   /**
189      * Release the resource associated with an earlier acquired exclusive lock
190      *
191      * @param lockFile the name of the lock file
192      *
193      * @see #getExclusiveFileLock
194      */
195   public synchronized void releaseExclusiveFileLock()
196   {
197     if( exists())
198     {
199       delete(); 
200     }
201   } // End of releaseExclusiveFileLock
202 
203     /**
204      * Get a random access (read/write) file.
205      *
206      * @param name The name of the file.
207      * @param mode "r", "rw", "rws", or "rwd". The "rws" and "rwd" modes specify
208      *             that the data is to be written to persistent store, consistent with the
209      *             java.io.RandomAccessFile class ("synchronized" with the persistent
210      *             storage, in the file system meaning of the word "synchronized").  However
211      *             the implementation is not required to implement the "rws" or "rwd"
212      *             modes. The implementation may treat "rws" and "rwd" as "rw". It is up to
213      *             the user of this interface to call the StorageRandomAccessFile.sync
214      *             method. If the "rws" or "rwd" modes are supported and the
215      *             RandomAccessFile was opened in "rws" or "rwd" mode then the
216      *             implementation of StorageRandomAccessFile.sync need not do anything.
217      *
218      * @return an object that can be used for random access to the file.
219      *
220      * @exception IllegalArgumentException if the mode argument is not equal to one of "r", "rw".
221      * @exception FileNotFoundException if the file exists but is a directory rather than a regular
222      *              file, or cannot be opened or created for any other reason .
223      */
224     public StorageRandomAccessFile getRandomAccessFile( String mode) throws FileNotFoundException
225     {
226         // Assume that modes "rws" and "rwd" are not supported.
227         if( "rws".equals( mode) || "rwd".equals( mode))
228             mode = "rw";
229         return new DirRandomAccessFile( (File) this, mode);
230     } // end of getRandomAccessFile
231 
232     /**
233      * Rename the file denoted by this name. Note that StorageFile objects are immutable. This method
234      * renames the underlying file, it does not change this StorageFile object. The StorageFile object denotes the
235      * same name as before, however the exists() method will return false after the renameTo method
236      * executes successfully.
237      *
238      *<p>It is not specified whether this method will succeed if a file already exists under the new name.
239      *
240      * @param newName the new name.
241      *
242      * @return <b>true</b> if the rename succeeded, <b>false</b> if not.
243      */
244     public boolean renameTo( StorageFile newName)
245     {
246         return super.renameTo( (File) newName);
247     }
248 
249     /**
250      * Deletes the named file and, if it is a directory, all the files and directories it contains.
251      *
252      * @return <b>true</b> if the named file or directory is successfully deleted, <b>false</b> if not
253      */
254     public boolean deleteAll()
255     {
256         if( !exists())
257             return false;
258         if( isDirectory())
259         {
260             String[] childList = super.list();
261             String parentName = getPath();
262             for( int i = 0; i < childList.length; i++)
263             {
264                 if( childList[i].equals( ".") || childList[i].equals( ".."))
265                     continue;
266                 DirFile child = new DirFile( parentName, childList[i]);
267                 if( ! child.deleteAll())
268                     return false;
269             }
270         }
271         return delete();
272     } // end of deleteAll
273 }