| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 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 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
public class FileLocker |
| 50 | |
{ |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | 0 | private Object lock = null; |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | 0 | private Object lockChannel = null; |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
private File lockFile; |
| 67 | |
|
| 68 | |
public FileLocker(File file) |
| 69 | 0 | { |
| 70 | 0 | String lockFileName = file.getName() + ".lock"; |
| 71 | 0 | File parent = file.getParentFile(); |
| 72 | 0 | if (parent == null) |
| 73 | |
{ |
| 74 | 0 | lockFile = new File(lockFileName); |
| 75 | 0 | } |
| 76 | |
else |
| 77 | |
{ |
| 78 | 0 | lockFile = new File(parent, lockFileName); |
| 79 | |
} |
| 80 | 0 | lockFile.deleteOnExit(); |
| 81 | 0 | } |
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
public boolean lock() |
| 87 | |
{ |
| 88 | 0 | String useNioProperty = System.getProperty("cobertura.use.java.nio"); |
| 89 | 0 | if (System.getProperty("java.version").startsWith("1.3") || |
| 90 | |
((useNioProperty != null) && useNioProperty.equalsIgnoreCase("false"))) |
| 91 | |
{ |
| 92 | 0 | return true; |
| 93 | |
} |
| 94 | |
|
| 95 | |
try |
| 96 | |
{ |
| 97 | 0 | Class aClass = Class.forName("java.io.RandomAccessFile"); |
| 98 | 0 | Method method = aClass.getDeclaredMethod("getChannel", (Class[])null); |
| 99 | 0 | lockChannel = method.invoke(new RandomAccessFile(lockFile, "rw"), (Object[])null); |
| 100 | |
} |
| 101 | 0 | catch (FileNotFoundException e) |
| 102 | |
{ |
| 103 | 0 | System.err.println("Unable to get lock channel for " + lockFile.getAbsolutePath() |
| 104 | |
+ ": " + e.getLocalizedMessage()); |
| 105 | 0 | return false; |
| 106 | |
} |
| 107 | 0 | catch (InvocationTargetException e) |
| 108 | |
{ |
| 109 | 0 | System.err.println("Unable to get lock channel for " + lockFile.getAbsolutePath() |
| 110 | |
+ ": " + e.getLocalizedMessage()); |
| 111 | 0 | return false; |
| 112 | |
} |
| 113 | 0 | catch (Throwable t) |
| 114 | |
{ |
| 115 | 0 | System.err.println("Unable to execute RandomAccessFile.getChannel() using reflection: " |
| 116 | |
+ t.getLocalizedMessage()); |
| 117 | 0 | t.printStackTrace(); |
| 118 | 0 | } |
| 119 | |
|
| 120 | |
try |
| 121 | |
{ |
| 122 | 0 | Class aClass = Class.forName("java.nio.channels.FileChannel"); |
| 123 | 0 | Method method = aClass.getDeclaredMethod("lock", (Class[])null); |
| 124 | 0 | lock = method.invoke(lockChannel, (Object[])null); |
| 125 | |
} |
| 126 | 0 | catch (InvocationTargetException e) |
| 127 | |
{ |
| 128 | 0 | System.err.println("---------------------------------------"); |
| 129 | 0 | e.printStackTrace(System.err); |
| 130 | 0 | System.err.println("---------------------------------------"); |
| 131 | 0 | System.err.println("Unable to get lock on " + lockFile.getAbsolutePath() + ": " |
| 132 | |
+ e.getLocalizedMessage()); |
| 133 | 0 | System.err.println("This is known to happen on Linux kernel 2.6.20."); |
| 134 | 0 | System.err.println("Make sure cobertura.jar is in the root classpath of the jvm "); |
| 135 | 0 | System.err.println("process running the instrumented code. If the instrumented code "); |
| 136 | 0 | System.err.println("is running in a web server, this means cobertura.jar should be in "); |
| 137 | 0 | System.err.println("the web server's lib directory."); |
| 138 | 0 | System.err.println("Don't put multiple copies of cobertura.jar in different WEB-INF/lib directories."); |
| 139 | 0 | System.err.println("Only one classloader should load cobertura. It should be the root classloader."); |
| 140 | 0 | System.err.println("---------------------------------------"); |
| 141 | 0 | return false; |
| 142 | |
} |
| 143 | 0 | catch (Throwable t) |
| 144 | |
{ |
| 145 | 0 | System.err.println("Unable to execute FileChannel.lock() using reflection: " |
| 146 | |
+ t.getLocalizedMessage()); |
| 147 | 0 | t.printStackTrace(); |
| 148 | 0 | } |
| 149 | |
|
| 150 | 0 | return true; |
| 151 | |
} |
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
public void release() |
| 157 | |
{ |
| 158 | 0 | if (lock != null) |
| 159 | 0 | lock = releaseFileLock(lock); |
| 160 | 0 | if (lockChannel != null) |
| 161 | 0 | lockChannel = closeChannel(lockChannel); |
| 162 | 0 | lockFile.delete(); |
| 163 | 0 | } |
| 164 | |
|
| 165 | |
private static Object releaseFileLock(Object lock) |
| 166 | |
{ |
| 167 | |
try |
| 168 | |
{ |
| 169 | 0 | Class aClass = Class.forName("java.nio.channels.FileLock"); |
| 170 | 0 | Method method = aClass.getDeclaredMethod("isValid", (Class[])null); |
| 171 | 0 | if (((Boolean)method.invoke(lock, (Object[])null)).booleanValue()) |
| 172 | |
{ |
| 173 | 0 | method = aClass.getDeclaredMethod("release", (Class[])null); |
| 174 | 0 | method.invoke(lock, (Object[])null); |
| 175 | 0 | lock = null; |
| 176 | |
} |
| 177 | |
} |
| 178 | 0 | catch (Throwable t) |
| 179 | |
{ |
| 180 | 0 | System.err.println("Unable to release locked file: " + t.getLocalizedMessage()); |
| 181 | 0 | } |
| 182 | 0 | return lock; |
| 183 | |
} |
| 184 | |
|
| 185 | |
private static Object closeChannel(Object channel) |
| 186 | |
{ |
| 187 | |
try |
| 188 | |
{ |
| 189 | 0 | Class aClass = Class.forName("java.nio.channels.spi.AbstractInterruptibleChannel"); |
| 190 | 0 | Method method = aClass.getDeclaredMethod("isOpen", (Class[])null); |
| 191 | 0 | if (((Boolean)method.invoke(channel, (Object[])null)).booleanValue()) |
| 192 | |
{ |
| 193 | 0 | method = aClass.getDeclaredMethod("close", (Class[])null); |
| 194 | 0 | method.invoke(channel, (Object[])null); |
| 195 | 0 | channel = null; |
| 196 | |
} |
| 197 | |
} |
| 198 | 0 | catch (Throwable t) |
| 199 | |
{ |
| 200 | 0 | System.err.println("Unable to close file channel: " + t.getLocalizedMessage()); |
| 201 | 0 | } |
| 202 | 0 | return channel; |
| 203 | |
} |
| 204 | |
|
| 205 | |
} |