KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > StartupLock


1 /**
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.util;
5
6 import com.tc.io.TCFile;
7 import com.tc.io.TCFileChannel;
8 import com.tc.io.TCFileLock;
9 import com.tc.io.TCRandomFileAccess;
10 import com.tc.logging.TCLogger;
11 import com.tc.logging.TCLogging;
12 import com.tc.util.startuplock.FileNotCreatedException;
13 import com.tc.util.startuplock.LocationNotCreatedException;
14
15 import java.io.FileNotFoundException JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.nio.channels.OverlappingFileLockException JavaDoc;
18
19 /**
20  * Class for testing if it is safe to startup a process on a specified directory (i.e. will it corrupt a db)
21  */

22 public class StartupLock {
23   private static final TCLogger logger = TCLogging.getLogger(StartupLock.class);
24   private final TCFile location;
25   private TCFileLock lock;
26   private TCFileChannel channel;
27
28   public StartupLock(TCFile location) {
29     this.location = location;
30   }
31
32   public synchronized void release() {
33     try {
34       if (lock != null) {
35         try {
36           this.lock.release();
37         } catch (IOException JavaDoc e) {
38           logger.error(e);
39         }
40       }
41
42       if (channel != null) {
43         try {
44           channel.close();
45         } catch (IOException JavaDoc e) {
46           logger.error(e);
47         }
48       }
49     } finally {
50       lock = null;
51       channel = null;
52     }
53   }
54
55   public synchronized boolean canProceed(TCRandomFileAccess randomFileAccess, boolean block)
56       throws LocationNotCreatedException, FileNotCreatedException {
57     // Get a file channel for the file
58

59     TCFile tcFile = location.createNewTCFile(location, "startup.lck");
60
61     ensureLocationExists();
62     ensureFileExists(tcFile);
63
64     try {
65       channel = randomFileAccess.getChannel(tcFile, "rw");
66     } catch (FileNotFoundException JavaDoc fnfe) {
67       throw new TCAssertionError(fnfe);
68     }
69
70     try {
71       if (block) {
72         lock = channel.lock();
73       } else {
74         lock = channel.tryLock();
75       }
76     } catch (OverlappingFileLockException JavaDoc e) {
77       // File is already locked in this thread or virtual machine
78
} catch (IOException JavaDoc ioe) {
79       throw new TCAssertionError(ioe);
80     }
81
82     Assert.eval(tcFile.exists());
83     return lock != null;
84   }
85
86   private void ensureFileExists(TCFile file) throws FileNotCreatedException {
87     if (!file.exists()) {
88       try {
89         file.createNewFile();
90       } catch (IOException JavaDoc e) {
91         throw new FileNotCreatedException("Could not create file for startup lock: " + file
92                                           + ". Please ensure that this file can be created.");
93       }
94       Assert.eval(file.exists());
95     }
96   }
97
98   private void ensureLocationExists() throws LocationNotCreatedException {
99     if (!location.exists()) {
100       try {
101         location.forceMkdir();
102       } catch (IOException JavaDoc e) {
103         throw new LocationNotCreatedException("Could not create location for startup lock: " + location
104                                               + ". Please ensure that this directory can be created.");
105       }
106     }
107   }
108 }
109
Popular Tags