001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdi.internal;
011:
012: import java.io.FileOutputStream;
013: import java.io.IOException;
014: import java.io.InputStream;
015: import java.io.OutputStream;
016: import java.io.PrintWriter;
017: import java.net.URL;
018: import java.util.ArrayList;
019: import java.util.List;
020: import java.util.MissingResourceException;
021: import java.util.PropertyResourceBundle;
022:
023: import org.eclipse.jdi.internal.connect.SocketAttachingConnectorImpl;
024: import org.eclipse.jdi.internal.connect.SocketLaunchingConnectorImpl;
025: import org.eclipse.jdi.internal.connect.SocketListeningConnectorImpl;
026: import org.eclipse.jdi.internal.connect.SocketRawLaunchingConnectorImpl;
027: import org.eclipse.jdt.debug.core.JDIDebugModel;
028:
029: import com.sun.jdi.VirtualMachine;
030: import com.sun.jdi.VirtualMachineManager;
031: import com.sun.jdi.connect.LaunchingConnector;
032: import com.sun.jdi.connect.spi.Connection;
033:
034: /**
035: * this class implements the corresponding interfaces
036: * declared by the JDI specification. See the com.sun.jdi package
037: * for more information.
038: *
039: */
040: public class VirtualMachineManagerImpl implements VirtualMachineManager {
041: /** Major interface version. */
042: public static int MAJOR_INTERFACE_VERSION = 1;
043: /** Minor interface version. */
044: public static int MINOR_INTERFACE_VERSION = 5;
045: /** PrintWriter where verbose info is written to, null if no verbose must be given. */
046: private PrintWriter fVerbosePrintWriter = null;
047: /** List of all VMs that are currently connected. */
048: List fConnectedVMs = new ArrayList();
049: /** True if in verbose mode. */
050: private boolean fVerbose = false;
051: /** Name of verbose file. */
052: private String fVerboseFile = null;
053:
054: /**
055: * Creates new VirtualMachineManagerImpl.
056: */
057: public VirtualMachineManagerImpl() {
058:
059: getPreferences();
060:
061: // See if verbose info must be given.
062: if (fVerbose) {
063: OutputStream out;
064: if (fVerboseFile != null && fVerboseFile.length() > 0) {
065: try {
066: out = new FileOutputStream(fVerboseFile);
067: } catch (IOException e) {
068: out = System.out;
069: System.out
070: .println(JDIMessages.VirtualMachineManagerImpl_Could_not_open_verbose_file___1
071: + fVerboseFile
072: + JDIMessages.VirtualMachineManagerImpl_____2
073: + e); //
074: }
075: } else {
076: out = System.out;
077: }
078: fVerbosePrintWriter = new PrintWriter(out);
079: }
080: }
081:
082: /**
083: * Returns the major version number of the JDI interface.
084: */
085: public int majorInterfaceVersion() {
086: return MAJOR_INTERFACE_VERSION;
087: }
088:
089: /**
090: * Returns the minor version number of the JDI interface.
091: */
092: public int minorInterfaceVersion() {
093: return MINOR_INTERFACE_VERSION;
094: }
095:
096: /**
097: * Loads the user preferences from the jdi.ini file.
098: */
099: private void getPreferences() {
100: // Get jdi.ini info.
101: URL url = getClass().getResource("/jdi.ini"); //$NON-NLS-1$
102: if (url == null) {
103: return;
104: }
105:
106: try {
107: InputStream stream = url.openStream();
108: PropertyResourceBundle prefs = new PropertyResourceBundle(
109: stream);
110:
111: try {
112: fVerbose = Boolean.valueOf(
113: prefs.getString("User.verbose")).booleanValue(); //$NON-NLS-1$
114: } catch (MissingResourceException e) {
115: }
116:
117: try {
118: fVerboseFile = prefs.getString("Verbose.out"); //$NON-NLS-1$
119: } catch (MissingResourceException e) {
120: }
121:
122: } catch (IOException e) {
123: }
124:
125: }
126:
127: /**
128: * @return Returns Timeout value for requests to VM, if not overridden for the VM.
129: * This value is used to throw the exception TimeoutException in JDI calls.
130: * NOTE: This is not in compliance with the Sun's JDI.
131: */
132: public int getGlobalRequestTimeout() {
133: try {
134: if (JDIDebugModel.getPreferences() != null) {
135: return JDIDebugModel.getPreferences().getInt(
136: JDIDebugModel.PREF_REQUEST_TIMEOUT);
137: }
138: // JDI plug-in is not loaded
139: return JDIDebugModel.DEF_REQUEST_TIMEOUT;
140: } catch (NoClassDefFoundError e) {
141: }
142: // return the hard coded preference if the jdi debug plug-in does not exist
143: return 3000;
144: }
145:
146: /**
147: * Adds a VM to the connected VM list.
148: */
149: public void addConnectedVM(VirtualMachineImpl vm) {
150: fConnectedVMs.add(vm);
151: }
152:
153: /**
154: * Removes a VM from the connected VM list.
155: */
156: public void removeConnectedVM(VirtualMachineImpl vm) {
157: fConnectedVMs.remove(vm);
158: }
159:
160: /**
161: * @return Returns all target VMs which are connected to the debugger.
162: */
163: public List connectedVirtualMachines() {
164: return fConnectedVMs;
165: }
166:
167: /**
168: * @return Returns all connectors.
169: */
170: public List allConnectors() {
171: List result = new ArrayList(attachingConnectors());
172: result.addAll(launchingConnectors());
173: result.addAll(listeningConnectors());
174: return result;
175: }
176:
177: /**
178: * @return Returns attaching connectors.
179: */
180: public List attachingConnectors() {
181: ArrayList list = new ArrayList(1);
182: list.add(new SocketAttachingConnectorImpl(this ));
183: return list;
184: }
185:
186: /**
187: * @return Returns launching connectors.
188: */
189: public List launchingConnectors() {
190: ArrayList list = new ArrayList(2);
191: list.add(new SocketLaunchingConnectorImpl(this ));
192: list.add(new SocketRawLaunchingConnectorImpl(this ));
193: return list;
194: }
195:
196: /**
197: * @return Returns listening connectors.
198: */
199: public List listeningConnectors() {
200: ArrayList list = new ArrayList(1);
201: list.add(new SocketListeningConnectorImpl(this ));
202: return list;
203: }
204:
205: /**
206: * @return Returns default connector.
207: */
208: public LaunchingConnector defaultConnector() {
209: return new SocketLaunchingConnectorImpl(this );
210: }
211:
212: /**
213: * @return Returns PrintWriter to which verbose info must be written, or null if no verbose must be given.
214: */
215: public PrintWriter verbosePrintWriter() {
216: return fVerbosePrintWriter;
217: }
218:
219: public VirtualMachine createVirtualMachine(Connection connection)
220: throws IOException {
221: VirtualMachineImpl vmImpl = new VirtualMachineImpl(connection);
222: return vmImpl;
223: }
224:
225: public VirtualMachine createVirtualMachine(Connection connection,
226: Process process) throws IOException {
227: VirtualMachineImpl vmImpl = new VirtualMachineImpl(connection);
228: vmImpl.setLaunchedProcess(process);
229: return vmImpl;
230: }
231: }
|