jdk/src/jdk.attach/share/classes/com/sun/tools/attach/spi/AttachProvider.java
author sla
Thu, 07 May 2015 11:01:37 +0200
changeset 30670 9663e1b741df
parent 30042 4728ebcf8fd0
child 34894 3248b89d1921
permissions -rw-r--r--
8079360: AttachProviderImpl could not be instantiated Reviewed-by: dsamersoff, sspitsyn, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 20742
diff changeset
     2
 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.attach.spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import com.sun.tools.attach.VirtualMachine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import com.sun.tools.attach.VirtualMachineDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.tools.attach.AttachPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.tools.attach.AttachNotSupportedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.ServiceLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Attach provider class for attaching to a Java virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p> An attach provider is a concrete subclass of this class that has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * zero-argument constructor and implements the abstract methods specified
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
    44
 * below.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p> An attach provider implementation is typically tied to a Java virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * machine implementation, version, or even mode of operation. That is, a specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * provider implementation will typically only be capable of attaching to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * a specific Java virtual machine implementation or version. For example, Sun's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * JDK implementation ships with provider implementations that can only attach to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Sun's <i>HotSpot</i> virtual machine. In general, if an environment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * consists of Java virtual machines of different versions and from different
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * vendors then there will be an attach provider implementation for each
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
    54
 * <i>family</i> of implementations or versions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p> An attach provider is identified by its {@link #name <i>name</i>} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * {@link #type <i>type</i>}. The <i>name</i> is typically, but not required to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * be, a name that corresponds to the VM vendor. The Sun JDK implementation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * for example, ships with attach providers that use the name <i>"sun"</i>. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <i>type</i> typically corresponds to the attach mechanism. For example, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * implementation that uses the Doors inter-process communication mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * might use the type <i>"doors"</i>. The purpose of the name and type is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * identify providers in environments where there are multiple providers
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
    64
 * installed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p> AttachProvider implementations are loaded and instantiated at the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * invocation of the {@link #providers() providers} method. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * attempts to load all provider implementations that are installed on the
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
    69
 * platform.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <p> All of the methods in this class are safe for use by multiple
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
    72
 * concurrent threads.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
20742
4ae78e8060d6 8008662: Add @jdk.Exported to JDK-specific/exported APIs
alanb
parents: 5506
diff changeset
    77
@jdk.Exported
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
public abstract class AttachProvider {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private static final Object lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static List<AttachProvider> providers = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
    84
     * Initializes a new instance of this class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *          If a security manager has been installed and it denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *          {@link com.sun.tools.attach.AttachPermission AttachPermission}
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
    89
     *          ("{@code createAttachProvider}")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    protected AttachProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (sm != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            sm.checkPermission(new AttachPermission("createAttachProvider"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
    98
     * Return this provider's name.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @return  The name of this provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public abstract String name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   105
     * Return this provider's type.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @return  The type of this provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public abstract String type();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Attaches to a Java virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * <p> A Java virtual machine is identified by an abstract identifier. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * nature of this identifier is platform dependent but in many cases it will be the
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   116
     * string representation of the process identifier (or pid).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * <p> This method parses the identifier and maps the identifier to a Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * virtual machine (in an implementation dependent manner). If the identifier
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   120
     * cannot be parsed by the provider then an
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   121
     * {@link com.sun.tools.attach.AttachNotSupportedException AttachNotSupportedException}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * is thrown. Once parsed this method attempts to attach to the Java virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * If the provider detects that the identifier corresponds to a Java virtual machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * that does not exist, or it corresponds to a Java virtual machine that does not support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * the attach mechanism implemented by this provider, or it detects that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Java virtual machine is a version to which this provider cannot attach, then
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   127
     * an {@code AttachNotSupportedException} is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param  id
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *         The abstract identifier that identifies the Java virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @return  VirtualMachine representing the target virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *          If a security manager has been installed and it denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *          {@link com.sun.tools.attach.AttachPermission AttachPermission}
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   137
     *          ("{@code attachVirtualMachine}"), or other permission
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *          required by the implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @throws  AttachNotSupportedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *          If the identifier cannot be parsed, or it corresponds to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *          to a Java virtual machine that does not exist, or it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *          corresponds to a Java virtual machine which this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *          provider cannot attach.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @throws  NullPointerException
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   150
     *          If {@code id} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public abstract VirtualMachine attachVirtualMachine(String id)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        throws AttachNotSupportedException, IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Attaches to a Java virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   158
     * <p> A Java virtual machine can be described using a
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   159
     * {@link com.sun.tools.attach.VirtualMachineDescriptor VirtualMachineDescriptor}.
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   160
     * This method invokes the descriptor's
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   161
     * {@link com.sun.tools.attach.VirtualMachineDescriptor#provider() provider()} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * to check that it is equal to this provider. It then attempts to attach to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Java virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @param  vmd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *         The virtual machine descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @return  VirtualMachine representing the target virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *          If a security manager has been installed and it denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *          {@link com.sun.tools.attach.AttachPermission AttachPermission}
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   173
     *          ("{@code attachVirtualMachine}"), or other permission
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *          required by the implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @throws  AttachNotSupportedException
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   177
     *          If the descriptor's
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   178
     *          {@link com.sun.tools.attach.VirtualMachineDescriptor#provider() provider()}
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   179
     *          method returns a provider that is not this provider, or it does not
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   180
     *          correspond to a Java virtual machine to which this provider can attach.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @throws  NullPointerException
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   186
     *          If {@code vmd} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public VirtualMachine attachVirtualMachine(VirtualMachineDescriptor vmd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        throws AttachNotSupportedException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (vmd.provider() != this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            throw new AttachNotSupportedException("provider mismatch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return attachVirtualMachine(vmd.id());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Lists the Java virtual machines known to this provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   200
     * <p> This method returns a list of
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   201
     * {@link com.sun.tools.attach.VirtualMachineDescriptor} elements. Each
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   202
     * {@code VirtualMachineDescriptor} describes a Java virtual machine
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * to which this provider can <i>potentially</i> attach.  There isn't any
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   204
     * guarantee that invoking
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   205
     * {@link #attachVirtualMachine(VirtualMachineDescriptor) attachVirtualMachine}
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   206
     * on each descriptor in the list will succeed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @return  The list of virtual machine descriptors which describe the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *          Java virtual machines known to this provider (may be empty).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public abstract List<VirtualMachineDescriptor> listVirtualMachines();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Returns a list of the installed attach providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * <p> An AttachProvider is installed on the platform if:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * <ul>
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   220
     *   <li>It is installed in a JAR file that is visible to the defining
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *   class loader of the AttachProvider type (usually, but not required
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *   to be, the {@link java.lang.ClassLoader#getSystemClassLoader system
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   223
     *   class loader}).</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   225
     *   <li>The JAR file contains a provider configuration named
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   226
     *   {@code com.sun.tools.attach.spi.AttachProvider} in the resource directory
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   227
     *   {@code META-INF/services}.</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   229
     *   <li>The provider configuration file lists the full-qualified class
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   230
     *   name of the AttachProvider implementation.</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * <p> The format of the provider configuration file is one fully-qualified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * class name per line. Space and tab characters surrounding each class name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * as well as blank lines are ignored. The comment character is
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   236
     *  {@code '#'} ({@code 0x23}), and on each line all characters following
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * the first comment character are ignored. The file must be encoded in
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   238
     * UTF-8.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * <p> AttachProvider implementations are loaded and instantiated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * (using the zero-arg constructor) at the first invocation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * The list returned by the first invocation of this method is the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * of providers. Subsequent invocations of this method return a list of the same
30670
9663e1b741df 8079360: AttachProviderImpl could not be instantiated
sla
parents: 30042
diff changeset
   244
     * providers. The list is unmodifiable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @return  A list of the installed attach providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public static List<AttachProvider> providers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            if (providers == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                providers = new ArrayList<AttachProvider>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                ServiceLoader<AttachProvider> providerLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    ServiceLoader.load(AttachProvider.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                                       AttachProvider.class.getClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                Iterator<AttachProvider> i = providerLoader.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                        providers.add(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                        if (t instanceof ThreadDeath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                            ThreadDeath td = (ThreadDeath)t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                            throw td;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                        }
30670
9663e1b741df 8079360: AttachProviderImpl could not be instantiated
sla
parents: 30042
diff changeset
   267
                        // Log errors and exceptions since we cannot return them
9663e1b741df 8079360: AttachProviderImpl could not be instantiated
sla
parents: 30042
diff changeset
   268
                        t.printStackTrace();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            return Collections.unmodifiableList(providers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
}