Search in sources :

Example 31 with SubMonitor

use of org.eclipse.core.runtime.SubMonitor in project bndtools by bndtools.

the class JAREntryPart method createProgressMonitor.

private static SubMonitor createProgressMonitor(ZipEntry entry, long limit, IProgressMonitor monitor) {
    SubMonitor progress;
    long size = entry.getSize();
    if (size == -1) {
        progress = SubMonitor.convert(monitor);
    } else {
        long ticks = (limit == -1) ? size : Math.min(size, limit);
        progress = SubMonitor.convert(monitor, (int) ticks);
    }
    return progress;
}
Also used : SubMonitor(org.eclipse.core.runtime.SubMonitor)

Example 32 with SubMonitor

use of org.eclipse.core.runtime.SubMonitor in project bndtools by bndtools.

the class JAREntryPart method readAsText.

protected static void readAsText(ZipFile zipFile, ZipEntry entry, String encoding, Writer out, long limit, IProgressMonitor monitor) throws IOException {
    SubMonitor progress = createProgressMonitor(entry, limit, monitor);
    boolean limitReached = false;
    try (InputStream stream = zipFile.getInputStream(entry)) {
        long total = 0;
        byte[] buffer = new byte[1024];
        while (true) {
            if (progress.isCanceled())
                return;
            int bytesRead = stream.read(buffer, 0, 1024);
            if (bytesRead < 0)
                break;
            String string = new String(buffer, 0, bytesRead, encoding);
            out.write(string);
            total += bytesRead;
            progress.worked(bytesRead);
            if (limit >= 0 && total >= limit) {
                limitReached = true;
                return;
            }
        }
    } finally {
        if (limitReached) {
            out.write("\nLimit of " + (limit >> 10) + "Kb reached, the rest of the entry is not shown.");
        }
    }
}
Also used : InputStream(java.io.InputStream) SubMonitor(org.eclipse.core.runtime.SubMonitor)

Example 33 with SubMonitor

use of org.eclipse.core.runtime.SubMonitor in project bndtools by bndtools.

the class IconLoaderJob method run.

@Override
protected IStatus run(IProgressMonitor monitor) {
    SubMonitor progress = SubMonitor.convert(monitor, templates.size());
    Map<Template, byte[]> batch = new IdentityHashMap<>();
    for (Template template : templates) {
        InputStream iconStream = null;
        try {
            URI iconUri = template.getIcon();
            if (iconUri != null) {
                iconStream = iconUri.toURL().openStream();
                byte[] bytes = IO.read(iconStream);
                batch.put(template, bytes);
                if (batch.size() >= batchLimit) {
                    processBatch(batch);
                    batch = new IdentityHashMap<>();
                }
            }
        } catch (Exception e) {
            log.log(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error reading icon for template '" + template.getName() + "'", e));
        } finally {
            IO.close(iconStream);
        }
        progress.worked(1);
    }
    processBatch(batch);
    return Status.OK_STATUS;
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IdentityHashMap(java.util.IdentityHashMap) SubMonitor(org.eclipse.core.runtime.SubMonitor) URI(java.net.URI) Template(org.bndtools.templating.Template)

Example 34 with SubMonitor

use of org.eclipse.core.runtime.SubMonitor in project bndtools by bndtools.

the class AddJpmDependenciesWizard method performFinish.

@Override
public boolean performFinish() {
    result.clear();
    result.addAll(depsPage.getDirectResources());
    result.addAll(depsPage.getSelectedIndirectResources());
    final Set<ResourceDescriptor> indirectResources;
    if (depsPage.getIndirectResources() != null) {
        indirectResources = new HashSet<SearchableRepository.ResourceDescriptor>(depsPage.getIndirectResources());
        indirectResources.removeAll(result);
    } else {
        indirectResources = Collections.<ResourceDescriptor>emptySet();
    }
    final MultiStatus status = new MultiStatus(Plugin.PLUGIN_ID, 0, "Errors occurred while processing JPM4J dependencies.", null);
    IRunnableWithProgress runnable = new IRunnableWithProgress() {

        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            SubMonitor progress = SubMonitor.convert(monitor, result.size() + indirectResources.size());
            progress.setTaskName("Processing dependencies...");
            // Process all resources (including non-selected ones) into the repository
            for (ResourceDescriptor resource : result) processResource(resource, status, progress.newChild(1));
            for (ResourceDescriptor resource : indirectResources) processResource(resource, status, progress.newChild(1));
        }
    };
    try {
        getContainer().run(true, true, runnable);
        if (!status.isOK())
            ErrorDialog.openError(getShell(), "Errors", null, status);
        return true;
    } catch (InvocationTargetException e) {
        MessageDialog.openError(getShell(), "Error", e.getCause().getMessage());
        return false;
    } catch (InterruptedException e) {
        return false;
    }
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) SubMonitor(org.eclipse.core.runtime.SubMonitor) MultiStatus(org.eclipse.core.runtime.MultiStatus) InvocationTargetException(java.lang.reflect.InvocationTargetException) ResourceDescriptor(aQute.bnd.service.repository.SearchableRepository.ResourceDescriptor) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress)

Example 35 with SubMonitor

use of org.eclipse.core.runtime.SubMonitor in project bndtools by bndtools.

the class ExportPatternsListPart method generatePackageInfos.

private static void generatePackageInfos(final Collection<? extends FileVersionTuple> pkgs) throws CoreException {
    final IWorkspaceRunnable wsOperation = new IWorkspaceRunnable() {

        @Override
        public void run(IProgressMonitor monitor) throws CoreException {
            SubMonitor progress = SubMonitor.convert(monitor, pkgs.size());
            MultiStatus status = new MultiStatus(Plugin.PLUGIN_ID, 0, "Errors occurred while creating packageinfo files.", null);
            for (FileVersionTuple pkg : pkgs) {
                IContainer[] locations = ResourcesPlugin.getWorkspace().getRoot().findContainersForLocationURI(pkg.getFile().toURI());
                if (locations != null && locations.length > 0) {
                    IContainer container = locations[0];
                    PackageInfoStyle packageInfoStyle = PackageInfoStyle.calculatePackageInfoStyle(container.getProject());
                    IFile pkgInfoFile = container.getFile(new Path(packageInfoStyle.getFileName()));
                    try {
                        String formattedPackageInfo = packageInfoStyle.format(pkg.getVersion(), pkg.getName());
                        ByteArrayInputStream input = new ByteArrayInputStream(formattedPackageInfo.getBytes("UTF-8"));
                        if (pkgInfoFile.exists())
                            pkgInfoFile.setContents(input, false, true, progress.newChild(1, 0));
                        else
                            pkgInfoFile.create(input, false, progress.newChild(1, 0));
                    } catch (CoreException e) {
                        status.add(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error creating file " + pkgInfoFile.getFullPath(), e));
                    } catch (UnsupportedEncodingException e) {
                    /* just ignore, should never happen */
                    }
                }
            }
            if (!status.isOK())
                throw new CoreException(status);
        }
    };
    IRunnableWithProgress uiOperation = new IRunnableWithProgress() {

        @Override
        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            try {
                ResourcesPlugin.getWorkspace().run(wsOperation, monitor);
            } catch (CoreException e) {
                throw new InvocationTargetException(e);
            }
        }
    };
    try {
        PlatformUI.getWorkbench().getActiveWorkbenchWindow().run(true, true, uiOperation);
    } catch (InvocationTargetException e) {
        throw (CoreException) e.getTargetException();
    } catch (InterruptedException e) {
    // ignore
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) IFile(org.eclipse.core.resources.IFile) SubMonitor(org.eclipse.core.runtime.SubMonitor) MultiStatus(org.eclipse.core.runtime.MultiStatus) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) FileVersionTuple(bndtools.editor.contents.PackageInfoDialog.FileVersionTuple) CoreException(org.eclipse.core.runtime.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) IContainer(org.eclipse.core.resources.IContainer) PackageInfoStyle(bndtools.editor.contents.PackageInfoStyle)

Aggregations

SubMonitor (org.eclipse.core.runtime.SubMonitor)35 IStatus (org.eclipse.core.runtime.IStatus)14 CoreException (org.eclipse.core.runtime.CoreException)13 Status (org.eclipse.core.runtime.Status)13 IOException (java.io.IOException)8 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)8 InputStream (java.io.InputStream)7 IFile (org.eclipse.core.resources.IFile)7 File (java.io.File)5 IProject (org.eclipse.core.resources.IProject)4 MultiStatus (org.eclipse.core.runtime.MultiStatus)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ArrayList (java.util.ArrayList)3 IContainer (org.eclipse.core.resources.IContainer)3 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)3 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)3 ResourceDescriptor (aQute.bnd.service.repository.SearchableRepository.ResourceDescriptor)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 SQLException (java.sql.SQLException)2