KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.io.DirFile4
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 import org.apache.derby.io.StorageFile;
26 import org.apache.derby.io.StorageRandomAccessFile;
27
28 import java.io.FileNotFoundException JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.io.OutputStream JavaDoc;
31 import java.io.File JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.RandomAccessFile JavaDoc;
34 import java.nio.channels.FileChannel JavaDoc;
35 import java.nio.channels.FileLock JavaDoc;
36
37 /**
38  * This class implements the StorageFile interface using features of Java 1.4 not available in earlier
39  * versions of Java.
40  */

41 class DirFile4 extends DirFile
42 {
43
44     private RandomAccessFile lockFileOpen;
45     private FileChannel JavaDoc lockFileChannel;
46     private FileLock JavaDoc dbLock;
47
48     private final boolean rwsOK;
49
50     /**
51      * Construct a DirFile from a path name.
52      *
53      * @param path The path name.
54      */

55     DirFile4( String JavaDoc path, boolean rwsOK)
56     {
57         super( path);
58         this.rwsOK = rwsOK;
59     }
60
61     /**
62      * Construct a DirFile from a directory name and a 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     DirFile4( String JavaDoc directoryName, String JavaDoc fileName, boolean rwsOK)
68     {
69         super( directoryName, fileName);
70         this.rwsOK = rwsOK;
71     }
72
73     /**
74      * Construct a DirFile from a directory name and a file name.
75      *
76      * @param directoryName The directory part of the path name.
77      * @param fileName The name of the file within the directory.
78      */

79     DirFile4( DirFile directoryName, String JavaDoc fileName, boolean rwsOK)
80     {
81         super( directoryName, fileName);
82         this.rwsOK = rwsOK;
83     }
84
85     /**
86      * Get the name of the parent directory if this name includes a parent.
87      *
88      * @return An StorageFile denoting the parent directory of this StorageFile, if it has a parent, null if
89      * it does not have a parent.
90      */

91     public StorageFile getParentDir()
92     {
93         String JavaDoc parent = getParent();
94         if( parent == null)
95             return null;
96         return new DirFile4( parent, rwsOK);
97     }
98     
99     /**
100      * Creates an output stream from a file name.
101      *
102      * @param append If true then data will be appended to the end of the file, if it already exists.
103      * If false and a normal file already exists with this name the file will first be truncated
104      * to zero length.
105      *
106      * @return an output stream suitable for writing to the file.
107      *
108      * @exception FileNotFoundException if the file exists but is a directory
109      * rather than a regular file, does not exist but cannot be created, or
110      * cannot be opened for any other reason.
111      */

112     public OutputStream JavaDoc getOutputStream( final boolean append) throws FileNotFoundException JavaDoc
113     {
114         return new FileOutputStream JavaDoc( (File) this, append);
115     }
116
117     public synchronized int getExclusiveFileLock()
118     {
119         boolean validExclusiveLock = false;
120         int status;
121
122         /*
123         ** There can be a scenario where there is some other JVM that is before jkdk1.4
124         ** had booted the system and jdk1.4 trying to boot it, in this case we will get the
125         ** Exclusive Lock even though some other JVM has already booted the database. But
126         ** the lock is not a reliable one , so we should still throw the warning.
127         ** The Way we identify this case is if "dbex.lck" file size is differen
128         ** for pre jdk1.4 jvms and jdk1.4 or above.
129         ** Zero size "dbex.lck" file is created by a jvm i.e before jdk1.4 and
130         ** File created by jdk1.4 or above writes EXCLUSIVE_FILE_LOCK value into the file.
131         ** If we are unable to acquire the lock means other JVM that
132         ** currently booted the system is also JDK1.4 or above;
133         ** In this case we could confidently throw a exception instead of
134         ** of a warning.
135         **/

136
137         try
138         {
139             //create the file that us used to acquire exclusive lock if it does not exists.
140
if(createNewFile())
141             {
142                 validExclusiveLock = true;
143             }
144             else
145             {
146                 if(length() > 0)
147                     validExclusiveLock = true;
148             }
149
150             //If we can acquire a reliable exclusive lock , try to get it.
151
if(validExclusiveLock)
152             {
153                 lockFileOpen = new RandomAccessFile((File) this, "rw");
154                 lockFileChannel = lockFileOpen.getChannel();
155                 dbLock =lockFileChannel.tryLock();
156                 if(dbLock == null)
157                 {
158                     lockFileChannel.close();
159                     lockFileChannel=null;
160                     lockFileOpen.close();
161                     lockFileOpen = null;
162                     status = EXCLUSIVE_FILE_LOCK_NOT_AVAILABLE;
163                 }
164                 else
165                 {
166                     lockFileOpen.writeInt(EXCLUSIVE_FILE_LOCK);
167                     lockFileChannel.force(true);
168                     status = EXCLUSIVE_FILE_LOCK;
169                 }
170             }
171             else
172             {
173                 status = NO_FILE_LOCK_SUPPORT;
174             }
175         
176         }catch(IOException JavaDoc ioe)
177         {
178             // do nothing - it may be read only medium, who knows what the
179
// problem is
180

181             //release all the possible resource we created in this functions.
182
releaseExclusiveFileLock();
183             status = NO_FILE_LOCK_SUPPORT;
184             if (SanityManager.DEBUG)
185             {
186                 SanityManager.THROWASSERT("Unable to Acquire Exclusive Lock on "
187                                           + getPath());
188             }
189         }
190     
191         return status;
192     } // end of getExclusiveFileLock
193

194     public synchronized void releaseExclusiveFileLock()
195     {
196         try
197         {
198             if(dbLock!=null)
199             {
200                 dbLock.release();
201                 dbLock =null;
202             }
203         
204             if(lockFileChannel !=null)
205             {
206                 lockFileChannel.close();
207                 lockFileChannel = null;
208             }
209
210             if(lockFileOpen !=null)
211             {
212                 lockFileOpen.close();
213                 lockFileOpen = null;
214             }
215
216             //delete the exclusive lock file name.
217
super.releaseExclusiveFileLock();
218         }catch (IOException JavaDoc ioe)
219         {
220             // do nothing - it may be read only medium, who knows what the
221
// problem is
222
}
223     } // End of releaseExclusiveFileLock
224

225     /**
226      * Get a random access (read/write) file.
227      *
228      * @param mode "r", "rw", "rws", or "rwd". The "rws" and "rwd" modes specify
229      * that the data is to be written to persistent store, consistent with the
230      * java.io.RandomAccessFile class ("synchronized" with the persistent
231      * storage, in the file system meaning of the word "synchronized"). However
232      * the implementation is not required to implement the "rws" or "rwd"
233      * modes. The implementation may treat "rws" and "rwd" as "rw". It is up to
234      * the user of this interface to call the StorageRandomAccessFile.sync
235      * method. If the "rws" or "rwd" modes are supported and the
236      * RandomAccessFile was opened in "rws" or "rwd" mode then the
237      * implementation of StorageRandomAccessFile.sync need not do anything.
238      *
239      * @return an object that can be used for random access to the file.
240      *
241      * @exception IllegalArgumentException if the mode argument is not equal to one of "r", "rw", "rws", or "rwd".
242      * @exception FileNotFoundException if the file exists but is a directory rather than a regular
243      * file, or cannot be opened or created for any other reason .
244      */

245     public StorageRandomAccessFile getRandomAccessFile( String JavaDoc mode) throws FileNotFoundException JavaDoc
246     {
247         // Assume that modes "rws" and "rwd" are not supported.
248
if(!rwsOK && ("rws".equals( mode) || "rwd".equals( mode)))
249             mode = "rw";
250         return new DirRandomAccessFile4( (File) this, mode);
251     } // end of getRandomAccessFile
252
}
253
Popular Tags