1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 /** 20 * @author Vitaly A. Provodin 21 */ 22 23 /** 24 * Created on 29.01.2005 25 */ 26 package org.apache.harmony.jpda.tests.jdwp.share; 27 28 import java.io.IOException; 29 30 import org.apache.harmony.jpda.tests.framework.LogWriter; 31 import org.apache.harmony.jpda.tests.framework.TestErrorException; 32 import org.apache.harmony.jpda.tests.framework.jdwp.TransportWrapper; 33 import org.apache.harmony.jpda.tests.share.JPDATestOptions; 34 35 /** 36 * This class provides DebuggeeWrapper implementation based on JUnit framework. 37 * Debuggee is always launched on local machine and attaches to debugger. 38 */ 39 public class JDWPUnitDebuggeeWrapper extends JDWPUnitDebuggeeProcessWrapper { 40 41 /** 42 * Auxiliary options passed to the target VM on its launch. 43 */ 44 public String savedVMOptions = null; 45 46 /** 47 * Wrapper around JDWP transport connection. 48 */ 49 protected TransportWrapper transport; 50 51 /** 52 * JDWP transport address. 53 */ 54 protected String address; 55 56 /** 57 * Is this a "listen" JDWP connection? (If false, it is a an "attach" connection.) 58 */ 59 boolean isListenConnection; 60 61 62 /** 63 * Creates new instance with given data. 64 * 65 * @param settings 66 * test run options 67 * @param logWriter 68 * where to print log messages 69 */ JDWPUnitDebuggeeWrapper(JPDATestOptions settings, LogWriter logWriter)70 public JDWPUnitDebuggeeWrapper(JPDATestOptions settings, LogWriter logWriter) { 71 super(settings, logWriter); 72 } 73 74 /** 75 * Set up server side JDWP connection before launching the debuggee. 76 */ setUpConnection()77 public void setUpConnection() { 78 isListenConnection = settings.isListenConnectorKind(); 79 transport = createTransportWrapper(); 80 address = settings.getTransportAddress(); 81 82 if (isListenConnection) { 83 logWriter.println("Start listening on: " + address); 84 try { 85 address = transport.startListening(address); 86 } catch (IOException e) { 87 throw new TestErrorException(e); 88 } 89 logWriter.println("Listening on: " + address); 90 } else { 91 logWriter.println("Attach to: " + address); 92 } 93 } 94 95 /** 96 * Launches new debuggee process according to test run options and 97 * establishes JDWP connection. 98 */ start()99 public void start() { 100 String cmdLine = settings.getDebuggeeJavaPath() + " -cp \"" 101 + settings.getDebuggeeClassPath() + "\" -agentlib:" 102 + settings.getDebuggeeAgentName() + "=" 103 + settings.getDebuggeeAgentOptions(address, isListenConnection) 104 + " " + settings.getDebuggeeVMExtraOptions() + " " 105 + (savedVMOptions != null ? savedVMOptions : "") + " " 106 + settings.getDebuggeeClassName(); 107 108 logWriter.println("Launch: " + cmdLine); 109 110 try { 111 launchProcessAndRedirectors(cmdLine); 112 logWriter.println("Launched debuggee process"); 113 openConnection(); 114 logWriter.println("Established transport connection"); 115 } catch (Exception e) { 116 throw new TestErrorException(e); 117 } 118 } 119 120 /** 121 * Closes all connections, stops redirectors, and waits for debuggee process 122 * exit for default timeout. 123 */ stop()124 public void stop() { 125 disposeConnection(); 126 127 finishProcessAndRedirectors(); 128 129 closeConnection(); 130 if (settings.isListenConnectorKind()) { 131 try { 132 transport.stopListening(); 133 } catch (IOException e) { 134 logWriter.println("IOException in stopping transport listening: " + e); 135 } 136 } 137 } 138 139 /** 140 * Opens connection with debuggee. 141 */ openConnection()142 protected void openConnection() { 143 try { 144 if (settings.isListenConnectorKind()) { 145 logWriter.println("Accepting JDWP connection"); 146 transport.accept(settings.getTimeout(), settings.getTimeout()); 147 } else { 148 String address = settings.getTransportAddress(); 149 logWriter.println("Attaching for JDWP connection"); 150 transport.attach(address, settings.getTimeout(), settings.getTimeout()); 151 } 152 setConnection(transport); 153 } catch (IOException e) { 154 logWriter.printError(e); 155 throw new TestErrorException(e); 156 } 157 } 158 159 /** 160 * Disposes JDWP connection stored in VmMirror. 161 */ disposeConnection()162 protected void disposeConnection() { 163 if (vmMirror != null) { 164 try { 165 vmMirror.dispose(); 166 } catch (Exception e) { 167 logWriter.println("Ignoring exception in disposing debuggee VM: " + e); 168 } 169 } 170 } 171 172 /** 173 * Closes JDWP connection stored in VmMirror. 174 */ closeConnection()175 protected void closeConnection() { 176 if (vmMirror != null) { 177 try { 178 vmMirror.closeConnection(); 179 } catch (IOException e) { 180 logWriter.println("Ignoring exception in closing connection: " + e); 181 } 182 } 183 } 184 } 185