View Javadoc

1   /***
2    * 
3    * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  package org.logicblaze.lingo.util.locks;
19  
20  import edu.emory.mathcs.backport.java.util.concurrent.locks.Condition;
21  import edu.emory.mathcs.backport.java.util.concurrent.locks.Lock;
22  import edu.emory.mathcs.backport.java.util.concurrent.locks.ReentrantLock;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  /***
28   * Represents a client side {@link ConditionManager} which is used to
29   * communicate with a possibly remote {@link ConditionServer}
30   * 
31   * @version $Revision$
32   */
33  public class ConditionManagerImpl implements ConditionListener, ConnectionManager {
34  
35      private ConditionServer server;
36      private Map map = new HashMap();
37  
38      /***
39       * Gets the condition for the given ID lazily creating one if required.
40       */
41      public Condition getCondition(String id) {
42          synchronized (map) {
43              ConditionClient client = (ConditionClient) map.get(id);
44              if (client == null) {
45                  client = createCondition(id);
46                  map.put(id, client);
47              }
48              return client;
49          }
50      }
51  
52      /***
53       * Removes the condition of the given ID
54       */
55      public boolean removeCondition(String id) {
56          synchronized (map) {
57              return map.remove(id) != null;
58          }
59      }
60  
61      public void onSignal(String id) {
62          synchronized (map) {
63              ConditionClient client = (ConditionClient) map.get(id);
64              if (client != null) {
65                  client.onSignal();
66              }
67          }
68      }
69  
70      public void onSignalAll(String id) {
71          ConditionClient client = (ConditionClient) map.get(id);
72          if (client != null) {
73              client.onSignalAll();
74          }
75      }
76  
77      // Implementation methods
78      // -------------------------------------------------------------------------
79  
80      /***
81       * Factory method to create a new condition
82       */
83      protected ConditionClient createCondition(String id) {
84          return new ConditionClient(server, this, id, createLock(id));
85      }
86  
87      /***
88       * Factory method to change a lock
89       */
90      protected Lock createLock(String id) {
91          return new ReentrantLock();
92      }
93  }