Search in sources :

Example 41 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 42 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 43 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 44 with SubMonitor

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

the class AbstractNewBndProjectWizard method processGeneratedProject.

/**
     * Modify the newly generated Java project; this method is executed from within a workspace operation so is free to
     * make workspace resource modifications.
     *
     * @throws CoreException
     */
protected void processGeneratedProject(ProjectPaths projectPaths, BndEditModel bndModel, IJavaProject project, IProgressMonitor monitor) throws CoreException {
    SubMonitor progress = SubMonitor.convert(monitor, 3);
    Document document = new Document("");
    bndModel.saveChangesTo(document);
    progress.worked(1);
    ByteArrayInputStream bndInput;
    try {
        bndInput = new ByteArrayInputStream(document.get().getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        return;
    }
    IFile bndBndFile = project.getProject().getFile(Project.BNDFILE);
    if (bndBndFile.exists()) {
        bndBndFile.setContents(bndInput, false, false, progress.newChild(1));
    }
    BndProject proj = generateBndProject(project.getProject(), progress.newChild(1));
    progress.setWorkRemaining(proj.getResources().size());
    for (Map.Entry<String, BndProjectResource> resource : proj.getResources().entrySet()) {
        importResource(project.getProject(), resource.getKey(), resource.getValue(), progress.newChild(1));
    }
    if (!bndBndFile.exists()) {
        bndBndFile.create(bndInput, false, progress.newChild(1));
    }
    /* Version control ignores */
    VersionControlIgnoresManager versionControlIgnoresManager = Plugin.getDefault().getVersionControlIgnoresManager();
    Set<String> enabledIgnorePlugins = new BndPreferences().getVersionControlIgnoresPluginsEnabled(versionControlIgnoresManager, project, null);
    Map<String, String> sourceOutputLocations = JavaProjectUtils.getSourceOutputLocations(project);
    versionControlIgnoresManager.createProjectIgnores(enabledIgnorePlugins, project.getProject().getLocation().toFile(), sourceOutputLocations, projectPaths.getTargetDir());
    /* Headless build files */
    HeadlessBuildManager headlessBuildManager = Plugin.getDefault().getHeadlessBuildManager();
    Set<String> enabledPlugins = new BndPreferences().getHeadlessBuildPluginsEnabled(headlessBuildManager, null);
    headlessBuildManager.setup(enabledPlugins, false, project.getProject().getLocation().toFile(), true, enabledIgnorePlugins, new LinkedList<String>());
    /* refresh the project; files were created outside of Eclipse API */
    project.getProject().refreshLocal(IResource.DEPTH_INFINITE, progress);
    project.getProject().build(IncrementalProjectBuilder.CLEAN_BUILD, progress);
}
Also used : IFile(org.eclipse.core.resources.IFile) BndPreferences(bndtools.preferences.BndPreferences) VersionControlIgnoresManager(org.bndtools.versioncontrol.ignores.manager.api.VersionControlIgnoresManager) HeadlessBuildManager(org.bndtools.headless.build.manager.api.HeadlessBuildManager) SubMonitor(org.eclipse.core.runtime.SubMonitor) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Document(aQute.bnd.properties.Document) BndProject(bndtools.editor.model.BndProject) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) BndProjectResource(org.bndtools.api.BndProjectResource)

Example 45 with SubMonitor

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

the class RepoDownloadJob method run.

@Override
protected IStatus run(IProgressMonitor progress) {
    SubMonitor monitor = SubMonitor.convert(progress);
    boolean locked = LOCK.tryLock();
    try {
        while (!locked) {
            monitor.setBlocked(new Status(IStatus.INFO, Plugin.PLUGIN_ID, 0, "Waiting for other download jobs to complete.", null));
            if (progress.isCanceled())
                return Status.CANCEL_STATUS;
            try {
                locked = LOCK.tryLock(5, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
            }
        }
        monitor.clearBlocked();
        MultiStatus status = new MultiStatus(Plugin.PLUGIN_ID, 0, "One or more repository files failed to download.", null);
        monitor.setTaskName("Expanding repository contents");
        List<RepositoryBundleVersion> rbvs = new LinkedList<RepositoryBundleVersion>();
        try {
            for (RemoteRepositoryPlugin repo : repos) {
                expandContentsInto(repo, rbvs);
            }
            for (RepositoryBundle bundle : bundles) {
                expandContentsInto(bundle, rbvs);
            }
            rbvs.addAll(bundleVersions);
        } catch (Exception e) {
            return new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error listing repository contents", e);
        }
        monitor.setWorkRemaining(rbvs.size());
        for (RepositoryBundleVersion rbv : rbvs) {
            if (monitor.isCanceled())
                return Status.CANCEL_STATUS;
            String resourceName = "<<unknown>>";
            try {
                RemoteRepositoryPlugin repo = (RemoteRepositoryPlugin) rbv.getRepo();
                ResourceHandle handle = repo.getHandle(rbv.getBsn(), rbv.getVersion().toString(), Strategy.EXACT, Collections.<String, String>emptyMap());
                resourceName = handle.getName();
                Location location = handle.getLocation();
                if (location == Location.remote) {
                    monitor.setTaskName("Downloading " + handle.getName());
                    handle.request();
                }
            } catch (Exception e) {
                status.add(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, String.format("Download of %s:%s with remote name %s failed", rbv.getBsn(), rbv.getVersion(), resourceName), e));
            } finally {
                monitor.worked(1);
            }
        }
        return status;
    } finally {
        if (locked)
            LOCK.unlock();
    }
}
Also used : MultiStatus(org.eclipse.core.runtime.MultiStatus) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) RepositoryBundle(bndtools.model.repo.RepositoryBundle) RepositoryBundleVersion(bndtools.model.repo.RepositoryBundleVersion) SubMonitor(org.eclipse.core.runtime.SubMonitor) MultiStatus(org.eclipse.core.runtime.MultiStatus) RemoteRepositoryPlugin(aQute.bnd.service.RemoteRepositoryPlugin) LinkedList(java.util.LinkedList) ResourceHandle(aQute.bnd.service.ResourceHandle) Location(aQute.bnd.service.ResourceHandle.Location)

Aggregations

SubMonitor (org.eclipse.core.runtime.SubMonitor)61 CoreException (org.eclipse.core.runtime.CoreException)27 IStatus (org.eclipse.core.runtime.IStatus)25 Status (org.eclipse.core.runtime.Status)24 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)15 IOException (java.io.IOException)12 IFile (org.eclipse.core.resources.IFile)10 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)10 InputStream (java.io.InputStream)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 IPath (org.eclipse.core.runtime.IPath)9 IProject (org.eclipse.core.resources.IProject)8 File (java.io.File)7 MultiStatus (org.eclipse.core.runtime.MultiStatus)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)5 IContainer (org.eclipse.core.resources.IContainer)4 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)4 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)4 URI (java.net.URI)3