/** * Adds listener to monitor, and calls listener with any content monitor already has. * NOTE: This methods synchronises on monitor while listener is called. Listener may * not wait on any thread that waits for monitors monitor, what would result in dead-lock. */ public static void addAndNotifyStreamListener(IStreamMonitor monitor, IStreamListener listener) { // Synchronise on monitor to prevent writes to stream while we are adding listener. // It's weird to synchronise on monitor because that's a shared object, but that's // what ProcessConsole does. synchronized (monitor) { String contents = monitor.getContents(); if (!contents.isEmpty()) { // Call to unknown code while synchronising on monitor. This is dead-lock prone! // Listener must not wait for other threads that are waiting in line to // synchronise on monitor. listener.streamAppended(contents, monitor); } monitor.addListener(listener); } }
private void onAfterCodeServerStarted(DebugEvent event) { if (!(event.getSource() instanceof IProcess)) { return; } IProcess runtimeProcess = (IProcess) event.getSource(); final ILaunch launch = runtimeProcess.getLaunch(); IProcess[] processes = launch.getProcesses(); final IProcess process = processes[0]; // Look for the links in the sdm console output consoleStreamListenerCodeServer = new IStreamListener() { @Override public void streamAppended(String text, IStreamMonitor monitor) { displayCodeServerUrlInDevMode(launch, text); } }; // Listen to Console output streamMonitorCodeServer = process.getStreamsProxy().getOutputStreamMonitor(); streamMonitorCodeServer.addListener(consoleStreamListenerCodeServer); }
@Override public synchronized void flush() throws IOException { super.flush(); String cont; try { cont = this.toString(encoding); } catch (UnsupportedEncodingException ex) { DbgActivator.getDefault().logError("Encoding problem", ex); cont = this.toString(); } String appended = cont.substring(flushLength); flushLength = cont.length(); if (this.content != null) { this.reset(); flushLength = 0; } // System.out.println(oldLength + ":" + this.toString() + ":" + appended + ":" + this.listeners.size()); for (IStreamListener l : this.listeners) { l.streamAppended(appended, monitor); } }
/** * Executes the given command and returns a handle to the * process. Should only be used if access to {@link IProcess} * instance is desired. * * @param command * @param workingDirectory * @param monitor * @param envp * @param launchConfiguration * @param outStreamListener * @param errorStreamListener * @return the process * @throws CoreException */ public IProcess exec(String[] command, File workingDirectory, IProgressMonitor monitor, String[] envp, ILaunchConfiguration launchConfiguration, IStreamListener outStreamListener, IStreamListener errorStreamListener ) throws CoreException{ checkCommands(command); checkWorkingDirectory(workingDirectory); if(monitor == null ){ monitor = new NullProgressMonitor(); } if (envp == null && launchConfiguration != null ){ envp = DebugPlugin.getDefault().getLaunchManager().getEnvironment(launchConfiguration); } if (monitor.isCanceled()) { return null; } Process process = DebugPlugin.exec(command, workingDirectory, envp); Map<String, String> processAttributes = generateProcessAttributes(command, launchConfiguration); Launch launch = new Launch(launchConfiguration, "run", null); IProcess prcs = DebugPlugin.newProcess(launch, process, command[0], processAttributes); setTracing(command, outStreamListener, errorStreamListener, prcs); DebugPlugin.getDefault().getLaunchManager().addLaunch(launch); return prcs; }
private void setTracing(String[] command, IStreamListener outStreamListener, IStreamListener errorStreamListener, IProcess prcs) { if(HybridCore.DEBUG){ HybridCore.trace("Creating TracingStreamListeners for " + Arrays.toString(command)); outStreamListener = new TracingStreamListener(outStreamListener); errorStreamListener = new TracingStreamListener(outStreamListener); } if( outStreamListener != null ){ prcs.getStreamsProxy().getOutputStreamMonitor().addListener(outStreamListener); //See bug 121454. Ensure that output to fast processes is processed outStreamListener.streamAppended(prcs.getStreamsProxy().getOutputStreamMonitor().getContents(), null); } if( errorStreamListener != null ){ prcs.getStreamsProxy().getErrorStreamMonitor().addListener(errorStreamListener); //See bug 121454. Ensure that output to fast processes is processed errorStreamListener.streamAppended(prcs.getStreamsProxy().getErrorStreamMonitor().getContents(), null); } }
public synchronized void addListener(IStreamListener listener) { if (!this.listeners.contains(listener)) { this.listeners.add(listener); } try { this.content = this.toString(encoding); } catch (UnsupportedEncodingException ex) { this.content = this.toString(); } synchronized (this.lock) { this.lock.notify(); } }
@Override public synchronized void flush() { if (!isFlushing) { return; } String text = writer.toString(); int lastLinePos; final boolean flushOnlyFullLines = true; if (flushOnlyFullLines) { int pos = text.lastIndexOf('\n'); if (pos == -1) { // No full line in the buffer. return; } lastLinePos = pos + 1; } else { lastLinePos = text.length(); } String readyText = text.substring(0, lastLinePos); writer = new StringWriter(); if (lastLinePos != text.length()) { String rest = text.substring(lastLinePos); writer.append(rest); } for (IStreamListener listener : listeners) { listener.streamAppended(readyText, this); } }
private CordovaProjectCLI setupMockCLI(HybridProject project) throws CoreException { IProcess mockProcess = mock(IProcess.class); IStreamsProxy2 mockStreams = mock(IStreamsProxy2.class); CordovaProjectCLI mockCLI = spy(CordovaProjectCLI.newCLIforProject(mockProject)); doReturn(mockProcess).when(mockCLI).startShell(any(IStreamListener.class), any(IProgressMonitor.class), any(ILaunchConfiguration.class)); when(mockProcess.getStreamsProxy()).thenReturn(mockStreams); when(mockProcess.isTerminated()).thenReturn(Boolean.TRUE); doReturn(mockCLI).when(project).getProjectCLI(); return mockCLI; }
@Test public void testAdditionalEnvProperties() throws CoreException{ CordovaProjectCLI mockCLI = getMockCLI(); IProcess mockProcess = mock(IProcess.class); IStreamsProxy2 mockStreams = mock(IStreamsProxy2.class); setupMocks(mockCLI, mockProcess, mockStreams); ArgumentCaptor<ILaunchConfiguration> confCaptor = ArgumentCaptor.forClass(ILaunchConfiguration.class); mockCLI.prepare(new NullProgressMonitor(), "android"); verify(mockCLI).startShell(any(IStreamListener.class), any(IProgressMonitor.class), confCaptor.capture()); Map<String,String> attr = confCaptor.getValue().getAttribute(ILaunchManager.ATTR_ENVIRONMENT_VARIABLES, (Map<String,String>)null); assertEquals(EnvironmentPropsExt.ENV_VALUE, attr.get(EnvironmentPropsExt.ENV_KEY)); }
@Test public void testAdditionalEnvProperties() throws CoreException{ CordovaCLI mockCLI = getMockCLI(); IProcess mockProcess = mock(IProcess.class); IStreamsProxy2 mockStreams = mock(IStreamsProxy2.class); setupMocks(mockCLI, mockProcess, mockStreams); ArgumentCaptor<ILaunchConfiguration> confCaptor = ArgumentCaptor.forClass(ILaunchConfiguration.class); mockCLI.version(new NullProgressMonitor()); verify(mockCLI).startShell(any(IStreamListener.class), any(IProgressMonitor.class), confCaptor.capture()); Map<String,String> attr = confCaptor.getValue().getAttribute(ILaunchManager.ATTR_ENVIRONMENT_VARIABLES, (Map<String,String>)null); assertEquals(EnvironmentPropsExt.ENV_VALUE, attr.get(EnvironmentPropsExt.ENV_KEY)); }
public void logcat(String filter, IStreamListener outListener, IStreamListener errorListener, String serialNumber) throws CoreException{ ExternalProcessUtility processUtility = new ExternalProcessUtility(); StringBuilder command = new StringBuilder(getADBCommand()); command.append(" -s ").append(serialNumber); command.append(" logcat "); if(filter !=null && !filter.isEmpty()){ command.append(filter); } processUtility.execAsync(command.toString(), null, outListener, errorListener, null); }
/** * Convenience method to specify command line as a string for {@link #execAsync(String[], File, IStreamListener, IStreamListener, String[])} */ public void execAsync ( String commandLine, File workingDirectory, IStreamListener outStreamListener, IStreamListener errorStreamListener, String[] envp) throws CoreException{ checkCommandLine(commandLine); this.execAsync(DebugPlugin.parseArguments(commandLine), workingDirectory, outStreamListener, errorStreamListener, envp); }
/** * Executes the given commands synchronously. * * <p> * If the workingDirectory is null, the current directory for process is used. * </p> * @param command the command line can not be null or empty * @param workingDirectory working directory for the executed command, can be null * @param outStreamListener listener for output, can be null * @param errorStreamListene listener for error output, can be null * @param envp environment variables to set in the process, can be null * @param launchConfiguration the launch to add as part of this call, can be null * @return the exit code for the process * @throws CoreException if the execution fails */ public int execSync ( String[] command, File workingDirectory, IStreamListener outStreamListener, IStreamListener errorStreamListener, IProgressMonitor monitor, String[] envp, ILaunchConfiguration launchConfiguration) throws CoreException{ if(monitor == null){ monitor = new NullProgressMonitor(); } HybridCore.trace("Sync Execute command line: "+Arrays.toString(command)); IProcess prcs = exec(command, workingDirectory, monitor, envp, launchConfiguration,outStreamListener,errorStreamListener); if(prcs == null ){ return 0; } while (!prcs.isTerminated()) { try { if (monitor.isCanceled()) { prcs.terminate(); break; } Thread.sleep(50); } catch (InterruptedException e) { HybridCore.log(IStatus.INFO, "Exception waiting for process to terminate", e); } } return prcs.getExitValue(); }
/** * Convenience method to specify command line as a String * for {@link #execSync(String[], File, IStreamListener, IStreamListener, IProgressMonitor, String[], ILaunchConfiguration)} */ public int execSync ( String commandLine, File workingDirectory, IStreamListener outStreamListener, IStreamListener errorStreamListener, IProgressMonitor monitor, String[] envp, ILaunchConfiguration launchConfiguration) throws CoreException{ String[] cmd = DebugPlugin.parseArguments(commandLine); return this.execSync(cmd, workingDirectory, outStreamListener, errorStreamListener, monitor, envp, launchConfiguration); }
public IProcess startShell(final IStreamListener listener, final IProgressMonitor monitor, final ILaunchConfiguration launchConfiguration) throws CoreException{ ArrayList<String> commandList = new ArrayList<String>(); if(isWindows()){ commandList.add("cmd"); }else{ commandList.add("/bin/bash"); commandList.add("-l"); } ExternalProcessUtility ep = new ExternalProcessUtility(); IProcess process = ep.exec(commandList.toArray(new String[commandList.size()]), getWorkingDirectory(), monitor, null, launchConfiguration, listener, listener); return process; }
private void fireStreamAppend(String text) { synchronized (listeners) { for (IStreamListener oneListener : listeners) { oneListener.streamAppended(text, this); } } }
public void addMonListener(IConsole parserConsole, IStreamMonitor monitor, IStreamListener listener){ if (parserListeners==null){ MessageUI.error("BUG in addMonListener(): parserListeners=null - enable breakpoints"); System.out.println("BUG in addMonListener(): parserListeners=null"); return; } // synchronized (parserListeners){ parserListeners.put(parserConsole, new MonListener(monitor, listener)); // java.lang.NullPointerException // } }
@Override public void shellOutputChanged(IHostShellChangeEvent event) { IHostOutput[] lines = event.getLines(); StringBuilder buf = new StringBuilder(); for (IHostOutput line : lines) { String lineContent = line.getString(); if (lineContent.length() == 0) { continue; } buf.append(lineContent); buf.append(System.getProperty("line.separator")); } String newContent = null; if (buf.length() != 0) { newContent = buf.toString(); contents.append(newContent); } else { newContent = ""; } for (Object listener : listeners.getListeners()) { IStreamListener streamListener = (IStreamListener) listener; streamListener.streamAppended(newContent, this); } }
@Override @SuppressWarnings("unchecked") public void addListener(IStreamListener listener) { list.add(listener); }
@Override public void removeListener(IStreamListener listener) { list.remove(listener); }
@Override public void addListener(IStreamListener listener) { listeners.add(listener); }
@Override public void removeListener(IStreamListener listener) { listeners.remove(listener); }
protected void fireEvent(String newText) { contentsBuffer.append(newText); for (IStreamListener listener : listeners) { listener.streamAppended(newText, this); } }
@Override public void addListener(IStreamListener listener) { this.stream.addListener(listener); }
@Override public void removeListener(IStreamListener listener) { this.stream.removeListener(listener); }
public synchronized void removeListener(IStreamListener listener) { this.listeners.remove(listener); }
public void addListener(IStreamListener listener) { }
public void removeListener(IStreamListener listener) { }
public synchronized void addListener(IStreamListener listener) { listeners.add(listener); }
public synchronized void removeListener(IStreamListener listener) { listeners.remove(listener); }
private void setupMocks(CordovaCLI mockCLI, IProcess mockProcess, IStreamsProxy2 mockStreams) throws CoreException { doReturn(mockProcess).when(mockCLI).startShell(any(IStreamListener.class), any(IProgressMonitor.class),any(ILaunchConfiguration.class)); when(mockProcess.getStreamsProxy()).thenReturn(mockStreams); when(mockProcess.isTerminated()).thenReturn(Boolean.TRUE); }
public TracingStreamListener(IStreamListener delegate){ this.delegate = delegate; }
@Override public void addListener(IStreamListener listener) { synchronized (listeners) { listeners.add(listener); } }
@Override public void removeListener(IStreamListener listener) { synchronized (listeners) { listeners.remove(listener); } }
public IStreamListener getListener() { return listener; }
public MonListener(IStreamMonitor monitor, IStreamListener listener) { super(); this.monitor = monitor; this.listener = listener; }
@Override public void addListener(IStreamListener listener) { }