001: /*
002:
003: * <copyright>
004: *
005: * Copyright 2002-2007 BBNT Solutions, LLC
006: * under sponsorship of the Defense Advanced Research Projects
007: * Agency (DARPA).
008: *
009: * You can redistribute this software and/or modify it under the
010: * terms of the Cougaar Open Source License as published on the
011: * Cougaar Open Source Website (www.cougaar.org).
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
014: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
015: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
016: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
017: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
018: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
019: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
020: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
021: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
022: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
023: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
024: *
025: * </copyright>
026:
027: */
028:
029: package org.cougaar.qos.qrs;
030:
031: import java.io.BufferedReader;
032: import java.io.InputStream;
033: import java.io.InputStreamReader;
034: import java.net.URL;
035:
036: import org.cougaar.util.log.Logger;
037:
038: /**
039: * This is really just an interface to the db stored in
040: * RSSNetUtilities.SiteAddress.
041: */
042: public class SitesDB implements Constants {
043: private static final String MAGIC = "Site_Flow_";
044: private static final int MAGIC_LENGTH = MAGIC.length();
045:
046: private final Logger logger;
047:
048: public SitesDB() {
049: logger = Logging.getLogger(SitesDB.class);
050: }
051:
052: public SiteAddress lookup(String address) {
053: for (SiteAddress site : SiteAddress.elements()) {
054: if (site.contains(address)) {
055: return site;
056: }
057: }
058: return null;
059: }
060:
061: private void addPropertyKey(String key) {
062: if (key.startsWith(MAGIC)) {
063: int start = MAGIC_LENGTH;
064: int end = key.indexOf(KEY_SEPR, start);
065: if (end != -1) {
066: String mask = key.substring(start, end);
067: SiteAddress.getSiteAddress(mask);
068: start = end + 1;
069: end = key.indexOf(KEY_SEPR, start);
070: if (end != -1) {
071: mask = key.substring(start, end);
072: SiteAddress.getSiteAddress(mask);
073: }
074: }
075: }
076: }
077:
078: public void populate(InputStream stream) {
079: try {
080: InputStreamReader isr = new InputStreamReader(stream);
081: BufferedReader rdr = new BufferedReader(isr);
082: String key = rdr.readLine();
083: while (key != null) {
084: addPropertyKey(key);
085: key = rdr.readLine();
086: }
087: rdr.close();
088: } catch (java.io.IOException ex) {
089: logger.error(null, ex);
090: return;
091: }
092: }
093:
094: public void populate(URL url) {
095: try {
096: InputStream stream = url.openStream();
097: populate(stream);
098: } catch (java.io.IOException ex) {
099: logger.error(null, ex);
100: return;
101: }
102: }
103:
104: }
|