01: /*
02: * Copyright 2004-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.compass.needle.coherence;
18:
19: import java.io.IOException;
20:
21: import com.tangosol.net.NamedCache;
22: import org.apache.commons.logging.Log;
23: import org.apache.commons.logging.LogFactory;
24: import org.apache.lucene.store.Lock;
25: import org.apache.lucene.store.LockFactory;
26: import org.apache.lucene.store.LockObtainFailedException;
27:
28: /**
29: * A locak factory using.
30: *
31: * @author kimchy
32: */
33: public class DefaultCoherenceLockFactory extends LockFactory {
34:
35: private static final Log log = LogFactory
36: .getLog(DefaultCoherenceLockFactory.class);
37:
38: private NamedCache cache;
39:
40: private String indexName;
41:
42: public DefaultCoherenceLockFactory(NamedCache cache,
43: String indexName) {
44: this .cache = cache;
45: this .indexName = indexName;
46: }
47:
48: public void clearLock(String lockName) throws IOException {
49: cache.unlock(new FileLockKey(indexName, lockName));
50: }
51:
52: public Lock makeLock(String lockName) {
53: return new CoherenceLock(lockName);
54: }
55:
56: public class CoherenceLock extends Lock {
57:
58: private FileLockKey fileLock;
59:
60: public CoherenceLock(String lockName) {
61: this .fileLock = new FileLockKey(indexName, lockName);
62: }
63:
64: public boolean isLocked() {
65: // TOOD how to we really check if something is locked?
66: return false;
67: }
68:
69: public boolean obtain() throws IOException {
70: return cache.lock(fileLock);
71: }
72:
73: public boolean obtain(long lockWaitTimeout)
74: throws LockObtainFailedException, IOException {
75: return cache.lock(fileLock, lockWaitTimeout);
76: }
77:
78: public void release() {
79: try {
80: cache.unlock(fileLock);
81: } catch (Exception e) {
82: if (log.isWarnEnabled()) {
83: log.warn("Failed to release locke on index ["
84: + indexName + "]", e);
85: }
86: }
87: }
88: }
89: }
|