001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.core.blackboard;
028:
029: import java.io.IOException;
030: import java.io.NotActiveException;
031: import java.io.ObjectInputStream;
032:
033: import org.cougaar.bootstrap.SystemProperties;
034:
035: /**
036: * Implementation of the {@link Claimable} API.
037: * <p>
038: * Extends PublishableAdapter so that subclasses may
039: * implicitly collect changes.
040: *
041: * @property org.cougaar.core.blackboard.Claimable.debug
042: * If set to true, every Claimable instance will keep additional
043: * information for debugging claim conflicts, that is, multiple
044: * plugins attempting to operate directly on the same blackboard
045: * objects. This information adds significant additional memory
046: * load.
047: */
048: public class ClaimableImpl extends PublishableAdapter // sigh.
049: implements Claimable {
050: private static boolean isDebugging = false;
051: static {
052: isDebugging = SystemProperties.getBoolean(
053: "org.cougaar.core.blackboard.Claimable.debug",
054: isDebugging);
055: }
056:
057: private transient Object claimer = null;
058:
059: private transient Throwable claimerStack = null;
060:
061: private static final Object postRehydrationClaimer = new Object();
062:
063: public final boolean isClaimed() {
064: return (claimer != null);
065: }
066:
067: public final Object getClaim() {
068: return claimer;
069: }
070:
071: public final String getClaimClassName() {
072: // for beanInfo use
073: return ((claimer != null) ? claimer.getClass().getName()
074: : "null");
075: }
076:
077: public final void setClaim(Object pch) {
078: doClaim(pch, pch, "setClaim", " to ");
079: }
080:
081: public final synchronized boolean tryClaim(Object pch) {
082: if (claimer == null) {
083: // got the claim
084: _claim(pch);
085: return true;
086: } else if (pch == claimer) {
087: // already owned the claim - probably bogus, but...
088: return true;
089: } else {
090: return false;
091: }
092: }
093:
094: public final void resetClaim(Object pch) {
095: doClaim(pch, null, "resetClaim", " from ");
096: }
097:
098: private synchronized void doClaim(Object pch, Object newClaimer,
099: String verb, String prep) {
100: if (pch instanceof PrivilegedClaimant) {
101: // PrivilegedClaimant can do what he wants
102: } else if (claimer == postRehydrationClaimer) {
103: // Actual claimer lost thru rehydration, allow anything
104: } else if (pch == null) {
105: // Must have a valid pch
106: complain("Tried to " + verb + " of " + this + prep
107: + "null.");
108: } else if (pch == claimer) {
109: // Current claimer can do what he wants
110: } else if (claimer != null) {
111: // Already claimed by somebody else
112: complain("Tried to " + verb + " of " + this + prep + pch
113: + "\n\tbut it was " + claimer + ".");
114: }
115: _claim(newClaimer);
116: }
117:
118: // must be calling within a synchronized block
119: private void _claim(Object newClaimer) {
120: // Always carry out the request even if complaints were issued
121: claimer = newClaimer;
122: // Remember how we got here for debugging.
123: if (isDebugging) {
124: claimerStack = new Throwable();
125: }
126: }
127:
128: /**
129: * true when we've complained once and told the user how to enable loud mode.
130: * Only used when in non-loud mode.
131: */
132: private static boolean hasComplained = false;
133:
134: private void complain(String complaint) {
135: synchronized (System.err) {
136: System.err.println(complaint);
137: if (isDebugging) {
138: System.err.println("Current stack:");
139: Thread.dumpStack();
140: System.err.println("Claimer stack:");
141: if (claimerStack != null)
142: claimerStack.printStackTrace();
143: else
144: System.err.println("(Never been claimed)");
145: } else {
146: if (!hasComplained) {
147: System.err
148: .println("(Set system property"
149: + " org.cougaar.core.blackboard.Claimable.debug=true"
150: + " for details)");
151: hasComplained = true;
152: }
153: }
154: }
155: }
156:
157: private void readObject(ObjectInputStream is)
158: throws NotActiveException, ClassNotFoundException,
159: IOException {
160: is.defaultReadObject();
161: claimer = postRehydrationClaimer;
162: }
163: }
|