1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.danube.webdav.fs;
14
15 import org.abstracthorizon.danube.webdav.lock.Lock;
16 import org.abstracthorizon.danube.webdav.lock.impl.SimpleInMemoryLockingMechanism;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.io.RandomAccessFile;
21 import java.util.Collection;
22 import java.util.HashMap;
23 import java.util.Map;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 public class FSLockingMechanism extends SimpleInMemoryLockingMechanism {
39
40
41 protected Map<Object, RandomAccessFile> fsLocks = new HashMap<Object, RandomAccessFile>();
42
43
44
45
46 public FSLockingMechanism() {
47 }
48
49
50
51
52
53
54
55 @Override
56 public synchronized boolean lockResource(Lock lock, Object resource) {
57 if (super.lockResource(lock, resource)) {
58 try {
59 File file = (File)resource;
60 RandomAccessFile raf = new RandomAccessFile(file, "rw");
61 fsLocks.put(resource, raf);
62 } catch (IOException ignore) {
63 }
64 return true;
65 } else {
66 return false;
67 }
68 }
69
70
71
72
73
74 public synchronized void unlockResources(Lock lock) {
75 Collection<Object> resources = lockResources.get(lock);
76 if ((resources != null) && (resources.size() > 0)) {
77 for (Object resource : resources) {
78 RandomAccessFile raf = fsLocks.remove(resource);
79 if (raf != null) {
80 try {
81 raf.close();
82 } catch (IOException ignore) {
83 }
84 }
85 }
86 }
87 super.unlockResources(lock);
88 }
89 }