usr/src/java/vpanels/panel/org/opensolaris/os/vp/panel/common/ConnectionInfo.java
author Dan Labrecque <Dan.Labrecque@oracle.com>
Mon, 22 Nov 2010 10:50:58 -0500
changeset 603 71a20acea802
parent 602 c60ad6a81113
child 651 eeb57de7d602
permissions -rw-r--r--
17449 AppLoginHistory's factory method ignores its argument at times 17450 AppLoginHistory instance should not belong to the AppInstance class 17451 AppLoginHistory is too narrowly packaged

/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
 */

package org.opensolaris.os.vp.panel.common;

import java.net.*;
import javax.management.remote.JMXConnector;
import org.opensolaris.os.vp.util.misc.*;

/**
 * The {@code ConnectionInfo} class encapsulate an open {@code JMXConnector} and
 * some of its attributes.
 */
public class ConnectionInfo implements LoginInfo {
    //
    // Instance data
    //

    private String host;
    private String user;
    private String role;
    private JMXConnector connector;
    private InetAddress[] addrs;
    private Boolean isLocalAddress;

    //
    // Constructors
    //

    public ConnectionInfo(String host, String user, String role,
	JMXConnector connector) {

	this.host = host;
	this.user = user;
	this.role = role;
	this.connector = connector;
    }

    //
    // LoginInfo methods
    //

    @Override
    public String getHost() {
	return host;
    }

    @Override
    public String getRole() {
	return role;
    }

    @Override
    public String getUser() {
	return user;
    }

    @Override
    public boolean matches(String host, String user, String role) {
	return matchesUser(user) && matchesRole(role) && matchesHost(host);
    }

    @Override
    public boolean matches(LoginInfo info) {
	return matches(info.getHost(), info.getUser(), info.getRole());
    }

    //
    // Object methods
    //

    @Override
    public void finalize() throws Throwable {
	IOUtil.closeIgnore(connector);
	super.finalize();
    }

    @Override
    public String toString() {
	String fmt = role == null ?
	    "%2$s@%1$s" : "%3$s@%1$s (via %2$s)";
	return String.format(fmt, host, user, role);
    }

    //
    // ConnectionInfo methods
    //

    public JMXConnector getConnector() {
	return connector;
    }

    public synchronized InetAddress[] getInetAddresses() {
	if (addrs == null) {
	    try {
		addrs = InetAddress.getAllByName(getHost());
	    } catch (UnknownHostException e) {
		addrs = new InetAddress[0];
	    }
	}
	return addrs;
    }

    public boolean matchesHost(String host) {
	// Avoid IP resolution if possible
	if (ObjectUtil.equals(host, getHost())) {
	    return true;
	}

	// Test for localhost.
	if (isLocalAddress == null) {
	    isLocalAddress = NetUtil.isLocalAddress(getHost());
	}
	if (isLocalAddress && NetUtil.isLocalAddress(host)) {
	    return true;
	}

	try {
	    InetAddress[] addrs = getInetAddresses();
	    InetAddress[] addrs2 = InetAddress.getAllByName(host);

	    for (InetAddress addr : addrs) {
		for (InetAddress addr2 : addrs2) {
		    if (addr.equals(addr2)) {
			return true;
		    }
		}
	    }
	} catch (UnknownHostException stop) {
	}
	return false;
    }

    public boolean matchesRole(String role) {
	// May be null
	return ObjectUtil.equals(role, getRole());
    }

    public boolean matchesUser(String user) {
	return user.equals(getUser());
    }
}