GP-1816 - OSGi Bundles - minor refactoring of OSGi bundle code during exploration.

This commit is contained in:
dragonmacher 2022-04-13 12:56:21 -04:00
parent d89764d9bb
commit 2d5f53e051
20 changed files with 629 additions and 583 deletions

View file

@ -18,16 +18,15 @@ package ghidra.app.plugin.core.osgi;
import generic.jar.ResourceFile; import generic.jar.ResourceFile;
/** /**
* An error produced during {@link GhidraBundle#build()} with a time stamp * An error produced during {@link GhidraBundle#build()} with a timestamp.
*/ */
public class BuildError { public class BuildError {
// the lastModified time of the source causing this error // the lastModified time of the source causing this error
private final long lastModified; private final long lastModified;
private final StringBuilder message = new StringBuilder(); private final StringBuilder message = new StringBuilder();
/** /**
* Construct an object to record error message produced for {@code sourceFile} * Construct an object to record error message produced for {@code sourceFile}.
* @param sourceFile the file causing this error * @param sourceFile the file causing this error
*/ */
public BuildError(ResourceFile sourceFile) { public BuildError(ResourceFile sourceFile) {
@ -35,15 +34,15 @@ public class BuildError {
} }
/** /**
* Append {@code str} to the current error message * Append the given string to the current error message.
* * @param s the string to append
* @param str the string to append
*/ */
public void append(String str) { public void append(String s) {
message.append(str); message.append(s);
} }
/** /**
* The error message.
* @return the error message * @return the error message
*/ */
String getMessage() { String getMessage() {
@ -51,6 +50,7 @@ public class BuildError {
} }
/** /**
* The last modified time of the source for this build error.
* @return the last modified time of the source for this build error * @return the last modified time of the source for this build error
*/ */
public long getLastModified() { public long getLastModified() {

View file

@ -15,7 +15,8 @@
*/ */
package ghidra.app.plugin.core.osgi; package ghidra.app.plugin.core.osgi;
import java.io.*; import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.*; import java.util.*;
@ -46,19 +47,19 @@ import ghidra.util.task.TaskMonitor;
* Hosts the embedded OSGi framework and manages {@link GhidraBundle}s. * Hosts the embedded OSGi framework and manages {@link GhidraBundle}s.
* *
* <br/><br/> * <br/><br/>
* note: {@link GhidraBundle}, its implementations, and this class constitute * Note: {@link GhidraBundle}, its implementations, and this class constitute a bridge between
* a bridge between OSGi's {@link Bundle} and Ghidra. * OSGi's {@link Bundle} and Ghidra.
* <ul> * <ul>
* <li> unqualified, "bundle" will mean {@link GhidraBundle} * <li> unqualified, "bundle" will mean {@link GhidraBundle}
* <li> use of OSGi types, including {@link Bundle} and {@link Framework}, should be package * <li> use of OSGi types, including {@link Bundle} and {@link Framework}, should be package scoped
* scoped (not public) * (not public)
* <li> bundle lifecycle is simplified to "active"(same as OSGi "active" state) * <li> bundle lifecycle is simplified to "active"(same as OSGi "active" state) and "inactive"
* and "inactive" (OSGi "uninstalled" state) * (OSGi "uninstalled" state)
* </ul> * </ul>
*/ */
public class BundleHost { public class BundleHost {
public static final String ACTIVATING_BUNDLE_ERROR_MSG = "activating bundle"; public static final String ACTIVATING_BUNDLE_ERROR_MSG = "activating bundle";
protected static final boolean STDERR_DEBUGGING = false; private static final boolean STDERR_DEBUGGING = false;
private static final String SAVE_STATE_TAG_FILE = "BundleHost_FILE"; private static final String SAVE_STATE_TAG_FILE = "BundleHost_FILE";
private static final String SAVE_STATE_TAG_ENABLE = "BundleHost_ENABLE"; private static final String SAVE_STATE_TAG_ENABLE = "BundleHost_ENABLE";
private static final String SAVE_STATE_TAG_ACTIVE = "BundleHost_ACTIVE"; private static final String SAVE_STATE_TAG_ACTIVE = "BundleHost_ACTIVE";
@ -66,22 +67,10 @@ public class BundleHost {
private final BundleMap bundleMap = new BundleMap(); private final BundleMap bundleMap = new BundleMap();
BundleContext frameworkBundleContext; private BundleContext frameworkBundleContext;
Framework felixFramework; private Framework felixFramework;
List<BundleHostListener> listeners = new CopyOnWriteArrayList<>(); private List<BundleHostListener> listeners = new CopyOnWriteArrayList<>();
/** constructor */
public BundleHost() {
//
}
/**
* stop the framework.
*/
public void dispose() {
stopFramework();
}
/** /**
* If a {@link GhidraBundle} hasn't already been added for {@bundleFile}, add it now as a * If a {@link GhidraBundle} hasn't already been added for {@bundleFile}, add it now as a
@ -132,8 +121,8 @@ public class BundleHost {
} }
/** /**
* Assuming there is currently a bundle managed with file {@code bundleFile}, * Assuming there is currently a bundle managed with file {@code bundleFile}, return its
* return its {@link GhidraBundle}, otherwise show an error dialog and return {@code null}. * {@link GhidraBundle}, otherwise show an error dialog and return {@code null}.
* *
* @param bundleFile the bundleFile of the sought bundle * @param bundleFile the bundleFile of the sought bundle
* @return a {@link GhidraBundle} or {@code null} * @return a {@link GhidraBundle} or {@code null}
@ -149,8 +138,8 @@ public class BundleHost {
} }
/** /**
* If there is currently a bundle managed with file {@code bundleFile}, * If there is currently a bundle managed with file {@code bundleFile}, return its
* return its {@link GhidraBundle}, otherwise return {@code null}. * {@link GhidraBundle}, otherwise return {@code null}.
* *
* @param bundleFile the bundleFile of the sought bundle * @param bundleFile the bundleFile of the sought bundle
* @return a {@link GhidraBundle} or {@code null} * @return a {@link GhidraBundle} or {@code null}
@ -180,7 +169,7 @@ public class BundleHost {
} }
/** /**
* Create a new GhidraBundle and add to the list of managed bundles * Create a new GhidraBundle and add to the list of managed bundles.
* *
* @param bundleFile the bundle file * @param bundleFile the bundle file
* @param enabled if the new bundle should be enabled * @param enabled if the new bundle should be enabled
@ -282,7 +271,7 @@ public class BundleHost {
} }
} }
Bundle installFromLoc(String bundleLocation) throws GhidraBundleException { private Bundle installFromLoc(String bundleLocation) throws GhidraBundleException {
try { try {
return frameworkBundleContext.installBundle(bundleLocation); return frameworkBundleContext.installBundle(bundleLocation);
} }
@ -291,17 +280,8 @@ public class BundleHost {
} }
} }
Bundle installAsLoc(String bundleLocation, InputStream contents) throws GhidraBundleException {
try {
return frameworkBundleContext.installBundle(bundleLocation, contents);
}
catch (BundleException e) {
throw new GhidraBundleException(bundleLocation, "installing as bundle location", e);
}
}
/** /**
* return all of the currently managed bundles * Return all of the currently managed bundles.
* *
* @return all the bundles * @return all the bundles
*/ */
@ -310,7 +290,7 @@ public class BundleHost {
} }
/** /**
* return the list of currently managed bundle files * Return the list of currently managed bundle files.
* *
* @return all the bundle files * @return all the bundle files
*/ */
@ -318,14 +298,6 @@ public class BundleHost {
return bundleMap.getBundleFiles(); return bundleMap.getBundleFiles();
} }
void dumpLoadedBundles() {
System.err.printf("=== Bundles ===\n");
for (Bundle bundle : frameworkBundleContext.getBundles()) {
System.err.printf("%s: %s: %s: %s\n", bundle.getBundleId(), bundle.getSymbolicName(),
bundle.getState(), bundle.getVersion());
}
}
/** /**
* Attempt to resolve a list of BundleRequirements with active Bundle capabilities. * Attempt to resolve a list of BundleRequirements with active Bundle capabilities.
* *
@ -370,7 +342,7 @@ public class BundleHost {
return tmpRequirements.isEmpty(); return tmpRequirements.isEmpty();
} }
protected String buildExtraSystemPackages() { private String buildExtraSystemPackages() {
Set<String> packages = new HashSet<>(); Set<String> packages = new HashSet<>();
OSGiUtils.getPackagesFromClasspath(packages); OSGiUtils.getPackagesFromClasspath(packages);
return packages.stream().collect(Collectors.joining(",")); return packages.stream().collect(Collectors.joining(","));
@ -387,11 +359,11 @@ public class BundleHost {
} }
/** /**
* A directory for use by the OSGi framework as a cache * A directory for use by the OSGi framework as a cache.
* *
* @return the directory * @return the directory
*/ */
protected static Path getCacheDir() { private static Path getCacheDir() {
return BundleHost.getOsgiDir().resolve("felixcache"); return BundleHost.getOsgiDir().resolve("felixcache");
} }
@ -401,16 +373,19 @@ public class BundleHost {
return cacheDir.toAbsolutePath().toString(); return cacheDir.toAbsolutePath().toString();
} }
protected void createAndConfigureFramework() throws IOException { private void createAndConfigureFramework() throws IOException {
Properties config = new Properties(); Properties config = new Properties();
// allow multiple bundles w/ the same symbolic name -- location can distinguish // allow multiple bundles w/ the same symbolic name -- location can distinguish
config.setProperty(Constants.FRAMEWORK_BSNVERSION, Constants.FRAMEWORK_BSNVERSION_MULTIPLE); config.setProperty(Constants.FRAMEWORK_BSNVERSION, Constants.FRAMEWORK_BSNVERSION_MULTIPLE);
// use the default, inferred from environment
// config.setProperty(Constants.FRAMEWORK_SYSTEMCAPABILITIES,"osgi.ee; osgi.ee=\"JavaSE\";version:List=\"...\"");
// compute and add everything in the class path. extra packages have lower precedence than imports, // use the default, inferred from environment
// so an Import-Package / @importpackage will override the "living off the land" default // config.setProperty(Constants.FRAMEWORK_SYSTEMCAPABILITIES,
// "osgi.ee; osgi.ee=\"JavaSE\";version:List=\"...\"");
// compute and add everything in the class path. extra packages have lower precedence than
// imports, so an Import-Package / @importpackage will override the "living off the land"
// default
config.setProperty(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, buildExtraSystemPackages()); config.setProperty(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, buildExtraSystemPackages());
// only clean on first startup, o/w keep our storage around // only clean on first startup, o/w keep our storage around
@ -423,41 +398,43 @@ public class BundleHost {
config.put(FelixConstants.LOG_LEVEL_PROP, "1"); config.put(FelixConstants.LOG_LEVEL_PROP, "1");
if (STDERR_DEBUGGING) { if (STDERR_DEBUGGING) {
config.put(FelixConstants.LOG_LEVEL_PROP, "999"); config.put(FelixConstants.LOG_LEVEL_PROP, "999");
// config.put(FelixConstants.LOG_LOGGER_PROP, new org.apache.felix.framework.Logger() {...}); // config.put(FelixConstants.LOG_LOGGER_PROP,
// new org.apache.felix.framework.Logger() {...});
} }
FrameworkFactory factory = new FrameworkFactory(); FrameworkFactory factory = new FrameworkFactory();
felixFramework = factory.newFramework(config); felixFramework = factory.newFramework(config);
} }
protected void addDebuggingListeners() { private void addDebuggingListeners() {
frameworkBundleContext.addFrameworkListener(new FrameworkListener() { if (!STDERR_DEBUGGING) {
@Override return;
public void frameworkEvent(FrameworkEvent event) { }
System.err.printf("%s %s\n", event.getBundle(), event);
frameworkBundleContext.addFrameworkListener(
event -> {
String msg = String.format("AA: %s %s\n", event.getBundle(), event);
Msg.debug(this, msg);
});
frameworkBundleContext.addServiceListener(event -> {
String type = "?";
if (event.getType() == ServiceEvent.REGISTERED) {
type = "registered";
} }
}); else if (event.getType() == ServiceEvent.UNREGISTERING) {
frameworkBundleContext.addServiceListener(new ServiceListener() { type = "unregistering";
@Override
public void serviceChanged(ServiceEvent event) {
String type = "?";
if (event.getType() == ServiceEvent.REGISTERED) {
type = "registered";
}
else if (event.getType() == ServiceEvent.UNREGISTERING) {
type = "unregistering";
}
System.err.printf("%s %s from %s\n", event.getSource(), type,
event.getServiceReference().getBundle().getLocation());
} }
String msg = String.format("BB: %s %s from %s\n", event.getSource(), type,
event.getServiceReference().getBundle().getLocation());
Msg.debug(this, msg);
}); });
} }
/** /**
* start the framework * Start the framework.
* *
* @throws OSGiException framework failures * @throws OSGiException framework failures
* @throws IOException filesystem setup * @throws IOException filesystem setup
@ -469,48 +446,53 @@ public class BundleHost {
felixFramework.init(); felixFramework.init();
} }
catch (BundleException e) { catch (BundleException e) {
throw new OSGiException("initializing felix OSGi framework", e); throw new OSGiException("Exception initializing felix OSGi framework", e);
} }
frameworkBundleContext = felixFramework.getBundleContext(); frameworkBundleContext = felixFramework.getBundleContext();
if (STDERR_DEBUGGING) { addDebuggingListeners();
addDebuggingListeners();
}
frameworkBundleContext Bundle bundle = frameworkBundleContext.getBundle();
.addBundleListener(new MyBundleListener(frameworkBundleContext.getBundle())); frameworkBundleContext.addBundleListener(new MyBundleListener(bundle));
try { try {
felixFramework.start(); felixFramework.start();
} }
catch (BundleException e) { catch (BundleException e) {
throw new OSGiException("starting felix OSGi framework", e); throw new OSGiException("Exception starting felix OSGi framework", e);
} }
} }
/** /**
* stop the OSGi framework synchronously * Stop the OSGi framework.
*
* <p>This may wait for up to 5 seconds for the framework to fully stop. If that timeout
* passes an error will be logged.
*/ */
protected void stopFramework() { public void stopFramework() {
if (felixFramework != null) { if (felixFramework == null) {
try { return;
felixFramework.stop(); }
// any bundles that linger after a few seconds might be the source
// of subtle problems, so wait for them to stop and report any problems. try {
FrameworkEvent event = felixFramework.waitForStop(5000); felixFramework.stop();
if (event.getType() == FrameworkEvent.WAIT_TIMEDOUT) {
Msg.error(this, "Stopping OSGi framework timed out after 5 seconds."); // any bundles that linger after a few seconds might be the source of subtle problems,
} // so wait for them to stop and report any problems.
felixFramework = null; FrameworkEvent event = felixFramework.waitForStop(5000);
} if (event.getType() == FrameworkEvent.WAIT_TIMEDOUT) {
catch (BundleException | InterruptedException e) { Msg.error(this, "Stopping OSGi framework timed-out after 5 seconds.");
Msg.error(this, "Failed to stop OSGi framework.");
e.printStackTrace();
} }
felixFramework = null;
}
catch (BundleException | InterruptedException e) {
Msg.error(this, "Failed to stop OSGi framework.", e);
} }
} }
/** /**
* Gets the host framework.
* @return the OSGi framework * @return the OSGi framework
*/ */
Framework getHostFramework() { Framework getHostFramework() {
@ -565,7 +547,8 @@ public class BundleHost {
} }
/** /**
* Deactivate a bundle. Either an exception is thrown or the bundle will be in "UNINSTALLED" state. * Deactivate a bundle. Either an exception is thrown or the bundle will be in "UNINSTALLED"
* state.
* *
* @param bundle the bundle * @param bundle the bundle
* @throws GhidraBundleException if there's a problem activating * @throws GhidraBundleException if there's a problem activating
@ -575,7 +558,7 @@ public class BundleHost {
return; return;
} }
FrameworkWiring frameworkWiring = felixFramework.adapt(FrameworkWiring.class); FrameworkWiring frameworkWiring = felixFramework.adapt(FrameworkWiring.class);
LinkedList<Bundle> dependentBundles = new LinkedList<Bundle>( LinkedList<Bundle> dependentBundles = new LinkedList<>(
frameworkWiring.getDependencyClosure(Collections.singleton(bundle))); frameworkWiring.getDependencyClosure(Collections.singleton(bundle)));
while (!dependentBundles.isEmpty()) { while (!dependentBundles.isEmpty()) {
Bundle dependentBundle = dependentBundles.pop(); Bundle dependentBundle = dependentBundles.pop();
@ -596,7 +579,8 @@ public class BundleHost {
} }
/** /**
* Deactivate a bundle. Either an exception is thrown or the bundle will be in "UNINSTALLED" state. * Deactivate a bundle. Either an exception is thrown or the bundle will be in "UNINSTALLED"
* state.
* *
* @param bundleLocation the bundle location identifier * @param bundleLocation the bundle location identifier
* @throws InterruptedException if the wait is interrupted * @throws InterruptedException if the wait is interrupted
@ -611,8 +595,8 @@ public class BundleHost {
} }
/** /**
* Refreshes the specified bundles. This forces the update (replacement) * Refreshes the specified bundles. This forces the update (replacement) or removal of packages
* or removal of packages exported by the specified bundles. * exported by the specified bundles.
* *
* @param bundles the bundles to refresh * @param bundles the bundles to refresh
* @see FrameworkWiring#refreshBundles * @see FrameworkWiring#refreshBundles
@ -620,22 +604,19 @@ public class BundleHost {
protected void refreshBundlesSynchronously(Collection<Bundle> bundles) { protected void refreshBundlesSynchronously(Collection<Bundle> bundles) {
FrameworkWiring frameworkWiring = felixFramework.adapt(FrameworkWiring.class); FrameworkWiring frameworkWiring = felixFramework.adapt(FrameworkWiring.class);
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1);
frameworkWiring.refreshBundles(bundles, new FrameworkListener() { frameworkWiring.refreshBundles(bundles, event -> {
@Override if (event.getType() == FrameworkEvent.ERROR) {
public void frameworkEvent(FrameworkEvent event) { Bundle bundle = event.getBundle();
if (event.getType() == FrameworkEvent.ERROR) { Msg.error(BundleHost.this,
Bundle bundle = event.getBundle(); String.format("OSGi error refreshing bundle: %s", bundle));
Msg.error(BundleHost.this,
String.format("OSGi error refreshing bundle: %s", bundle));
}
latch.countDown();
} }
latch.countDown();
}); });
try { try {
latch.await(); latch.await();
} }
catch (InterruptedException e) { catch (InterruptedException e) {
e.printStackTrace(); Msg.error(this, "Exception waiting for bundles to refresh", e);
} }
} }
@ -649,8 +630,11 @@ public class BundleHost {
} }
/** /**
* Activate a set of bundles and any dependencies in topological order. This method doesn't rely on the * Activate a set of bundles and any dependencies in topological order. This method doesn't
* framework, and so will add non-active dependencies. * rely on the framework, and so will add non-active dependencies.
*
* <p>To load bundles without loading inactive dependencies, call
* {@link #activateInStages(Collection, TaskMonitor, PrintWriter)}.
* *
* @param bundles bundles to activate * @param bundles bundles to activate
* @param monitor a task monitor * @param monitor a task monitor
@ -671,9 +655,13 @@ public class BundleHost {
activateSynchronously(bundle.getLocationIdentifier()); activateSynchronously(bundle.getLocationIdentifier());
} }
catch (GhidraBundleException e) { catch (GhidraBundleException e) {
// TODO should we report failing bundles to the console as well so they get logged
// in headless mode?
fireBundleException(e); fireBundleException(e);
} }
catch (Exception e) { catch (Exception e) {
// write the error to the console or log file
console.println("Unexpected error activating bundles: " + bundles);
e.printStackTrace(console); e.printStackTrace(console);
} }
monitor.incrementProgress(1); monitor.incrementProgress(1);
@ -682,7 +670,10 @@ public class BundleHost {
/** /**
* Activate a set of bundles in dependency topological order by resolving against currently * Activate a set of bundles in dependency topological order by resolving against currently
* active bundles in stages. No bundles outside those requested will be activated. * active bundles in stages. <b>No bundles outside those requested will be activated.</b>
*
* <p>To have inactive dependencies loaded, call
* {@link #activateAll(Collection, TaskMonitor, PrintWriter)}.
* *
* @param bundles bundles to activate * @param bundles bundles to activate
* @param monitor a task monitor * @param monitor a task monitor
@ -702,11 +693,13 @@ public class BundleHost {
requirementMap.put(bundle, requirements); requirementMap.put(bundle, requirements);
} }
catch (GhidraBundleException e) { catch (GhidraBundleException e) {
// TODO should we report failing bundles to the console as well so they get logged
// in headless mode?
fireBundleException(e); fireBundleException(e);
} }
} }
List<GhidraBundle> bundlesRemaining = new ArrayList<>(requirementMap.keySet());
List<GhidraBundle> bundlesRemaining = new ArrayList<>(requirementMap.keySet());
monitor.setMaximum(bundlesRemaining.size()); monitor.setMaximum(bundlesRemaining.size());
while (!bundlesRemaining.isEmpty() && !monitor.isCancelled()) { while (!bundlesRemaining.isEmpty() && !monitor.isCancelled()) {
List<GhidraBundle> resolvableBundles = bundlesRemaining.stream() List<GhidraBundle> resolvableBundles = bundlesRemaining.stream()
@ -731,9 +724,13 @@ public class BundleHost {
activateSynchronously(bundle.getLocationIdentifier()); activateSynchronously(bundle.getLocationIdentifier());
} }
catch (GhidraBundleException e) { catch (GhidraBundleException e) {
// TODO should we report failing bundles to the console as well so they get logged
// in headless mode?
fireBundleException(e); fireBundleException(e);
} }
catch (Exception e) { catch (Exception e) {
// write the error to the console or log file
console.println("Unexpected error activating bundles: " + bundles);
e.printStackTrace(console); e.printStackTrace(console);
} }
monitor.incrementProgress(1); monitor.incrementProgress(1);
@ -813,7 +810,7 @@ public class BundleHost {
* *
* <p>Bundles that had been active are reactivated. * <p>Bundles that had been active are reactivated.
* *
* <p>note: This is done once on startup after system bundles have been added. * <p>Note: This is done once on startup after system bundles have been added.
* *
* @param saveState the state object * @param saveState the state object
* @param tool the tool * @param tool the tool
@ -863,7 +860,7 @@ public class BundleHost {
} }
if (!bundlesToActivate.isEmpty()) { if (!bundlesToActivate.isEmpty()) {
TaskLauncher.launchNonModal("restoring bundle state", TaskLauncher.launchNonModal("Restoring bundle state",
(monitor) -> activateInStages(bundlesToActivate, monitor, new NullPrintWriter())); (monitor) -> activateInStages(bundlesToActivate, monitor, new NullPrintWriter()));
} }
} }
@ -896,35 +893,43 @@ public class BundleHost {
saveState.putBooleans(SAVE_STATE_TAG_SYSTEM, bundleIsSystem); saveState.putBooleans(SAVE_STATE_TAG_SYSTEM, bundleIsSystem);
} }
private static class Dependency { //=================================================================================================
// exists only to be distinguished by id // Inner Classes
//=================================================================================================
private static class BundleEdge {
// edge type for dependency graph
} }
/** /**
* Utility class to build a dependency graph from bundles where capabilities map to requirements. * Utility class to build a dependency graph from bundles where capabilities map to
* requirements.
*/ */
private class BundleDependencyGraph extends DirectedMultigraph<GhidraBundle, Dependency> { private class BundleDependencyGraph extends DirectedMultigraph<GhidraBundle, BundleEdge> {
final Map<GhidraBundle, List<BundleCapability>> capabilityMap = new HashMap<>();
final List<GhidraBundle> availableBundles;
final TaskMonitor monitor;
BundleDependencyGraph(Collection<GhidraBundle> startingBundles, TaskMonitor monitor) { private final List<GhidraBundle> availableBundles = new ArrayList<>();
private final Map<GhidraBundle, List<BundleCapability>> capabilityMap = new HashMap<>();
private final TaskMonitor monitor;
BundleDependencyGraph(Collection<GhidraBundle> activatingBundles, TaskMonitor monitor) {
super(null, null, false); super(null, null, false);
this.monitor = monitor; this.monitor = monitor;
// maintain a list of bundles available for resolution, starting with all of the enabled bundles // maintain a list of bundles available for resolution, starting with all of the
this.availableBundles = new ArrayList<>(); // enabled bundles
for (GhidraBundle bundle : getGhidraBundles()) { for (GhidraBundle bundle : getGhidraBundles()) {
if (bundle.isEnabled()) { if (bundle.isEnabled()) {
addToAvailable(bundle); addToAvailable(bundle);
} }
} }
// An edge A->B indicates that the capabilities of A resolve some requirement(s) of B // An edge A->B indicates that the capabilities of A resolve some requirement(s) of B
// "front" accumulates bundles and links to bundles already in the graph that they provide capabilities for. // "front" accumulates bundles and links to bundles already in the graph that they
// e.g. if front[A]=[B,...] then A->B, B is already in the graph, and we will add A next iteration. // provide capabilities for. e.g., if front[A]=[B,...] then A->B, B is already in the
// graph, and we will add A next iteration.
Map<GhidraBundle, Set<GhidraBundle>> front = new HashMap<>(); Map<GhidraBundle, Set<GhidraBundle>> front = new HashMap<>();
for (GhidraBundle bundle : startingBundles) { for (GhidraBundle bundle : activatingBundles) {
front.put(bundle, null); front.put(bundle, null);
} }
@ -939,46 +944,45 @@ public class BundleHost {
handleBackEdges(newFront); handleBackEdges(newFront);
front = newFront; front = newFront;
} }
} }
Iterable<GhidraBundle> inTopologicalOrder() { Iterable<GhidraBundle> inTopologicalOrder() {
return () -> new TopologicalOrderIterator<>(this); return () -> new TopologicalOrderIterator<>(this);
} }
void handleBackEdges(Map<GhidraBundle, Set<GhidraBundle>> newFront) { private void handleBackEdges(Map<GhidraBundle, Set<GhidraBundle>> newFront) {
Iterator<Entry<GhidraBundle, Set<GhidraBundle>>> newFrontIter = Iterator<Entry<GhidraBundle, Set<GhidraBundle>>> it =
newFront.entrySet().iterator(); newFront.entrySet().iterator();
while (newFrontIter.hasNext() && !monitor.isCancelled()) { while (it.hasNext() && !monitor.isCancelled()) {
Entry<GhidraBundle, Set<GhidraBundle>> entry = newFrontIter.next(); Entry<GhidraBundle, Set<GhidraBundle>> entry = it.next();
GhidraBundle source = entry.getKey(); GhidraBundle source = entry.getKey();
if (containsVertex(source)) { if (containsVertex(source)) {
for (GhidraBundle destination : entry.getValue()) { for (GhidraBundle destination : entry.getValue()) {
if (source != destination) { if (source != destination) {
addEdge(source, destination, new Dependency()); addEdge(source, destination, new BundleEdge());
} }
} }
newFrontIter.remove(); it.remove();
} }
} }
} }
void addFront(Map<GhidraBundle, Set<GhidraBundle>> front) { private void addFront(Map<GhidraBundle, Set<GhidraBundle>> front) {
for (Entry<GhidraBundle, Set<GhidraBundle>> e : front.entrySet()) { for (Entry<GhidraBundle, Set<GhidraBundle>> entry : front.entrySet()) {
GhidraBundle source = e.getKey(); GhidraBundle source = entry.getKey();
if (addToAvailable(source)) { if (addToAvailable(source)) {
addVertex(source); addVertex(source);
Set<GhidraBundle> destinations = e.getValue(); Set<GhidraBundle> dependents = entry.getValue();
if (destinations != null) { if (dependents != null) {
for (GhidraBundle destination : destinations) { for (GhidraBundle destination : dependents) {
addEdge(source, destination, new Dependency()); addEdge(source, destination, new BundleEdge());
} }
} }
} }
} }
} }
boolean addToAvailable(GhidraBundle bundle) { private boolean addToAvailable(GhidraBundle bundle) {
try { try {
capabilityMap.put(bundle, bundle.getAllCapabilities()); capabilityMap.put(bundle, bundle.getAllCapabilities());
availableBundles.add(bundle); availableBundles.add(bundle);
@ -990,33 +994,38 @@ public class BundleHost {
} }
} }
// populate newFront with edges depBundle -> bundle, // Populate newFront with edges supplierBundle -> dependentBundle, where supplierBundle has
// where depBundle has a capability that resolves a requirement of bundle // a capability that resolves a requirement of dependentBundle. Items added to newFront are
void resolve(GhidraBundle bundle, Map<GhidraBundle, Set<GhidraBundle>> newFront) { // already in the graph. These items will be added if the given bundle to resolve
// becomes a dependent on this added supplier.
private void resolve(GhidraBundle toResolve,
Map<GhidraBundle, Set<GhidraBundle>> newFront) {
List<BundleRequirement> requirements; List<BundleRequirement> requirements;
try { try {
requirements = new ArrayList<>(bundle.getAllRequirements()); List<BundleRequirement> bundleRequirements = toResolve.getAllRequirements();
if (requirements.isEmpty()) { if (bundleRequirements.isEmpty()) {
return; return; // no dependencies to resolve
} }
requirements = new ArrayList<>(bundleRequirements);
} }
catch (GhidraBundleException e) { catch (GhidraBundleException e) {
fireBundleException(e); fireBundleException(e);
removeVertex(bundle); removeVertex(toResolve);
return; return;
} }
for (GhidraBundle depBundle : availableBundles) { for (GhidraBundle supplierBundle : availableBundles) {
for (BundleCapability capability : capabilityMap.get(depBundle)) { for (BundleCapability capability : capabilityMap.get(supplierBundle)) {
if (monitor.isCancelled()) { if (monitor.isCancelled()) {
return; return;
} }
Iterator<BundleRequirement> reqIter = requirements.iterator(); Iterator<BundleRequirement> it = requirements.iterator();
while (reqIter.hasNext()) { while (it.hasNext()) {
BundleRequirement req = reqIter.next(); BundleRequirement req = it.next();
if (req.matches(capability)) { if (req.matches(capability)) {
newFront.computeIfAbsent(depBundle, b -> new HashSet<>()).add(bundle); newFront.computeIfAbsent(supplierBundle, b -> new HashSet<>())
reqIter.remove(); .add(toResolve);
it.remove();
} }
} }
if (requirements.isEmpty()) { if (requirements.isEmpty()) {
@ -1024,13 +1033,15 @@ public class BundleHost {
} }
} }
} }
// if requirements remain, some will be resolved by system
// and others will generate helpful errors for the user during activation // If requirements remain, some will be resolved by system and others will generate
// helpful errors for the user during activation
} }
} }
/** /**
* The {@code BundleListener} that notifies {@link BundleHostListener}s of bundle activation changes * The {@code BundleListener} that notifies {@link BundleHostListener}s of bundle activation
* changes.
*/ */
private class MyBundleListener implements BundleListener { private class MyBundleListener implements BundleListener {
private final Bundle systemBundle; private final Bundle systemBundle;
@ -1050,8 +1061,10 @@ public class BundleHost {
if (STDERR_DEBUGGING) { if (STDERR_DEBUGGING) {
String symbolicName = osgiBundle.getSymbolicName(); String symbolicName = osgiBundle.getSymbolicName();
String locationIdentifier = osgiBundle.getLocation(); String locationIdentifier = osgiBundle.getLocation();
System.err.printf("%s %s from %s\n", OSGiUtils.getEventTypeString(event), String message =
symbolicName, locationIdentifier); String.format("CC: %s %s from %s\n", OSGiUtils.getEventTypeString(event),
symbolicName, locationIdentifier);
Msg.debug(this, message);
} }
GhidraBundle bundle; GhidraBundle bundle;
switch (event.getType()) { switch (event.getType()) {

View file

@ -45,7 +45,7 @@ public class BundleMap {
bundlesByLocation.put(bundle.getLocationIdentifier(), bundle); bundlesByLocation.put(bundle.getLocationIdentifier(), bundle);
} }
finally { finally {
lock.writeLock().unlock(); writeLock.unlock();
} }
} }
@ -197,6 +197,7 @@ public class BundleMap {
} }
/** /**
* Returns the currently mapped bundles.
* @return the currently mapped bundles * @return the currently mapped bundles
*/ */
public Collection<GhidraBundle> getGhidraBundles() { public Collection<GhidraBundle> getGhidraBundles() {
@ -210,6 +211,7 @@ public class BundleMap {
} }
/** /**
* Returns the currently mapped bundle files.
* @return the currently mapped bundle files * @return the currently mapped bundle files
*/ */
public Collection<ResourceFile> getBundleFiles() { public Collection<ResourceFile> getBundleFiles() {

View file

@ -45,7 +45,7 @@ import resources.Icons;
import resources.ResourceManager; import resources.ResourceManager;
/** /**
* component for managing OSGi bundle status * Component for managing OSGi bundle status
*/ */
public class BundleStatusComponentProvider extends ComponentProviderAdapter { public class BundleStatusComponentProvider extends ComponentProviderAdapter {
@ -289,7 +289,7 @@ public class BundleStatusComponentProvider extends ComponentProviderAdapter {
files.stream().map(ResourceFile::new).collect(Collectors.toUnmodifiableList()); files.stream().map(ResourceFile::new).collect(Collectors.toUnmodifiableList());
Collection<GhidraBundle> bundles = bundleHost.add(resourceFiles, true, false); Collection<GhidraBundle> bundles = bundleHost.add(resourceFiles, true, false);
TaskLauncher.launchNonModal("activating new bundles", (monitor) -> { TaskLauncher.launchNonModal("Activating new bundles", (monitor) -> {
bundleHost.activateAll(bundles, monitor, bundleHost.activateAll(bundles, monitor,
getTool().getService(ConsoleService.class).getStdErr()); getTool().getService(ConsoleService.class).getStdErr());
}); });
@ -339,9 +339,6 @@ public class BundleStatusComponentProvider extends ComponentProviderAdapter {
return panel; return panel;
} }
/**
* cleanup this component
*/
public void dispose() { public void dispose() {
filterPanel.dispose(); filterPanel.dispose();
} }
@ -351,9 +348,10 @@ public class BundleStatusComponentProvider extends ComponentProviderAdapter {
} }
/** /**
* This is for testing only! during normal execution, statuses are only added through BundleHostListener bundle(s) added events. * This is for testing only! during normal execution, statuses are only added through
* BundleHostListener bundle(s) added events.
* *
* <p>each new bundle will be enabled and writable * <p>Each new bundle will be enabled and writable
* *
* @param bundleFiles the files to use * @param bundleFiles the files to use
*/ */
@ -363,6 +361,10 @@ public class BundleStatusComponentProvider extends ComponentProviderAdapter {
.collect(Collectors.toList())); .collect(Collectors.toList()));
} }
//=================================================================================================
// Inner Classes
//=================================================================================================
private final class RemoveBundlesTask extends Task { private final class RemoveBundlesTask extends Task {
private final DeactivateAndDisableBundlesTask deactivateBundlesTask; private final DeactivateAndDisableBundlesTask deactivateBundlesTask;
private final List<BundleStatus> statuses; private final List<BundleStatus> statuses;
@ -378,7 +380,7 @@ public class BundleStatusComponentProvider extends ComponentProviderAdapter {
public void run(TaskMonitor monitor) throws CancelledException { public void run(TaskMonitor monitor) throws CancelledException {
deactivateBundlesTask.run(monitor); deactivateBundlesTask.run(monitor);
monitor.checkCanceled(); monitor.checkCanceled();
// partition bundles into system (bundles.get(true)) and non-system (bundles.get(false)). // partition bundles into system (bundles.get(true)) / non-system (bundles.get(false))
Map<Boolean, List<GhidraBundle>> bundles = statuses.stream() Map<Boolean, List<GhidraBundle>> bundles = statuses.stream()
.map(bs -> bundleHost.getExistingGhidraBundle(bs.getFile())) .map(bs -> bundleHost.getExistingGhidraBundle(bs.getFile()))
.collect(Collectors.partitioningBy(GhidraBundle::isSystemBundle)); .collect(Collectors.partitioningBy(GhidraBundle::isSystemBundle));
@ -483,8 +485,8 @@ public class BundleStatusComponentProvider extends ComponentProviderAdapter {
} }
/* /*
* Activating/deactivating a single bundle doesn't require resolving dependents, * Activating/deactivating a single bundle doesn't require resolving dependents, so this task
* so this task is slightly different from the others. * is slightly different from the others.
*/ */
private class ActivateDeactivateBundleTask extends Task { private class ActivateDeactivateBundleTask extends Task {
private final BundleStatus status; private final BundleStatus status;

View file

@ -22,7 +22,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.swing.*; import javax.swing.JLabel;
import javax.swing.event.TableModelEvent; import javax.swing.event.TableModelEvent;
import org.osgi.framework.Bundle; import org.osgi.framework.Bundle;
@ -32,8 +32,10 @@ import generic.jar.ResourceFile;
import generic.util.Path; import generic.util.Path;
import ghidra.docking.settings.Settings; import ghidra.docking.settings.Settings;
import ghidra.framework.plugintool.ServiceProvider; import ghidra.framework.plugintool.ServiceProvider;
import ghidra.util.*; import ghidra.util.Swing;
import ghidra.util.table.column.*; import ghidra.util.SystemUtilities;
import ghidra.util.table.column.AbstractGColumnRenderer;
import ghidra.util.table.column.GColumnRenderer;
/** /**
* Model for {@link BundleStatus} objects. * Model for {@link BundleStatus} objects.
@ -73,10 +75,10 @@ public class BundleStatusTableModel
} }
private BundleStatus getStatus(GhidraBundle bundle) { private BundleStatus getStatus(GhidraBundle bundle) {
return getStatusFromLoc(bundle.getLocationIdentifier()); return getStatusFromLocation(bundle.getLocationIdentifier());
} }
private BundleStatus getStatusFromLoc(String bundleLoc) { private BundleStatus getStatusFromLocation(String bundleLoc) {
return bundleLocToStatusMap.get(bundleLoc); return bundleLocToStatusMap.get(bundleLoc);
} }
@ -254,7 +256,7 @@ public class BundleStatusTableModel
// wrap the assigned comparator to detect if the order changes // wrap the assigned comparator to detect if the order changes
AtomicBoolean changed = new AtomicBoolean(false); AtomicBoolean changed = new AtomicBoolean(false);
Comparator<BundleStatus> wrapper = new Comparator<BundleStatus>() { Comparator<BundleStatus> wrapper = new Comparator<>() {
Comparator<BundleStatus> comparator = sortingContext.getComparator(); Comparator<BundleStatus> comparator = sortingContext.getComparator();
@Override @Override
@ -367,7 +369,7 @@ public class BundleStatusTableModel
@Override @Override
public void bundleException(GhidraBundleException exception) { public void bundleException(GhidraBundleException exception) {
Swing.runLater(() -> { Swing.runLater(() -> {
BundleStatus status = getStatusFromLoc(exception.getBundleLocation()); BundleStatus status = getStatusFromLocation(exception.getBundleLocation());
if (status != null) { if (status != null) {
status.setSummary(exception.getMessage()); status.setSummary(exception.getMessage());
int rowIndex = getRowIndex(status); int rowIndex = getRowIndex(status);

View file

@ -30,8 +30,20 @@ import generic.jar.ResourceFile;
*/ */
public abstract class GhidraBundle { public abstract class GhidraBundle {
protected final ResourceFile file; /**
* A {@link GhidraBundle} can be
* <ul>
* <li>a Bndtools .bnd script</li>
* <li>an OSGi bundle .jar file</li>
* <li>a directory of Java source</li>
* </ul>
*
*/
enum Type {
BND_SCRIPT, JAR, SOURCE_DIR, INVALID
}
protected final ResourceFile bundleFile; // can be a dir or a jar file
protected final BundleHost bundleHost; protected final BundleHost bundleHost;
protected boolean enabled; protected boolean enabled;
protected boolean systemBundle; protected boolean systemBundle;
@ -39,29 +51,29 @@ public abstract class GhidraBundle {
GhidraBundle(BundleHost bundleHost, ResourceFile bundleFile, boolean enabled, GhidraBundle(BundleHost bundleHost, ResourceFile bundleFile, boolean enabled,
boolean systemBundle) { boolean systemBundle) {
this.bundleHost = bundleHost; this.bundleHost = bundleHost;
this.file = bundleFile; this.bundleFile = bundleFile;
this.enabled = enabled; this.enabled = enabled;
this.systemBundle = systemBundle; this.systemBundle = systemBundle;
} }
/** /**
* clean build artifacts generated during build of this bundle * Clean build artifacts generated during build of this bundle.
* *
* @return true if anything was done * @return true if anything was done
*/ */
abstract boolean clean(); abstract boolean clean();
/** /**
* build OSGi bundle if possible * Build OSGi bundle if needed and if possible.
* *
* @param writer console for build messages to user * @param writer console for build messages to user
* @return true if build happened, false if already built * @return true if build happened, false if already built or could not build
* @throws Exception if the build cannot complete * @throws Exception if the build cannot complete
*/ */
public abstract boolean build(PrintWriter writer) throws Exception; public abstract boolean build(PrintWriter writer) throws Exception;
/** /**
* same as {@link #build(PrintWriter)} with writer = {@link System#err}. * Same as {@link #build(PrintWriter)} with writer = {@link System#err}.
* *
* @return true if build happened, false if already built * @return true if build happened, false if already built
* @throws Exception if the build cannot complete * @throws Exception if the build cannot complete
@ -74,28 +86,43 @@ public abstract class GhidraBundle {
* Return the location identifier of the bundle that this GhidraBundle represents. * Return the location identifier of the bundle that this GhidraBundle represents.
* *
* <p>The location identifier is used by the framework, e.g. it is passed to * <p>The location identifier is used by the framework, e.g. it is passed to
* {@link org.osgi.framework.BundleContext#installBundle} when the bundle is * {@link org.osgi.framework.BundleContext#installBundle} when the bundle is first installed.
* first installed.
* *
* <p>Although the bundle location is a URI, outside of interactions with the framework, * <p>Although the bundle location is a URI, outside of interactions with the framework, the
* the bundle location should remain opaque. * bundle location should remain opaque.
* *
* @return location identifier of this bundle * @return location identifier of this bundle
*/ */
public abstract String getLocationIdentifier(); public abstract String getLocationIdentifier();
/**
* Returns all bundle requirements.
*
* @return the requirements
* @throws GhidraBundleException if there is an exception parsing / loading bundle requirements
*/
public abstract List<BundleRequirement> getAllRequirements() throws GhidraBundleException; public abstract List<BundleRequirement> getAllRequirements() throws GhidraBundleException;
/**
* Returns all bundle capabilities.
*
* @return the capabilities
* @throws GhidraBundleException if there is an exception parsing / loading bundle capabilities
*/
public abstract List<BundleCapability> getAllCapabilities() throws GhidraBundleException; public abstract List<BundleCapability> getAllCapabilities() throws GhidraBundleException;
/** /**
* @return the file where this bundle is loaded from * The file where this bundle is loaded from.
*
* @return the file from where this bundle is loaded
*/ */
public ResourceFile getFile() { public ResourceFile getFile() {
return file; return bundleFile;
} }
/** /**
* True if this bundle is enabled.
*
* @return true if this bundle is enabled * @return true if this bundle is enabled
*/ */
public boolean isEnabled() { public boolean isEnabled() {
@ -103,7 +130,7 @@ public abstract class GhidraBundle {
} }
/** /**
* set the enablement flag for this bundle. * Set the enablement flag for this bundle.
* *
* <p>If a bundle is enabled its contents will be scanned, e.g. for scripts. * <p>If a bundle is enabled its contents will be scanned, e.g. for scripts.
* *
@ -123,7 +150,7 @@ public abstract class GhidraBundle {
} }
/** /**
* Get the type of a GhidraBundle from its file. * Get the type of {@link GhidraBundle} from its file.
* *
* @param file a bundle file * @param file a bundle file
* @return the type * @return the type
@ -163,8 +190,8 @@ public abstract class GhidraBundle {
} }
/** /**
* Get the OSGi bundle represented by this GhidraBundle or null if it isn't in * Get the OSGi bundle represented by this GhidraBundle or null if it isn't in the "installed"
* the "installed" state. * state.
* *
* @return a Bundle or null * @return a Bundle or null
*/ */
@ -173,6 +200,8 @@ public abstract class GhidraBundle {
} }
/** /**
* True if this bundle is active.
*
* @return true if this bundle is active * @return true if this bundle is active
*/ */
public boolean isActive() { public boolean isActive() {
@ -180,17 +209,8 @@ public abstract class GhidraBundle {
return (bundle != null) && bundle.getState() == Bundle.ACTIVE; return (bundle != null) && bundle.getState() == Bundle.ACTIVE;
} }
/** @Override
* A GhidraBundle can be public String toString() {
* <ul> return getOSGiBundle().getSymbolicName();
* <li>a Bndtools .bnd script</li>
* <li>an OSGi bundle .jar file</li>
* <li>a directory of Java source</li>
* </ul>
*
*/
enum Type {
BND_SCRIPT, JAR, SOURCE_DIR, INVALID
} }
} }

View file

@ -67,7 +67,7 @@ public class GhidraJarBundle extends GhidraBundle {
} }
protected ManifestParser createManifestParser() throws GhidraBundleException { protected ManifestParser createManifestParser() throws GhidraBundleException {
try (Jar jar = new Jar(file.getFile(true))) { try (Jar jar = new Jar(bundleFile.getFile(true))) {
Manifest manifest = jar.getManifest(); Manifest manifest = jar.getManifest();
if (manifest == null) { if (manifest == null) {
throw new GhidraBundleException(bundleLocation, "jar bundle with no manifest"); throw new GhidraBundleException(bundleLocation, "jar bundle with no manifest");

View file

@ -281,5 +281,4 @@ public class OSGiUtils {
Msg.error(OSGiUtils.class, "Error while collecting packages from jar", e); Msg.error(OSGiUtils.class, "Error while collecting packages from jar", e);
} }
} }
} }

View file

@ -1166,7 +1166,7 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
} }
} }
class RefreshingBundleHostListener implements BundleHostListener { private class RefreshingBundleHostListener implements BundleHostListener {
@Override @Override
public void bundleBuilt(GhidraBundle bundle, String summary) { public void bundleBuilt(GhidraBundle bundle, String summary) {
@ -1179,8 +1179,8 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
GhidraSourceBundle sourceBundle = (GhidraSourceBundle) bundle; GhidraSourceBundle sourceBundle = (GhidraSourceBundle) bundle;
ResourceFile sourceDirectory = sourceBundle.getFile(); ResourceFile sourceDirectory = sourceBundle.getFile();
if (summary == null) { if (summary == null) {
// a null summary means the build didn't change anything, // a null summary means the build didn't change anything, so use any errors from
// so use any errors from the last build // the last build
for (ResourceFile sourceFile : sourceBundle.getAllErrors().keySet()) { for (ResourceFile sourceFile : sourceBundle.getAllErrors().keySet()) {
if (sourceFile.getParentFile().equals(sourceDirectory)) { if (sourceFile.getParentFile().equals(sourceDirectory)) {
ScriptInfo scriptInfo = infoManager.getScriptInfo(sourceFile); ScriptInfo scriptInfo = infoManager.getScriptInfo(sourceFile);

View file

@ -83,7 +83,7 @@ public class GhidraScriptUtil {
} }
/** /**
* initialize state of GhidraScriptUtil with user, system paths, and optional extra system paths. * Initialize state of GhidraScriptUtil with user, system, and optional extra system paths.
* *
* @param aBundleHost the host to use * @param aBundleHost the host to use
* @param extraSystemPaths additional system paths for this run, can be null * @param extraSystemPaths additional system paths for this run, can be null
@ -106,7 +106,7 @@ public class GhidraScriptUtil {
*/ */
public static void dispose() { public static void dispose() {
if (bundleHost != null) { if (bundleHost != null) {
bundleHost.dispose(); bundleHost.stopFramework();
bundleHost = null; bundleHost = null;
} }
providers = null; providers = null;

View file

@ -121,8 +121,8 @@ public class JavaScriptProvider extends GhidraScriptProvider {
throw new ClassNotFoundException( throw new ClassNotFoundException(
"Failed to find source bundle containing script: " + sourceFile.toString()); "Failed to find source bundle containing script: " + sourceFile.toString());
} }
bundleHost.activateAll(Collections.singletonList(bundle), TaskMonitor.DUMMY, writer);
bundleHost.activateAll(Collections.singletonList(bundle), TaskMonitor.DUMMY, writer);
String classname = bundle.classNameForScript(sourceFile); String classname = bundle.classNameForScript(sourceFile);
Class<?> clazz = bundle.getOSGiBundle().loadClass(classname); // throws ClassNotFoundException Class<?> clazz = bundle.getOSGiBundle().loadClass(classname); // throws ClassNotFoundException
return clazz; return clazz;

View file

@ -87,7 +87,7 @@ public class BundleHostTest extends AbstractGhidraHeadlessIntegrationTest {
@After @After
public void tearDown() { public void tearDown() {
bundleHost.dispose(); bundleHost.stopFramework();
capturingBundleHostListener = null; capturingBundleHostListener = null;
bundleHost = null; bundleHost = null;

View file

@ -435,11 +435,11 @@ public class BundleStatusManagerTest extends AbstractGhidraScriptMgrPluginTest {
} }
void awaitActivation() throws InterruptedException { void awaitActivation() throws InterruptedException {
assertTrue(activationLatch.await(5000, TimeUnit.MILLISECONDS)); assertTrue(activationLatch.await(30000, TimeUnit.MILLISECONDS));
} }
void awaitDisablement() throws InterruptedException { void awaitDisablement() throws InterruptedException {
assertTrue(disablementLatch.await(5000, TimeUnit.MILLISECONDS)); assertTrue(disablementLatch.await(30000, TimeUnit.MILLISECONDS));
} }
} }

View file

@ -18,6 +18,7 @@ package ghidra.app.plugin.core.script;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.io.*; import java.io.*;
import java.nio.file.Path;
import org.apache.logging.log4j.*; import org.apache.logging.log4j.*;
import org.apache.logging.log4j.core.config.Configurator; import org.apache.logging.log4j.core.config.Configurator;
@ -150,7 +151,7 @@ public class GhidraScriptMgrPlugin2Test extends AbstractGhidraScriptMgrPluginTes
// remove all class files from the user script bin dir // remove all class files from the user script bin dir
File userScriptsBinDir = File userScriptsBinDir =
GhidraSourceBundle.getBindirFromScriptFile(new ResourceFile(newScriptFile)).toFile(); getBinDirFromScriptFile(new ResourceFile(newScriptFile)).toFile();
File[] userScriptBinDirFiles; File[] userScriptBinDirFiles;
if (userScriptsBinDir.exists()) { if (userScriptsBinDir.exists()) {
userScriptBinDirFiles = userScriptsBinDir.listFiles(classFileFilter); userScriptBinDirFiles = userScriptsBinDir.listFiles(classFileFilter);
@ -195,7 +196,7 @@ public class GhidraScriptMgrPlugin2Test extends AbstractGhidraScriptMgrPluginTes
// verify that the generated class file is placed in the default scripting home/bin // verify that the generated class file is placed in the default scripting home/bin
File userScriptsBinDir = File userScriptsBinDir =
GhidraSourceBundle.getBindirFromScriptFile(systemScriptFile).toFile(); getBinDirFromScriptFile(systemScriptFile).toFile();
String className = scriptName.replace(".java", ".class"); String className = scriptName.replace(".java", ".class");
File expectedClassFile = new File(userScriptsBinDir, className); File expectedClassFile = new File(userScriptsBinDir, className);
@ -232,7 +233,7 @@ public class GhidraScriptMgrPlugin2Test extends AbstractGhidraScriptMgrPluginTes
waitForScriptCompletion(scriptID, 20000); waitForScriptCompletion(scriptID, 20000);
// verify a bin dir was created and that the class file is in it // verify a bin dir was created and that the class file is in it
File binDir = GhidraSourceBundle.getBindirFromScriptFile(newScriptFile).toFile(); File binDir = getBinDirFromScriptFile(newScriptFile).toFile();
assertTrue("bin output dir not created", binDir.exists()); assertTrue("bin output dir not created", binDir.exists());
File scriptClassFile = new File(binDir, rawScriptName + ".class"); File scriptClassFile = new File(binDir, rawScriptName + ".class");
@ -493,4 +494,11 @@ public class GhidraScriptMgrPlugin2Test extends AbstractGhidraScriptMgrPluginTes
assertContainsText("The field of the script still has state--the script was not recreated", assertContainsText("The field of the script still has state--the script was not recreated",
"*2*", output); "*2*", output);
} }
private Path getBinDirFromScriptFile(ResourceFile sourceFile) {
ResourceFile tmpSourceDir = sourceFile.getParentFile();
String tmpSymbolicName = GhidraSourceBundle.sourceDirHash(tmpSourceDir);
return GhidraSourceBundle.getCompiledBundlesDir().resolve(tmpSymbolicName);
}
} }

View file

@ -1,6 +1,5 @@
/* ### /* ###
* IP: GHIDRA * IP: GHIDRA
* REVIEWED: YES
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -24,69 +23,62 @@ import java.io.*;
* *
* *
*/ */
@Deprecated(forRemoval = true, since = "10.2") // This is not used
public class JavaCompiler { public class JavaCompiler {
private IOThread cmdOut; private IOThread cmdOut;
private IOThread cmdErr; private IOThread cmdErr;
/** public void compile(File javaFile) {
* Compile a java file.
*/
public void compile(File javaFile) {
String name = javaFile.getName(); String name = javaFile.getName();
String className = name.substring(0, name.indexOf(".")) + ".class"; String className = name.substring(0, name.indexOf(".")) + ".class";
File parent = javaFile.getParentFile(); File parent = javaFile.getParentFile();
String parentPath = parent.getAbsolutePath(); String parentPath = parent.getAbsolutePath();
int pos = parentPath.lastIndexOf("ghidra"); int pos = parentPath.lastIndexOf("ghidra");
String destPath = parentPath.substring(0, pos-1); String destPath = parentPath.substring(0, pos - 1);
javaFile.deleteOnExit(); javaFile.deleteOnExit();
File classFile = new File(parent, className); File classFile = new File(parent, className);
classFile.deleteOnExit(); classFile.deleteOnExit();
String classpath = System.getProperty("java.class.path"); String classpath = System.getProperty("java.class.path");
String javaLoc = System.getProperty("java.home"); String javaLoc = System.getProperty("java.home");
if (javaLoc.endsWith("jre")) { if (javaLoc.endsWith("jre")) {
javaLoc = javaLoc.substring(0, javaLoc.indexOf("jre")-1); javaLoc = javaLoc.substring(0, javaLoc.indexOf("jre") - 1);
} }
String argV[] = new String[6]; String argV[] = new String[6];
argV[0] = javaLoc + File.separator + "bin" + File.separator +"javac"; argV[0] = javaLoc + File.separator + "bin" + File.separator + "javac";
argV[1] = "-classpath"; argV[1] = "-classpath";
argV[2] = classpath; argV[2] = classpath;
argV[3] = "-d"; argV[3] = "-d";
argV[4] = destPath; argV[4] = destPath;
argV[5] = javaFile.getAbsolutePath(); argV[5] = javaFile.getAbsolutePath();
try { try {
Process p = Runtime.getRuntime().exec(argV); Process p = Runtime.getRuntime().exec(argV);
for (int i=0; i<argV.length; i++) { for (String element : argV) {
System.out.print(argV[i] + " "); System.out.print(element + " ");
} }
System.out.println(); System.out.println();
InputStream stderrStream = p.getErrorStream(); InputStream stderrStream = p.getErrorStream();
InputStream stdinStream = p.getInputStream(); InputStream stdinStream = p.getInputStream();
setupIO(stdinStream, stderrStream); setupIO(stdinStream, stderrStream);
p.waitFor(); p.waitFor();
cmdOut.join(); cmdOut.join();
cmdErr.join(); cmdErr.join();
} catch (Exception e) { }
catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
/** private void setupIO(InputStream stdin, InputStream stderr) {
* Set up threads to read from stdin and stderr and output that
* to stdout.
* @param stdin
* @param stderr
*/
private void setupIO (InputStream stdin, InputStream stderr){
cmdOut = new IOThread(stdin); // cmdOut = new IOThread(stdin); //
cmdErr = new IOThread(stderr); cmdErr = new IOThread(stderr);
cmdOut.start(); cmdOut.start();
@ -100,19 +92,19 @@ public class JavaCompiler {
private class IOThread extends Thread { private class IOThread extends Thread {
private BufferedReader shellOutput; private BufferedReader shellOutput;
public IOThread (InputStream input) { public IOThread(InputStream input) {
shellOutput = new BufferedReader(new InputStreamReader(input)); shellOutput = new BufferedReader(new InputStreamReader(input));
} }
@Override @Override
public void run() { public void run() {
String line = null; String line = null;
try { try {
while ((line = shellOutput.readLine()) != null) { while ((line = shellOutput.readLine()) != null) {
System.out.println(line); System.out.println(line);
} }
} }
catch(Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View file

@ -19,6 +19,10 @@ import java.io.PrintWriter;
public class NullPrintWriter extends PrintWriter { public class NullPrintWriter extends PrintWriter {
public static PrintWriter dummyIfNull(PrintWriter pw) {
return pw == null ? new NullPrintWriter() : pw;
}
public NullPrintWriter() { public NullPrintWriter() {
super(new NullWriter()); super(new NullWriter());
} }

View file

@ -20,11 +20,9 @@ import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.*;
import java.util.Properties;
import ghidra.util.HTMLUtilities; import ghidra.util.HTMLUtilities;
import ghidra.util.SystemUtilities;
public class FileLocker { public class FileLocker {
@ -91,28 +89,16 @@ public class FileLocker {
Properties properties = new Properties(); Properties properties = new Properties();
InputStream is = null; try (InputStream is = new FileInputStream(lockFile)) {
try {
is = new FileInputStream(lockFile);
properties.load(is); properties.load(is);
return properties; return properties;
} }
catch (FileNotFoundException e) { catch (FileNotFoundException e) {
// should never happen // should not happen
} }
catch (IOException e) { catch (IOException e) {
// ignore // ignore
} }
finally {
if (is != null) {
try {
is.close();
}
catch (IOException e) {
// we tried!
}
}
}
return null; return null;
} }
@ -178,25 +164,13 @@ public class FileLocker {
private boolean storeProperties(Properties properties) { private boolean storeProperties(Properties properties) {
OutputStream os = null; try (OutputStream os = new FileOutputStream(lockFile)) {
try {
os = new FileOutputStream(lockFile);
properties.store(os, "Ghidra Lock File"); properties.store(os, "Ghidra Lock File");
return true; return true;
} }
catch (IOException e) { catch (IOException e) {
return false; return false;
} }
finally {
if (os != null) {
try {
os.close();
}
catch (IOException e) {
// don't care; we tried
}
}
}
} }
private boolean isLockOwner() { private boolean isLockOwner() {
@ -212,7 +186,7 @@ public class FileLocker {
for (String key : PROPERTY_KEYS) { for (String key : PROPERTY_KEYS) {
String originalProperty = createdLockProperties.getProperty(key); String originalProperty = createdLockProperties.getProperty(key);
String currentProperty = currentLockProperties.getProperty(key); String currentProperty = currentLockProperties.getProperty(key);
if (!SystemUtilities.isEqual(originalProperty, currentProperty)) { if (!Objects.equals(originalProperty, currentProperty)) {
return false; return false;
} }
} }

View file

@ -1,6 +1,5 @@
/* ### /* ###
* IP: GHIDRA * IP: GHIDRA
* REVIEWED: YES
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,12 +15,12 @@
*/ */
package ghidra.framework.project; package ghidra.framework.project;
import java.io.File;
import generic.util.FileLocker; import generic.util.FileLocker;
import generic.util.LockFactory; import generic.util.LockFactory;
import ghidra.framework.model.ProjectLocator; import ghidra.framework.model.ProjectLocator;
import java.io.File;
/** /**
* A simple delegate for creating and using locks in Ghidra. * A simple delegate for creating and using locks in Ghidra.
*/ */
@ -29,7 +28,7 @@ class ProjectLock {
private final File lockFile; private final File lockFile;
FileLocker locker; private FileLocker locker;
public ProjectLock(ProjectLocator projectLocator) { public ProjectLock(ProjectLocator projectLocator) {
this.lockFile = projectLocator.getProjectLockFile(); this.lockFile = projectLocator.getProjectLockFile();

View file

@ -880,7 +880,8 @@ public final class FileUtilities {
* *
* @param f1 the parent file * @param f1 the parent file
* @param f2 the child file * @param f2 the child file
* @return the portion of the second file that trails the full path of the first file. * @return the portion of the second file that trails the full path of the first file; null as
* described above
* @throws IOException if there is an error canonicalizing the path * @throws IOException if there is an error canonicalizing the path
*/ */
public static String relativizePath(File f1, File f2) throws IOException { public static String relativizePath(File f1, File f2) throws IOException {
@ -903,17 +904,18 @@ public final class FileUtilities {
} }
/** /**
* Return the relative path string of one resource file in another. If * Return the relative path string of one resource file in another. If no path can be
* no path can be constructed or the files are the same, then null is returned. * constructed or the files are the same, then null is returned.
* *
* Note: unlike {@link #relativizePath(File, File)}, this function does not resolve symbolic links. * Note: unlike {@link #relativizePath(File, File)}, this function does not resolve symbolic
* links.
* *
* <P>For example, given, in this order, two files with these paths * <P>For example, given, in this order, two files with these paths
* <code>/a/b</code> and <code>/a/b/c</code>, this method will return 'c'. * <code>/a/b</code> and <code>/a/b/c</code>, this method will return 'c'.
* *
* @param f1 the parent resource file * @param f1 the parent resource file
* @param f2 the child resource file * @param f2 the child resource file
* @return the relative path of {@code f2} in {@code f1} * @return the relative path of {@code f2} in {@code f1}; null if f1 is not a parent of f2
*/ */
public static String relativizePath(ResourceFile f1, ResourceFile f2) { public static String relativizePath(ResourceFile f1, ResourceFile f2) {
StringBuilder sb = new StringBuilder(f2.getName()); StringBuilder sb = new StringBuilder(f2.getName());