01: /*
02: * <copyright>
03: *
04: * Copyright 1997-2004 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26:
27: package org.cougaar.planning.ldm;
28:
29: import java.util.HashMap;
30: import java.util.Iterator;
31: import java.util.Map;
32: import org.cougaar.planning.ldm.asset.Asset;
33: import org.cougaar.core.mts.MessageAddress;
34:
35: /**
36: * <b>PRIVATE</b> registry of (agent -to- LDMServesPlugin) mapping,
37: * for Asset prototype serialization.
38: * <p>
39: * Asset deserialization can use the ClusterContext to figure out
40: * the address of the agent being [de]serialized. Beyond that, an
41: * Asset needs to deserialize its prototype and bind to the agent's
42: * LDM. This class allows the Asset to find the appropriate
43: * LDMServesPlugin.
44: * <p>
45: * See <b>bug 1576</b> and <b>bug 1659</b> for future refactoring
46: * plans. This implementation is a temporary hack!
47: * <p>
48: * The only valid clients:
49: *
50: * @see org.cougaar.planning.ldm.asset.Asset
51: * @see org.cougaar.planning.ldm.PlanningDomain
52: */
53: public final class LDMContextTable {
54:
55: private static final Map table = new HashMap();
56:
57: /** @see org.cougaar.planning.ldm.PlanningDomain */
58: static void setLDM(MessageAddress agentAddr, LDMServesPlugin ldm) {
59: synchronized (table) {
60: Object o = table.get(agentAddr);
61: if (o instanceof LDMServesPlugin.Delegator) {
62: LDMServesPlugin.Delegator delegator = (LDMServesPlugin.Delegator) o;
63: synchronized (delegator) { /*prevent anyone else from mutating it while we're cutting over*/
64: delegator.setLDM(ldm);
65: HashMap oc = delegator
66: .flushTemporaryPrototypeCache();
67: if (oc != null) {
68: for (Iterator i = oc.entrySet().iterator(); i
69: .hasNext();) {
70: Map.Entry entry = (Map.Entry) i.next();
71: ldm.cachePrototype((String) entry.getKey(),
72: (Asset) entry.getValue());
73: }
74: }
75: }
76: }
77: table.put(agentAddr, ldm);
78: }
79: }
80:
81: /** @see org.cougaar.planning.ldm.asset.Asset */
82: public static LDMServesPlugin getLDM(MessageAddress agentAddr) {
83: synchronized (table) {
84: LDMServesPlugin result = (LDMServesPlugin) table
85: .get(agentAddr);
86: if (result == null) {
87: result = new LDMServesPlugin.Delegator();
88: table.put(agentAddr, result);
89: }
90: return result;
91: }
92: }
93:
94: }
|