Skip to content

Commit

Permalink
Convert BndExportJarHandler into reusable UI component
Browse files Browse the repository at this point in the history
Currently the BndExportJarHandler is bound to Bndtools tooling
completely, but now using the adapter pattern can be used in any context
where a IProject can be adapted to (bnd) Project

Signed-off-by: Christoph Läubrich <laeubi@laeubi-soft.de>
  • Loading branch information
laeubi committed Nov 15, 2023
1 parent 1e9abcc commit 26f297a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
8 changes: 4 additions & 4 deletions bndtools.core/src/bndtools/command/BndExportJarHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.resources.WorkspaceJob;
import org.eclipse.core.runtime.Adapters;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
Expand All @@ -24,7 +25,6 @@
import aQute.lib.io.IO;
import biz.aQute.resolve.Bndrun;
import bndtools.Plugin;
import bndtools.central.Central;
import bndtools.launch.util.LaunchUtils;

public class BndExportJarHandler extends AbstractHandler {
Expand All @@ -47,8 +47,8 @@ public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {

File exportDir = bndrun.getBase();

if (Central.isBndProject(project)) {
Project bndProject = Central.getProject(project);
Project bndProject = Adapters.adapt(project, Project.class);
if (bndProject != null) {
bndrun.setBase(bndProject.getBase());
exportDir = IO.getBasedFile(bndProject.getTargetDir(), "export");
exportDir.mkdirs();
Expand All @@ -61,7 +61,7 @@ public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
File exported = IO.getBasedFile(exportDir, export.getKey());
resource.write(exported);
exported.setLastModified(resource.lastModified());
if (Central.isBndProject(project)) {
if (bndProject != null) {
project.refreshLocal(IResource.DEPTH_INFINITE, monitor);
}
}
Expand Down
17 changes: 14 additions & 3 deletions bndtools.core/src/bndtools/internal/BndAdapter.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package bndtools.internal;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdapterFactory;
import org.osgi.service.component.annotations.Component;

import aQute.bnd.build.Project;
import bndtools.Activator;
import bndtools.Plugin;
import bndtools.central.Central;

@Component(service = IAdapterFactory.class, property = {
@Component(property = {
IAdapterFactory.SERVICE_PROPERTY_ADAPTABLE_CLASS + "=org.eclipse.core.resources.IProject",
IAdapterFactory.SERVICE_PROPERTY_ADAPTER_NAMES + "=aQute.bnd.build.Project"
})
Expand All @@ -22,10 +24,19 @@ public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) {
if (project.hasNature(Plugin.BNDTOOLS_NATURE)) {
// this project is for sure a bndtools project ...
Project bndproject = Central.getProject(project);
return adapterType.cast(project);
return adapterType.cast(bndproject);
}
} catch (Exception e) {
// can't adapt that project then...
if (e instanceof CoreException core) {
Activator.getDefault()
.getLog()
.log(core.getStatus());

} else {
Activator.getDefault()
.getLog()
.error("Adaption of " + adaptableObject + " as a bndtools project failed", e);
}
}
}
}
Expand Down

0 comments on commit 26f297a

Please sign in to comment.