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