Path: blob/master/src/jdk.attach/share/classes/com/sun/tools/attach/VirtualMachineDescriptor.java
43942 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.attach;2627import com.sun.tools.attach.spi.AttachProvider;2829/**30* Describes a Java virtual machine.31*32* <p> A {@code VirtualMachineDescriptor} is a container class used to33* describe a Java virtual machine. It encapsulates an identifier that identifies34* a target virtual machine, and a reference to the {@link35* com.sun.tools.attach.spi.AttachProvider AttachProvider} that should be used36* when attempting to attach to the virtual machine. The identifier is37* implementation-dependent but is typically the process identifier (or pid)38* environments where each Java virtual machine runs in its own operating system39* process. </p>40*41* <p> A {@code VirtualMachineDescriptor} also has a {@link #displayName() displayName}.42* The display name is typically a human readable string that a tool might43* display to a user. For example, a tool that shows a list of Java44* virtual machines running on a system might use the display name rather45* than the identifier. A {@code VirtualMachineDescriptor} may be46* created without a <i>display name</i>. In that case the identifier is47* used as the <i>display name</i>.48*49* <p> {@code VirtualMachineDescriptor} instances are typically created by50* invoking the {@link com.sun.tools.attach.VirtualMachine#list VirtualMachine.list()}51* method. This returns the complete list of descriptors to describe the52* Java virtual machines known to all installed {@link53* com.sun.tools.attach.spi.AttachProvider attach providers}.54*55* @since 1.656*/57public class VirtualMachineDescriptor {5859private AttachProvider provider;60private String id;61private String displayName;6263private volatile int hash; // 0 => not computed6465/**66* Creates a virtual machine descriptor from the given components.67*68* @param provider The AttachProvider to attach to the Java virtual machine.69* @param id The virtual machine identifier.70* @param displayName The display name.71*72* @throws NullPointerException73* If any of the arguments are {@code null}74*/75public VirtualMachineDescriptor(AttachProvider provider, String id, String displayName) {76if (provider == null) {77throw new NullPointerException("provider cannot be null");78}79if (id == null) {80throw new NullPointerException("identifier cannot be null");81}82if (displayName == null) {83throw new NullPointerException("display name cannot be null");84}85this.provider = provider;86this.id = id;87this.displayName = displayName;88}8990/**91* Creates a virtual machine descriptor from the given components.92*93* <p> This convenience constructor works as if by invoking the94* three-argument constructor as follows:95*96* <blockquote><code>97* new {@link #VirtualMachineDescriptor(AttachProvider, String, String)98* VirtualMachineDescriptor}(provider, id, id);99* </code></blockquote>100*101* <p> That is, it creates a virtual machine descriptor such that102* the <i>display name</i> is the same as the virtual machine103* identifier.104*105* @param provider The AttachProvider to attach to the Java virtual machine.106* @param id The virtual machine identifier.107*108* @throws NullPointerException109* If {@code provider} or {@code id} is {@code null}.110*/111public VirtualMachineDescriptor(AttachProvider provider, String id) {112this(provider, id, id);113}114115/**116* Return the {@code AttachProvider} that this descriptor references.117*118* @return The {@code AttachProvider} that this descriptor references.119*/120public AttachProvider provider() {121return provider;122}123124/**125* Return the identifier component of this descriptor.126*127* @return The identifier component of this descriptor.128*/129public String id() {130return id;131}132133/**134* Return the <i>display name</i> component of this descriptor.135*136* @return The display name component of this descriptor.137*/138public String displayName() {139return displayName;140}141142/**143* Returns a hash-code value for this VirtualMachineDescriptor. The hash144* code is based upon the descriptor's components, and satifies145* the general contract of the {@link java.lang.Object#hashCode()146* Object.hashCode} method.147*148* @return A hash-code value for this descriptor.149*/150public int hashCode() {151if (hash != 0) {152return hash;153}154hash = provider.hashCode() * 127 + id.hashCode();155return hash;156}157158/**159* Tests this VirtualMachineDescriptor for equality with another object.160*161* <p> If the given object is not a VirtualMachineDescriptor then this162* method returns {@code false}. For two VirtualMachineDescriptors to163* be considered equal requires that they both reference the same164* provider, and their {@link #id() identifiers} are equal. </p>165*166* <p> This method satisfies the general contract of the {@link167* java.lang.Object#equals(Object) Object.equals} method. </p>168*169* @param ob The object to which this object is to be compared170*171* @return {@code true} if, and only if, the given object is172* a VirtualMachineDescriptor that is equal to this173* VirtualMachineDescriptor.174*/175public boolean equals(Object ob) {176if (ob == this)177return true;178if (!(ob instanceof VirtualMachineDescriptor))179return false;180VirtualMachineDescriptor other = (VirtualMachineDescriptor)ob;181if (other.provider() != this.provider()) {182return false;183}184if (!other.id().equals(this.id())) {185return false;186}187return true;188}189190/**191* Returns the string representation of the {@code VirtualMachineDescriptor}.192*/193public String toString() {194String s = provider.toString() + ": " + id;195if (displayName != id) {196s += " " + displayName;197}198return s;199}200201202}203204205