|
| 1 | +/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 | + |
| 3 | +/* |
| 4 | + Part of the Processing project - http://processing.org |
| 5 | +
|
| 6 | + Copyright (c) 2017 The Processing Foundation |
| 7 | + |
| 8 | + This program is free software; you can redistribute it and/or modify |
| 9 | + it under the terms of the GNU General Public License version 2 |
| 10 | + as published by the Free Software Foundation. |
| 11 | +
|
| 12 | + This program is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + GNU General Public License for more details. |
| 16 | +
|
| 17 | + You should have received a copy of the GNU General Public License |
| 18 | + along with this program; if not, write to the Free Software Foundation, |
| 19 | + Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | +*/ |
| 21 | + |
| 22 | +package processing.mode.android; |
| 23 | + |
| 24 | +import java.io.File; |
| 25 | +import java.net.URL; |
| 26 | +import java.net.URLClassLoader; |
| 27 | + |
| 28 | +import processing.app.Base; |
| 29 | +import processing.app.Messages; |
| 30 | +import processing.app.Util; |
| 31 | +import processing.app.contrib.ContributionType; |
| 32 | +import processing.app.contrib.IgnorableException; |
| 33 | +import processing.app.contrib.LocalContribution; |
| 34 | +import processing.app.contrib.ToolContribution; |
| 35 | +import processing.app.tools.Tool; |
| 36 | + |
| 37 | +public class AndroidTool extends LocalContribution implements Tool, Comparable<ToolContribution> { |
| 38 | + private AndroidSDK sdk; |
| 39 | + private Tool tool; |
| 40 | + |
| 41 | + AndroidTool(File toolFolder, AndroidSDK sdk) throws Throwable { |
| 42 | + super(toolFolder); |
| 43 | + this.sdk = sdk; |
| 44 | + |
| 45 | + String className = initLoader(null); |
| 46 | + if (className != null) { |
| 47 | + Class<?> toolClass = loader.loadClass(className); |
| 48 | + tool = (Tool) toolClass.newInstance(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public void init(Base base) { |
| 53 | + tool.init(base); |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + public void run() { |
| 58 | + tool.run(); |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + public String getMenuTitle() { |
| 63 | + return tool.getMenuTitle(); |
| 64 | + } |
| 65 | + |
| 66 | + public String initLoader(String className) throws Exception { |
| 67 | + File toolDir = new File(folder, "tool"); |
| 68 | + if (toolDir.exists()) { |
| 69 | + Messages.log("checking mode folder regarding " + className); |
| 70 | + // If no class name specified, search the main <modename>.jar for the |
| 71 | + // full name package and mode name. |
| 72 | + if (className == null) { |
| 73 | + String shortName = folder.getName(); |
| 74 | + File mainJar = new File(toolDir, shortName + ".jar"); |
| 75 | + if (mainJar.exists()) { |
| 76 | + className = findClassInZipFile(shortName, mainJar); |
| 77 | + } else { |
| 78 | + throw new IgnorableException(mainJar.getAbsolutePath() + " does not exist."); |
| 79 | + } |
| 80 | + |
| 81 | + if (className == null) { |
| 82 | + throw new IgnorableException("Could not find " + shortName + |
| 83 | + " class inside " + mainJar.getAbsolutePath()); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // Add .jar and .zip files from the "tool" and the SDK/tools/lib |
| 88 | + // folder into the classpath |
| 89 | + File libDir = new File(sdk.getToolsFolder(), "lib"); |
| 90 | + File[] toolArchives = Util.listJarFiles(toolDir); |
| 91 | + File[] libArchives = Util.listJarFiles(libDir); |
| 92 | + |
| 93 | + if (toolArchives != null && toolArchives.length > 0 && |
| 94 | + libArchives != null && libArchives.length > 0) { |
| 95 | + URL[] urlList = new URL[toolArchives.length + libArchives.length]; |
| 96 | + |
| 97 | + int j; |
| 98 | + for (j = 0; j < toolArchives.length; j++) { |
| 99 | + Messages.log("Found archive " + toolArchives[j] + " for " + getName()); |
| 100 | + urlList[j] = toolArchives[j].toURI().toURL(); |
| 101 | + } |
| 102 | + for (int k = 0; k < libArchives.length; k++, j++) { |
| 103 | + Messages.log("Found archive " + libArchives[k] + " for " + getName()); |
| 104 | + urlList[j] = libArchives[k].toURI().toURL(); |
| 105 | + } |
| 106 | + |
| 107 | + loader = new URLClassLoader(urlList); |
| 108 | + Messages.log("loading above JARs with loader " + loader); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // If no archives were found, just use the regular ClassLoader |
| 113 | + if (loader == null) { |
| 114 | + loader = Thread.currentThread().getContextClassLoader(); |
| 115 | + } |
| 116 | + return className; |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + @Override |
| 121 | + public int compareTo(ToolContribution o) { |
| 122 | + return getMenuTitle().compareTo(o.getMenuTitle()); |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + @Override |
| 127 | + public ContributionType getType() { |
| 128 | + return ContributionType.TOOL; |
| 129 | + } |
| 130 | +} |
0 commit comments