All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.unlaxer.parser.jdi.VirtualMachineCreator Maven / Gradle / Ivy

package org.unlaxer.parser.jdi;

import java.io.IOException;
import java.util.Map;

import com.sun.jdi.Bootstrap;
import com.sun.jdi.VirtualMachine;
import com.sun.jdi.VirtualMachineManager;
import com.sun.jdi.connect.AttachingConnector;
import com.sun.jdi.connect.Connector;
import com.sun.jdi.connect.IllegalConnectorArgumentsException;

public class VirtualMachineCreator {

	public VirtualMachine connect(int port) throws IOException {
		AttachingConnector connector = getConnector();
		try {
			return connect(connector, port);
		} catch (IllegalConnectorArgumentsException e) {
			throw new IllegalStateException(e);
		}
	}

	private AttachingConnector getConnector() {

		VirtualMachineManager vmManager = Bootstrap.virtualMachineManager();

		for (Connector connector : vmManager.attachingConnectors()) {
			if ("com.sun.jdi.SocketAttach".equals(connector.name())) {
				return (AttachingConnector) connector;
			}
		}
		throw new IllegalStateException();
	}

	private VirtualMachine connect(AttachingConnector connector, int port)
			throws IllegalConnectorArgumentsException, IOException {

		Map args = connector.defaultArguments();
		Connector.Argument argument = args.get("port");

		if (argument == null) {
			throw new IllegalStateException();
		}
		argument.setValue(String.valueOf(port));

		return connector.attach(args);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy