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 class="striped"> 94 * <caption>Parameters</caption> 95 * <thead> 96 * <tr> 97 * <th scope="col">Parameter</th> 98 * <th scope="col">Description</th> 99 * <th scope="col">Constant Field</th> 100 * </tr> 101 * </thead> 102 * <tbody> 103 * <tr> 104 * <th scope="row">remoteAgent</th> 105 * <td>the remote agent to launch</td> 106 * <td>{@link #PARAM_REMOTE_AGENT}</td> 107 * </tr> 108 * <tr> 109 * <th scope="row">timeout</th> 110 * <td>milliseconds before connect timeout</td> 111 * <td>{@link #PARAM_TIMEOUT}</td> 112 * </tr> 113 * <tr> 114 * <th scope="row">launch</th> 115 * <td>"true" for JDI controlled launch</td> 116 * <td>{@link #PARAM_LAUNCH}</td> 117 * </tr> 118 * <tr> 119 * <th scope="row">hostname</th> 120 * <td>connect to the named of the local host ("" for discovered)</td> 121 * <td>{@link #PARAM_HOST_NAME}</td> 122 * </tr> 123 * </tbody> 124 * </table> 125 * 126 * @return the default parameter map 127 */ 128 @Override 129 public Map<String, String> defaultParameters() { 130 Map<String, String> dp = new HashMap<>(); 131 dp.put(PARAM_REMOTE_AGENT, RemoteExecutionControl.class.getName()); 132 dp.put(PARAM_TIMEOUT, "" + DEFAULT_TIMEOUT); 133 dp.put(PARAM_HOST_NAME, ""); 134 dp.put(PARAM_LAUNCH, "false"); 135 return dp; 136 } 137 138 @Override 139 public ExecutionControl generate(ExecutionEnv env, Map<String, String> parameters) 140 throws IOException { 141 Map<String, String> dp = defaultParameters(); 142 if (parameters == null) { 143 parameters = dp; 144 } 145 String remoteAgent = parameters.getOrDefault(PARAM_REMOTE_AGENT, dp.get(PARAM_REMOTE_AGENT)); 146 int timeout = Integer.parseUnsignedInt( 147 parameters.getOrDefault(PARAM_TIMEOUT, dp.get(PARAM_TIMEOUT))); 148 String host = parameters.getOrDefault(PARAM_HOST_NAME, dp.get(PARAM_HOST_NAME)); 149 String sIsLaunch = parameters.getOrDefault(PARAM_LAUNCH, dp.get(PARAM_LAUNCH)).toLowerCase(Locale.ROOT); 150 boolean isLaunch = sIsLaunch.length() > 0 151 && ("true".startsWith(sIsLaunch) || "yes".startsWith(sIsLaunch)); 152 return JdiDefaultExecutionControl.create(env, remoteAgent, isLaunch, host, timeout); 153 } 154 155} 156