Search in sources :

Example 1 with IJobFunction

use of org.eclipse.core.runtime.jobs.IJobFunction in project org.csstudio.display.builder by kasemir.

the class WorkspaceResourceHelperImpl method writeWorkspaceResource.

@Override
public OutputStream writeWorkspaceResource(final String resource_name) throws Exception {
    final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    final IFile file = root.getFile(new Path(resource_name));
    // IFile API requires an InputStream for the content.
    // That content, however, doesn't exist at this time, because
    // it's about to be written to an OutputStream by the caller
    // of this function.
    // -> Provide pipe, with background job to read from pipe and write the file
    final PipedOutputStream buf = new PipedOutputStream();
    final PipedInputStream input = new PipedInputStream(buf);
    final IJobFunction writer = monitor -> {
        try {
            if (file.exists())
                file.setContents(input, true, false, monitor);
            else
                file.create(input, true, monitor);
        } catch (Exception ex) {
            logger.log(Level.WARNING, "Cannot write to " + resource_name, ex);
        }
        return Status.OK_STATUS;
    };
    Job.create("Workspace Writer", writer).schedule();
    // Provide caller with output end of pipe to fill
    return buf;
}
Also used : Path(org.eclipse.core.runtime.Path) OutputStream(java.io.OutputStream) ResourcesPlugin(org.eclipse.core.resources.ResourcesPlugin) Job(org.eclipse.core.runtime.jobs.Job) Status(org.eclipse.core.runtime.Status) ModelPlugin.logger(org.csstudio.display.builder.model.ModelPlugin.logger) PipedOutputStream(java.io.PipedOutputStream) Level(java.util.logging.Level) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IJobFunction(org.eclipse.core.runtime.jobs.IJobFunction) PipedInputStream(java.io.PipedInputStream) Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) InputStream(java.io.InputStream) URIUtil(org.eclipse.core.filesystem.URIUtil) IFile(org.eclipse.core.resources.IFile) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IJobFunction(org.eclipse.core.runtime.jobs.IJobFunction) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream)

Aggregations

InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 Level (java.util.logging.Level)1 ModelPlugin.logger (org.csstudio.display.builder.model.ModelPlugin.logger)1 URIUtil (org.eclipse.core.filesystem.URIUtil)1 IFile (org.eclipse.core.resources.IFile)1 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)1 ResourcesPlugin (org.eclipse.core.resources.ResourcesPlugin)1 Path (org.eclipse.core.runtime.Path)1 Status (org.eclipse.core.runtime.Status)1 IJobFunction (org.eclipse.core.runtime.jobs.IJobFunction)1 Job (org.eclipse.core.runtime.jobs.Job)1