1 /* 2 * Copyright (c) 2016, 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 package jdk.internal.jshell.jdi; 26 27 import java.util.ArrayList; 28 import java.util.Collection; 29 import java.util.List; 30 import jdk.internal.jshell.debug.InternalDebugControl; 31 import jdk.jshell.JShellException; 32 import jdk.jshell.spi.ExecutionControl; 33 import jdk.jshell.spi.ExecutionEnv; 34 35 /** 36 * A meta implementation of ExecutionControl which cycles through the specified 37 * ExecutionControl instances until it finds one that starts. 38 */ 39 public class FailOverExecutionControl implements ExecutionControl { 40 41 private final List<ExecutionControl> ecl = new ArrayList<>(); 42 private ExecutionControl active = null; 43 private final List<Exception> thrown = new ArrayList<>(); 44 45 /** 46 * Create the ExecutionControl instance with at least one actual 47 * ExecutionControl instance. 48 * 49 * @param ec0 the first instance to try 50 * @param ecs the second and on instance to try 51 */ 52 public FailOverExecutionControl(ExecutionControl ec0, ExecutionControl... ecs) { 53 ecl.add(ec0); 54 for (ExecutionControl ec : ecs) { 55 ecl.add(ec); 56 } 57 } 58 59 @Override 60 public void start(ExecutionEnv env) throws Exception { 61 for (ExecutionControl ec : ecl) { 62 try { 63 ec.start(env); 64 // Success! This is our active ExecutionControl 65 active = ec; 66 return; 67 } catch (Exception ex) { 68 thrown.add(ex); 69 } catch (Throwable ex) { 70 thrown.add(new RuntimeException(ex)); 71 } 72 InternalDebugControl.debug(env.state(), env.userErr(), 73 thrown.get(thrown.size() - 1), "failed one in FailOverExecutionControl"); 74 } 75 // They have all failed -- rethrow the first exception we encountered 76 throw thrown.get(0); 77 } 78 79 @Override 80 public void close() { 81 active.close(); 82 } 83 84 @Override 85 public boolean addToClasspath(String path) { 86 return active.addToClasspath(path); 87 } 88 89 @Override 90 public String invoke(String classname, String methodname) throws JShellException { 91 return active.invoke(classname, methodname); 92 } 93 94 @Override 95 public boolean load(Collection<String> classes) { 96 return active.load(classes); 97 } 98 99 @Override 100 public boolean redefine(Collection<String> classes) { 101 return active.redefine(classes); 102 } 103 104 @Override 105 public ClassStatus getClassStatus(String classname) { 106 return active.getClassStatus(classname); 107 } 108 109 @Override 110 public void stop() { 111 active.stop(); 112 } 113 114 @Override 115 public String varValue(String classname, String varname) { 116 return active.varValue(classname, varname); 117 } 118 119 }