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.Set;
25 import java.util.concurrent.ConcurrentHashMap;
26 import org.apache.lucene.store.Directory;
27 import org.apache.lucene.store.Lock;
28 import org.apache.lucene.store.LockFactory;
29 import static com.google.common.base.Preconditions.checkNotNull;
30 import java.util.HashSet;
31
32
33
34
35
36 final class TrackingLockFactory
37 extends LockFactory
38 {
39
40 private final LockFactory delegate;
41
42 private final Set<TrackingLock> emittedLocks;
43
44 TrackingLockFactory( final LockFactory delegate )
45 {
46 this.delegate = checkNotNull( delegate );
47 this.emittedLocks = Collections.newSetFromMap( new ConcurrentHashMap<TrackingLock, Boolean>() );
48 }
49
50 Set<? extends Lock> getEmittedLocks( String name )
51 {
52 final Set<Lock> result = new HashSet<>();
53 for ( TrackingLock lock : emittedLocks )
54 {
55 if ( name == null || name.equals( lock.getName() ) )
56 {
57 result.add( lock );
58 }
59 }
60 return result;
61 }
62
63 @Override
64 public Lock obtainLock( Directory dir, String lockName )
65 throws IOException
66 {
67 final TrackingLock lck = new TrackingLock( delegate.obtainLock( dir, lockName ), lockName );
68 emittedLocks.add( lck );
69 return lck;
70 }
71
72 private final class TrackingLock
73 extends Lock
74 {
75 private final Lock delegate;
76
77 private final String name;
78
79 TrackingLock( final Lock delegate, final String name )
80 {
81 this.delegate = checkNotNull( delegate );
82 this.name = checkNotNull( name );
83 }
84
85 String getName()
86 {
87 return name;
88 }
89
90 @Override
91 public void close()
92 throws IOException
93 {
94 try
95 {
96 delegate.close();
97 }
98 finally
99 {
100 emittedLocks.remove( this );
101 }
102 }
103
104 @Override
105 public void ensureValid()
106 throws IOException
107 {
108 delegate.ensureValid();
109 }
110 }
111 }