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.util.ArrayList;
029: import java.util.Collection;
030: import java.util.Collections;
031: import java.util.Iterator;
032: import java.util.List;
033: import java.util.Map;
034: import java.util.HashMap;
035:
036: import javax.naming.NamingEnumeration;
037: import javax.naming.NamingException;
038: import javax.naming.directory.Attribute;
039: import javax.naming.directory.BasicAttribute;
040: import javax.naming.directory.Attributes;
041: import javax.naming.directory.ModificationItem;
042: import javax.naming.directory.DirContext;
043:
044: import org.cougaar.core.service.community.Entity;
045:
046: /**
047: * Static Community helper methods.
048: */
049: public class CommunityUtils {
050:
051: /**
052: * Clone attributes.
053: * @param attrs Attributes Original copy
054: * @return Attributes Clone of attributes
055: */
056: public static Attributes cloneAttributes(Attributes attrs) {
057: Attributes clone = (Attributes) attrs.clone();
058: NamingEnumeration ne = attrs.getAll();
059: try {
060: while (ne.hasMore()) {
061: Attribute attr = (Attribute) ne.next();
062: String id = new String(attr.getID());
063: Attribute attrClone = new BasicAttribute(id);
064: NamingEnumeration ne1 = attr.getAll();
065: while (ne1.hasMore()) {
066: String val = new String(ne1.next().toString());
067: attrClone.add(val);
068: }
069: clone.put(attrClone);
070: }
071: } catch (NamingException ex) {
072: ex.printStackTrace();
073: }
074: return clone;
075: }
076:
077: /**
078: * Clone Entities.
079: * @param entities Map Original copy
080: * @return Map Clone of Entities
081: */
082: public static Map cloneEntities(Collection entities) {
083: Map clone = Collections.synchronizedMap(new HashMap());
084: for (Iterator it = entities.iterator(); it.hasNext();) {
085: Entity entityClone = (Entity) ((EntityImpl) it.next())
086: .clone();
087: clone.put(entityClone.getName(), entityClone);
088: }
089: return clone;
090: }
091:
092: /**
093: * Return a collection of entity names.
094: * @param entities Collection Entities
095: * @return Collection Collection of names
096: */
097: public static Collection getEntityNames(Collection entities) {
098: Collection names = new ArrayList();
099: for (Iterator it = entities.iterator(); it.hasNext();) {
100: Entity entity = (Entity) it.next();
101: names.add(entity.getName());
102: }
103: return names;
104: }
105:
106: /**
107: * Create an attribute ModificationArray based on the differences between
108: * 2 attribute sets.
109: * @param oldAttrs Attributes
110: * @param newAttrs Attributes
111: * @return ModificationItem[]
112: */
113: public static ModificationItem[] getAttributeModificationItems(
114: Attributes oldAttrs, Attributes newAttrs) {
115: List modsList = new ArrayList();
116: for (NamingEnumeration enums = oldAttrs.getAll(); enums
117: .hasMoreElements();) {
118: Attribute oldAttr = (Attribute) enums.nextElement();
119: Attribute newAttr = (Attribute) newAttrs.get(oldAttr
120: .getID());
121: if (newAttr == null || !newAttr.equals(oldAttr)) {
122: modsList.add(new ModificationItem(
123: DirContext.REMOVE_ATTRIBUTE, oldAttr));
124: }
125: }
126: for (NamingEnumeration enums = newAttrs.getAll(); enums
127: .hasMoreElements();) {
128: Attribute newAttr = (Attribute) enums.nextElement();
129: Attribute oldAttr = (Attribute) oldAttrs.get(newAttr
130: .getID());
131: if (oldAttr != null || !newAttr.equals(oldAttr)) {
132: modsList.add(new ModificationItem(
133: DirContext.ADD_ATTRIBUTE, newAttr));
134: }
135: }
136: return (ModificationItem[]) modsList
137: .toArray(new ModificationItem[0]);
138: }
139:
140: /**
141: * Add new attribute id/value to attribute set,
142: * @param attrs Attributes
143: * @param id String
144: * @param value String
145: */
146: public static void setAttribute(Attributes attrs, String id,
147: String value) {
148: if (attrs != null) {
149: Attribute attr = attrs.get(id);
150: if (attr == null) {
151: attrs.put(new BasicAttribute(id, value));
152: } else {
153: if (!attr.contains(value)) {
154: attr.add(value);
155: }
156: }
157: }
158: }
159:
160: /**
161: * Check for existence of attribute id and value in attribute set.
162: * @param attrs Attributes
163: * @param id String
164: * @param value String
165: * @return boolean
166: */
167: public static boolean hasAttribute(Attributes attrs, String id,
168: String value) {
169: boolean result = false;
170: if (attrs != null) {
171: Attribute attr = attrs.get(id);
172: result = attr != null && attr.contains(value);
173: }
174: return result;
175: }
176:
177: /**
178: * Creates a string representation of an Attribute set.
179: */
180: public static String attrsToString(Attributes attrs) {
181: StringBuffer sb = new StringBuffer("[");
182: try {
183: for (NamingEnumeration en = attrs.getAll(); en.hasMore();) {
184: Attribute attr = (Attribute) en.next();
185: sb.append(attr.getID() + "=(");
186: for (NamingEnumeration enum1 = attr.getAll(); enum1
187: .hasMore();) {
188: sb.append((String) enum1.next());
189: if (enum1.hasMore())
190: sb.append(",");
191: else
192: sb.append(")");
193: }
194: if (en.hasMore())
195: sb.append(",");
196: }
197: sb.append("]");
198: } catch (NamingException ne) {
199: }
200: return sb.toString();
201: }
202:
203: /**
204: * Converts a collection of entities to a compact string representation of names
205: */
206: public static String entityNames(Collection members) {
207: StringBuffer sb = new StringBuffer("[");
208: for (Iterator it = members.iterator(); it.hasNext();) {
209: sb.append(it.next().toString() + (it.hasNext() ? "," : ""));
210: }
211: return (sb.append("]").toString());
212: }
213:
214: }
|