1 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 ; 29 import java.io.FileOutputStream ; 30 import java.io.OutputStream ; 31 import java.io.File ; 32 import java.io.IOException ; 33 import java.io.RandomAccessFile ; 34 import java.nio.channels.FileChannel ; 35 import java.nio.channels.FileLock ; 36 37 41 class DirFile4 extends DirFile 42 { 43 44 private RandomAccessFile lockFileOpen; 45 private FileChannel lockFileChannel; 46 private FileLock dbLock; 47 48 private final boolean rwsOK; 49 50 55 DirFile4( String path, boolean rwsOK) 56 { 57 super( path); 58 this.rwsOK = rwsOK; 59 } 60 61 67 DirFile4( String directoryName, String fileName, boolean rwsOK) 68 { 69 super( directoryName, fileName); 70 this.rwsOK = rwsOK; 71 } 72 73 79 DirFile4( DirFile directoryName, String fileName, boolean rwsOK) 80 { 81 super( directoryName, fileName); 82 this.rwsOK = rwsOK; 83 } 84 85 91 public StorageFile getParentDir() 92 { 93 String parent = getParent(); 94 if( parent == null) 95 return null; 96 return new DirFile4( parent, rwsOK); 97 } 98 99 112 public OutputStream getOutputStream( final boolean append) throws FileNotFoundException 113 { 114 return new FileOutputStream ( (File) this, append); 115 } 116 117 public synchronized int getExclusiveFileLock() 118 { 119 boolean validExclusiveLock = false; 120 int status; 121 122 136 137 try 138 { 139 if(createNewFile()) 141 { 142 validExclusiveLock = true; 143 } 144 else 145 { 146 if(length() > 0) 147 validExclusiveLock = true; 148 } 149 150 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 ioe) 177 { 178 181 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 } 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 super.releaseExclusiveFileLock(); 218 }catch (IOException ioe) 219 { 220 } 223 } 225 245 public StorageRandomAccessFile getRandomAccessFile( String mode) throws FileNotFoundException 246 { 247 if(!rwsOK && ("rws".equals( mode) || "rwd".equals( mode))) 249 mode = "rw"; 250 return new DirRandomAccessFile4( (File) this, mode); 251 } } 253 | Popular Tags |