add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / test-framework / org / apache / lucene / store / MockLockFactoryWrapper.java
1 package org.apache.lucene.store;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.IOException;
21
22 public class MockLockFactoryWrapper extends LockFactory {
23   MockDirectoryWrapper dir;
24   LockFactory delegate;
25   
26   public MockLockFactoryWrapper(MockDirectoryWrapper dir, LockFactory delegate) {
27     this.dir = dir;
28     this.delegate = delegate;
29   }
30   
31   @Override
32   public void setLockPrefix(String lockPrefix) {
33     delegate.setLockPrefix(lockPrefix);
34   }
35
36   @Override
37   public String getLockPrefix() {
38     return delegate.getLockPrefix();
39   }
40
41   @Override
42   public Lock makeLock(String lockName) {
43     return new MockLock(delegate.makeLock(lockName), lockName);
44   }
45
46   @Override
47   public void clearLock(String lockName) throws IOException {
48     delegate.clearLock(lockName);
49     dir.openLocks.remove(lockName);
50   }
51   
52   @Override
53   public String toString() {
54     return "MockLockFactoryWrapper(" + delegate.toString() + ")";
55   }
56
57   private class MockLock extends Lock {
58     private Lock delegateLock;
59     private String name;
60     
61     MockLock(Lock delegate, String name) {
62       this.delegateLock = delegate;
63       this.name = name;
64     }
65
66     @Override
67     public boolean obtain() throws IOException {
68       if (delegateLock.obtain()) {
69         dir.openLocks.add(name);
70         return true;
71       } else {
72         return false;
73       }
74     }
75
76     @Override
77     public void release() throws IOException {
78       delegateLock.release();
79       dir.openLocks.remove(name);
80     }
81
82     @Override
83     public boolean isLocked() throws IOException {
84       return delegateLock.isLocked();
85     }
86   }
87 }