KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > configurator > Locker_JavaIo


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.update.internal.configurator;
12
13 import java.io.*;
14
15 /**
16  * Internal class.
17  */

18 public class Locker_JavaIo implements Locker {
19     private File lockFile;
20     private RandomAccessFile lockRAF;
21
22     public Locker_JavaIo(File lockFile) {
23         this.lockFile = lockFile;
24     }
25
26     public synchronized boolean lock() throws IOException {
27         //if the lock file already exists, try to delete,
28
//assume failure means another eclipse has it open
29
if (lockFile.exists())
30             lockFile.delete();
31         if (lockFile.exists())
32             return false;
33
34         //open the lock file so other instances can't co-exist
35
lockRAF = new RandomAccessFile(lockFile, "rw"); //$NON-NLS-1$
36
lockRAF.writeByte(0);
37
38         return true;
39     }
40
41     public synchronized void release() {
42         try {
43             if (lockRAF != null) {
44                 lockRAF.close();
45                 lockRAF = null;
46             }
47         } catch (IOException e) {
48             //don't complain, we're making a best effort to clean up
49
}
50         if (lockFile != null)
51             lockFile.delete();
52     }
53 }
54
Popular Tags