Skip to content

Commit

Permalink
Implements cancel for resolve job bndtools#4154
Browse files Browse the repository at this point in the history
- Adds the ability to keep the resolve log after it is canceled

Signed-off-by: Juergen Albert <j.albert@data-in-motion.biz>
  • Loading branch information
juergen-albert committed Jun 10, 2020
1 parent 16d4167 commit d3367fe
Show file tree
Hide file tree
Showing 11 changed files with 292 additions and 23 deletions.
8 changes: 7 additions & 1 deletion biz.aQute.resolve/src/biz/aQute/resolve/ResolverLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class ResolverLogger implements LogService, AutoCloseable {

private String log;

private boolean keepLogFile = false;

public ResolverLogger() {
this(DEFAULT_LEVEL);
}
Expand Down Expand Up @@ -133,11 +135,15 @@ public String getLog() {
@Override
public void close() {
IO.close(printer);
if (file != null) {
if (file != null && !keepLogFile) {
IO.delete(file);
}
}

public void setKeepLogFileTillExit(boolean keep) {
this.keepLogFile = keep;
}

public int getLogLevel() {
return level;
}
Expand Down
26 changes: 24 additions & 2 deletions biz.aQute.resolve/src/biz/aQute/resolve/RunResolution.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,28 @@ public class RunResolution {
*/
public static RunResolution resolve(Project project, Processor actualProperties,
Collection<ResolutionCallback> callbacks) {
return resolve(project, actualProperties, callbacks, new ResolverLogger());
}

/**
* The main workhorse to resolve
*
* @param project used for reporting errors
* @param actualProperties the actual properties used for resolving. This
* can be the project in builders that do not use the
* {@link BndEditModel}, otherwise it is generally the
* {@link BndEditModel}.
* @param callbacks any callbacks
* @param resolverLogger an optional Logger for the resolve Process. The
* Logger needs to be closed manually.
* @return a Resolution
*/
public static RunResolution resolve(Project project, Processor actualProperties,
Collection<ResolutionCallback> callbacks, ResolverLogger resolverLogger) {
if (callbacks == null)
callbacks = Collections.emptyList();

try (ResolverLogger logger = new ResolverLogger()) {
ResolverLogger logger = resolverLogger == null ? new ResolverLogger() : resolverLogger;
try {
try {
ResolveProcess resolve = new ResolveProcess();
Resolver resolver = new BndResolver(logger);
Expand All @@ -88,6 +106,10 @@ public static RunResolution resolve(Project project, Processor actualProperties,
} catch (Exception e) {
return new RunResolution(project, actualProperties, e, logger.getLog());
}
} finally {
if (resolverLogger == null) {
logger.close();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion biz.aQute.resolve/src/biz/aQute/resolve/package-info.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@Version("6.0.0")
@Version("6.1.0")
package biz.aQute.resolve;

import org.osgi.annotation.versioning.Version;
6 changes: 4 additions & 2 deletions bndtools.core/src/bndtools/editor/BndEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
import aQute.bnd.properties.BadLocationException;
import aQute.lib.exceptions.Exceptions;
import biz.aQute.resolve.Bndrun;
import biz.aQute.resolve.ResolverLogger;
import bndtools.Plugin;
import bndtools.central.Central;
import bndtools.editor.common.IPriority;
Expand Down Expand Up @@ -328,8 +329,9 @@ public IStatus runInUIThread(IProgressMonitor monitor) {
sourcePage.commit(false);
}

ResolverLogger logger = new ResolverLogger();
// Create resolver job and pre-validate
final ResolveJob job = new ResolveJob(model, inputResource);
final ResolveJob job = new ResolveJob(model, inputResource, logger);
IStatus validation = job.validateBeforeRun();
if (!validation.isOK()) {
if (onSave)
Expand All @@ -344,7 +346,7 @@ public IStatus runInUIThread(IProgressMonitor monitor) {
@Override
public IStatus runInUIThread(IProgressMonitor monitor) {
ResolutionResult result = job.getResolutionResult();
ResolutionWizard wizard = new ResolutionWizard(model, file, result);
ResolutionWizard wizard = new ResolutionWizard(model, file, result, logger);

if (onSave) {
// We are in auto-resolve-on-save, only show the
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.bndtools.core.resolve;

import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.IProgressMonitor;
import org.osgi.resource.Capability;
import org.osgi.resource.Requirement;

import biz.aQute.resolve.ResolutionCallback;

public class CancelOperationCallback implements ResolutionCallback {

private IProgressMonitor monitor;

public CancelOperationCallback(IProgressMonitor monitor) {
this.monitor = monitor;
}

@Override
public void processCandidates(Requirement requirement, Set<Capability> wired, List<Capability> candidates) {
if (monitor.isCanceled()) {
throw new ResolveCancelledException();
}
}

}
18 changes: 16 additions & 2 deletions bndtools.core/src/org/bndtools/core/resolve/ResolveJob.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.bndtools.core.resolve;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -17,6 +18,7 @@
import aQute.bnd.osgi.Constants;
import aQute.bnd.osgi.Processor;
import biz.aQute.resolve.ResolutionCallback;
import biz.aQute.resolve.ResolverLogger;
import bndtools.Plugin;

public class ResolveJob extends Job {
Expand All @@ -26,11 +28,13 @@ public class ResolveJob extends Job {
private final List<ResolutionCallback> callbacks = new LinkedList<>();

private ResolutionResult result;
private ResolverLogger logger;

public ResolveJob(BndEditModel model, IResource inputResource) {
public ResolveJob(BndEditModel model, IResource inputResource, ResolverLogger logger) {
super("Resolving " + model.getBndResourceName());
this.model = model;
this.inputResource = inputResource;
this.logger = logger;
}

public IStatus validateBeforeRun() {
Expand Down Expand Up @@ -85,13 +89,23 @@ public IStatus validateBeforeRun() {

@Override
protected IStatus run(IProgressMonitor monitor) {
ResolveOperation operation = new ResolveOperation(model, callbacks);
List<ResolutionCallback> operationCallbacks = new ArrayList<ResolutionCallback>(callbacks.size() + 1);
operationCallbacks.addAll(callbacks);
operationCallbacks.add(new CancelOperationCallback(monitor));
ResolveOperation operation = new ResolveOperation(model, operationCallbacks, logger);
operation.run(monitor);

result = operation.getResult();

return Status.OK_STATUS;
}


@Override
protected void canceling() {
this.getThread().interrupt();
}

public ResolutionResult getResolutionResult() {
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import aQute.bnd.build.model.BndEditModel;
import aQute.lib.exceptions.RunnableWithException;
import biz.aQute.resolve.ResolutionCallback;
import biz.aQute.resolve.ResolverLogger;
import biz.aQute.resolve.RunResolution;
import bndtools.Plugin;

Expand All @@ -27,14 +28,16 @@ public class ResolveOperation implements IRunnableWithProgress {
private final List<ResolutionCallback> callbacks;

private ResolutionResult result;
private ResolverLogger logger;

public ResolveOperation(BndEditModel model) {
this(model, Collections.<ResolutionCallback> emptyList());
this(model, Collections.<ResolutionCallback> emptyList(), null);
}

public ResolveOperation(BndEditModel model, List<ResolutionCallback> callbacks) {
public ResolveOperation(BndEditModel model, List<ResolutionCallback> callbacks, ResolverLogger logger) {
this.model = model;
this.callbacks = callbacks;
this.logger = logger;
}

@Override
Expand All @@ -44,7 +47,8 @@ public void run(IProgressMonitor monitor) {

try {
coordinate(() -> {
RunResolution resolution = RunResolution.resolve(model.getProject(), model.getProperties(), callbacks);
RunResolution resolution = RunResolution.resolve(model.getProject(), model.getProperties(), callbacks,
logger);
if (resolution.isOK()) {
result = new ResolutionResult(Outcome.Resolved, resolution, status);
} else if (resolution.exception instanceof ResolveCancelledException) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package org.bndtools.core.resolve.ui;

import org.bndtools.core.resolve.ResolutionResult;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.service.resolver.ResolutionException;

import biz.aQute.resolve.ResolverLogger;
import bndtools.Plugin;

public class ResolutionCanceledPanel {

private final Image clipboardImg = AbstractUIPlugin
.imageDescriptorFromPlugin(Plugin.PLUGIN_ID, "icons/page_copy.png")
.createImage();
private final Image treeViewImg = AbstractUIPlugin
.imageDescriptorFromPlugin(Plugin.PLUGIN_ID, "icons/tree_mode.gif")
.createImage();
private final Image flatViewImg = AbstractUIPlugin
.imageDescriptorFromPlugin(Plugin.PLUGIN_ID, "icons/flat_mode.gif")
.createImage();

private Composite composite;

private Text processingLog;
private Section sectProcessingLog;
private ResolverLogger logger;
private Section sectProcessingLogFile;
private Button button;

public ResolutionCanceledPanel(ResolverLogger logger) {
this.logger = logger;
}

public void createControl(final Composite parent) {
FormToolkit toolkit = new FormToolkit(parent.getDisplay());
composite = toolkit.createComposite(parent);

composite.setLayout(new GridLayout(1, false));
GridData gd;

sectProcessingLog = toolkit.createSection(composite,
ExpandableComposite.TITLE_BAR | ExpandableComposite.EXPANDED);
sectProcessingLog.setText("Processing Log:");

processingLog = toolkit.createText(sectProcessingLog, "",
SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.READ_ONLY | SWT.V_SCROLL);
sectProcessingLog.setClient(processingLog);

gd = new GridData(SWT.FILL, SWT.FILL, true, true);
gd.widthHint = 600;
gd.heightHint = 300;
sectProcessingLog.setLayoutData(gd);

sectProcessingLogFile = toolkit.createSection(composite,
ExpandableComposite.TITLE_BAR | ExpandableComposite.EXPANDED);
sectProcessingLogFile.setText("Processing Logfile:");
Composite composite = toolkit.createComposite(sectProcessingLogFile);
sectProcessingLogFile.setClient(composite);

button = new Button(composite, SWT.CHECK);
button.setText("Keep Resolver log file after cancel");
button.setToolTipText("This is still a temporary file and will be deleted when Eclipse closes.");

button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
logger.setKeepLogFileTillExit(button.getSelection());
}
});

GridLayout layout = new GridLayout(1, false);
layout.horizontalSpacing = 0;
layout.verticalSpacing = 0;
layout.marginHeight = 0;
layout.marginWidth = 0;
composite.setLayout(layout);

gd = new GridData(SWT.FILL, SWT.FILL, true, true);
gd.widthHint = 600;
gd.heightHint = 100;
sectProcessingLogFile.setLayoutData(gd);
}

public Control getControl() {
return composite;
}

public void setInput(ResolutionResult resolutionResult) {
if (composite == null)
throw new IllegalStateException("Control not created");
else if (composite.isDisposed())
throw new IllegalStateException("Control already disposed");

ResolutionException resolutionException = resolutionResult.getResolutionException();

processingLog.setText(resolutionResult.getLog());
}

public void dispose() {
logger.close();
clipboardImg.dispose();
treeViewImg.dispose();
flatViewImg.dispose();
}
}
Loading

0 comments on commit d3367fe

Please sign in to comment.