JdiExecutionControlProvider.java revision 4150:fff0714129d8
1/*
2 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package jdk.jshell.execution;
27
28import java.io.IOException;
29import java.util.HashMap;
30import java.util.Locale;
31import java.util.Map;
32import jdk.jshell.spi.ExecutionControl;
33import jdk.jshell.spi.ExecutionControlProvider;
34import jdk.jshell.spi.ExecutionEnv;
35
36/**
37 * A provider of remote JDI-controlled execution engines.
38 *
39 * @author Robert Field
40 * @since 9
41 */
42public class JdiExecutionControlProvider implements ExecutionControlProvider {
43
44    /**
45     * The remote agent to launch.
46     */
47    public static final String PARAM_REMOTE_AGENT = "remoteAgent";
48
49    /**
50     * Milliseconds before connect timeout.
51     */
52    public static final String PARAM_TIMEOUT = "timeout";
53
54    /**
55     * The local hostname to connect to.
56     */
57    public static final String PARAM_HOST_NAME = "hostname";
58
59    /**
60     * Should JDI-controlled launching be used?
61     */
62    public static final String PARAM_LAUNCH = "launch";
63
64    /**
65     * Default time-out expressed in milliseconds.
66     */
67    private static final int DEFAULT_TIMEOUT = 5000;
68
69    /**
70     * Create an instance.  An instance can be used to
71     * {@linkplain  #generate generate} an {@link ExecutionControl} instance
72     * that uses the Java Debug Interface as part of the control of a remote
73     * process.
74     */
75    public JdiExecutionControlProvider() {
76    }
77
78    /**
79     * The unique name of this {@code ExecutionControlProvider}.
80     *
81     * @return "jdi"
82     */
83    @Override
84    public String name() {
85        return "jdi";
86    }
87
88    /**
89     * Create and return the default parameter map for this
90     * {@code ExecutionControlProvider}. The map can optionally be modified;
91     * Modified or unmodified it can be passed to
92     * {@link #generate(jdk.jshell.spi.ExecutionEnv, java.util.Map) }.
93     * <table>
94     * <caption>Parameters</caption>
95     *   <tr>
96     *     <th>Parameter</th>
97     *     <th>Description</th>
98     *     <th>Constant Field</th>
99     *   </tr>
100     *   <tr>
101     *     <td>remoteAgent</td>
102     *     <td>the remote agent to launch</td>
103     *     <td>{@link #PARAM_REMOTE_AGENT}</td>
104     *   </tr>
105     *   <tr>
106     *     <td>timeout</td>
107     *     <td>milliseconds before connect timeout</td>
108     *     <td>{@link #PARAM_TIMEOUT}</td>
109     *   </tr>
110     *   <tr>
111     *     <td>launch</td>
112     *     <td>"true" for JDI controlled launch</td>
113     *     <td>{@link #PARAM_LAUNCH}</td>
114     *   </tr>
115     *   <tr>
116     *     <td>hostname</td>
117     *     <td>connect to the named of the local host ("" for discovered)</td>
118     *     <td>{@link #PARAM_HOST_NAME}</td>
119     *   </tr>
120     * </table>
121     *
122     * @return the default parameter map
123     */
124    @Override
125    public Map<String, String> defaultParameters() {
126        Map<String, String> dp = new HashMap<>();
127        dp.put(PARAM_REMOTE_AGENT, RemoteExecutionControl.class.getName());
128        dp.put(PARAM_TIMEOUT, "" + DEFAULT_TIMEOUT);
129        dp.put(PARAM_HOST_NAME, "");
130        dp.put(PARAM_LAUNCH, "false");
131        return dp;
132    }
133
134    @Override
135    public ExecutionControl generate(ExecutionEnv env, Map<String, String> parameters)
136            throws IOException {
137        Map<String, String> dp  = defaultParameters();
138        if (parameters == null) {
139            parameters = dp;
140        }
141        String remoteAgent = parameters.getOrDefault(PARAM_REMOTE_AGENT, dp.get(PARAM_REMOTE_AGENT));
142        int timeout = Integer.parseUnsignedInt(
143                parameters.getOrDefault(PARAM_TIMEOUT, dp.get(PARAM_TIMEOUT)));
144        String host = parameters.getOrDefault(PARAM_HOST_NAME, dp.get(PARAM_HOST_NAME));
145        String sIsLaunch = parameters.getOrDefault(PARAM_LAUNCH, dp.get(PARAM_LAUNCH)).toLowerCase(Locale.ROOT);
146        boolean isLaunch = sIsLaunch.length() > 0
147                && ("true".startsWith(sIsLaunch) || "yes".startsWith(sIsLaunch));
148        return JdiDefaultExecutionControl.create(env, remoteAgent, isLaunch, host, timeout);
149    }
150
151}
152