View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * @author Tomas Zezula
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  }