001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 Mobile Intelligence Corp
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.community.requests;
028:
029: import org.cougaar.core.service.community.CommunityResponse;
030: import org.cougaar.core.util.UID;
031: import org.cougaar.core.util.UniqueObject;
032:
033: /**
034: * Base class for community requests (i.e., Join, Leave, GetDescriptor, etc.).
035: */
036: public class CommunityRequest implements java.io.Serializable,
037: UniqueObject {
038:
039: public static final long NEVER = -1;
040: public static final long DEFAULT_TIMEOUT = NEVER;
041:
042: private String communityName;
043: private String requestType;
044: private CommunityResponse resp;
045: private long timeout = DEFAULT_TIMEOUT;
046: private UID uid;
047:
048: /**
049: * Default constructor.
050: */
051: public CommunityRequest() {
052: }
053:
054: public CommunityRequest(String communityName, UID uid, long timeout) {
055: this .communityName = communityName;
056: this .timeout = timeout;
057: this .uid = uid;
058: String classname = this .getClass().getName();
059: int lastSeparator = classname.lastIndexOf(".");
060: requestType = (lastSeparator == -1) ? classname : classname
061: .substring(lastSeparator + 1, classname.length());
062: }
063:
064: public CommunityRequest(String communityName, UID uid) {
065: this (communityName, uid, DEFAULT_TIMEOUT);
066: }
067:
068: public String getCommunityName() {
069: return communityName;
070: }
071:
072: public String getRequestType() {
073: return requestType;
074: }
075:
076: public long getTimeout() {
077: return timeout;
078: }
079:
080: public void setResponse(CommunityResponse resp) {
081: this .resp = resp;
082: }
083:
084: public CommunityResponse getResponse() {
085: return resp;
086: }
087:
088: public String toString() {
089: return "request=" + getRequestType() + " community="
090: + getCommunityName() + " timeout=" + getTimeout()
091: + " uid=" + getUID();
092: }
093:
094: /**
095: * Returns true if CommunityRequests have same request type
096: * and target community.
097: * @param o CommunityRequest to compare
098: * @return true if CommunityRequests have same request type
099: */
100: public boolean equals(Object o) {
101: if (!(o instanceof CommunityRequest))
102: return false;
103: CommunityRequest cr = (CommunityRequest) o;
104: if (!requestType.equals(cr.getRequestType()))
105: return false;
106: if (communityName == null) {
107: return cr.getCommunityName() == null;
108: } else {
109: return communityName.equals(cr.getCommunityName());
110: }
111: }
112:
113: //
114: // UniqueObject Interface methods
115: //
116: public void setUID(UID uid) {
117: if (uid != null) {
118: RuntimeException rt = new RuntimeException(
119: "Attempt to call setUID() more than once.");
120: throw rt;
121: }
122: this .uid = uid;
123: }
124:
125: public UID getUID() {
126: return this.uid;
127: }
128: }
|