1 25 26 package net.sourceforge.cobertura.util; 27 28 import java.io.File ; 29 import java.io.FileNotFoundException ; 30 import java.io.RandomAccessFile ; 31 import java.lang.reflect.InvocationTargetException ; 32 import java.lang.reflect.Method ; 33 34 49 public class FileLocker 50 { 51 52 55 private Object lock = null; 56 57 60 private Object lockChannel = null; 61 62 66 private File lockFile; 67 68 public FileLocker(File file) 69 { 70 String lockFileName = file.getName() + ".lock"; 71 File parent = file.getParentFile(); 72 if (parent == null) 73 { 74 lockFile = new File (lockFileName); 75 } 76 else 77 { 78 lockFile = new File (parent, lockFileName); 79 } 80 lockFile.deleteOnExit(); 81 } 82 83 86 public boolean lock() 87 { 88 if (System.getProperty("java.version").startsWith("1.3")) 89 { 90 return true; 91 } 92 93 try 94 { 95 Class aClass = Class.forName("java.io.RandomAccessFile"); 96 Method method = aClass.getDeclaredMethod("getChannel", (Class [])null); 97 lockChannel = method.invoke(new RandomAccessFile (lockFile, "rw"), (Object [])null); 98 } 99 catch (FileNotFoundException e) 100 { 101 System.err.println("Unable to get lock channel for " + lockFile.getAbsolutePath() 102 + ": " + e.getLocalizedMessage()); 103 return false; 104 } 105 catch (InvocationTargetException e) 106 { 107 System.err.println("Unable to get lock channel for " + lockFile.getAbsolutePath() 108 + ": " + e.getLocalizedMessage()); 109 return false; 110 } 111 catch (Throwable t) 112 { 113 System.err.println("Unable to execute RandomAccessFile.getChannel() using reflection: " 114 + t.getLocalizedMessage()); 115 t.printStackTrace(); 116 } 117 118 try 119 { 120 Class aClass = Class.forName("java.nio.channels.FileChannel"); 121 Method method = aClass.getDeclaredMethod("lock", (Class [])null); 122 lock = method.invoke(lockChannel, (Object [])null); 123 } 124 catch (InvocationTargetException e) 125 { 126 System.err.println("Unable to get lock on " + lockFile.getAbsolutePath() + ": " 127 + e.getLocalizedMessage()); 128 return false; 129 } 130 catch (Throwable t) 131 { 132 System.err.println("Unable to execute FileChannel.lock() using reflection: " 133 + t.getLocalizedMessage()); 134 t.printStackTrace(); 135 } 136 137 return true; 138 } 139 140 143 public void release() 144 { 145 if (lock != null) 146 lock = releaseFileLock(lock); 147 if (lockChannel != null) 148 lockChannel = closeChannel(lockChannel); 149 lockFile.delete(); 150 } 151 152 private static Object releaseFileLock(Object lock) 153 { 154 try 155 { 156 Class aClass = Class.forName("java.nio.channels.FileLock"); 157 Method method = aClass.getDeclaredMethod("isValid", (Class [])null); 158 if (((Boolean )method.invoke(lock, (Object [])null)).booleanValue()) 159 { 160 method = aClass.getDeclaredMethod("release", (Class [])null); 161 method.invoke(lock, (Object [])null); 162 lock = null; 163 } 164 } 165 catch (Throwable t) 166 { 167 System.err.println("Unable to release locked file: " + t.getLocalizedMessage()); 168 } 169 return lock; 170 } 171 172 private static Object closeChannel(Object channel) 173 { 174 try 175 { 176 Class aClass = Class.forName("java.nio.channels.spi.AbstractInterruptibleChannel"); 177 Method method = aClass.getDeclaredMethod("isOpen", (Class [])null); 178 if (((Boolean )method.invoke(channel, (Object [])null)).booleanValue()) 179 { 180 method = aClass.getDeclaredMethod("close", (Class [])null); 181 method.invoke(channel, (Object [])null); 182 channel = null; 183 } 184 } 185 catch (Throwable t) 186 { 187 System.err.println("Unable to close file channel: " + t.getLocalizedMessage()); 188 } 189 return channel; 190 } 191 192 } 193 | Popular Tags |