JdiExecutionControlProvider.java 5.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package jdk.jshell.execution;

import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import jdk.jshell.spi.ExecutionControl;
import jdk.jshell.spi.ExecutionControlProvider;
import jdk.jshell.spi.ExecutionEnv;

/**
 * A provider of remote JDI-controlled execution engines.
R
rfield 已提交
38
 *
39
 * @author Robert Field
R
rfield 已提交
40
 * @since 9
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
 */
public class JdiExecutionControlProvider implements ExecutionControlProvider {

    /**
     * The remote agent to launch.
     */
    public static final String PARAM_REMOTE_AGENT = "remoteAgent";

    /**
     * Milliseconds before connect timeout.
     */
    public static final String PARAM_TIMEOUT = "timeout";

    /**
     * The local hostname to connect to.
     */
    public static final String PARAM_HOST_NAME = "hostname";

    /**
     * Should JDI-controlled launching be used?
     */
    public static final String PARAM_LAUNCH = "launch";

    /**
     * Default time-out expressed in milliseconds.
     */
    private static final int DEFAULT_TIMEOUT = 5000;

    /**
     * Create an instance.  An instance can be used to
     * {@linkplain  #generate generate} an {@link ExecutionControl} instance
     * that uses the Java Debug Interface as part of the control of a remote
     * process.
     */
    public JdiExecutionControlProvider() {
    }

    /**
     * The unique name of this {@code ExecutionControlProvider}.
     *
     * @return "jdi"
     */
    @Override
    public String name() {
        return "jdi";
    }

    /**
     * Create and return the default parameter map for this
     * {@code ExecutionControlProvider}. The map can optionally be modified;
     * Modified or unmodified it can be passed to
     * {@link #generate(jdk.jshell.spi.ExecutionEnv, java.util.Map) }.
     * <table summary="Parameters">
     *   <tr>
     *     <th>Parameter</th>
     *     <th>Description</th>
     *     <th>Constant Field</th>
     *   </tr>
     *   <tr>
     *     <td>remoteAgent</td>
     *     <td>the remote agent to launch</td>
     *     <td>{@link #PARAM_REMOTE_AGENT}</td>
     *   </tr>
     *   <tr>
     *     <td>timeout</td>
     *     <td>milliseconds before connect timeout</td>
     *     <td>{@link #PARAM_TIMEOUT}</td>
     *   </tr>
     *   <tr>
     *     <td>launch</td>
     *     <td>"true" for JDI controlled launch</td>
     *     <td>{@link #PARAM_LAUNCH}</td>
     *   </tr>
     *   <tr>
     *     <td>hostname</td>
     *     <td>connect to the named of the local host ("" for discovered)</td>
     *     <td>{@link #PARAM_HOST_NAME}</td>
     *   </tr>
     * </table>
     *
     * @return the default parameter map
     */
    @Override
    public Map<String, String> defaultParameters() {
        Map<String, String> dp = new HashMap<>();
        dp.put(PARAM_REMOTE_AGENT, RemoteExecutionControl.class.getName());
        dp.put(PARAM_TIMEOUT, "" + DEFAULT_TIMEOUT);
        dp.put(PARAM_HOST_NAME, "");
        dp.put(PARAM_LAUNCH, "false");
        return dp;
    }

    @Override
    public ExecutionControl generate(ExecutionEnv env, Map<String, String> parameters)
            throws IOException {
        Map<String, String> dp  = defaultParameters();
        if (parameters == null) {
            parameters = dp;
        }
        String remoteAgent = parameters.getOrDefault(PARAM_REMOTE_AGENT, dp.get(PARAM_REMOTE_AGENT));
        int timeout = Integer.parseUnsignedInt(
                parameters.getOrDefault(PARAM_TIMEOUT, dp.get(PARAM_TIMEOUT)));
        String host = parameters.getOrDefault(PARAM_HOST_NAME, dp.get(PARAM_HOST_NAME));
        String sIsLaunch = parameters.getOrDefault(PARAM_LAUNCH, dp.get(PARAM_LAUNCH)).toLowerCase(Locale.ROOT);
        boolean isLaunch = sIsLaunch.length() > 0
                && ("true".startsWith(sIsLaunch) || "yes".startsWith(sIsLaunch));
        return JdiDefaultExecutionControl.create(env, remoteAgent, isLaunch, host, timeout);
    }

}