KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > store > mem > TransientLockStore


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/stores/org/apache/slide/store/mem/TransientLockStore.java,v 1.2 2004/07/28 09:34:05 ib Exp $
3  * $Revision: 1.2 $
4  * $Date: 2004/07/28 09:34:05 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23 package org.apache.slide.store.mem;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.apache.slide.common.ServiceAccessException;
30 import org.apache.slide.common.Uri;
31 import org.apache.slide.lock.LockTokenNotFoundException;
32 import org.apache.slide.lock.NodeLock;
33 import org.apache.slide.store.LockStore;
34
35
36 /**
37  */

38 public class TransientLockStore extends AbstractTransientStore
39    implements LockStore
40 {
41    // Note: we don't clone Locks because ExtendedStore clones
42

43    public void putLock(Uri uri, NodeLock lock) throws ServiceAccessException {
44       debug("putLock {0} {1}", uri, lock.getLockId());
45       List JavaDoc list = (List JavaDoc)get(uri.toString());
46       if (list != null) {
47          list = new ArrayList JavaDoc(list);
48       } else {
49          list = new ArrayList JavaDoc();
50       }
51       list.add(lock);
52       put(uri.toString(), list);
53    }
54    
55    public void renewLock(Uri uri, NodeLock lock)
56       throws ServiceAccessException, LockTokenNotFoundException
57    {
58       debug("renewLock {0} {1}", uri, lock.getLockId());
59       List JavaDoc list = (List JavaDoc)get(uri.toString());
60       if (list == null || !list.contains(lock)) {
61          throw new LockTokenNotFoundException(lock);
62       }
63       list = new ArrayList JavaDoc(list);
64       list.remove(lock);
65       list.add(lock);
66       put(uri.toString(), list);
67    }
68
69    public void removeLock(Uri uri, NodeLock lock)
70       throws ServiceAccessException, LockTokenNotFoundException
71    {
72       debug("removeLock {0} {1}", uri, lock.getLockId());
73       List JavaDoc list = (List JavaDoc)get(uri.toString());
74       if (list == null) {
75          throw new LockTokenNotFoundException(lock);
76       }
77       if (!list.contains(lock)) {
78          throw new LockTokenNotFoundException(lock);
79       } else {
80          if (list.size() == 1) {
81             remove(uri.toString());
82          } else {
83             list = new ArrayList JavaDoc(list);
84             list.remove(lock);
85             put(uri.toString(), list);
86          }
87       }
88    }
89    
90    public void killLock(Uri uri, NodeLock lock)
91       throws ServiceAccessException, LockTokenNotFoundException
92    {
93       debug("killLock {0} {1}", uri, lock.getLockId());
94       removeLock(uri, lock);
95    }
96
97    public Enumeration JavaDoc enumerateLocks(Uri uri) throws ServiceAccessException
98    {
99       debug("enumerateLocks {0}", uri);
100
101       List JavaDoc list = (List JavaDoc)get(uri.toString());
102       if (list != null) {
103          return new IteratorEnum(list.iterator());
104       } else {
105          return EMPTY_ENUM;
106       }
107    }
108 }
109
Popular Tags