From 5cac9e29f596bec2ab036a55e66304836e65cfa9 Mon Sep 17 00:00:00 2001 From: Hannes Wellmann Date: Sat, 9 Nov 2024 10:08:06 +0100 Subject: [PATCH] Simplify event publication in TargetPlatformService Instead of implementing an asynchronous event dispatching mechanism in TargetPlatformService, simply use IEventBroker.post(). --- .../core/target/TargetPlatformService.java | 125 +++--------------- 1 file changed, 21 insertions(+), 104 deletions(-) diff --git a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/target/TargetPlatformService.java b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/target/TargetPlatformService.java index a63ae635e6..5dd599ab64 100644 --- a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/target/TargetPlatformService.java +++ b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/target/TargetPlatformService.java @@ -33,7 +33,6 @@ import java.util.Map; import java.util.Objects; import java.util.Set; -import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -41,10 +40,7 @@ import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IResource; -import org.eclipse.core.resources.IResourceProxy; -import org.eclipse.core.resources.IResourceProxyVisitor; import org.eclipse.core.resources.ResourcesPlugin; -import org.eclipse.core.runtime.Assert; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IConfigurationElement; import org.eclipse.core.runtime.IPath; @@ -100,44 +96,14 @@ public class TargetPlatformService implements ITargetPlatformService { * The target definition currently being used as the target platform for * the workspace. */ - private final AtomicReference fWorkspaceTarget; + private final AtomicReference fWorkspaceTarget = new AtomicReference<>(); /** * vm arguments for default target */ private StringBuilder fVMArguments; - private final EventDispatcher eventSendingJob; - - /** - * Collects target files in the workspace - */ - static class ResourceProxyVisitor implements IResourceProxyVisitor { - - private final List fList; - - protected ResourceProxyVisitor(List list) { - fList = list; - } - - /** - * @see org.eclipse.core.resources.IResourceProxyVisitor#visit(org.eclipse.core.resources.IResourceProxy) - */ - @Override - public boolean visit(IResourceProxy proxy) { - if (proxy.getType() == IResource.FILE) { - if (ICoreConstants.TARGET_FILE_EXTENSION.equalsIgnoreCase(IPath.fromOSString(proxy.getName()).getFileExtension())) { - fList.add(proxy.requestResource()); - } - return false; - } - return true; - } - } - private TargetPlatformService() { - fWorkspaceTarget = new AtomicReference<>(); - eventSendingJob = new EventDispatcher("Sending 'workspace target changed' event", TargetPlatformService.class); //$NON-NLS-1$ } /** @@ -271,21 +237,23 @@ private List findLocalTargetDefinitions() { * @return all target definition handles in the workspace */ private List findWorkspaceTargetDefinitions() { - List files = new ArrayList<>(10); - ResourceProxyVisitor visitor = new ResourceProxyVisitor(files); + List files = new ArrayList<>(10); try { - ResourcesPlugin.getWorkspace().getRoot().accept(visitor, IResource.NONE); + ResourcesPlugin.getWorkspace().getRoot().accept(proxy -> { + if (proxy.getType() == IResource.FILE) { + if (ICoreConstants.TARGET_FILE_EXTENSION + .equalsIgnoreCase(IPath.fromOSString(proxy.getName()).getFileExtension())) { + files.add((IFile) proxy.requestResource()); + } + return false; + } + return true; + }, IResource.NONE); } catch (CoreException e) { PDECore.log(e); - return new ArrayList<>(0); - } - Iterator iter = files.iterator(); - List handles = new ArrayList<>(files.size()); - while (iter.hasNext()) { - IFile file = (IFile) iter.next(); - handles.add(new WorkspaceFileTargetHandle(file)); + return List.of(); } - return handles; + return files.stream().map(WorkspaceFileTargetHandle::new).toList(); } @Override @@ -361,71 +329,20 @@ public synchronized ITargetDefinition getWorkspaceTargetDefinition() throws Core */ public void setWorkspaceTargetDefinition(ITargetDefinition target, boolean asyncEvents) { ITargetDefinition oldTarget = fWorkspaceTarget.getAndSet(target); - boolean changed = !Objects.equals(oldTarget, target); - if (changed) { - if (asyncEvents) { - eventSendingJob.schedule(target); - } else { - notifyTargetChanged(target); - } + if (!Objects.equals(oldTarget, target)) { + notifyEvent(TargetEvents.TOPIC_WORKSPACE_TARGET_CHANGED, target, asyncEvents); } } - static void notifyTargetChanged(ITargetDefinition target) { + private static void notifyEvent(String topic, Object data, boolean asyncEvents) { IEclipseContext context = EclipseContextFactory.getServiceContext(PDECore.getDefault().getBundleContext()); IEventBroker broker = context.get(IEventBroker.class); if (broker != null) { - broker.send(TargetEvents.TOPIC_WORKSPACE_TARGET_CHANGED, target); - } - } - - static class EventDispatcher extends Job { - - private final ConcurrentLinkedQueue queue; - private final Object myFamily; - - /** - * @param jobName - * descriptive job name - * @param family - * non null object to control this job execution - **/ - public EventDispatcher(String jobName, Object family) { - super(jobName); - Assert.isNotNull(family); - this.myFamily = family; - this.queue = new ConcurrentLinkedQueue<>(); - setSystem(true); - } - - @Override - public boolean belongsTo(Object family) { - return myFamily == family; - } - - @Override - protected IStatus run(IProgressMonitor monitor) { - ITargetDefinition target; - while ((target = queue.poll()) != null && !monitor.isCanceled()) { - notifyTargetChanged(target); - } - if (!queue.isEmpty() && !monitor.isCanceled()) { - // in case actions got faster scheduled then processed - schedule(); - } - if (monitor.isCanceled()) { - queue.clear(); - return Status.CANCEL_STATUS; + if (asyncEvents) { + broker.post(topic, data); + } else { + broker.send(topic, data); } - return Status.OK_STATUS; - } - - /** - * Enqueue a task asynchronously. - **/ - public void schedule(ITargetDefinition target) { - queue.offer(target); - schedule(); // will reschedule if already running } }