1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.index.context;
20
21 import java.io.IOException;
22 import java.util.Collections;
23 import java.util.HashSet;
24 import java.util.Set;
25 import java.util.concurrent.ConcurrentHashMap;
26
27 import org.apache.lucene.store.Directory;
28 import org.apache.lucene.store.Lock;
29 import org.apache.lucene.store.LockFactory;
30
31 import static java.util.Objects.requireNonNull;
32
33
34
35
36
37 final class TrackingLockFactory extends LockFactory {
38
39 private final LockFactory delegate;
40
41 private final Set<TrackingLock> emittedLocks;
42
43 TrackingLockFactory(final LockFactory delegate) {
44 this.delegate = requireNonNull(delegate);
45 this.emittedLocks = Collections.newSetFromMap(new ConcurrentHashMap<TrackingLock, Boolean>());
46 }
47
48 Set<? extends Lock> getEmittedLocks(String name) {
49 final Set<Lock> result = new HashSet<>();
50 for (TrackingLock lock : emittedLocks) {
51 if (name == null || name.equals(lock.getName())) {
52 result.add(lock);
53 }
54 }
55 return result;
56 }
57
58 @Override
59 public Lock obtainLock(Directory dir, String lockName) throws IOException {
60 final TrackingLock lck = new TrackingLock(delegate.obtainLock(dir, lockName), lockName);
61 emittedLocks.add(lck);
62 return lck;
63 }
64
65 private final class TrackingLock extends Lock {
66 private final Lock delegate;
67
68 private final String name;
69
70 TrackingLock(final Lock delegate, final String name) {
71 this.delegate = requireNonNull(delegate);
72 this.name = requireNonNull(name);
73 }
74
75 String getName() {
76 return name;
77 }
78
79 @Override
80 public void close() throws IOException {
81 try {
82 delegate.close();
83 } finally {
84 emittedLocks.remove(this);
85 }
86 }
87
88 @Override
89 public void ensureValid() throws IOException {
90 delegate.ensureValid();
91 }
92 }
93 }