001: /*
002: * <copyright>
003: *
004: * Copyright 2002-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.core.wp.bootstrap;
028:
029: import java.io.PrintStream;
030: import java.net.InetAddress;
031: import java.net.NetworkInterface;
032: import java.net.SocketException;
033: import java.net.URI;
034: import java.net.UnknownHostException;
035: import java.util.Enumeration;
036: import java.util.Map;
037: import org.cougaar.core.mts.MessageAddress;
038: import org.cougaar.core.service.wp.AddressEntry;
039: import org.cougaar.util.log.Logger;
040: import org.cougaar.util.log.Logging;
041:
042: /**
043: * Bootstrap utility methods.
044: */
045: public final class Util {
046:
047: private Util() {
048: }
049:
050: public static final MessageAddress parseServer(Bundle b) {
051: // either (name=X .. server=..)
052: // or (name=WP-123 .. alias=name://X)
053: String agent = null;
054: if (b != null) {
055: Map entries = b.getEntries();
056: if (entries != null) {
057: if (entries.containsKey("server")) {
058: agent = b.getName();
059: } else {
060: Object o = entries.get("alias");
061: if (o instanceof AddressEntry) {
062: String name = b.getName();
063: if (name != null && name.matches("WP(-\\d+)?")) {
064: AddressEntry ae = (AddressEntry) o;
065: agent = ae.getURI().getPath().substring(1);
066: }
067: }
068: }
069: }
070: }
071: if (agent == null) {
072: return null;
073: }
074: return MessageAddress.getMessageAddress(agent);
075: }
076:
077: /** Generic utility method to check if a host is localhost */
078: public static final boolean isLocalHost(String addr) {
079: Logger logger = Logging.getLogger(Util.class.getName());
080:
081: // quick test for localhost...
082: if (addr.equals("localhost") || addr.equals("127.0.0.1")
083: || addr.equals("localHost") // bogus
084: ) {
085: if (logger.isDetailEnabled()) {
086: logger.detail("isLocalHost(" + addr + ") is true");
087: }
088: return true;
089: }
090: try {
091: InetAddress de = InetAddress.getByName(addr);
092: // quick test for "getLocalHost"
093: InetAddress lh = InetAddress.getLocalHost();
094: if (logger.isDetailEnabled()) {
095: logger.detail("isLocalHost(" + addr + "), getByName("
096: + addr + ")=" + de + ", getLocalHost()=" + lh
097: + ", equal=" + (lh.equals(de)));
098: }
099: if (lh.equals(de)) {
100: return true;
101: }
102: // check all network interfaces
103: for (Enumeration e1 = NetworkInterface
104: .getNetworkInterfaces(); e1.hasMoreElements();) {
105: NetworkInterface iface = (NetworkInterface) e1
106: .nextElement();
107: for (Enumeration e2 = iface.getInetAddresses(); e2
108: .hasMoreElements();) {
109: InetAddress me = (InetAddress) e2.nextElement();
110: if (logger.isDetailEnabled()) {
111: logger.detail("isLocalHost(" + addr
112: + "), getByName(" + addr + ")=" + de
113: + ", network_interface[?]=" + me
114: + ", equal=" + (me.equals(de)));
115: }
116: if (me.equals(de)) {
117: return true;
118: }
119: }
120: }
121: } catch (UnknownHostException e) {
122: if (logger.isInfoEnabled()) {
123: logger.info("Unknown host " + addr);
124: }
125: } catch (SocketException e) {
126: if (logger.isWarnEnabled()) {
127: logger.warn("isLocalHost(" + addr + ") failed", e);
128: }
129: }
130: return false;
131: }
132: }
|