View Javadoc

1   package org.apache.fulcrum.yaafi.framework.util;
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 org.apache.avalon.framework.logger.Logger;
23  import org.apache.fulcrum.yaafi.framework.locking.AvalonLoggerFacade;
24  
25  
26  /**
27   * A simple lock manager supporting read locks and write locks. The main
28   * intention is to shield the application from the implementation details.
29   *
30   * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
31   */
32  public class ReadWriteLock
33  {
34      /** Read/Write lock to synchronize acess to services */
35      private final org.apache.fulcrum.yaafi.framework.locking.ReadWriteLock lock;
36  
37      /**
38       * Constructor
39       *
40       * @param name the name of the lock
41       * @param logger the logger to be used
42       */
43      public ReadWriteLock( String name, Logger logger )
44      {
45          this.lock = new org.apache.fulcrum.yaafi.framework.locking.ReadWriteLock(
46              name,
47              new AvalonLoggerFacade(logger)
48              );
49      }
50  
51      /**
52       * @return a read lock
53       */
54      public Object getReadLock(String ownerId)
55      	throws InterruptedException
56      {
57          this.lock.acquireRead(ownerId,0);
58          return this;
59      }
60  
61      /**
62       * @return a write lock
63       */
64      public Object getWriteLock(String ownerId)
65      	throws InterruptedException
66      {
67          this.lock.acquireWrite(ownerId,0);
68          return this;
69      }
70  
71      /**
72       * releases the lock
73       * @param lock the lock
74       */
75      public void releaseLock(Object lock, String ownerId)
76      {
77          this.lock.release(ownerId);
78      }
79  }