Skip to content

Commit

Permalink
Clean-up/simplify PDEProjectHelper and AbstractMavenProjectTestCase
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Oct 12, 2022
1 parent 88452df commit 03b0152
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
package org.eclipse.m2e.pde.connector;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Stream;

import org.apache.maven.project.MavenProject;
Expand All @@ -29,7 +30,6 @@
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
Expand All @@ -45,57 +45,41 @@ public class PDEProjectHelper {
@SuppressWarnings("restriction")
private static final String PDE_PLUGIN_NATURE = org.eclipse.pde.internal.core.natures.PDE.PLUGIN_NATURE;

private static boolean isListeningForPluginModelChanges = false;
private static AtomicBoolean isListeningForPluginModelChanges = new AtomicBoolean(false);

private static final List<IProject> PROJECTS_FOR_UPDATE_CLASSPATH = new ArrayList<>();
private static final Set<IProject> PROJECTS_FOR_UPDATE_CLASSPATH = ConcurrentHashMap.newKeySet();

private PDEProjectHelper() {
}

@SuppressWarnings("restriction")
private static final org.eclipse.pde.internal.core.IPluginModelListener CLASSPATH_UPDATER = delta -> {
synchronized (PROJECTS_FOR_UPDATE_CLASSPATH) {
PROJECTS_FOR_UPDATE_CLASSPATH.removeIf(project -> {
IPluginModelBase model = PluginRegistry.findModel(project);
if (model != null) {
UpdateClasspathWorkspaceJob job = new UpdateClasspathWorkspaceJob(project, model);
job.schedule();
return true;
}
return false;
});
}
PROJECTS_FOR_UPDATE_CLASSPATH.removeIf(project -> {
IPluginModelBase model = PluginRegistry.findModel(project);
if (model != null) {
new WorkspaceJob("Updating classpath") {
@Override
public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
setClasspath(project, model, monitor);
return Status.OK_STATUS;
}
}.schedule();
return true;
}
return false;
});
};

private static class UpdateClasspathWorkspaceJob extends WorkspaceJob {
private final IProject project;

private final IPluginModelBase model;

public UpdateClasspathWorkspaceJob(IProject project, IPluginModelBase model) {
super("Updating classpath");
this.project = project;
this.model = model;
}

@Override
public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
setClasspath(project, model, monitor);
return Status.OK_STATUS;
}
}

@SuppressWarnings("restriction")
public static void configurePDEBundleProject(IProject project, MavenProject mavenProject, IProgressMonitor monitor)
static void configurePDEBundleProject(IProject project, MavenProject mavenProject, IProgressMonitor monitor)
throws CoreException {
// see org.eclipse.pde.internal.ui.wizards.plugin.NewProjectCreationOperation

if (!project.hasNature(PDE_PLUGIN_NATURE)) {
org.eclipse.pde.internal.core.util.CoreUtility.addNatureToProject(project, PDE_PLUGIN_NATURE, null);
AbstractProjectConfigurator.addNature(project, PDE_PLUGIN_NATURE, null);
}

if (!project.hasNature(JavaCore.NATURE_ID)) {
org.eclipse.pde.internal.core.util.CoreUtility.addNatureToProject(project, JavaCore.NATURE_ID, null);
AbstractProjectConfigurator.addNature(project, JavaCore.NATURE_ID, null);
}

// PDE can't handle default JDT classpath
Expand All @@ -119,13 +103,10 @@ public static void configurePDEBundleProject(IProject project, MavenProject mave

@SuppressWarnings("restriction")
private static void addProjectForUpdateClasspath(IProject project) {
synchronized (PROJECTS_FOR_UPDATE_CLASSPATH) {
PROJECTS_FOR_UPDATE_CLASSPATH.add(project);
if (!isListeningForPluginModelChanges) {
org.eclipse.pde.internal.core.PDECore pdeCore = org.eclipse.pde.internal.core.PDECore.getDefault();
pdeCore.getModelManager().addPluginModelListener(CLASSPATH_UPDATER);
isListeningForPluginModelChanges = true;
}
PROJECTS_FOR_UPDATE_CLASSPATH.add(project);
if (isListeningForPluginModelChanges.compareAndSet(false, true)) {
org.eclipse.pde.internal.core.PDECore pdeCore = org.eclipse.pde.internal.core.PDECore.getDefault();
pdeCore.getModelManager().addPluginModelListener(CLASSPATH_UPDATER);
}
}

Expand All @@ -139,8 +120,7 @@ private static IPath getOutputLocation(IProject project, MavenProject mavenProje
return folder.getFullPath();
}

public static void addPDENature(IProject project, IPath manifestPath, IProgressMonitor monitor)
throws CoreException {
static void addPDENature(IProject project, IPath manifestPath, IProgressMonitor monitor) throws CoreException {
AbstractProjectConfigurator.addNature(project, PDE_PLUGIN_NATURE, monitor);
IProjectDescription description = project.getDescription();
Stream<ICommand> builders = Arrays.stream(description.getBuildSpec())
Expand All @@ -151,7 +131,7 @@ public static void addPDENature(IProject project, IPath manifestPath, IProgressM
setManifestLocaton(project, manifestPath, monitor);
}

protected static void setManifestLocaton(IProject project, IPath manifestPath, IProgressMonitor monitor)
private static void setManifestLocaton(IProject project, IPath manifestPath, IProgressMonitor monitor)
throws CoreException {
IBundleProjectService projectService = Activator.getBundleProjectService().get();
if (manifestPath != null && manifestPath.segmentCount() > 1) {
Expand All @@ -168,16 +148,12 @@ protected static void setManifestLocaton(IProject project, IPath manifestPath, I
* Returns bundle manifest as known to PDE project metadata. Returned file may
* not exist in the workspace or on the filesystem. Never returns null.
*/
public static IFile getBundleManifest(IProject project) {
@SuppressWarnings("restriction")
private static IFile getBundleManifest(IProject project) {
// PDE API is very inconvenient, lets use internal classes instead
@SuppressWarnings("restriction")
IContainer metainf = org.eclipse.pde.internal.core.project.PDEProject.getBundleRoot(project);
if (metainf == null || metainf instanceof IProject) {
metainf = project.getFolder("META-INF");
} else {
metainf = metainf.getFolder(new Path("META-INF"));
}
return metainf.getFile(new Path("MANIFEST.MF"));
IContainer metaInf = org.eclipse.pde.internal.core.project.PDEProject.getBundleRoot(project);
return (metaInf == null || metaInf instanceof IProject ? project : metaInf)
.getFile(org.eclipse.pde.internal.core.ICoreConstants.MANIFEST_PATH);
}

private static void setClasspath(IProject project, IPluginModelBase model, IProgressMonitor monitor)
Expand Down Expand Up @@ -208,7 +184,7 @@ private static void setClasspath(IProject project, IPluginModelBase model, IProg
// model, re-resolve dependencies
// and recalculate PDE classpath

IFile manifest = PDEProjectHelper.getBundleManifest(project);
IFile manifest = getBundleManifest(project);
if (manifest.isAccessible()) {
manifest.touch(monitor);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008-2018 Sonatype, Inc.
* Copyright (c) 2008-2022 Sonatype, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -13,7 +13,6 @@

package org.eclipse.m2e.tests.common;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -52,7 +51,6 @@
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceDescription;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
Expand Down Expand Up @@ -96,6 +94,7 @@
import org.eclipse.m2e.core.project.IMavenProjectFacade;
import org.eclipse.m2e.core.project.IMavenProjectImportResult;
import org.eclipse.m2e.core.project.IMavenProjectRegistry;
import org.eclipse.m2e.core.project.IProjectConfigurationManager;
import org.eclipse.m2e.core.project.IProjectCreationListener;
import org.eclipse.m2e.core.project.MavenProjectChangedEvent;
import org.eclipse.m2e.core.project.MavenProjectInfo;
Expand Down Expand Up @@ -478,11 +477,10 @@ protected IProject[] importProjects(String basedir, String[] pomNames, ResolverC

protected IProject[] importProjects(String basedir, String[] pomNames, ResolverConfiguration configuration,
boolean skipSanityCheck, IProjectCreationListener listener) throws IOException, CoreException {
MavenModelManager mavenModelManager = MavenPlugin.getMavenModelManager();
IWorkspaceRoot root = workspace.getRoot();

MavenModelManager mavenModelManager = MavenPlugin.getMavenModelManager();
File src = new File(basedir);
File dst = new File(root.getLocation().toFile(), src.getName());
File dst = new File(workspace.getRoot().getLocation().toFile(), src.getName());
copyDir(src, dst);

final List<MavenProjectInfo> projectInfos = new ArrayList<>();
Expand All @@ -494,14 +492,12 @@ protected IProject[] importProjects(String basedir, String[] pomNames, ResolverC
projectInfos.add(projectInfo);
}

final ProjectImportConfiguration importConfiguration = new ProjectImportConfiguration(configuration);

final ArrayList<IMavenProjectImportResult> importResults = new ArrayList<>();

workspace.run(
m -> importResults.addAll(MavenPlugin.getProjectConfigurationManager().importProjects(projectInfos,
importConfiguration, listener, m)),
MavenPlugin.getProjectConfigurationManager().getRule(), IWorkspace.AVOID_UPDATE, monitor);
ProjectImportConfiguration importConfiguration = new ProjectImportConfiguration(configuration);
List<IMavenProjectImportResult> importResults = new ArrayList<>();
IProjectConfigurationManager configManager = MavenPlugin.getProjectConfigurationManager();
workspace.run(m -> {
importResults.addAll(configManager.importProjects(projectInfos, importConfiguration, listener, m));
}, configManager.getRule(), IWorkspace.AVOID_UPDATE, monitor);

IProject[] projects = new IProject[projectInfos.size()];
for(int i = 0; i < projectInfos.size(); i++ ) {
Expand Down Expand Up @@ -667,18 +663,6 @@ protected static Set<IProject> getProjectsFromEvents(Collection<MavenProjectChan
return projects;
}

/**
* Assert that provided list <b>only</b> contains specified expected items.
*
* @since 1.6.0
*/
@SafeVarargs
protected static <T> void assertContainsOnly(Set<? extends T> actual, T... expected) {
Set<T> expectedSet = new HashSet<>();
Collections.addAll(expectedSet, expected);
assertEquals(expectedSet, actual);
}

protected void injectFilexWagon() throws Exception {
PlexusContainer container = ((MavenImpl) MavenPlugin.getMaven()).getPlexusContainer();
if(container.getContainerRealm().getResource(FilexWagon.class.getName().replace('.', '/') + ".class") == null) {
Expand Down

0 comments on commit 03b0152

Please sign in to comment.