⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 emulatorrunner.java

📁 eclipseme的最新版本的source,欢迎j2me程序员使用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			((AttachingConnector) connector).attach(map) :
			waitForDebuggerConnection(
					(ListeningConnector) connector, p, process, map, monitor);
		return vm;
	}

	/**
	 * Get the appropriate Connector dependent on what
	 * the IEmulator instance requires.
	 * 
	 * @return
	 */
	private Connector getConnector() {
		Connector connector = (device.isDebugServer()) ?
			(Connector) getAttachingConnector() :
			(Connector) getListeningConnector();
		return connector;
	}

	/**
	 * Wait for the debugger to connect to our connector
	 * and return the new VirtualMachine.
	 * 
	 * @param connector
	 * @param p
	 * @param process
	 * @param map
	 * @param monitor
	 * @return
	 * @throws CoreException
	 * @throws IOException
	 * @throws IllegalConnectorArgumentsException
	 */
	private VirtualMachine waitForDebuggerConnection(
			ListeningConnector connector,
			Process p,
			IProcess process,
			Map map,
			IProgressMonitor monitor) 
				throws CoreException, IOException, 
				IllegalConnectorArgumentsException
	{
		VirtualMachine vm = null;
		
		boolean retry = false;
		do  {
			try {
				ConnectRunnable runnable = 
					new ConnectRunnable(connector, map);
				Thread connectThread = 
					new Thread(runnable, "Listening Connector");
				connectThread.start();
				
				while (connectThread.isAlive()) {
					if (monitor.isCanceled()) {
						connector.stopListening(map);
						p.destroy();
						
						break;
					}
					try {
						p.exitValue();
						// process has terminated - stop waiting for a connection
						try {
							connector.stopListening(map); 
						} catch (IOException e) {
							// expected
						}
						checkErrorMessage(process);
					} catch (IllegalThreadStateException e) {
						// expected while process is alive
					}
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
					}
				}

				Exception ex = runnable.getException();
				
				if (ex instanceof IllegalConnectorArgumentsException) {
					throw (IllegalConnectorArgumentsException)ex;
				}
				if (ex instanceof InterruptedIOException) {
					throw (InterruptedIOException)ex;
				}
				if (ex instanceof IOException) {
					throw (IOException)ex;
				}
				
				vm = runnable.getVirtualMachine();
				
				break;
			} catch (InterruptedIOException e) {
				
				checkErrorMessage(process);
				
				// timeout, consult status handler if there is one
				IStatus status = new Status(
					IStatus.ERROR, 
					IEclipseMECoreConstants.PLUGIN_ID, 
					IJavaLaunchConfigurationConstants.ERR_VM_CONNECT_TIMEOUT, 
					"", e);
				IStatusHandler handler = DebugPlugin.getDefault().getStatusHandler(status);
				
				retry = false;
				if (handler == null) {
					// if there is no handler, throw the exception
					throw new CoreException(status);
				} else {
					Object result = handler.handleStatus(status, this);
					if (result instanceof Boolean) {
						retry = ((Boolean)result).booleanValue();
					}
				} 
			}
		} while (retry);
		
		return vm;
	}

	/**
	 * Run the emulator without debugging.
	 * 
	 * @param vmRunnerConfig
	 * @param launchConfig
	 * @param launch
	 * @param monitor
	 * @throws CoreException
	 */
	public void runWithoutDebug(
		VMRunnerConfiguration vmRunnerConfig,
		ILaunchConfiguration launchConfig,
		ILaunch launch,
		IProgressMonitor monitor)
			throws CoreException 
	{

		if (monitor == null) {
			monitor = new NullProgressMonitor();
		}
		
		IProgressMonitor subMonitor = new SubProgressMonitor(monitor, 1);
		subMonitor.beginTask(EclipseMECoreStrings.getString("debugvmrunner.launching_vm"), 3);
		
		// check for cancellation
		if (monitor.isCanceled()) {
			return;
		}		
		
		subMonitor.subTask(EclipseMECoreStrings.getString("debugvmrunner.constructing_cmd_line"));

		String[] cmdLine = getCommandLine(launchConfig, -1, monitor);
		Utils.dumpCommandLine(cmdLine);
			
		// check for cancellation
		if (monitor.isCanceled()) {
			return;
		}		
		
		subMonitor.worked(1);
		subMonitor.subTask(EclipseMECoreStrings.getString("debugvmrunner.starting_VM"));
		
		// check for cancellation
		if (monitor.isCanceled()) {
			return;
		}				
		
		File workingDir = getWorkingDir(vmRunnerConfig);
		Process p = exec(cmdLine, workingDir);				
		if (p == null) {
			return;
		}
		
		// check for cancellation
		if (monitor.isCanceled()) {
			p.destroy();
			return;
		}				
		
		IProcess process = DebugPlugin.newProcess(
			launch, p, 
			renderProcessLabel(cmdLine), 
			getDefaultProcessMap());
		process.setAttribute(
			IProcess.ATTR_CMDLINE, 
			renderCommandLine(cmdLine));
		
		subMonitor.worked(1);
	}

	/**
	 * @see org.eclipse.jdt.launching.AbstractVMRunner#getPluginIdentifier()
	 */
	protected String getPluginIdentifier() {
		return IEclipseMECoreConstants.PLUGIN_ID;
	}
	
	/**
	 * Add the specified arguments array to the list of arguments.
	 * 
	 * @param args
	 * @param allArgs
	 */
	protected void addArguments(String[] args, List allArgs) {
		if (args != null) {
			for (int i= 0; i < args.length; i++) {
				allArgs.add(args[i]);
			}
		}
	}
	
	/**
	 * Check for an error message and throw an exception as necessary.
	 * 
	 * @param process
	 * @throws CoreException
	 */	
	protected void checkErrorMessage(IProcess process) throws CoreException {
		String errorMessage = process.getStreamsProxy().getErrorStreamMonitor().getContents();
		
		if (errorMessage.length() == 0) {
			errorMessage = process.getStreamsProxy().getOutputStreamMonitor().getContents();
		}
		
		if (errorMessage.length() != 0) {
			abort(errorMessage, null, IJavaLaunchConfigurationConstants.ERR_VM_LAUNCH_ERROR);
		}										
	}

	/**
	 * Get the appropriate JDI AttachingConnector instance.
	 * 
	 * @return
	 */
	private AttachingConnector getAttachingConnector() {
		AttachingConnector connector = null;
		
		List connectors = 
			Bootstrap.virtualMachineManager().attachingConnectors();
			
		for (int i= 0; i < connectors.size(); i++) {
			AttachingConnector c = (AttachingConnector) connectors.get(i);
			if ("com.sun.jdi.SocketAttach".equals(c.name()))
				connector = c;
		}
		
		return connector;
	}
	
	/**
	 * Get the appropriate JDI ListenerConnector instance.
	 * 
	 * @return
	 */
	private ListeningConnector getListeningConnector() {
		ListeningConnector connector = null;
		
		List connectors = Bootstrap.virtualMachineManager().listeningConnectors();
			
		for (int i= 0; i < connectors.size(); i++) {
			ListeningConnector c = (ListeningConnector) connectors.get(i);
			if ("com.sun.jdi.SocketListen".equals(c.name()))
				connector = c;
		}
		
		return connector;
	}
	
	/**
	 * Returns the working directory to use for the launched VM,
	 * or <code>null</code> if the working directory is to be inherited
	 * from the current process.
	 * 
	 * @return the working directory to use
	 * @exception CoreException if the working directory specified by
	 *  the configuration does not exist or is not a directory
	 */	
	private File getWorkingDir(VMRunnerConfiguration config) 
		throws CoreException 
	{
		File dir = null;
		
		String path = null;
		if (device instanceof IDevice2) {
			File deviceWorkingDirectory = ((IDevice2) device).getWorkingDirectory();
			if ((deviceWorkingDirectory != null) && deviceWorkingDirectory.exists()) {
				path = deviceWorkingDirectory.getPath();
			}
		}
		
		if (path == null) {
			path = config.getWorkingDirectory();
		}
		
		if (path != null) {
			dir = new File(path);
			if (!dir.isDirectory()) {
				abort(EclipseMECoreStrings.getString(
						"debugvmrunner.workingdir_not_dir",
						new String[] {path}),
					null,
					IJavaLaunchConfigurationConstants.ERR_WORKING_DIRECTORY_DOES_NOT_EXIST);
			}
		}
		
		return dir;
	}
	
	/**
	 * Specify new connector arguments to the JDI connector.
	 * 
	 * @param map
	 * @param portNumber
	 */
	private void specifyArguments(Map map, int portNumber) {
		Connector.IntegerArgument port = 
			(Connector.IntegerArgument) map.get("port");
		port.setValue(portNumber);
		
		Connector.IntegerArgument timeoutArg = 
			(Connector.IntegerArgument) map.get("timeout");
		if (timeoutArg != null) {
			int timeout = 
				JavaRuntime.getPreferences().getInt(JavaRuntime.PREF_CONNECT_TIMEOUT);
			timeoutArg.setValue(timeout);
		}
	}

	/**
	 * @param monitor 
	 * @see eclipseme.core.internal.launching.EmulatorRunner#getCommandLine(org.eclipse.jdt.launching.VMRunnerConfiguration, int)
	 */
	protected String[] getCommandLine(ILaunchConfiguration config, int port, IProgressMonitor monitor) 
		throws CoreException 
	{
		LaunchEnvironment launchEnvironment = new LaunchEnvironment();
		launchEnvironment.setDebugLaunch(debugMode);
		launchEnvironment.setDebugListenerPort(port);
		launchEnvironment.setLaunchConfiguration(config);
		launchEnvironment.setMidletSuite(suite);
		
		String commandLineString = device.getLaunchCommand(launchEnvironment, monitor);
		ExecutionArguments execArgs = new ExecutionArguments("", commandLineString);
		String[] cmdLine = execArgs.getProgramArgumentsArray();
		
		return cmdLine;
	}

	/**
	 * @see org.eclipse.jdt.launching.IVMRunner#run(org.eclipse.jdt.launching.VMRunnerConfiguration, org.eclipse.debug.core.ILaunch, org.eclipse.core.runtime.IProgressMonitor)
	 */
	public void run(VMRunnerConfiguration configuration, ILaunch launch, IProgressMonitor monitor) 
		throws CoreException 
	{
		// Method provided to meet the superclass requirement.  Is not called.
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -