01: /*
02: * Copyright 2005-2006 The Kuali Foundation.
03: *
04: *
05: * Licensed under the Educational Community License, Version 1.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.opensource.org/licenses/ecl1.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 edu.iu.uis.eden.core;
18:
19: import org.apache.log4j.Logger;
20:
21: /**
22: * Synchronization primitive that implements a condition that can be waited upon.
23: *
24: * @author Aaron Hamid (arh14 at cornell dot edu)
25: */
26: public class ContextualConfigLock {
27: private static final Logger LOG = Logger
28: .getLogger(ContextualConfigLock.class);
29:
30: private String name;
31: private boolean fired;
32: private Object lock = new Object();
33:
34: public ContextualConfigLock() {
35: this (null);
36: }
37:
38: public ContextualConfigLock(String name) {
39: if (name == null) {
40: this .name = "<<anonymous>>";
41: } else {
42: this .name = name;
43: }
44: }
45:
46: public void await() {
47: synchronized (lock) {
48: while (!fired) {
49: try {
50: lock.wait();
51: } catch (InterruptedException ie) {
52: LOG.warn(
53: "Interrupted while waiting for condition: "
54: + name, ie);
55: }
56: }
57: }
58: }
59:
60: public boolean hasFired() {
61: synchronized (lock) {
62: return fired;
63: }
64: }
65:
66: public void fire() {
67: synchronized (lock) {
68: if (fired)
69: return;
70: fired = true;
71: lock.notifyAll();
72: }
73: }
74:
75: public void reset() {
76: synchronized (lock) {
77: fired = false;
78: }
79: }
80:
81: public String toString() {
82: return "[Condition: name=" + name + "]";
83: }
84: }
|