0N/A/*
2362N/A * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.tools.attach;
0N/A
0N/Aimport com.sun.tools.attach.spi.AttachProvider;
0N/A
0N/A/**
0N/A * Describes a Java virtual machine.
0N/A *
0N/A * <p> A <code>VirtualMachineDescriptor</code> is a container class used to
0N/A * describe a Java virtual machine. It encapsulates an identifier that identifies
0N/A * a target virtual machine, and a reference to the {@link
0N/A * com.sun.tools.attach.spi.AttachProvider AttachProvider} that should be used
0N/A * when attempting to attach to the virtual machine. The identifier is
0N/A * implementation-dependent but is typically the process identifier (or pid)
0N/A * environments where each Java virtual machine runs in its own operating system
0N/A * process. </p>
0N/A *
0N/A * <p> A <code>VirtualMachineDescriptor</code> also has a {@link #displayName() displayName}.
0N/A * The display name is typically a human readable string that a tool might
0N/A * display to a user. For example, a tool that shows a list of Java
0N/A * virtual machines running on a system might use the display name rather
0N/A * than the identifier. A <code>VirtualMachineDescriptor</code> may be
0N/A * created without a <i>display name</i>. In that case the identifier is
0N/A * used as the <i>display name</i>.
0N/A *
0N/A * <p> <code>VirtualMachineDescriptor</code> instances are typically created by
0N/A * invoking the {@link com.sun.tools.attach.VirtualMachine#list VirtualMachine.list()}
0N/A * method. This returns the complete list of descriptors to describe the
0N/A * Java virtual machines known to all installed {@link
0N/A * com.sun.tools.attach.spi.AttachProvider attach providers}.
0N/A *
0N/A * @since 1.6
0N/A */
0N/Apublic class VirtualMachineDescriptor {
0N/A
0N/A private AttachProvider provider;
0N/A private String id;
0N/A private String displayName;
0N/A
0N/A private volatile int hash; // 0 => not computed
0N/A
0N/A /**
0N/A * Creates a virtual machine descriptor from the given components.
0N/A *
0N/A * @param provider The AttachProvider to attach to the Java virtual machine.
0N/A * @param id The virtual machine identifier.
0N/A * @param displayName The display name.
0N/A *
0N/A * @throws NullPointerException
0N/A * If any of the arguments are <code>null</code>
0N/A */
0N/A public VirtualMachineDescriptor(AttachProvider provider, String id, String displayName) {
0N/A if (provider == null) {
0N/A throw new NullPointerException("provider cannot be null");
0N/A }
0N/A if (id == null) {
0N/A throw new NullPointerException("identifier cannot be null");
0N/A }
0N/A if (displayName == null) {
0N/A throw new NullPointerException("display name cannot be null");
0N/A }
0N/A this.provider = provider;
0N/A this.id = id;
0N/A this.displayName = displayName;
0N/A }
0N/A
0N/A /**
0N/A * Creates a virtual machine descriptor from the given components.
0N/A *
0N/A * <p> This convenience constructor works as if by invoking the
0N/A * three-argument constructor as follows:
0N/A *
0N/A * <blockquote><tt>
0N/A * new&nbsp;{@link #VirtualMachineDescriptor(AttachProvider, String, String)
0N/A * VirtualMachineDescriptor}(provider, &nbsp;id, &nbsp;id);
0N/A * </tt></blockquote>
0N/A *
0N/A * <p> That is, it creates a virtual machine descriptor such that
0N/A * the <i>display name</i> is the same as the virtual machine
0N/A * identifier.
0N/A *
0N/A * @param provider The AttachProvider to attach to the Java virtual machine.
0N/A * @param id The virtual machine identifier.
0N/A *
0N/A * @throws NullPointerException
0N/A * If <tt>provider</tt> or <tt>id</tt> is <tt>null</tt>.
0N/A */
0N/A public VirtualMachineDescriptor(AttachProvider provider, String id) {
0N/A this(provider, id, id);
0N/A }
0N/A
0N/A /**
0N/A * Return the <code>AttachProvider</code> that this descriptor references.
0N/A *
0N/A * @return The <code>AttachProvider</code> that this descriptor references.
0N/A */
0N/A public AttachProvider provider() {
0N/A return provider;
0N/A }
0N/A
0N/A /**
0N/A * Return the identifier component of this descriptor.
0N/A *
0N/A * @return The identifier component of this descriptor.
0N/A */
0N/A public String id() {
0N/A return id;
0N/A }
0N/A
0N/A /**
0N/A * Return the <i>display name</i> component of this descriptor.
0N/A *
0N/A * @return The display name component of this descriptor.
0N/A */
0N/A public String displayName() {
0N/A return displayName;
0N/A }
0N/A
0N/A /**
0N/A * Returns a hash-code value for this VirtualMachineDescriptor. The hash
0N/A * code is based upon the descriptor's components, and satifies
0N/A * the general contract of the {@link java.lang.Object#hashCode()
0N/A * Object.hashCode} method.
0N/A *
0N/A * @return A hash-code value for this descriptor.
0N/A */
0N/A public int hashCode() {
0N/A if (hash != 0) {
0N/A return hash;
0N/A }
0N/A hash = provider.hashCode() * 127 + id.hashCode();
0N/A return hash;
0N/A }
0N/A
0N/A /**
0N/A * Tests this VirtualMachineDescriptor for equality with another object.
0N/A *
0N/A * <p> If the given object is not a VirtualMachineDescriptor then this
0N/A * method returns <tt>false</tt>. For two VirtualMachineDescriptors to
0N/A * be considered equal requires that they both reference the same
0N/A * provider, and their {@link #id() identifiers} are equal. </p>
0N/A *
0N/A * <p> This method satisfies the general contract of the {@link
0N/A * java.lang.Object#equals(Object) Object.equals} method. </p>
0N/A *
0N/A * @param ob The object to which this object is to be compared
0N/A *
0N/A * @return <tt>true</tt> if, and only if, the given object is
0N/A * a VirtualMachineDescriptor that is equal to this
0N/A * VirtualMachineDescriptor.
0N/A */
0N/A public boolean equals(Object ob) {
0N/A if (ob == this)
0N/A return true;
0N/A if (!(ob instanceof VirtualMachineDescriptor))
0N/A return false;
0N/A VirtualMachineDescriptor other = (VirtualMachineDescriptor)ob;
0N/A if (other.provider() != this.provider()) {
0N/A return false;
0N/A }
0N/A if (!other.id().equals(this.id())) {
0N/A return false;
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Returns the string representation of the <code>VirtualMachineDescriptor</code>.
0N/A */
0N/A public String toString() {
0N/A String s = provider.toString() + ": " + id;
0N/A if (displayName != id) {
0N/A s += " " + displayName;
0N/A }
0N/A return s;
0N/A }
0N/A
0N/A
0N/A}