001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Vitaly A. Provodin
021: * @version $Revision: 1.4 $
022: */
023:
024: /**
025: * Created on 29.01.2005
026: */package org.apache.harmony.jpda.tests.jdwp.share;
027:
028: import java.io.IOException;
029:
030: import org.apache.harmony.jpda.tests.framework.LogWriter;
031: import org.apache.harmony.jpda.tests.framework.TestErrorException;
032: import org.apache.harmony.jpda.tests.framework.jdwp.TransportWrapper;
033: import org.apache.harmony.jpda.tests.share.JPDATestOptions;
034:
035: /**
036: * This class provides DebuggeeWrapper implementation based on JUnit framework.
037: * Debuggee is always launched on local machine and attaches to debugger.
038: */
039: public class JDWPUnitDebuggeeWrapper extends
040: JDWPUnitDebuggeeProcessWrapper {
041:
042: /**
043: * Auxiliary options passed to the target VM on its launch.
044: */
045: public String savedVMOptions = null;
046:
047: protected TransportWrapper transport;
048:
049: /**
050: * Creates new instance with given data.
051: *
052: * @param settings
053: * test run options
054: * @param logWriter
055: * where to print log messages
056: */
057: public JDWPUnitDebuggeeWrapper(JPDATestOptions settings,
058: LogWriter logWriter) {
059: super (settings, logWriter);
060: }
061:
062: /**
063: * Launches new debuggee process according to test run options and
064: * establishes JDWP connection.
065: */
066: public void start() {
067: boolean isListenConnection = settings.isListenConnectorKind();
068: transport = createTransportWrapper();
069: String address = settings.getTransportAddress();
070:
071: if (isListenConnection) {
072: logWriter.println("Start listening on: " + address);
073: try {
074: address = transport.startListening(address);
075: } catch (IOException e) {
076: throw new TestErrorException(e);
077: }
078: logWriter.println("Listening on: " + address);
079: } else {
080: logWriter.println("Attach to: " + address);
081: }
082:
083: String cmdLine = settings.getDebuggeeJavaPath()
084: + " -cp \""
085: + settings.getDebuggeeClassPath()
086: + "\" -agentlib:"
087: + settings.getDebuggeeAgentName()
088: + "="
089: + settings.getDebuggeeAgentOptions(address,
090: isListenConnection) + " "
091: + settings.getDebuggeeVMExtraOptions() + " "
092: + (savedVMOptions != null ? savedVMOptions : "") + " "
093: + settings.getDebuggeeClassName();
094:
095: logWriter.println("Launch: " + cmdLine);
096:
097: try {
098: launchProcessAndRedirectors(cmdLine);
099: logWriter.println("Launched debuggee process");
100: openConnection();
101: logWriter.println("Established transport connection");
102: } catch (Exception e) {
103: throw new TestErrorException(e);
104: }
105: }
106:
107: /**
108: * Closes all connections, stops redirectors, and waits for debuggee process
109: * exit for default timeout.
110: */
111: public void stop() {
112: disposeConnection();
113:
114: finishProcessAndRedirectors();
115:
116: closeConnection();
117: if (settings.isListenConnectorKind()) {
118: try {
119: transport.stopListening();
120: } catch (IOException e) {
121: logWriter
122: .println("IOException in stopping transport listening: "
123: + e);
124: }
125: }
126: }
127:
128: /**
129: * Opens connection with debuggee.
130: */
131: protected void openConnection() {
132: try {
133: if (settings.isListenConnectorKind()) {
134: logWriter.println("Accepting JDWP connection");
135: transport.accept(settings.getTimeout(), settings
136: .getTimeout());
137: } else {
138: String address = settings.getTransportAddress();
139: logWriter.println("Attaching for JDWP connection");
140: transport.attach(address, settings.getTimeout(),
141: settings.getTimeout());
142: }
143: setConnection(transport);
144: } catch (IOException e) {
145: logWriter.printError(e);
146: throw new TestErrorException(e);
147: }
148: }
149:
150: /**
151: * Disposes JDWP connection stored in VmMirror.
152: */
153: protected void disposeConnection() {
154: if (vmMirror != null) {
155: try {
156: vmMirror.dispose();
157: } catch (Exception e) {
158: logWriter
159: .println("Ignoring exception in disposing debuggee VM: "
160: + e);
161: }
162: }
163: }
164:
165: /**
166: * Closes JDWP connection stored in VmMirror.
167: */
168: protected void closeConnection() {
169: if (vmMirror != null) {
170: try {
171: vmMirror.closeConnection();
172: } catch (IOException e) {
173: logWriter
174: .println("Ignoring exception in closing connection: "
175: + e);
176: }
177: }
178: }
179: }
|