Skip to content

Commit

Permalink
[builder facade] Add appropriate dependencies on IWorkspace
Browse files Browse the repository at this point in the history
Since the upgrade to Eclipse baseline of 2022-09, it seems that Eclipse
registers the workspace as an IWorkspace object. An exception stacktrace
even recommends depending on this using DS or similar instead of using the
static method ResourcePlugin.getWorkspace(). Adding this dependency fixes
some runtime startup ordering problems.

Signed-off-by: Fr Jeremy Krieg <[email protected]>
  • Loading branch information
kriegfrj committed Oct 28, 2023
1 parent 4da6209 commit a230789
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 46 deletions.
5 changes: 4 additions & 1 deletion bndtools.core/src/bndtools/central/Central.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
Expand All @@ -47,6 +48,7 @@
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.osgi.util.function.Consumer;
import org.osgi.util.promise.Deferred;
import org.osgi.util.promise.Promise;
Expand Down Expand Up @@ -104,7 +106,8 @@ public class Central implements ICentral {
private static BundleContext context;

@Activate
void activate(BundleContext bc) {
public Central(BundleContext bc, @Reference
IWorkspace notused) {
context = bc;
instance = this;
repoListenerTracker = new RepositoryListenerPluginTracker(bc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.SubMonitor;
Expand Down Expand Up @@ -44,25 +43,28 @@
*/
@Component(enabled = false)
public class SynchronizeWorkspaceWithEclipse {
static IWorkspace eclipse = ResourcesPlugin.getWorkspace();
final static IWorkspaceRoot root = eclipse.getRoot();
final IWorkspace eclipse;
final IWorkspaceRoot root;
final Workspace workspace;
// This restricted API warning can be ignored because the constant
// is resolved at compile time.
@SuppressWarnings("restriction")
final static String BNDTOOLS_NATURE = bndtools.Plugin.BNDTOOLS_NATURE;
final TriggerRepeat lock = new TriggerRepeat();
ScheduledFuture<?> schedule;
long lastModified = -1;
FileWatcher watcher;
File dir;
boolean macos;
OnWorkspace event;

@Reference
Workspace workspace;
final static String BNDTOOLS_NATURE = bndtools.Plugin.BNDTOOLS_NATURE;
final TriggerRepeat lock = new TriggerRepeat();
ScheduledFuture<?> schedule;
long lastModified = -1;
FileWatcher watcher;
File dir;
boolean macos;
OnWorkspace event;

@Activate
void activate() throws IOException {
public SynchronizeWorkspaceWithEclipse(@Reference
IWorkspace eclipse, @Reference
Workspace workspace) throws IOException {
this.eclipse = eclipse;
this.workspace = workspace;
this.root = eclipse.getRoot();
schedule = Processor.getScheduledExecutor()
.scheduleAtFixedRate(this::check, 1000, 300, TimeUnit.MILLISECONDS);

Expand Down
17 changes: 9 additions & 8 deletions bndtools.core/src/bndtools/central/sync/WorkspaceChange.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
Expand All @@ -19,16 +18,18 @@

@Component
public class WorkspaceChange {
final static IWorkspace workspace = ResourcesPlugin.getWorkspace();
final IWorkspace workspace;
final Workspace ws;

@Reference
Workspace ws;

boolean first = true;
volatile boolean cancel = false;
boolean first = true;
volatile boolean cancel = false;

@Activate
void activate() {
public WorkspaceChange(@Reference
IWorkspace workspace, @Reference
Workspace ws) {
this.workspace = workspace;
this.ws = ws;
ws.on("workspace-changes")
.initial(this::init)
.repositoriesReady(this::done);
Expand Down
1 change: 1 addition & 0 deletions bndtools.test/bndws/validators/nested/bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@

import org.bndtools.api.ILogger;
import org.bndtools.api.Logger;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.compiler.CompilationParticipant;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

@Component(service = CompilationParticipant.class)
public class BndContainerCompilationParticipant extends CompilationParticipant {
private static final ILogger logger = Logger.getLogger(BndContainerCompilationParticipant.class);

public BndContainerCompilationParticipant() {
@Activate
public BndContainerCompilationParticipant(@Reference
IWorkspace notused) {
super();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
import org.eclipse.jdt.internal.core.JavaModelManager;
import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
import org.eclipse.jdt.launching.JavaRuntime;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import aQute.bnd.build.CircularDependencyException;
import aQute.bnd.build.Container;
Expand Down Expand Up @@ -69,7 +71,9 @@ public class BndContainerInitializer extends ClasspathContainerInitializer imple
.getLogger(BndContainerInitializer.class);
private static final ClasspathContainerSerializationHelper<BndContainer> serializationHelper = new ClasspathContainerSerializationHelper<>();

public BndContainerInitializer() {
@Activate
public BndContainerInitializer(@Reference
Central central) {
super();
Central.onCnfWorkspace(workspace -> Central.getInstance()
.addModelListener(BndContainerInitializer.this));
Expand Down Expand Up @@ -277,26 +281,26 @@ void updateClasspathContainer(boolean init) throws CoreException {
static void setClasspathContainer(IJavaProject javaProject, BndContainer container) throws JavaModelException {
BndPreferences prefs = new BndPreferences();
// if (prefs.getBuildLogging() == BuildLoggerConstants.LOG_FULL) {
StringBuilder sb = new StringBuilder();
sb.append(container.getDescription())
.append(" for ")
.append(javaProject.getProject()
.getName())
.append("\n\n=== Compile Classpath ===");
for (IClasspathEntry cpe : container.getClasspathEntries()) {
sb.append("\n--- ")
.append(cpe);
}
sb.append("\n\n=== Runtime Classpath ===");
for (IRuntimeClasspathEntry cpe : container.getRuntimeClasspathEntries()) {
sb.append("\n--- ")
.append(cpe);
}
String msg = sb.append("\n")
.toString();
// logger.logInfo(msg, null);
consoleLog.debug(msg);
// }
StringBuilder sb = new StringBuilder();
sb.append(container.getDescription())
.append(" for ")
.append(javaProject.getProject()
.getName())
.append("\n\n=== Compile Classpath ===");
for (IClasspathEntry cpe : container.getClasspathEntries()) {
sb.append("\n--- ")
.append(cpe);
}
sb.append("\n\n=== Runtime Classpath ===");
for (IRuntimeClasspathEntry cpe : container.getRuntimeClasspathEntries()) {
sb.append("\n--- ")
.append(cpe);
}
String msg = sb.append("\n")
.toString();
// logger.logInfo(msg, null);
consoleLog.debug(msg);
// }

JavaCore.setClasspathContainer(BndtoolsConstants.BND_CLASSPATH_ID, new IJavaProject[] {
javaProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
import org.bndtools.api.Logger;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.compiler.CompilationParticipant;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import aQute.bnd.build.Project;
import aQute.bnd.build.Workspace;
Expand All @@ -26,6 +29,10 @@ public class BndSourceGenerateCompilationParticipant extends CompilationParticip
.getLogger(BndSourceGenerateCompilationParticipant.class);
public static final String MARKER_BND_GENERATE = "bndtools.builder.bndgenerate";

@Activate
public BndSourceGenerateCompilationParticipant(@Reference
IWorkspace notused) {}

@Override
public boolean isActive(IJavaProject javaProject) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.CoreException;
Expand All @@ -39,6 +40,7 @@
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.ui.preferences.ScopedPreferenceStore;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ServiceScope;
Expand Down Expand Up @@ -96,6 +98,10 @@ public class BndtoolsBuilder extends ProjectBuilderDelegate {
@Reference
volatile List<IProjectValidator> projectValidators;

@Activate
public BndtoolsBuilder(@Reference
IWorkspace notused) {}

void validate(Project model) throws Exception {
// Cache reference as it is volatile
List<IValidator> validators = this.validators;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.runtime.CoreException;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

@Component
public class NewProjectResourceListener implements IResourceChangeListener {
private static final ILogger logger = Logger.getLogger(NewProjectResourceListener.class);

@Activate
public NewProjectResourceListener(@Reference
IWorkspace notused) {}

@Override
public void resourceChanged(IResourceChangeEvent event) {
IResourceDelta delta = event.getDelta();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

import org.bndtools.api.IValidator;
import org.eclipse.core.runtime.IStatus;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import aQute.bnd.build.Project;
import aQute.bnd.osgi.Builder;
Expand All @@ -16,6 +18,14 @@
@Component
public class BsnValidator implements IValidator {

final Central central;

@Activate
public BsnValidator(@Reference
Central central) {
this.central = central;
}

@Override
public IStatus validate(Builder builder) throws Exception {
Project project;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@
import org.bndtools.api.IProjectValidator;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import aQute.bnd.build.Project;
import aQute.bnd.osgi.Constants;
import bndtools.central.Central;

@Component
public class JavaVersionsValidator implements IProjectValidator {
@Activate
public JavaVersionsValidator(@Reference
Central notused) {}

@Override
public void validateProject(Project model) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import aQute.bnd.build.Project;
import aQute.bnd.osgi.Constants;
Expand All @@ -34,6 +36,10 @@
public class ProjectPathsValidator implements IProjectValidator {
final static IPath JRE_CONTAINER = new Path("org.eclipse.jdt.launching.JRE_CONTAINER");

@Activate
public ProjectPathsValidator(@Reference
Central notused) {}

/*
* The parts of the test, needed to know what we missed
*/
Expand Down

0 comments on commit a230789

Please sign in to comment.