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.logistics.plugin.policy;
028:
029: import java.util.Set;
030: import java.util.Collections;
031: import org.cougaar.core.relay.*;
032: import org.cougaar.core.mts.MessageAddress;
033: import org.cougaar.core.util.UID;
034: import org.cougaar.planning.ldm.policy.Policy;
035: import org.cougaar.core.util.UniqueObject;
036: import java.util.HashSet;
037:
038: /**
039: * class to hold instance of Policy using Relay to propagate itself to a given role
040: */
041: public class LogisticsPolicy extends java.lang.Object implements
042: Relay.Source, Relay.Target, UniqueObject {
043:
044: private MessageAddress source;
045: private String authority;
046: private HashSet _targets = new HashSet();
047: private Policy policy;
048: private UID uid;
049: private String role;
050:
051: /**
052: * constructor
053: * @param policy Policy for LogisticsPolicy to hold
054: * @param source MessageAddress of Organization which is sending the LogisticsPolicy
055: * @param target Set of MessageAddresses which the LogisticsPolicy to be delivered
056: * @param authority name of the originator of this LogisticsPolicy
057: * @param role Role to which the Policy should be propagated
058: */
059: public LogisticsPolicy(Policy policy, MessageAddress source,
060: Set target, String authority, String role) {
061: this .policy = policy;
062: this .source = source;
063: this .authority = authority;
064: this ._targets.addAll(target);
065: this .role = role;
066: }
067:
068: /**
069: * constructor used by Factory
070: * @param other LogisticsPolicy which prompted the creation of this new one
071: * @param src MessageAddress of sender of the LogisticsPolicy
072: */
073:
074: protected LogisticsPolicy(LogisticsPolicy other, MessageAddress src) {
075: this (other.getPolicy(), src, Collections.EMPTY_SET, other
076: .getAuthority(), other.getRole());
077: this .setUID(other.getUID());
078: }
079:
080: /**
081: * method to retrieve role to which this LogisticsPolicy is to be propagated
082: */
083:
084: public String getRole() {
085: return role;
086: }
087:
088: /**
089: * method to retrieve originator
090: */
091: public String getAuthority() {
092: return authority;
093: }
094:
095: /**
096: * method to retrieve the sender
097: */
098: public MessageAddress getSource() {
099: return source;
100: }
101:
102: /**
103: * method to set sender
104: * @param ma MessageAddress of sender
105: */
106: public void setSource(MessageAddress ma) {
107: this .source = ma;
108: }
109:
110: /**
111: * returns null
112: */
113: public Object getResponse() {
114: return null;
115: }
116:
117: /**
118: * returns Relay.NO_CHANGE
119: */
120: public int updateResponse(MessageAddress t, Object response) {
121: return Relay.NO_CHANGE;
122: }
123:
124: /**
125: * returns Relay.NO_CHANGE
126: */
127: public int updateContent(Object content, Token token) {
128: return NO_CHANGE;
129: }
130:
131: /**
132: * returns this
133: */
134: public Object getContent() {
135: return this ;
136: }
137:
138: /**
139: * method to get Set of MessageAddresses of destinations for this LogisticsPolicy
140: */
141: public Set getTargets() {
142: if (_targets != null) {
143: return _targets;
144: } else {
145: return Collections.EMPTY_SET;
146: }
147: }
148:
149: // public void addTargets(Set newTargets) {
150: // if (_targets.isEmpty()) {
151: // _targets = newTargets;
152: // } else {
153: // this._targets.addAll(newTargets);
154: // }
155: // if (_targets != null){
156: // d.print("addTargets - all targets: " + this._targets.toString());
157: // } else d.print("addTargets - targets are null");
158: // }
159:
160: /**
161: * method to set targets
162: * @param newTargets Set of MessageAddresses to direct this LogisticsPolicy
163: */
164: public void setTargets(Set newTargets) {
165: this ._targets = new HashSet(newTargets);
166: }
167:
168: public void addTarget(MessageAddress target) {
169: if (_targets == null) {
170: _targets = new HashSet();
171: }
172: if (_targets instanceof HashSet) {
173: }
174: _targets.add(target);
175: }
176:
177: /**
178: * method to get UID
179: */
180: public UID getUID() {
181: return uid;
182: }
183:
184: /**
185: * method to set UID
186: * @param uid new UID for the LogisticsPolicy
187: */
188: public void setUID(UID uid) {
189: this .uid = uid;
190: }
191:
192: /**
193: * method to get Policy from within the LogisticsPolicy
194: */
195: public Policy getPolicy() {
196: return policy;
197: }
198:
199: /**
200: * Factory to create new LogisticsPolicies
201: */
202: private static final class SimpleRelayFactory implements
203: TargetFactory, java.io.Serializable {
204:
205: public static final SimpleRelayFactory INSTANCE = new SimpleRelayFactory();
206:
207: private SimpleRelayFactory() {
208: }
209:
210: /**
211: * method to create new instance of LogisticsPolicy
212: * @param uid UID
213: * @param source MessageAddress of sender
214: * @param content
215: * @param token
216: */
217: public Relay.Target create(UID uid, MessageAddress source,
218: Object content, Token token) {
219: LogisticsPolicy p = null;
220: try {
221: p = (LogisticsPolicy) content;
222: } catch (ClassCastException cce) {
223: ;
224: }
225: return new LogisticsPolicy(p, source);
226: }
227:
228: private Object readResolve() {
229: return INSTANCE;
230: }
231: };
232:
233: /**
234: * method to retrieve instance of SimpleRelayFactory
235: */
236: public TargetFactory getTargetFactory() {
237: return SimpleRelayFactory.INSTANCE;
238:
239: }
240: }
|