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.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   * @author Tomas Zezula
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 }