use of org.eclipse.core.runtime.SubMonitor in project eclipse.platform.text by eclipse.
the class TextFileBufferOperation method run.
@Override
public void run(IFileBuffer fileBuffer, IProgressMonitor progressMonitor) throws CoreException, OperationCanceledException {
if (fileBuffer instanceof ITextFileBuffer) {
ITextFileBuffer textFileBuffer = (ITextFileBuffer) fileBuffer;
IPath path = textFileBuffer.getLocation();
String taskName = path == null ? getOperationName() : path.lastSegment();
SubMonitor subMonitor = SubMonitor.convert(progressMonitor, taskName, 100);
MultiTextEditWithProgress edit = computeTextEdit(textFileBuffer, subMonitor.split(10));
if (edit != null) {
Map<String, IDocumentPartitioner> stateData = startRewriteSession(textFileBuffer);
try {
applyTextEdit(textFileBuffer, edit, subMonitor.split(90));
} finally {
stopRewriteSession(textFileBuffer, stateData);
}
}
}
}
use of org.eclipse.core.runtime.SubMonitor in project linuxtools by eclipse.
the class SystemTapLaunchConfigurationDelegate method launch.
@Override
public void launch(ILaunchConfiguration config, String mode, ILaunch launch, IProgressMonitor m) throws CoreException {
if (m == null) {
m = new NullProgressMonitor();
}
SubMonitor monitor = SubMonitor.convert(m, "SystemTap runtime monitor", // $NON-NLS-1$
5);
initialize();
// check for cancellation
if (monitor.isCanceled()) {
return;
}
/*
* Set variables
*/
if (!config.getAttribute(LaunchConfigurationConstants.ARGUMENTS, LaunchConfigurationConstants.DEFAULT_ARGUMENTS).equals(LaunchConfigurationConstants.DEFAULT_ARGUMENTS)) {
arguments = config.getAttribute(LaunchConfigurationConstants.ARGUMENTS, LaunchConfigurationConstants.DEFAULT_ARGUMENTS);
needsArguments = true;
}
if (!config.getAttribute(LaunchConfigurationConstants.BINARY_PATH, LaunchConfigurationConstants.DEFAULT_BINARY_PATH).equals(LaunchConfigurationConstants.DEFAULT_BINARY_PATH)) {
binaryPath = config.getAttribute(LaunchConfigurationConstants.BINARY_PATH, LaunchConfigurationConstants.DEFAULT_BINARY_PATH);
needsBinary = true;
}
if (!config.getAttribute(LaunchConfigurationConstants.BINARY_ARGUMENTS, LaunchConfigurationConstants.DEFAULT_BINARY_ARGUMENTS).equals(LaunchConfigurationConstants.DEFAULT_BINARY_ARGUMENTS)) {
binaryArguments = config.getAttribute(LaunchConfigurationConstants.BINARY_ARGUMENTS, LaunchConfigurationConstants.DEFAULT_BINARY_ARGUMENTS);
}
if (!config.getAttribute(LaunchConfigurationConstants.SCRIPT_PATH, LaunchConfigurationConstants.DEFAULT_SCRIPT_PATH).equals(LaunchConfigurationConstants.DEFAULT_SCRIPT_PATH)) {
scriptPath = config.getAttribute(LaunchConfigurationConstants.SCRIPT_PATH, LaunchConfigurationConstants.DEFAULT_SCRIPT_PATH);
}
// Generate script if needed
if (config.getAttribute(LaunchConfigurationConstants.NEED_TO_GENERATE, LaunchConfigurationConstants.DEFAULT_NEED_TO_GENERATE)) {
temporaryScript = new File(scriptPath);
temporaryScript.delete();
try {
temporaryScript.createNewFile();
try (FileWriter fstream = new FileWriter(temporaryScript);
BufferedWriter out = new BufferedWriter(fstream)) {
out.write(config.getAttribute(LaunchConfigurationConstants.GENERATED_SCRIPT, LaunchConfigurationConstants.DEFAULT_GENERATED_SCRIPT));
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
stap = config.getAttribute(LaunchConfigurationConstants.COMMAND, PluginConstants.STAP_PATH);
/**
* Generate partial command
*/
partialCommand = ConfigurationOptionsSetter.setOptions(config);
outputPath = config.getAttribute(LaunchConfigurationConstants.OUTPUT_PATH, PluginConstants.getDefaultOutput());
boolean fileExists = true;
try {
// Make sure the output file exists
File tempFile = new File(outputPath);
tempFile.delete();
fileExists = tempFile.createNewFile();
} catch (IOException e1) {
fileExists = false;
}
// check for cancellation
if (!fileExists || monitor.isCanceled()) {
SystemTapUIErrorMessages mess = new // $NON-NLS-1$
SystemTapUIErrorMessages(// $NON-NLS-1$
Messages.getString("SystemTapLaunchConfigurationDelegate.0"), Messages.getString("SystemTapLaunchConfigurationDelegate.1"), // $NON-NLS-1$ //$NON-NLS-2$
Messages.getString("SystemTapLaunchConfigurationDelegate.2") + outputPath + // $NON-NLS-1$
Messages.getString("SystemTapLaunchConfigurationDelegate.3"));
mess.schedule();
return;
}
finishLaunch(launch, config, m);
}
use of org.eclipse.core.runtime.SubMonitor in project linuxtools by eclipse.
the class RunImageCommandHandler method runImage.
/**
* Run the given {@link IDockerImage} with the given settings
*
* @param image
* @param containerConfig
* @param hostConfig
* @param containerName
* @param removeWhenExits
*/
public static void runImage(final IDockerImage image, final IDockerContainerConfig containerConfig, final IDockerHostConfig hostConfig, final String containerName, final boolean removeWhenExits) {
final IDockerConnection connection = image.getConnection();
if (containerConfig.tty()) {
// show the console view
Display.getDefault().asyncExec(() -> {
try {
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(IConsoleConstants.ID_CONSOLE_VIEW);
} catch (Exception e) {
Activator.log(e);
}
});
}
// Create the container in a non-UI thread.
final Job runImageJob = new Job(// $NON-NLS-1$
DVMessages.getString("RunImageCreateContainer.job")) {
@Override
protected IStatus run(final IProgressMonitor monitor) {
monitor.beginTask(DVMessages.getString("RunImageRunningTask.msg"), // $NON-NLS-1$
2);
String containerId = null;
RunConsole console = null;
try {
final SubMonitor createContainerMonitor = SubMonitor.convert(monitor, 1);
// create the container
createContainerMonitor.beginTask(DVMessages.getString(// $NON-NLS-1$
"RunImageCreatingContainerTask.msg"), 1);
containerId = ((DockerConnection) connection).createContainer(containerConfig, hostConfig, containerName);
final IDockerContainer container = ((DockerConnection) connection).getContainer(containerId);
createContainerMonitor.done();
// abort if operation was cancelled
if (monitor.isCanceled()) {
return Status.CANCEL_STATUS;
}
// start the container
final SubMonitor startContainerMonitor = SubMonitor.convert(monitor, 1);
startContainerMonitor.beginTask(DVMessages.getString("RunImageStartingContainerTask.msg"), // $NON-NLS-1$
1);
console = getRunConsole(connection, container);
if (console != null) {
// if we are auto-logging, show the console
console.showConsole();
((DockerConnection) connection).startContainer(containerId, console.getOutputStream());
} else {
((DockerConnection) connection).startContainer(containerId, null);
}
startContainerMonitor.done();
// create a launch configuration from the container
LaunchConfigurationUtils.createRunImageLaunchConfiguration(image, containerConfig, hostConfig, containerName, removeWhenExits);
} catch (final DockerException | InterruptedException e) {
if (console != null) {
RunConsole.removeConsole(console);
}
Display.getDefault().syncExec(() -> MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), DVMessages.getFormattedString(ERROR_CREATING_CONTAINER, containerConfig.image()), e.getMessage()));
} finally {
final String tmpContainerId = containerId;
if (removeWhenExits) {
final Job waitContainerJob = new Job(CommandMessages.getString(// $NON-NLS-1$
"RunImageCommandHandler.waitContainer.label")) {
@Override
protected IStatus run(IProgressMonitor monitor) {
try {
if (tmpContainerId != null) {
// Wait for the container to finish
((DockerConnection) connection).waitForContainer(tmpContainerId);
// Drain the logging thread before we remove the
// container
((DockerConnection) connection).stopLoggingThread(tmpContainerId);
while (((DockerConnection) connection).loggingStatus(tmpContainerId) == EnumDockerLoggingStatus.LOGGING_ACTIVE) {
Thread.sleep(1000);
}
}
} catch (DockerContainerNotFoundException e) {
// container not created correctly
return Status.OK_STATUS;
} catch (DockerException | InterruptedException e) {
// ignore any errors in waiting for container or
// draining log
}
try {
// try and remove the container if it was created
if (tmpContainerId != null)
((DockerConnection) connection).removeContainer(tmpContainerId);
} catch (DockerException | InterruptedException e) {
final String id = tmpContainerId;
Display.getDefault().syncExec(() -> MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), DVMessages.getFormattedString(ERROR_REMOVING_CONTAINER, id), e.getMessage()));
}
return Status.OK_STATUS;
}
};
// Do not display this job in the UI
waitContainerJob.setSystem(true);
waitContainerJob.schedule();
}
monitor.done();
}
return Status.OK_STATUS;
}
};
runImageJob.schedule();
}
use of org.eclipse.core.runtime.SubMonitor in project linuxtools by eclipse.
the class ValgrindLaunchConfigurationDelegate method launch.
@Override
public void launch(ILaunchConfiguration config, String mode, ILaunch launch, IProgressMonitor m) throws CoreException {
if (m == null) {
m = new NullProgressMonitor();
}
// $NON-NLS-1$
SubMonitor monitor = SubMonitor.convert(m, Messages.getString("ValgrindLaunchConfigurationDelegate.Profiling_Local_CCPP_Application"), 10);
// check for cancellation
if (monitor.isCanceled()) {
return;
}
this.config = config;
this.launch = launch;
try {
IProject project = CDebugUtils.verifyCProject(config).getProject();
ValgrindUIPlugin.getDefault().setProfiledProject(project);
command = getValgrindCommand();
// remove any output from previous run
ValgrindUIPlugin.getDefault().resetView();
// reset stored launch data
getPlugin().setCurrentLaunchConfiguration(null);
getPlugin().setCurrentLaunch(null);
String valgrindCommand = getValgrindCommand().getValgrindCommand();
// also ensure Valgrind version is usable
try {
valgrindVersion = getPlugin().getValgrindVersion(project);
} catch (CoreException e) {
// if versioning failed, issue an error dialog and return
errorDialog(// $NON-NLS-1$
Messages.getString("ValgrindLaunchConfigurationDelegate.Valgrind_version_failed_msg"), e.getMessage());
return;
}
monitor.worked(1);
IPath exePath = CDebugUtils.verifyProgramPath(config);
String[] arguments = getProgramArgumentsArray(config);
File workDir = getWorkingDirectory(config);
if (workDir == null) {
// $NON-NLS-1$ //$NON-NLS-2$
workDir = new File(System.getProperty("user.home", "."));
}
// set output directory in config
IValgrindOutputDirectoryProvider provider = getPlugin().getOutputDirectoryProvider();
setOutputPath(config, provider.getOutputPath());
outputPath = verifyOutputPath(config);
// create/empty output directory
createDirectory(outputPath);
// tool that was launched
toolID = getTool(config);
// ask tool extension for arguments
dynamicDelegate = getDynamicDelegate(toolID);
String[] opts = getValgrindArgumentsArray(config);
// set the default source locator if required
setDefaultSourceLocator(launch, config);
ArrayList<String> cmdLine = new ArrayList<>(1 + arguments.length);
cmdLine.add(valgrindCommand);
cmdLine.addAll(Arrays.asList(opts));
cmdLine.add(exePath.toPortableString());
cmdLine.addAll(Arrays.asList(arguments));
String[] commandArray = cmdLine.toArray(new String[cmdLine.size()]);
boolean usePty = config.getAttribute(ICDTLaunchConfigurationConstants.ATTR_USE_TERMINAL, ICDTLaunchConfigurationConstants.USE_TERMINAL_DEFAULT);
monitor.worked(1);
// check for cancellation
if (monitor.isCanceled()) {
return;
}
// call Valgrind
command.execute(commandArray, getEnvironment(config), workDir, usePty, project);
monitor.worked(3);
process = createNewProcess(launch, command.getProcess(), commandArray[0]);
// set the command line used
process.setAttribute(IProcess.ATTR_CMDLINE, command.getCommandLine());
while (!process.isTerminated()) {
Thread.sleep(100);
}
// store these for use by other classes
getPlugin().setCurrentLaunchConfiguration(config);
getPlugin().setCurrentLaunch(launch);
// parse Valgrind logs
IValgrindMessage[] messages = parseLogs(outputPath);
// create launch summary string to distinguish this launch
launchStr = createLaunchStr();
// create view
ValgrindUIPlugin.getDefault().createView(launchStr, toolID);
// set log messages
ValgrindViewPart view = ValgrindUIPlugin.getDefault().getView();
view.setMessages(messages);
monitor.worked(1);
// pass off control to extender
dynamicDelegate.handleLaunch(config, launch, outputPath, monitor.newChild(2));
// initialize tool-specific part of view
dynamicDelegate.initializeView(view.getDynamicView(), launchStr, monitor.newChild(1));
// refresh view
ValgrindUIPlugin.getDefault().refreshView();
// show view
ValgrindUIPlugin.getDefault().showView();
// set up resource listener for post-build events.
ResourcesPlugin.getWorkspace().addResourceChangeListener(new ProjectBuildListener(project), IResourceChangeEvent.POST_BUILD);
monitor.worked(1);
} catch (IOException e) {
// $NON-NLS-1$
abort(Messages.getString("ValgrindLaunchConfigurationDelegate.Error_starting_process"), e, ICDTLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
m.done();
}
}
use of org.eclipse.core.runtime.SubMonitor in project linuxtools by eclipse.
the class ValgrindRemoteProxyLaunchDelegate method launch.
@Override
public void launch(final ILaunchConfiguration config, String mode, final ILaunch launch, IProgressMonitor m) throws CoreException {
if (m == null) {
m = new NullProgressMonitor();
}
// Clear process as we wait on it to be instantiated
process = null;
SubMonitor monitor = SubMonitor.convert(m, Messages.getString("ValgrindRemoteLaunchDelegate.task_name"), // $NON-NLS-1$
10);
// check for cancellation
if (monitor.isCanceled()) {
return;
}
this.config = config;
this.launch = launch;
try {
// remove any output from previous run
ValgrindUIPlugin.getDefault().resetView();
// reset stored launch data
getPlugin().setCurrentLaunchConfiguration(null);
getPlugin().setCurrentLaunch(null);
this.configUtils = new ConfigUtils(config);
IProject project = configUtils.getProject();
ValgrindUIPlugin.getDefault().setProfiledProject(project);
URI exeURI = new URI(configUtils.getExecutablePath());
RemoteConnection exeRC = new RemoteConnection(exeURI);
monitor.worked(1);
String valgrindPathString = RuntimeProcessFactory.getFactory().whichCommand(VALGRIND_CMD, project);
IPath valgrindFullPath = Path.fromOSString(valgrindPathString);
boolean copyExecutable = configUtils.getCopyExecutable();
if (copyExecutable) {
URI copyExeURI = new URI(configUtils.getCopyFromExecutablePath());
RemoteConnection copyExeRC = new RemoteConnection(copyExeURI);
IRemoteFileProxy copyExeRFP = copyExeRC.getRmtFileProxy();
IFileStore copyExeFS = copyExeRFP.getResource(copyExeURI.getPath());
IRemoteFileProxy exeRFP = exeRC.getRmtFileProxy();
IFileStore exeFS = exeRFP.getResource(exeURI.getPath());
IFileInfo exeFI = exeFS.fetchInfo();
if (exeFI.isDirectory()) {
// Assume the user wants to copy the file to the given directory, using
// the same filename as the "copy from" executable.
IPath copyExePath = Path.fromOSString(copyExeURI.getPath());
IPath newExePath = Path.fromOSString(exeURI.getPath()).append(copyExePath.lastSegment());
// update the exeURI with the new path.
exeURI = new URI(exeURI.getScheme(), exeURI.getAuthority(), newExePath.toString(), exeURI.getQuery(), exeURI.getFragment());
exeFS = exeRFP.getResource(exeURI.getPath());
}
copyExeFS.copy(exeFS, EFS.OVERWRITE | EFS.SHALLOW, SubMonitor.convert(monitor, 1));
// Note: assume that we don't need to create a new exeRC since the
// scheme and authority remain the same between the original exeURI and the new one.
}
valgrindVersion = getValgrindVersion(project);
IPath remoteBinFile = Path.fromOSString(exeURI.getPath());
String configWorkingDir = configUtils.getWorkingDirectory();
IFileStore workingDir;
if (configWorkingDir == null) {
// If no working directory was provided, use the directory containing the
// the executable as the working directory.
IPath workingDirPath = remoteBinFile.removeLastSegments(1);
IRemoteFileProxy workingDirRFP = exeRC.getRmtFileProxy();
workingDir = workingDirRFP.getResource(workingDirPath.toOSString());
} else {
URI workingDirURI = new URI(configUtils.getWorkingDirectory());
RemoteConnection workingDirRC = new RemoteConnection(workingDirURI);
IRemoteFileProxy workingDirRFP = workingDirRC.getRmtFileProxy();
workingDir = workingDirRFP.getResource(workingDirURI.getPath());
}
// $NON-NLS-1$
IPath remoteLogDir = Path.fromOSString("/tmp/");
// $NON-NLS-1$
outputPath = remoteLogDir.append("eclipse-valgrind-" + System.currentTimeMillis());
exeRC.createFolder(outputPath, SubMonitor.convert(monitor, 1));
// create/empty local output directory
IValgrindOutputDirectoryProvider provider = getPlugin().getOutputDirectoryProvider();
setOutputPath(config, provider.getOutputPath());
IPath localOutputDir = null;
try {
localOutputDir = provider.getOutputPath();
createDirectory(localOutputDir);
} catch (IOException e2) {
throw new CoreException(new Status(IStatus.ERROR, ValgrindLaunchPlugin.PLUGIN_ID, IStatus.OK, "", // $NON-NLS-1$
e2));
}
// tool that was launched
toolID = getTool(config);
// ask tool extension for arguments
dynamicDelegate = getDynamicDelegate(toolID);
String[] valgrindArgs = getValgrindArgumentsArray(config);
String[] executableArgs = getProgramArgumentsArray(config);
String[] allArgs = new String[executableArgs.length + valgrindArgs.length + 2];
int idx = 0;
allArgs[idx++] = VALGRIND_CMD;
for (String valgrindArg : valgrindArgs) {
allArgs[idx++] = valgrindArg;
}
allArgs[idx++] = remoteBinFile.toOSString();
for (String executableArg : executableArgs) {
allArgs[idx++] = executableArg;
}
Process p = RuntimeProcessFactory.getFactory().exec(allArgs, new String[0], workingDir, project);
int state = p.waitFor();
if (state != IRemoteCommandLauncher.OK) {
abort(// $NON-NLS-1$ //$NON-NLS-2$
Messages.getString("ValgrindLaunchConfigurationDelegate.Launch_exited_status") + " " + state + ". " + // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
NLS.bind(Messages.getString("ValgrindRemoteProxyLaunchDelegate.see_reference"), "IRemoteCommandLauncher") + // $NON-NLS-1$
"\n", null, ICDTLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
}
if (p.exitValue() != 0) {
String line = null;
StringBuilder valgrindOutSB = new StringBuilder();
BufferedReader valgrindOut = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = valgrindOut.readLine()) != null) {
valgrindOutSB.append(line);
}
StringBuilder valgrindErrSB = new StringBuilder();
BufferedReader valgrindErr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((line = valgrindErr.readLine()) != null) {
valgrindErrSB.append(line);
}
abort(// $NON-NLS-1$
NLS.bind("ValgrindRemoteProxyLaunchDelegate.Stdout", valgrindOutSB.toString()) + "\n" + // $NON-NLS-1$ //$NON-NLS-2$
NLS.bind("ValgrindRemoteProxyLaunchDelegate.Stderr", valgrindErrSB.toString()), null, ICDTLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
}
// move remote log files to local directory
exeRC.download(outputPath, localOutputDir, SubMonitor.convert(monitor, 1));
// remove remote log dir and all files under it
exeRC.delete(outputPath, SubMonitor.convert(monitor, 1));
// store these for use by other classes
getPlugin().setCurrentLaunchConfiguration(config);
getPlugin().setCurrentLaunch(launch);
// parse Valgrind logs
IValgrindMessage[] messages = parseLogs(localOutputDir);
// create launch summary string to distinguish this launch
launchStr = createLaunchStr(valgrindFullPath);
// create view
ValgrindUIPlugin.getDefault().createView(launchStr, toolID);
// set log messages
ValgrindViewPart view = ValgrindUIPlugin.getDefault().getView();
view.setMessages(messages);
monitor.worked(1);
// pass off control to extender
dynamicDelegate.handleLaunch(config, launch, localOutputDir, monitor.newChild(2));
// initialize tool-specific part of view
dynamicDelegate.initializeView(view.getDynamicView(), launchStr, monitor.newChild(1));
// refresh view
ValgrindUIPlugin.getDefault().refreshView();
// show view
ValgrindUIPlugin.getDefault().showView();
monitor.worked(1);
} catch (URISyntaxException | IOException | RemoteConnectionException | InterruptedException e) {
abort(e.getLocalizedMessage(), null, ICDTLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
} finally {
monitor.done();
m.done();
}
}
Aggregations