KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > header > TimerFileLockEnabled


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.header;
22
23 import java.io.*;
24
25 import com.db4o.*;
26 import com.db4o.ext.*;
27 import com.db4o.foundation.*;
28 import com.db4o.inside.*;
29
30
31 /**
32  * @exclude
33  */

34 public class TimerFileLockEnabled extends TimerFileLock{
35     
36     private final YapFile _file;
37     
38     private int _headerLockOffset = 2 + YapConst.INT_LENGTH;
39     
40     private final long _opentime;
41     
42     private int _baseAddress;
43     
44     private int _openTimeOffset;
45
46     private int _accessTimeOffset;
47     
48     private boolean _closed = false;
49     
50     
51     public TimerFileLockEnabled(YapFile file) {
52         _file = file;
53         _opentime = uniqueOpenTime();
54     }
55     
56     public void checkHeaderLock() {
57         YapWriter reader = headerLockIO();
58         reader.read();
59         if(reader.readInt() != (int)_opentime ){
60             throw new DatabaseFileLockedException();
61         }
62         writeHeaderLock();
63     }
64     
65     public void checkOpenTime() {
66         YapWriter reader = openTimeIO();
67         if(reader == null){
68             return;
69         }
70         reader.read();
71         if(reader.readLong() != _opentime){
72             Exceptions4.throwRuntimeException(22);
73         }
74         writeOpenTime();
75     }
76     
77     public void close() throws IOException {
78         writeAccessTime(true);
79         _closed = true;
80     }
81     
82     private YapWriter getWriter(int address, int offset, int length) {
83         YapWriter writer = _file.getWriter(_file.getTransaction(), address, length);
84         writer.moveForward(offset);
85         return writer;
86     }
87     
88     private YapWriter headerLockIO(){
89         YapWriter writer = getWriter(0, _headerLockOffset, YapConst.INT_LENGTH);
90         if (Debug.xbytes) {
91             writer.setID(YapConst.IGNORE_ID);
92         }
93         return writer;
94     }
95
96     public boolean lockFile() {
97         return true;
98     }
99     
100     public long openTime() {
101         return _opentime;
102     }
103
104     private YapWriter openTimeIO(){
105         if(_baseAddress == 0){
106             return null;
107         }
108         YapWriter writer = getWriter(_baseAddress, _openTimeOffset, YapConst.LONG_LENGTH);
109         if (Debug.xbytes) {
110             writer.setID(YapConst.IGNORE_ID);
111         }
112         return writer;
113     }
114
115     public void run(){
116         Thread JavaDoc t = Thread.currentThread();
117         t.setName("db4o file lock");
118         try{
119             while(writeAccessTime(false)){
120                 Cool.sleepIgnoringInterruption(YapConst.LOCK_TIME_INTERVAL);
121                 if(_closed){
122                     break;
123                 }
124             }
125         }catch(IOException e){
126         }
127     }
128
129     public void setAddresses(int baseAddress, int openTimeOffset, int accessTimeOffset){
130         _baseAddress = baseAddress;
131         _openTimeOffset = openTimeOffset;
132         _accessTimeOffset = accessTimeOffset;
133     }
134     
135     public void start() throws IOException{
136         writeAccessTime(false);
137         _file.syncFiles();
138         checkOpenTime();
139         // TODO: Thread could be a daemon.
140
new Thread JavaDoc(this).start();
141     }
142     
143     private long uniqueOpenTime(){
144         return System.currentTimeMillis();
145         // TODO: More security is possible here to make this time unique
146
// to other processes.
147
}
148     
149     private boolean writeAccessTime(boolean closing) throws IOException{
150         if(_baseAddress < 1){
151             return true;
152         }
153         long time = closing ? 0 : System.currentTimeMillis();
154         return _file.writeAccessTime(_baseAddress, _accessTimeOffset, time);
155     }
156
157
158     public void writeHeaderLock(){
159         YapWriter writer = headerLockIO();
160         writer.writeInt((int)_opentime);
161         writer.write();
162     }
163
164     public void writeOpenTime() {
165         YapWriter writer = openTimeIO();
166         if(writer== null){
167             return;
168         }
169         writer.writeLong(_opentime);
170         writer.write();
171     }
172     
173 }
174
175
176
Popular Tags