KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cobertura > util > FileLocker


1 /* Cobertura - http://cobertura.sourceforge.net/
2  *
3  * Copyright (C) 2006 John Lewis
4  * Copyright (C) 2006 Mark Doliner
5  *
6  * Note: This file is dual licensed under the GPL and the Apache
7  * Source License 1.1 (so that it can be used from both the main
8  * Cobertura classes and the ant tasks).
9  *
10  * Cobertura is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published
12  * by the Free Software Foundation; either version 2 of the License,
13  * or (at your option) any later version.
14  *
15  * Cobertura is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Cobertura; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23  * USA
24  */

25
26 package net.sourceforge.cobertura.util;
27
28 import java.io.File JavaDoc;
29 import java.io.FileNotFoundException JavaDoc;
30 import java.io.RandomAccessFile JavaDoc;
31 import java.lang.reflect.InvocationTargetException JavaDoc;
32 import java.lang.reflect.Method JavaDoc;
33
34 /**
35  * This class controls access to any file so that multiple JVMs will
36  * not be able to write to the file at the same time.
37  *
38  * A file called "filename.lock" is created and Java's FileLock class
39  * is used to lock the file.
40  *
41  * The java.nio classes were introduced in Java 1.4, so this class
42  * does a no-op when used with Java 1.3. The class maintains
43  * compatability with Java 1.3 by accessing the java.nio classes
44  * using reflection.
45  *
46  * @author John Lewis
47  * @author Mark Doliner
48  */

49 public class FileLocker
50 {
51
52     /**
53      * An object of type FileLock, created using reflection.
54      */

55     private Object JavaDoc lock = null;
56
57     /**
58      * An object of type FileChannel, created using reflection.
59      */

60     private Object JavaDoc lockChannel = null;
61
62     /**
63      * A file called "filename.lock" that resides in the same directory
64      * as "filename"
65      */

66     private File JavaDoc lockFile;
67
68     public FileLocker(File JavaDoc file)
69     {
70         String JavaDoc lockFileName = file.getName() + ".lock";
71         File JavaDoc parent = file.getParentFile();
72         if (parent == null)
73         {
74             lockFile = new File JavaDoc(lockFileName);
75         }
76         else
77         {
78             lockFile = new File JavaDoc(parent, lockFileName);
79         }
80         lockFile.deleteOnExit();
81     }
82
83     /**
84      * Obtains a lock on the file. This blocks until the lock is obtained.
85      */

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 JavaDoc aClass = Class.forName("java.io.RandomAccessFile");
96             Method JavaDoc method = aClass.getDeclaredMethod("getChannel", (Class JavaDoc[])null);
97             lockChannel = method.invoke(new RandomAccessFile JavaDoc(lockFile, "rw"), (Object JavaDoc[])null);
98         }
99         catch (FileNotFoundException JavaDoc e)
100         {
101             System.err.println("Unable to get lock channel for " + lockFile.getAbsolutePath()
102                     + ": " + e.getLocalizedMessage());
103             return false;
104         }
105         catch (InvocationTargetException JavaDoc e)
106         {
107             System.err.println("Unable to get lock channel for " + lockFile.getAbsolutePath()
108                     + ": " + e.getLocalizedMessage());
109             return false;
110         }
111         catch (Throwable JavaDoc 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 JavaDoc aClass = Class.forName("java.nio.channels.FileChannel");
121             Method JavaDoc method = aClass.getDeclaredMethod("lock", (Class JavaDoc[])null);
122             lock = method.invoke(lockChannel, (Object JavaDoc[])null);
123         }
124         catch (InvocationTargetException JavaDoc e)
125         {
126             System.err.println("Unable to get lock on " + lockFile.getAbsolutePath() + ": "
127                     + e.getLocalizedMessage());
128             return false;
129         }
130         catch (Throwable JavaDoc 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     /**
141      * Releases the lock on the file.
142      */

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 JavaDoc releaseFileLock(Object JavaDoc lock)
153     {
154         try
155         {
156             Class JavaDoc aClass = Class.forName("java.nio.channels.FileLock");
157             Method JavaDoc method = aClass.getDeclaredMethod("isValid", (Class JavaDoc[])null);
158             if (((Boolean JavaDoc)method.invoke(lock, (Object JavaDoc[])null)).booleanValue())
159             {
160                 method = aClass.getDeclaredMethod("release", (Class JavaDoc[])null);
161                 method.invoke(lock, (Object JavaDoc[])null);
162                 lock = null;
163             }
164         }
165         catch (Throwable JavaDoc t)
166         {
167             System.err.println("Unable to release locked file: " + t.getLocalizedMessage());
168         }
169         return lock;
170     }
171
172     private static Object JavaDoc closeChannel(Object JavaDoc channel)
173     {
174         try
175         {
176             Class JavaDoc aClass = Class.forName("java.nio.channels.spi.AbstractInterruptibleChannel");
177             Method JavaDoc method = aClass.getDeclaredMethod("isOpen", (Class JavaDoc[])null);
178             if (((Boolean JavaDoc)method.invoke(channel, (Object JavaDoc[])null)).booleanValue())
179             {
180                 method = aClass.getDeclaredMethod("close", (Class JavaDoc[])null);
181                 method.invoke(channel, (Object JavaDoc[])null);
182                 channel = null;
183             }
184         }
185         catch (Throwable JavaDoc t)
186         {
187             System.err.println("Unable to close file channel: " + t.getLocalizedMessage());
188         }
189         return channel;
190     }
191
192 }
193
Popular Tags