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.init;
028:
029: import java.util.Collection;
030: import java.util.HashMap;
031: import java.util.Iterator;
032: import java.util.Map;
033:
034: import javax.naming.NamingEnumeration;
035: import javax.naming.NamingException;
036: import javax.naming.directory.Attribute;
037: import javax.naming.directory.Attributes;
038: import javax.naming.directory.BasicAttribute;
039: import javax.naming.directory.BasicAttributes;
040:
041: /**
042: * Defines an initial configuration for a community.
043: */
044: public class CommunityConfig {
045:
046: private String name;
047: private Attributes attributes = new BasicAttributes();
048: private Map entities = new HashMap();
049:
050: public CommunityConfig(String name) {
051: this .name = name;
052: }
053:
054: public String getName() {
055: return name;
056: }
057:
058: public void addAttribute(String id, String value) {
059: Attribute attr = attributes.get(id);
060: if (attr == null) {
061: attr = new BasicAttribute(id, value);
062: attributes.put(attr);
063: } else {
064: if (!attr.contains(value))
065: attr.add(value);
066: }
067: }
068:
069: public void setAttributes(Attributes attrs) {
070: this .attributes = attrs;
071: }
072:
073: public Attributes getAttributes() {
074: return attributes;
075: }
076:
077: public void addEntity(EntityConfig entity) {
078: entities.put(entity.getName(), entity);
079: }
080:
081: public EntityConfig getEntity(String name) {
082: return (EntityConfig) entities.get(name);
083: }
084:
085: public Collection getEntities() {
086: return entities.values();
087: }
088:
089: public boolean hasEntity(String entityName) {
090: return entities.containsKey(entityName);
091: }
092:
093: /**
094: * Creates a printable representation of Community data.
095: */
096: public String toString() {
097: StringBuffer sb = new StringBuffer();
098: try {
099: sb.append("<Community Name=\"" + getName() + "\" >\n");
100: Attributes attributes = getAttributes();
101: if (attributes != null && attributes.size() > 0) {
102: NamingEnumeration en = attributes.getAll();
103: while (en.hasMore()) {
104: Attribute attr = (Attribute) en.next();
105: String id = attr.getID();
106: NamingEnumeration valuesEnum = attr.getAll();
107: while (valuesEnum.hasMore()) {
108: String value = (String) valuesEnum.next();
109: sb.append(" <Attribute ID=\"" + id
110: + " Value=\"" + value + "\" />\n");
111: }
112: }
113: }
114: Collection entities = getEntities();
115: for (Iterator it2 = entities.iterator(); it2.hasNext();) {
116: EntityConfig entity = (EntityConfig) it2.next();
117: sb
118: .append(" <Entity Name=\"" + entity.getName()
119: + "\"");
120: attributes = entity.getAttributes();
121: if (attributes.size() > 0) {
122: sb.append(" />\n");
123: } else {
124: sb.append(" >\n");
125: }
126: NamingEnumeration en = attributes.getAll();
127: while (en.hasMore()) {
128: Attribute attr = (Attribute) en.next();
129: String id = attr.getID();
130: NamingEnumeration valuesEnum = attr.getAll();
131: while (valuesEnum.hasMore()) {
132: String value = (String) valuesEnum.next();
133: sb.append(" <Attribute ID=\"" + id
134: + " Value=\"" + value + "\" />\n");
135: }
136: }
137: sb.append(" </Entity>\n");
138: }
139: sb.append("</Community>");
140: } catch (NamingException ne) {
141: }
142: return sb.toString();
143: }
144:
145: }
|