Skip to content

Commit

Permalink
Run Async if tree is locked
Browse files Browse the repository at this point in the history
  • Loading branch information
laeubi committed May 7, 2024
1 parent 7e279bc commit 868ba8f
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,16 @@ private void updateAffectedEntries(StateDelta delta, boolean runAsynch) {
containers[index] = entry.getValue();
index++;
}
for (IJavaProject jp : map.keySet()) {
try {
jp.getProject().setSessionProperty(PDECore.TOUCH_PROJECT, Boolean.TRUE);
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// TODO Consider always running in a job - better reporting and cancellation options
if (runAsynch) {
if (runAsynch || ResourcesPlugin.getWorkspace().isTreeLocked()) {
// We may be in the UI thread, so the classpath is updated in a job to avoid blocking (bug 376135)
fUpdateJob.add(projects, containers);
fUpdateJob.schedule();
Expand All @@ -393,6 +401,7 @@ private void updateAffectedEntries(StateDelta delta, boolean runAsynch) {
try {
JavaCore.setClasspathContainer(PDECore.REQUIRED_PLUGINS_CONTAINER_PATH, projects, containers, null);
} catch (JavaModelException e) {
e.printStackTrace();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
package org.eclipse.pde.internal.core.bnd;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -34,14 +37,23 @@
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.osgi.service.resolver.BundleSpecification;
import org.eclipse.osgi.service.resolver.ExportPackageDescription;
import org.eclipse.osgi.service.resolver.ImportPackageSpecification;
import org.eclipse.osgi.service.resolver.StateObjectFactory;
import org.eclipse.osgi.util.ManifestElement;
import org.eclipse.osgi.util.NLS;
import org.eclipse.pde.core.plugin.IPluginModelBase;
import org.eclipse.pde.internal.core.ICoreConstants;
import org.eclipse.pde.internal.core.PDECore;
import org.eclipse.pde.internal.core.PDECoreMessages;
import org.eclipse.pde.internal.core.PluginModelManager;
import org.eclipse.pde.internal.core.builders.PDEMarkerFactory;
import org.eclipse.pde.internal.core.natures.BndProject;
import org.eclipse.pde.internal.core.project.PDEProject;
import org.osgi.framework.BundleException;
import org.osgi.framework.FrameworkUtil;

import aQute.bnd.build.Project;
import aQute.bnd.build.ProjectBuilder;
Expand Down Expand Up @@ -87,7 +99,8 @@ public void done(IJobChangeEvent event) {
});
buildJob.schedule();
} else {
buildProjectJar(project, monitor);
List<IProject> affected = buildProjectJar(project, monitor);
requestProjectsRebuild(affected);
}
}
return new IProject[] { project };
Expand Down Expand Up @@ -128,14 +141,14 @@ public void run(IProgressMonitor monitor) {

}

private static void buildProjectJar(IProject project, IProgressMonitor monitor) {
private static List<IProject> buildProjectJar(IProject project, IProgressMonitor monitor) {
try {
Optional<Project> bndProject = BndProjectManager.getBndProject(project);
if (bndProject.isEmpty()) {
return;
return List.of();
}
if (monitor.isCanceled()) {
return;
return List.of();
}
try (Project bnd = bndProject.get(); ProjectBuilder builder = new ProjectBuilder(bnd) {
@Override
Expand Down Expand Up @@ -189,15 +202,65 @@ public void addClasspath(aQute.bnd.osgi.Jar jar) {
}
}
}
PluginModelManager modelManager = PDECore.getDefault().getModelManager();
modelManager.update(project);
}
if (monitor.isCanceled()) {
return;
jar.getManifestFile().touch(null);
BundleDescription bundleDescription = loadBundleDescription(jar.getManifestFile());
if (bundleDescription != null) {
// PDECore.getDefault().getModelManager().getState().updateBundleDescription(bundleDescription);
PluginModelManager modelManager = PDECore.getDefault().getModelManager();
IPluginModelBase[] models = modelManager.getWorkspaceModels();
List<IProject> affectedProjects = new ArrayList<>();
for (IPluginModelBase base : models) {
IResource resource = base.getUnderlyingResource();
if (resource == null) {
continue;
}
IProject modelProject = resource.getProject();
if (modelProject.equals(project)) {
continue;
}
if (isModelAffected(base, bundleDescription)) {
affectedProjects.add(modelProject);
}
if (monitor.isCanceled()) {
return List.of();
}
}
return affectedProjects;
}
}
} catch (Exception e) {
PDECore.log(e);
}
return List.of();
}

private static boolean isModelAffected(IPluginModelBase base, BundleDescription bundleDescription) {
BundleDescription description = base.getBundleDescription();
ImportPackageSpecification[] importPackages = description.getImportPackages();
for (ImportPackageSpecification imp : importPackages) {
for (ExportPackageDescription exp : bundleDescription.getExportPackages()) {
if (imp.isSatisfiedBy(exp)) {
return true;

}
}
}
for (BundleSpecification req : description.getRequiredBundles()) {
if (req.isSatisfiedBy(bundleDescription)) {
return true;
}
}
return false;
}

private static BundleDescription loadBundleDescription(IFile file) {
try (InputStream manifest = file.getContents()) {
Map<String, String> bundleManifest = ManifestElement.parseBundleManifest(manifest);
return StateObjectFactory.defaultFactory.createBundleDescription(null,
FrameworkUtil.asDictionary(bundleManifest), null, System.currentTimeMillis());
} catch (IOException | CoreException | BundleException e) {
return null;
}
}

private static boolean requireBuild(IProject project) {
Expand Down

0 comments on commit 868ba8f

Please sign in to comment.