0N/A/*
2362N/A * Copyright (c) 1998, 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.jdi.*;
0N/Aimport com.sun.jdi.connect.*;
0N/Aimport com.sun.jdi.connect.spi.*;
0N/Aimport java.lang.reflect.Constructor;
0N/Aimport java.lang.reflect.InvocationTargetException;
0N/Aimport java.util.List;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.Collections;
0N/Aimport java.util.Iterator;
0N/Aimport java.util.ResourceBundle;
0N/Aimport java.io.IOException;
0N/A
0N/Aimport java.util.ServiceLoader;
0N/A
0N/A/* Public for use by com.sun.jdi.Bootstrap */
0N/Apublic class VirtualMachineManagerImpl implements VirtualMachineManagerService {
0N/A private List<Connector> connectors = new ArrayList<Connector>();
0N/A private LaunchingConnector defaultConnector = null;
0N/A private List<VirtualMachine> targets = new ArrayList<VirtualMachine>();
0N/A private final ThreadGroup mainGroupForJDI;
0N/A private ResourceBundle messages = null;
0N/A private int vmSequenceNumber = 0;
0N/A private static final int majorVersion = 1;
0N/A private static final int minorVersion = 6;
0N/A
0N/A private static final Object lock = new Object();
0N/A private static VirtualMachineManagerImpl vmm;
0N/A
0N/A public static VirtualMachineManager virtualMachineManager() {
0N/A SecurityManager sm = System.getSecurityManager();
0N/A if (sm != null) {
0N/A JDIPermission vmmPermission =
0N/A new JDIPermission("virtualMachineManager");
0N/A sm.checkPermission(vmmPermission);
0N/A }
0N/A synchronized (lock) {
0N/A if (vmm == null) {
0N/A vmm = new VirtualMachineManagerImpl();
0N/A }
0N/A }
0N/A return vmm;
0N/A }
0N/A
0N/A protected VirtualMachineManagerImpl() {
0N/A
0N/A /*
0N/A * Create a top-level thread group
0N/A */
0N/A ThreadGroup top = Thread.currentThread().getThreadGroup();
0N/A ThreadGroup parent = null;
0N/A while ((parent = top.getParent()) != null) {
0N/A top = parent;
0N/A }
0N/A mainGroupForJDI = new ThreadGroup(top, "JDI main");
0N/A
0N/A /*
0N/A * Load the connectors
0N/A */
0N/A ServiceLoader<Connector> connectorLoader =
0N/A ServiceLoader.load(Connector.class, Connector.class.getClassLoader());
0N/A
0N/A Iterator<Connector> connectors = connectorLoader.iterator();
0N/A
0N/A while (connectors.hasNext()) {
0N/A Connector connector;
0N/A
0N/A try {
28N/A connector = connectors.next();
0N/A } catch (ThreadDeath x) {
0N/A throw x;
0N/A } catch (Exception x) {
0N/A System.err.println(x);
0N/A continue;
0N/A } catch (Error x) {
0N/A System.err.println(x);
0N/A continue;
0N/A }
0N/A
0N/A addConnector(connector);
0N/A }
0N/A
0N/A /*
0N/A * Load any transport services and encapsulate them with
0N/A * an attaching and listening connector.
0N/A */
0N/A ServiceLoader<TransportService> transportLoader =
0N/A ServiceLoader.load(TransportService.class,
0N/A TransportService.class.getClassLoader());
0N/A
0N/A Iterator<TransportService> transportServices =
0N/A transportLoader.iterator();
0N/A
0N/A while (transportServices.hasNext()) {
0N/A TransportService transportService;
0N/A
0N/A try {
28N/A transportService = transportServices.next();
0N/A } catch (ThreadDeath x) {
0N/A throw x;
0N/A } catch (Exception x) {
0N/A System.err.println(x);
0N/A continue;
0N/A } catch (Error x) {
0N/A System.err.println(x);
0N/A continue;
0N/A }
0N/A
0N/A addConnector(GenericAttachingConnector.create(transportService));
0N/A addConnector(GenericListeningConnector.create(transportService));
0N/A }
0N/A
0N/A // no connectors found
0N/A if (allConnectors().size() == 0) {
0N/A throw new Error("no Connectors loaded");
0N/A }
0N/A
0N/A // Set the default launcher. In order to be compatible
0N/A // 1.2/1.3/1.4 we try to make the default launcher
0N/A // "com.sun.jdi.CommandLineLaunch". If this connector
0N/A // isn't found then we arbitarly pick the first connector.
0N/A //
0N/A boolean found = false;
0N/A List<LaunchingConnector> launchers = launchingConnectors();
0N/A for (LaunchingConnector lc: launchers) {
0N/A if (lc.name().equals("com.sun.jdi.CommandLineLaunch")) {
0N/A setDefaultConnector(lc);
0N/A found = true;
0N/A break;
0N/A }
0N/A }
0N/A if (!found && launchers.size() > 0) {
0N/A setDefaultConnector(launchers.get(0));
0N/A }
0N/A
0N/A }
0N/A
0N/A public LaunchingConnector defaultConnector() {
0N/A if (defaultConnector == null) {
0N/A throw new Error("no default LaunchingConnector");
0N/A }
0N/A return defaultConnector;
0N/A }
0N/A
0N/A public void setDefaultConnector(LaunchingConnector connector) {
0N/A defaultConnector = connector;
0N/A }
0N/A
0N/A public List<LaunchingConnector> launchingConnectors() {
0N/A List<LaunchingConnector> launchingConnectors = new ArrayList<LaunchingConnector>(connectors.size());
0N/A for (Connector connector: connectors) {
0N/A if (connector instanceof LaunchingConnector) {
0N/A launchingConnectors.add((LaunchingConnector)connector);
0N/A }
0N/A }
0N/A return Collections.unmodifiableList(launchingConnectors);
0N/A }
0N/A
0N/A public List<AttachingConnector> attachingConnectors() {
0N/A List<AttachingConnector> attachingConnectors = new ArrayList<AttachingConnector>(connectors.size());
0N/A for (Connector connector: connectors) {
0N/A if (connector instanceof AttachingConnector) {
0N/A attachingConnectors.add((AttachingConnector)connector);
0N/A }
0N/A }
0N/A return Collections.unmodifiableList(attachingConnectors);
0N/A }
0N/A
0N/A public List<ListeningConnector> listeningConnectors() {
0N/A List<ListeningConnector> listeningConnectors = new ArrayList<ListeningConnector>(connectors.size());
0N/A for (Connector connector: connectors) {
0N/A if (connector instanceof ListeningConnector) {
0N/A listeningConnectors.add((ListeningConnector)connector);
0N/A }
0N/A }
0N/A return Collections.unmodifiableList(listeningConnectors);
0N/A }
0N/A
0N/A public List<Connector> allConnectors() {
0N/A return Collections.unmodifiableList(connectors);
0N/A }
0N/A
0N/A public List<VirtualMachine> connectedVirtualMachines() {
0N/A return Collections.unmodifiableList(targets);
0N/A }
0N/A
0N/A public void addConnector(Connector connector) {
0N/A connectors.add(connector);
0N/A }
0N/A
0N/A public void removeConnector(Connector connector) {
0N/A connectors.remove(connector);
0N/A }
0N/A
0N/A public synchronized VirtualMachine createVirtualMachine(
0N/A Connection connection,
0N/A Process process) throws IOException {
0N/A
0N/A if (!connection.isOpen()) {
0N/A throw new IllegalStateException("connection is not open");
0N/A }
0N/A
0N/A VirtualMachine vm;
0N/A try {
0N/A vm = new VirtualMachineImpl(this, connection, process,
0N/A ++vmSequenceNumber);
0N/A } catch (VMDisconnectedException e) {
0N/A throw new IOException(e.getMessage());
0N/A }
0N/A targets.add(vm);
0N/A return vm;
0N/A }
0N/A
0N/A public VirtualMachine createVirtualMachine(Connection connection) throws IOException {
0N/A return createVirtualMachine(connection, null);
0N/A }
0N/A
0N/A public void addVirtualMachine(VirtualMachine vm) {
0N/A targets.add(vm);
0N/A }
0N/A
0N/A void disposeVirtualMachine(VirtualMachine vm) {
0N/A targets.remove(vm);
0N/A }
0N/A
0N/A public int majorInterfaceVersion() {
0N/A return majorVersion;
0N/A }
0N/A
0N/A public int minorInterfaceVersion() {
0N/A return minorVersion;
0N/A }
0N/A
0N/A ThreadGroup mainGroupForJDI() {
0N/A return mainGroupForJDI;
0N/A }
0N/A
0N/A String getString(String key) {
0N/A if (messages == null) {
0N/A messages = ResourceBundle.getBundle("com.sun.tools.jdi.resources.jdi");
0N/A }
0N/A return messages.getString(key);
0N/A }
0N/A
0N/A}