View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    * 
4    * 
5    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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  package org.kuali.rice.core.resourceloader;
18  
19  import org.apache.log4j.Logger;
20  
21  /**
22   * Synchronization primitive that implements a condition that can be waited upon.
23   * 
24   * @author Kuali Rice Team (rice.collab@kuali.org)
25   */
26  public class ContextualConfigLock {
27      private static final Logger LOG = Logger.getLogger(ContextualConfigLock.class);
28  
29      private String name;
30      private boolean fired;
31      private Object lock = new Object();
32  
33      public ContextualConfigLock() {
34          this(null);
35      }
36  
37      public ContextualConfigLock(String name) {
38          if (name == null) {
39              this.name = "<<anonymous>>";
40          } else {
41              this.name = name;
42          }
43      }
44  
45      public void await() {
46          synchronized (lock) {
47              while (!fired) {
48                  try {
49                      lock.wait();
50                  } catch (InterruptedException ie) {
51                      LOG.warn("Interrupted while waiting for condition: " + name, ie);
52                  }
53              }
54          }
55      }
56  
57      public boolean hasFired() {
58          synchronized (lock) {
59              return fired;
60          }
61      }
62  
63      public void fire() {
64          synchronized (lock) {
65              if (fired) return;
66              fired = true;
67              lock.notifyAll();
68          }
69      }
70  
71      public void reset() {
72          synchronized (lock) {
73              fired = false;
74          }
75      }
76  
77      public String toString() {
78          return "[Condition: name=" + name + "]";
79      }
80  }