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