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.");
}
}
}
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;
}
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;
}
}
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);
}
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();
}
}
Aggregations