001: /*
002: * <copyright>
003: *
004: * Copyright 2002-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: package org.cougaar.servicediscovery.description;
027:
028: import java.util.Collection;
029: import java.util.Collections;
030: import java.util.HashMap;
031: import java.util.Map;
032: import java.util.Iterator;
033:
034: import org.cougaar.core.util.UID;
035: import org.cougaar.planning.ldm.plan.Role;
036: import org.cougaar.planning.ldm.plan.Schedule;
037: import org.cougaar.util.log.Logger;
038: import org.cougaar.util.log.Logging;
039:
040: /**
041: * Maintains the set of provider capabilities
042: */
043:
044: public class ProviderCapabilitiesImpl implements ProviderCapabilities {
045: private static Logger logger = Logging
046: .getLogger(ProviderCapabilitiesImpl.class);
047: private UID myUID = null;
048:
049: private HashMap myCapabilities = null;
050: private String myProviderName = null;
051:
052: public ProviderCapabilitiesImpl() {
053: this (null);
054: }
055:
056: public ProviderCapabilitiesImpl(String providerName) {
057: myProviderName = providerName;
058: myCapabilities = new HashMap();
059: }
060:
061: /**
062: * @return the name of the provider
063: **/
064: public String getProviderName() {
065: return myProviderName;
066: }
067:
068: /**
069: * @param providerName
070: **/
071: public void setProviderName(String providerName) {
072: // Not allowed to set name more than once
073: if (myProviderName != null) {
074: logger.error(myProviderName
075: + ":Attempt to reset provider name.");
076: return;
077: }
078: myProviderName = providerName;
079: }
080:
081: /**
082: * @return all provider capabilities
083: */
084: public Collection getCapabilities() {
085: return Collections.unmodifiableCollection(myCapabilities
086: .values());
087: }
088:
089: /**
090: * @return provider capability for a specific Role
091: */
092: public ProviderCapability getCapability(Role role) {
093: return (ProviderCapability) myCapabilities.get(role);
094: }
095:
096: /**
097: * Add a provider capability
098: * @param role
099: * @param echelon
100: * @param availableSchedule
101: */
102: public void addCapability(Role role, String echelon,
103: Schedule availableSchedule) {
104: addCapability(new ProviderCapabilityImpl(role, echelon,
105: availableSchedule));
106: }
107:
108: public void addCapability(ProviderCapability capability) {
109: if (logger.isDebugEnabled()) {
110: ProviderCapability current = (ProviderCapability) myCapabilities
111: .get(capability.getRole());
112: if (current != null) {
113: logger.debug(getProviderName()
114: + " replacing capability " + current + " with "
115: + capability);
116: } else {
117: logger.debug(getProviderName() + " added capability "
118: + capability);
119: }
120: }
121: myCapabilities.put(capability.getRole(), capability);
122: }
123:
124: public void removeCapability(ProviderCapability capability) {
125: if (logger.isDebugEnabled()) {
126: logger.debug(getProviderName() + " removed capability "
127: + capability);
128: }
129: myCapabilities.remove(capability.getRole());
130: }
131:
132: public void setUID(UID newUID) {
133: if (myUID != null) {
134: logger.error("Attempt to reset UID.");
135: return;
136: }
137:
138: myUID = newUID;
139: }
140:
141: public UID getUID() {
142: return myUID;
143: }
144:
145: public String getDebugString() {
146: StringBuffer buf = new StringBuffer();
147: buf.append("(ProviderCapabilities");
148: buf.append("\n uid=").append(myUID);
149: buf.append("\n providerName=").append(myProviderName);
150: int n = (myCapabilities == null ? -1 : myCapabilities.size());
151: buf.append("\n capabilities[").append(n).append("]");
152: if (n > 0) {
153: buf.append("= {");
154: for (Iterator iter = myCapabilities.entrySet().iterator(); iter
155: .hasNext();) {
156: Map.Entry me = (Map.Entry) iter.next();
157: buf.append("\n ");
158: buf.append(me.getKey());
159: buf.append(" -> ");
160: buf.append(me.getValue());
161: }
162: buf.append("\n }");
163: }
164: buf.append("\n)");
165: return buf.toString();
166: }
167: }
|