KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdi > internal > VirtualMachineManagerImpl


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdi.internal;
12
13
14 import java.io.FileOutputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.InputStream JavaDoc;
17 import java.io.OutputStream JavaDoc;
18 import java.io.PrintWriter JavaDoc;
19 import java.net.URL JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.MissingResourceException JavaDoc;
23 import java.util.PropertyResourceBundle JavaDoc;
24
25 import org.eclipse.jdi.internal.connect.SocketAttachingConnectorImpl;
26 import org.eclipse.jdi.internal.connect.SocketLaunchingConnectorImpl;
27 import org.eclipse.jdi.internal.connect.SocketListeningConnectorImpl;
28 import org.eclipse.jdi.internal.connect.SocketRawLaunchingConnectorImpl;
29 import org.eclipse.jdt.debug.core.JDIDebugModel;
30
31 import com.sun.jdi.VirtualMachine;
32 import com.sun.jdi.VirtualMachineManager;
33 import com.sun.jdi.connect.LaunchingConnector;
34 import com.sun.jdi.connect.spi.Connection;
35
36 /**
37  * this class implements the corresponding interfaces
38  * declared by the JDI specification. See the com.sun.jdi package
39  * for more information.
40  *
41  */

42 public class VirtualMachineManagerImpl implements VirtualMachineManager {
43     /** Major interface version. */
44     public static int MAJOR_INTERFACE_VERSION = 1;
45     /** Minor interface version. */
46     public static int MINOR_INTERFACE_VERSION = 5;
47     /** PrintWriter where verbose info is written to, null if no verbose must be given. */
48     private PrintWriter JavaDoc fVerbosePrintWriter = null;
49     /** List of all VMs that are currently connected. */
50     List JavaDoc fConnectedVMs = new ArrayList JavaDoc();
51     /** True if in verbose mode. */
52     private boolean fVerbose = false;
53     /** Name of verbose file. */
54     private String JavaDoc fVerboseFile = null;
55
56     /**
57      * Creates new VirtualMachineManagerImpl.
58      */

59     public VirtualMachineManagerImpl() {
60         
61         getPreferences();
62         
63         // See if verbose info must be given.
64
if (fVerbose) {
65             OutputStream JavaDoc out;
66             if (fVerboseFile != null && fVerboseFile.length() > 0) {
67                 try {
68                     out = new FileOutputStream JavaDoc(fVerboseFile);
69                 } catch (IOException JavaDoc e) {
70                     out = System.out;
71                     System.out.println(JDIMessages.VirtualMachineManagerImpl_Could_not_open_verbose_file___1 + fVerboseFile + JDIMessages.VirtualMachineManagerImpl_____2 + e); //
72
}
73             } else {
74                 out = System.out;
75             }
76             fVerbosePrintWriter = new PrintWriter JavaDoc(out);
77         }
78     }
79
80     /**
81      * Returns the major version number of the JDI interface.
82      */

83     public int majorInterfaceVersion() {
84         return MAJOR_INTERFACE_VERSION;
85     }
86     
87     /**
88      * Returns the minor version number of the JDI interface.
89      */

90     public int minorInterfaceVersion() {
91         return MINOR_INTERFACE_VERSION;
92     }
93     
94     /**
95      * Loads the user preferences from the jdi.ini file.
96      */

97     private void getPreferences() {
98         // Get jdi.ini info.
99
URL JavaDoc url = getClass().getResource("/jdi.ini"); //$NON-NLS-1$
100
if (url == null) {
101             return;
102         }
103             
104         try {
105             InputStream JavaDoc stream = url.openStream();
106             PropertyResourceBundle JavaDoc prefs = new PropertyResourceBundle JavaDoc(stream);
107             
108             try {
109                 fVerbose = Boolean.valueOf(prefs.getString("User.verbose")).booleanValue(); //$NON-NLS-1$
110
} catch (MissingResourceException JavaDoc e) {
111             }
112             
113             try {
114                 fVerboseFile = prefs.getString("Verbose.out"); //$NON-NLS-1$
115
} catch (MissingResourceException JavaDoc e) {
116             }
117
118         } catch (IOException JavaDoc e) {
119         }
120         
121     }
122
123     /**
124      * @return Returns Timeout value for requests to VM, if not overridden for the VM.
125      * This value is used to throw the exception TimeoutException in JDI calls.
126      * NOTE: This is not in compliance with the Sun's JDI.
127      */

128     public int getGlobalRequestTimeout() {
129         try {
130             if (JDIDebugModel.getPreferences() != null) {
131                 return JDIDebugModel.getPreferences().getInt(JDIDebugModel.PREF_REQUEST_TIMEOUT);
132             }
133             // JDI plug-in is not loaded
134
return JDIDebugModel.DEF_REQUEST_TIMEOUT;
135         } catch (NoClassDefFoundError JavaDoc e) {
136         }
137         // return the hard coded preference if the jdi debug plug-in does not exist
138
return 3000;
139     }
140     
141     /**
142      * Adds a VM to the connected VM list.
143      */

144     public void addConnectedVM(VirtualMachineImpl vm) {
145         fConnectedVMs.add(vm);
146     }
147
148     /**
149      * Removes a VM from the connected VM list.
150      */

151     public void removeConnectedVM(VirtualMachineImpl vm) {
152         fConnectedVMs.remove(vm);
153     }
154
155     /**
156      * @return Returns all target VMs which are connected to the debugger.
157      */

158     public List JavaDoc connectedVirtualMachines() {
159         return fConnectedVMs;
160     }
161
162     /**
163      * @return Returns all connectors.
164      */

165     public List JavaDoc allConnectors() {
166         List JavaDoc result = new ArrayList JavaDoc(attachingConnectors());
167         result.addAll(launchingConnectors());
168         result.addAll(listeningConnectors());
169         return result;
170     }
171
172     /**
173      * @return Returns attaching connectors.
174      */

175     public List JavaDoc attachingConnectors() {
176         ArrayList JavaDoc list = new ArrayList JavaDoc(1);
177         list.add(new SocketAttachingConnectorImpl(this));
178         return list;
179     }
180         
181     /**
182      * @return Returns launching connectors.
183      */

184     public List JavaDoc launchingConnectors() {
185         ArrayList JavaDoc list = new ArrayList JavaDoc(2);
186         list.add(new SocketLaunchingConnectorImpl(this));
187         list.add(new SocketRawLaunchingConnectorImpl(this));
188         return list;
189     }
190         
191     /**
192      * @return Returns listening connectors.
193      */

194     public List JavaDoc listeningConnectors() {
195         ArrayList JavaDoc list = new ArrayList JavaDoc(1);
196         list.add(new SocketListeningConnectorImpl(this));
197         return list;
198     }
199     
200     /**
201      * @return Returns default connector.
202      */

203     public LaunchingConnector defaultConnector() {
204         return new SocketLaunchingConnectorImpl(this);
205     }
206     
207     /**
208      * @return Returns PrintWriter to which verbose info must be written, or null if no verbose must be given.
209      */

210     public PrintWriter JavaDoc verbosePrintWriter() {
211         return fVerbosePrintWriter;
212     }
213     
214     public VirtualMachine createVirtualMachine(Connection connection) throws IOException JavaDoc {
215         VirtualMachineImpl vmImpl = new VirtualMachineImpl(connection);
216         return vmImpl;
217     }
218     
219     public VirtualMachine createVirtualMachine(Connection connection, Process JavaDoc process) throws IOException JavaDoc {
220         VirtualMachineImpl vmImpl = new VirtualMachineImpl(connection);
221         vmImpl.setLaunchedProcess(process);
222         return vmImpl;
223     }
224 }
225
Popular Tags