001: /*
002: * <copyright>
003: *
004: * Copyright 2000-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:
027: package org.cougaar.lib.web.service;
028:
029: import java.io.IOException;
030: import java.io.UnsupportedEncodingException;
031: import java.net.InetAddress;
032: import java.net.URI;
033: import java.net.URLDecoder;
034: import java.net.URLEncoder;
035: import java.util.Collections;
036: import java.util.HashSet;
037: import java.util.Iterator;
038: import java.util.Map;
039: import java.util.Set;
040:
041: import org.cougaar.core.service.wp.AddressEntry;
042: import org.cougaar.core.service.wp.Callback;
043: import org.cougaar.core.service.wp.Response;
044: import org.cougaar.core.service.wp.WhitePagesService;
045: import org.cougaar.lib.web.arch.root.GlobalRegistry;
046: import org.cougaar.util.log.Logger;
047: import org.cougaar.util.log.Logging;
048:
049: /**
050: * An implementation of {@link GlobalRegistry} that uses the
051: * {@link WhitePagesService}.
052: */
053: public class NamingServerRegistry implements GlobalRegistry {
054:
055: private static final Logger logger = Logging
056: .getLogger(NamingServerRegistry.class);
057:
058: private final WhitePagesService wp;
059:
060: private Map namingEntries;
061:
062: public NamingServerRegistry(WhitePagesService wp) {
063: this .wp = wp;
064: }
065:
066: public void configure(Map namingEntries) {
067: this .namingEntries = namingEntries;
068: }
069:
070: public void rebind(String encName) {
071: update(true, encName);
072: }
073:
074: public void unbind(String encName) {
075: update(false, encName);
076: }
077:
078: private void update(boolean bind, String encName) {
079: if (encName == null) {
080: throw new NullPointerException();
081: }
082:
083: if (namingEntries == null || namingEntries.isEmpty()) {
084: return;
085: }
086:
087: if (wp == null) {
088: if (logger.isInfoEnabled()) {
089: logger.info("Ignoring servlet WP "
090: + (bind ? "re" : "un") + "bind for " + encName);
091: }
092: return;
093: }
094:
095: Callback callback = new Callback() {
096: public void execute(Response res) {
097: if (res.isSuccess()) {
098: if (logger.isDebugEnabled()) {
099: logger.debug("WP Response: " + res);
100: }
101: } else {
102: logger.error("WP Error: " + res);
103: }
104: }
105: };
106:
107: try {
108: String rawName = decode(encName);
109: for (Iterator iter = namingEntries.entrySet().iterator(); iter
110: .hasNext();) {
111: Map.Entry me = (Map.Entry) iter.next();
112: String key = (String) me.getKey();
113: URI value = (URI) me.getValue();
114: AddressEntry entry = AddressEntry.getAddressEntry(
115: rawName, key, value);
116:
117: if (bind) {
118: wp.rebind(entry, callback);
119: } else {
120: wp.unbind(entry, callback);
121: }
122: }
123: } catch (Exception e) {
124: throw new RuntimeException("Unable to "
125: + (bind ? "" : "un") + "bind " + encName, e);
126: }
127: }
128:
129: public Map getAll(String encName, long timeout) {
130: if (encName == null || encName.length() == 0) {
131: return null;
132: }
133:
134: if (wp == null) {
135: if (logger.isInfoEnabled()) {
136: logger.info("Ignoring servlet WP get for " + encName
137: + ")");
138: }
139: return null;
140: }
141:
142: try {
143: String rawName = decode(encName);
144: return wp.getAll(rawName, timeout);
145: } catch (Exception e) {
146: throw new RuntimeException("Unable to getAll " + encName, e);
147: }
148: }
149:
150: public Set list(String encSuffix, long timeout) {
151: String rawSuffix = decode(encSuffix);
152:
153: if (wp == null) {
154: if (logger.isInfoEnabled()) {
155: logger.info("Ignoring servlet WP list(" + rawSuffix
156: + ", " + timeout + ")");
157: }
158: return Collections.EMPTY_SET;
159: }
160:
161: Set s;
162: try {
163: s = wp.list(rawSuffix, timeout);
164: } catch (Exception e) {
165: throw new RuntimeException("Unable to list(" + rawSuffix
166: + ")", e);
167: }
168: int n = s.size();
169: Set ret;
170: if (n <= 0) {
171: ret = Collections.EMPTY_SET;
172: } else {
173: ret = new HashSet(n);
174: Iterator iter = s.iterator();
175: for (int i = 0; i < n; i++) {
176: String rawName = (String) iter.next();
177: String encName = encode(rawName);
178: ret.add(encName);
179: }
180: }
181: return ret;
182: }
183:
184: private static final String encode(String raw) {
185: try {
186: return URLEncoder.encode(raw, "UTF-8");
187: } catch (UnsupportedEncodingException uee) {
188: throw new RuntimeException("Invalid name: " + raw, uee);
189: }
190: }
191:
192: private static final String decode(String enc) {
193: try {
194: return URLDecoder.decode(enc, "UTF-8");
195: } catch (UnsupportedEncodingException uee) {
196: throw new RuntimeException("Invalid name: " + enc, uee);
197: }
198: }
199: }
|