001: /*
002: * <copyright>
003: *
004: * Copyright 2003-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.community;
027:
028: import java.io.Serializable;
029:
030: import javax.naming.NamingEnumeration;
031: import javax.naming.NamingException;
032: import javax.naming.directory.Attribute;
033: import javax.naming.directory.Attributes;
034: import javax.naming.directory.BasicAttributes;
035:
036: import org.cougaar.core.service.community.Entity;
037:
038: /**
039: * Defines entities that are associated with a community.
040: */
041: public class EntityImpl implements Entity, Serializable, Cloneable {
042:
043: // Instance variables
044: protected String name;
045: protected Attributes attrs = new BasicAttributes();
046:
047: /**
048: * Constructor.
049: * @param name Name of new Entity
050: */
051: public EntityImpl(String name) {
052: this .name = name;
053: }
054:
055: /**
056: * Constructor.
057: * @param name Name of new Entity
058: * @param attrs Initial attributes
059: */
060: public EntityImpl(String name, Attributes attrs) {
061: this .name = name;
062: this .attrs = attrs;
063: }
064:
065: /**
066: * Set entity name.
067: * @param name Entity name
068: */
069: public void setName(String name) {
070: this .name = name;
071: }
072:
073: /**
074: * Get entity name.
075: * @return Entity name
076: */
077: public String getName() {
078: return this .name;
079: }
080:
081: /**
082: * Set entity attributes.
083: * @param attrs Entity attributes
084: */
085: public void setAttributes(Attributes attrs) {
086: this .attrs = attrs;
087: }
088:
089: /**
090: * Get entity attributes.
091: * @return Entity attributes
092: */
093: public Attributes getAttributes() {
094: return this .attrs;
095: }
096:
097: public boolean equals(Object o) {
098: return (o instanceof Entity
099: && name.equals(((Entity) o).getName()) && attrs
100: .equals(((Entity) o).getAttributes()));
101: }
102:
103: public int hashCode() {
104: return (name != null ? name.hashCode() : "".hashCode());
105: }
106:
107: /**
108: * Returns name of entity.
109: * @return entity name
110: */
111: public String toString() {
112: return getName();
113: }
114:
115: public Object clone() {
116: EntityImpl o = null;
117: try {
118: o = (EntityImpl) super .clone();
119: } catch (CloneNotSupportedException e) {
120: e.printStackTrace();
121: }
122: o.name = new String(name);
123: if (attrs == null) {
124: o.attrs = null;
125: } else {
126: o.attrs = CommunityUtils.cloneAttributes(attrs);
127: }
128: return o;
129: }
130:
131: /**
132: * Returns an XML representation of entity.
133: * @return XML representation of entity
134: */
135: public String toXml() {
136: return toXml("");
137: }
138:
139: /**
140: * Returns an XML representation of Entity.
141: * @param indent Blank string used to pad beginning of entry to control
142: * indentation formatting
143: * @return XML representation of entity
144: */
145: public String toXml(String indent) {
146: StringBuffer sb = new StringBuffer(indent + "<Entity name=\""
147: + name + "\" >\n");
148: if (attrs != null && attrs.size() > 0)
149: sb.append(attrsToString(attrs, indent + " "));
150: sb.append(indent + "</Entity>\n");
151: return sb.toString();
152: }
153:
154: /**
155: * Creates a string representation of an Attribute set.
156: * @param attrs Attributes
157: * @return String representation of attributes
158: */
159: public static String attrsToString(Attributes attrs) {
160: return attrsToString(attrs, "");
161: }
162:
163: /**
164: * Creates a string representation of an Attribute set.
165: * @param attrs Attributes
166: * @param indent Indentation for pretty printing
167: * @return String representation of attributes
168: */
169: public static String attrsToString(Attributes attrs, String indent) {
170: StringBuffer sb = new StringBuffer(indent + "<Attributes>\n");
171: try {
172: for (NamingEnumeration en = attrs.getAll(); en.hasMore();) {
173: Attribute attr = (Attribute) en.next();
174: sb.append(indent + " <Attribute id=\"" + attr.getID()
175: + "\" >\n");
176: for (NamingEnumeration enum1 = attr.getAll(); enum1
177: .hasMore();) {
178: sb.append(indent + " <Value>" + enum1.next()
179: + "</Value>\n");
180: }
181: sb.append(indent + " </Attribute>\n");
182: }
183: } catch (NamingException ne) {
184: }
185: sb.append(indent + "</Attributes>\n");
186: return sb.toString();
187: }
188:
189: /**
190: * Creates a string representation of an Attribute set.
191: * @return String representation of attributes
192: */
193: public String attrsToString() {
194: StringBuffer sb = new StringBuffer();
195: try {
196: for (NamingEnumeration en = attrs.getAll(); en.hasMore();) {
197: Attribute attr = (Attribute) en.next();
198: sb.append(attr.getID() + "=[");
199: for (NamingEnumeration enum1 = attr.getAll(); enum1
200: .hasMore();) {
201: sb.append((String) enum1.next());
202: if (enum1.hasMore())
203: sb.append(",");
204: else
205: sb.append("]");
206: }
207: if (en.hasMore())
208: sb.append(" ");
209: }
210: } catch (NamingException ne) {
211: }
212: return sb.toString();
213: }
214:
215: }
|