Fixed tool loading and installation

This commit is contained in:
pesckal
2011-07-29 04:12:56 +00:00
parent 01987841a4
commit 1631785484
3 changed files with 83 additions and 38 deletions
+14 -10
View File
@@ -62,8 +62,6 @@ public class ContributionManager {
JFrame dialog;
JProgressBar installProgressBar;
FilterField filterField;
ContributionListPanel contributionListPanel;
@@ -360,7 +358,7 @@ public class ContributionManager {
* Non-blocking call to download and install a contribution in a new thread.
*/
public void downloadAndInstall(URL url,
final Contribution info,
final Contribution advertisedContribution,
final JProgressMonitor downloadProgressMonitor,
final JProgressMonitor installProgressMonitor) {
@@ -380,7 +378,7 @@ public class ContributionManager {
ProgressMonitor.UNKNOWN);
InstalledContribution contribution = null;
switch (info.getType()) {
switch (advertisedContribution.getType()) {
case LIBRARY:
contribution = installLibrary(contributionFile, false);
break;
@@ -394,7 +392,7 @@ public class ContributionManager {
if (contribution != null) {
// XXX contributionListing.getInformationFromAdvertised(contribution); get the category at least
contribListing.replaceContribution(info, contribution);
contribListing.replaceContribution(advertisedContribution, contribution);
refreshInstalled();
}
@@ -537,13 +535,13 @@ public class ContributionManager {
protected ToolContribution installTool(File zippedToolFile) {
File tempDir = unzipFileToTemp(zippedToolFile);
ArrayList<ToolContribution> discoveredTools = ToolContribution.list(tempDir);
ArrayList<ToolContribution> discoveredTools = ToolContribution.list(tempDir, false);
if (discoveredTools.isEmpty()) {
// Sometimes tool authors place all their folders in the base
// directory of a zip file instead of in single folder as the
// guidelines suggest. If this is the case, we might be able to find the
// library by stepping up a directory and searching for libraries again.
discoveredTools = ToolContribution.list(tempDir.getParentFile());
discoveredTools = ToolContribution.list(tempDir.getParentFile(), false);
}
if (discoveredTools != null && discoveredTools.size() == 1) {
@@ -576,9 +574,9 @@ public class ContributionManager {
for (ToolContribution oldTool : oldTools) {
// XXX: Handle other cases when installing libraries.
// XXX: Handle other cases when installing tools.
// -What if a library by the same name is already installed?
// -What if newLibDest exists, but isn't used by an existing library?
// -What if newLibDest exists, but isn't used by an existing tools?
if (oldTool.getFolder().exists() && oldTool.getFolder().equals(newToolDest)) {
if (!backupContribution(oldTool)) {
@@ -589,7 +587,13 @@ public class ContributionManager {
// Move newLib to the sketchbook library folder
if (newTool.getFolder().renameTo(newToolDest)) {
return ToolContribution.getTool(newToolDest);
ToolContribution movedTool = ToolContribution.getTool(newToolDest);
try {
movedTool.initializeToolClass();
return newTool;
} catch (Exception e) {
e.printStackTrace();
}
} else {
Base.showWarning("Trouble moving new tool to the sketchbook",
"Could not move tool \"" + newTool.getName() + "\" to "
+2 -2
View File
@@ -816,8 +816,8 @@ public abstract class Editor extends JFrame implements RunnerListener {
protected void rebuildToolList() {
coreTools = ToolContribution.list(base.getToolsFolder());
contribTools = ToolContribution.list(base.getSketchbookToolsFolder());
coreTools = ToolContribution.list(base.getToolsFolder(), true);
contribTools = ToolContribution.list(base.getSketchbookToolsFolder(), true);
}
+67 -26
View File
@@ -32,19 +32,21 @@ import processing.app.tools.Tool;
public class ToolContribution extends InstalledContribution implements Tool {
String className;
URLClassLoader loader;
Tool tool;
static public ToolContribution getTool(File folder) {
try {
ToolContribution tool = new ToolContribution(folder);
if (tool.tool != null)
return tool;
} catch (Exception e) {
}
ToolContribution tool = new ToolContribution(folder);
if (tool.isValid())
return tool;
return null;
}
private ToolContribution(File folder) throws Exception {
private ToolContribution(File folder) {
super(folder, "tool.properties");
File toolDirectory = new File(folder, "tool");
@@ -59,16 +61,19 @@ public class ToolContribution extends InstalledContribution implements Tool {
}
});
URL[] urlList = new URL[archives.length];
for (int j = 0; j < urlList.length; j++) {
urlList[j] = archives[j].toURI().toURL();
}
URLClassLoader loader = new URLClassLoader(urlList);
String className = null;
for (int j = 0; j < archives.length; j++) {
className = findClassInZipFile(folder.getName(), archives[j]);
if (className != null) break;
try {
URL[] urlList = new URL[archives.length];
for (int j = 0; j < urlList.length; j++) {
urlList[j] = archives[j].toURI().toURL();
}
loader = new URLClassLoader(urlList);
for (int j = 0; j < archives.length; j++) {
className = findClassInZipFile(folder.getName(), archives[j]);
if (className != null) break;
}
} catch (MalformedURLException e) {
// Maybe log this
}
/*
@@ -94,14 +99,26 @@ public class ToolContribution extends InstalledContribution implements Tool {
}
}
*/
// If no class name found, just move on.
if (className == null) return;
}
/**
* @return true if a Tool class of the expected name was found in this tool's
* classpath
*/
private boolean isValid() {
return className != null;
}
/**
* Loads the tool, making it impossible (on Windows) to move the files in the
* classpath without restarting the PDE.
*/
public void initializeToolClass() throws Exception {
Class<?> toolClass = Class.forName(className, true, loader);
tool = (Tool) toolClass.newInstance();
}
protected String findClassInZipFile(String base, File file) {
static protected String findClassInZipFile(String base, File file) {
// Class file to search for
String classFileName = "/" + base + ".class";
@@ -132,13 +149,22 @@ public class ToolContribution extends InstalledContribution implements Tool {
return null;
}
static protected ArrayList<ToolContribution> list(File folder) {
/**
* Searches and returns a list of tools found in the immediate children of the
* given folder.
* @param doInitializeToolClass
* true if tools should be initialized before they are returned.
* Tools that failed to initialize for whatever reason are not
* returned
*/
static protected ArrayList<ToolContribution> list(File folder, boolean doInitializeToolClass) {
ArrayList<ToolContribution> tools = new ArrayList<ToolContribution>();
list(folder, tools);
list(folder, tools, doInitializeToolClass);
return tools;
}
static protected void list(File folder, ArrayList<ToolContribution> tools) {
static protected void list(File folder, ArrayList<ToolContribution> tools,
boolean doInitializeToolClass) {
File[] folders = folder.listFiles(new FileFilter() {
public boolean accept(File folder) {
@@ -149,6 +175,16 @@ public class ToolContribution extends InstalledContribution implements Tool {
}
return false;
}
// private boolean toolAlreadyExists(File folder) {
// boolean exists = true;
// for (ToolContribution contrib : tools) {
// if (contrib.getFolder().equals(folder)) {
// exists = false;
// }
// }
// return exists;
// }
});
if (folders == null || folders.length == 0) {
@@ -159,7 +195,12 @@ public class ToolContribution extends InstalledContribution implements Tool {
try {
final ToolContribution tool = getTool(folders[i]);
if (tool != null) {
tools.add(tool);
try {
if (doInitializeToolClass)
tool.initializeToolClass();
tools.add(tool);
} catch (Exception e) {
}
}
} catch (Exception e) {
e.printStackTrace();