0N/A/*
2362N/A * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.tools.jdi;
0N/A
0N/Aimport com.sun.tools.jdi.*;
0N/Aimport com.sun.jdi.connect.*;
0N/Aimport com.sun.jdi.connect.spi.*;
0N/Aimport com.sun.jdi.*;
0N/A
0N/Aimport java.util.Map;
0N/Aimport java.util.StringTokenizer;
0N/Aimport java.util.List;
0N/Aimport java.util.ArrayList;
0N/Aimport java.io.IOException;
0N/Aimport java.io.InterruptedIOException;
0N/A
0N/Aabstract class AbstractLauncher extends ConnectorImpl implements LaunchingConnector {
0N/A
0N/A abstract public VirtualMachine
0N/A launch(Map<String,? extends Connector.Argument> arguments)
0N/A throws IOException,
0N/A IllegalConnectorArgumentsException,
0N/A VMStartException;
0N/A abstract public String name();
0N/A abstract public String description();
0N/A
0N/A ThreadGroup grp;
0N/A
0N/A AbstractLauncher() {
0N/A super();
0N/A
0N/A grp = Thread.currentThread().getThreadGroup();
0N/A ThreadGroup parent = null;
0N/A while ((parent = grp.getParent()) != null) {
0N/A grp = parent;
0N/A }
0N/A }
0N/A
0N/A String[] tokenizeCommand(String command, char quote) {
0N/A String quoteStr = String.valueOf(quote); // easier to deal with
0N/A
0N/A /*
0N/A * Tokenize the command, respecting the given quote character.
0N/A */
0N/A StringTokenizer tokenizer = new StringTokenizer(command,
0N/A quote + " \t\r\n\f",
0N/A true);
0N/A String quoted = null;
0N/A String pending = null;
0N/A List<String> tokenList = new ArrayList<String>();
0N/A while (tokenizer.hasMoreTokens()) {
0N/A String token = tokenizer.nextToken();
0N/A if (quoted != null) {
0N/A if (token.equals(quoteStr)) {
0N/A tokenList.add(quoted);
0N/A quoted = null;
0N/A } else {
0N/A quoted += token;
0N/A }
0N/A } else if (pending != null) {
0N/A if (token.equals(quoteStr)) {
0N/A quoted = pending;
0N/A } else if ((token.length() == 1) &&
0N/A Character.isWhitespace(token.charAt(0))) {
0N/A tokenList.add(pending);
0N/A } else {
0N/A throw new InternalException("Unexpected token: " + token);
0N/A }
0N/A pending = null;
0N/A } else {
0N/A if (token.equals(quoteStr)) {
0N/A quoted = "";
0N/A } else if ((token.length() == 1) &&
0N/A Character.isWhitespace(token.charAt(0))) {
0N/A // continue
0N/A } else {
0N/A pending = token;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Add final token.
0N/A */
0N/A if (pending != null) {
0N/A tokenList.add(pending);
0N/A }
0N/A
0N/A /*
0N/A * An unclosed quote at the end of the command. Do an
0N/A * implicit end quote.
0N/A */
0N/A if (quoted != null) {
0N/A tokenList.add(quoted);
0N/A }
0N/A
0N/A String[] tokenArray = new String[tokenList.size()];
0N/A for (int i = 0; i < tokenList.size(); i++) {
28N/A tokenArray[i] = tokenList.get(i);
0N/A }
0N/A return tokenArray;
0N/A }
0N/A
0N/A protected VirtualMachine launch(String[] commandArray, String address,
0N/A TransportService.ListenKey listenKey,
0N/A TransportService ts)
0N/A throws IOException, VMStartException {
0N/A Helper helper = new Helper(commandArray, address, listenKey, ts);
0N/A helper.launchAndAccept();
0N/A
0N/A VirtualMachineManager manager =
0N/A Bootstrap.virtualMachineManager();
0N/A
0N/A return manager.createVirtualMachine(helper.connection(),
0N/A helper.process());
0N/A }
0N/A
0N/A /**
0N/A * This class simply provides a context for a single launch and
0N/A * accept. It provides instance fields that can be used by
0N/A * all threads involved. This stuff can't be in the Connector proper
6396N/A * because the connector is a singleton and is not specific to any
0N/A * one launch.
0N/A */
0N/A private class Helper {
0N/A private final String address;
0N/A private TransportService.ListenKey listenKey;
0N/A private TransportService ts;
0N/A private final String[] commandArray;
0N/A private Process process = null;
0N/A private Connection connection = null;
0N/A private IOException acceptException = null;
0N/A private boolean exited = false;
0N/A
0N/A Helper(String[] commandArray, String address, TransportService.ListenKey listenKey,
0N/A TransportService ts) {
0N/A this.commandArray = commandArray;
0N/A this.address = address;
0N/A this.listenKey = listenKey;
0N/A this.ts = ts;
0N/A }
0N/A
0N/A String commandString() {
0N/A String str = "";
0N/A for (int i = 0; i < commandArray.length; i++) {
0N/A if (i > 0) {
0N/A str += " ";
0N/A }
0N/A str += commandArray[i];
0N/A }
0N/A return str;
0N/A }
0N/A
0N/A synchronized void launchAndAccept() throws
0N/A IOException, VMStartException {
0N/A
0N/A process = Runtime.getRuntime().exec(commandArray);
0N/A
0N/A Thread acceptingThread = acceptConnection();
0N/A Thread monitoringThread = monitorTarget();
0N/A try {
0N/A while ((connection == null) &&
0N/A (acceptException == null) &&
0N/A !exited) {
0N/A wait();
0N/A }
0N/A
0N/A if (exited) {
0N/A throw new VMStartException(
0N/A "VM initialization failed for: " + commandString(), process);
0N/A }
0N/A if (acceptException != null) {
0N/A // Rethrow the exception in this thread
0N/A throw acceptException;
0N/A }
0N/A } catch (InterruptedException e) {
0N/A throw new InterruptedIOException("Interrupted during accept");
0N/A } finally {
0N/A acceptingThread.interrupt();
0N/A monitoringThread.interrupt();
0N/A }
0N/A }
0N/A
0N/A Process process() {
0N/A return process;
0N/A }
0N/A
0N/A Connection connection() {
0N/A return connection;
0N/A }
0N/A
0N/A synchronized void notifyOfExit() {
0N/A exited = true;
0N/A notify();
0N/A }
0N/A
0N/A synchronized void notifyOfConnection(Connection connection) {
0N/A this.connection = connection;
0N/A notify();
0N/A }
0N/A
0N/A synchronized void notifyOfAcceptException(IOException acceptException) {
0N/A this.acceptException = acceptException;
0N/A notify();
0N/A }
0N/A
0N/A Thread monitorTarget() {
0N/A Thread thread = new Thread(grp,
0N/A "launched target monitor") {
0N/A public void run() {
0N/A try {
0N/A process.waitFor();
0N/A /*
0N/A * Notify waiting thread of VM error termination
0N/A */
0N/A notifyOfExit();
0N/A } catch (InterruptedException e) {
0N/A // Connection has been established, stop monitoring
0N/A }
0N/A }
0N/A };
0N/A thread.setDaemon(true);
0N/A thread.start();
0N/A return thread;
0N/A }
0N/A
0N/A Thread acceptConnection() {
0N/A Thread thread = new Thread(grp,
0N/A "connection acceptor") {
0N/A public void run() {
0N/A try {
0N/A Connection connection = ts.accept(listenKey, 0, 0);
0N/A /*
0N/A * Notify waiting thread of connection
0N/A */
0N/A notifyOfConnection(connection);
0N/A } catch (InterruptedIOException e) {
0N/A // VM terminated, stop accepting
0N/A } catch (IOException e) {
0N/A // Report any other exception to waiting thread
0N/A notifyOfAcceptException(e);
0N/A }
0N/A }
0N/A };
0N/A thread.setDaemon(true);
0N/A thread.start();
0N/A return thread;
0N/A }
0N/A }
0N/A}