1,29 → 1,5 |
package net.outlyer.plugins; |
|
/* |
* Copyright (c) 2008, Toni Corvera. All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions are met: |
* |
* * Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* * Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* |
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
|
import java.io.File; |
import java.io.FileFilter; |
import java.io.IOException; |
38,10 → 14,11 |
import java.util.List; |
import java.util.Set; |
import java.util.jar.JarEntry; |
import net.outlyer.plugins.sandboxing.PluginEnvironment; |
import net.outlyer.plugins.sandboxing.Sandbox; |
|
/** |
* Simplifies seeking plugins and abstracts their location (whether they're |
* in a directory or inside a jar file). |
* |
*/ |
public class PluginLocator { |
|
49,7 → 26,6 |
private final Library library; |
private static final FileFilter jsFileFilter; |
private boolean scanned = false; |
private final PluginEnvironment environment; |
|
static { |
jsFileFilter = new FileFilter() { |
61,18 → 37,14 |
} |
|
private static class Library { |
// FIXME: Do something more elegant... |
|
private final HashMap<String, List<Sandbox>> byType; |
private final HashMap<String, Sandbox> byFilename; // FIXME: Not really unique |
private final HashMap<String, Sandbox> byFilename; |
private final HashMap<String, Sandbox> byName; |
private final HashSet<Sandbox> unique; |
|
{ |
byType = new HashMap<String, List<Sandbox>>(); |
byFilename = new HashMap<String, Sandbox>(); |
byName = new HashMap<String, Sandbox>(); |
unique = new HashSet<Sandbox>(); |
} |
|
void add(final String fileName, final BasePluginObject bpo, final Sandbox sbox) { |
83,7 → 55,6 |
byType.put(bpo.type, new LinkedList<Sandbox>()); |
} |
byType.get(bpo.type).add(sbox); |
unique.add(sbox); |
} |
|
void drop() { |
90,7 → 61,6 |
byType.clear(); |
byFilename.clear(); |
byName.clear(); |
unique.clear(); |
} |
} |
|
99,29 → 69,19 |
library = new Library(); |
} |
|
public PluginLocator(final PluginEnvironment pe, final URI...searchPaths) { |
if (null == searchPaths) { |
throw new IllegalArgumentException("Can't initialise PluginLocator with no search path"); |
public PluginLocator(final URI defaultPath) { |
if (null == defaultPath) { |
throw new IllegalArgumentException("Null path in list of paths"); |
} |
for (final URI path : searchPaths) { |
addSearchPath(path); |
} |
environment = pe; |
} |
|
public void addSearchPath(final URI path) { |
if (null == path) { |
throw new IllegalArgumentException("Can't have a null path in the search paths"); |
if (defaultPath.getScheme().equals("file")) { |
final File path = new File(defaultPath.normalize()).getAbsoluteFile(); |
if (path.exists() && path.isDirectory() && path.canRead()) { |
paths.add(path.toURI().normalize()); |
} |
|
if (path.getScheme().equals("file")) { |
final File file = new File(path.normalize()).getAbsoluteFile(); |
if (file.exists() && file.isDirectory() && file.canRead()) { |
paths.add(file.toURI().normalize()); |
} |
} |
else if (path.getScheme().equals("jar")) { |
final URI normalized = path.normalize(); |
else if (defaultPath.getScheme().equals("jar")) { |
final URI normalized = defaultPath.normalize(); |
if (!normalized.getSchemeSpecificPart().endsWith("/")) { |
throw new IllegalArgumentException("Jar file entries must" + |
" represent directories"); |
240,24 → 200,20 |
return Collections.unmodifiableSet(set); |
} |
|
public void scan() { |
public void scan(final PluginEnvironment pe) { |
if (scanned) { |
return; |
} |
if (paths.size() == 0) { // Nothing to scan |
scanned = true; |
return; |
} |
|
for (final URI uri : paths) { |
if (uri.getScheme().equals("file")) { |
final File dir = new File(uri); |
checkDirConsistency(dir); |
scan(dir); |
scan(pe, dir); |
} |
else { |
assert uri.getScheme().equals("jar"); |
scan(uri); |
scan(pe, uri); |
} |
} |
|
268,7 → 224,7 |
* Scan over a directory |
* @param dir Directory to scan |
*/ |
private void scan(final File dir) { |
private void scan(final PluginEnvironment pe, final File dir) { |
assert dir.isDirectory() && dir.canRead(); |
|
for (final File candidateFile : dir.listFiles(jsFileFilter)) { |
275,7 → 231,7 |
final URI fileURI = candidateFile.toURI(); |
|
try { |
final Sandbox sb = environment.createSandbox(fileURI); |
final Sandbox sb = pe.createSandbox(fileURI); |
|
final BasePluginObject bpo = sb.getPluginObject(); |
|
295,7 → 251,7 |
* @param jarUri Directory to scan |
* @return LIst of plugins in directory <code>jarUri</code> |
*/ |
private void scan(final URI jarUri) { |
private void scan(final PluginEnvironment pe, final URI jarUri) { |
assert jarUri.getScheme().equals("jar"); |
|
try { |
317,11 → 273,11 |
// Accepted URI |
|
try { |
final URI pluginUri = new URI("jar:"+jarFile+"!"+url); |
final Sandbox sb = pe.createSandbox(jarUri); |
|
final Sandbox sb = environment.createSandbox(pluginUri); |
final BasePluginObject bpo = sb.getPluginObject(); |
final String fileName = new File(jarURIElements[1]).getName(); |
|
final String fileName = new File(jarUri.getPath()).getName(); |
if (bpo.name == null) { |
bpo.name = fileName; |
} |
354,13 → 310,10 |
} |
|
public Collection<Sandbox> list() { |
scan(); |
return Collections.unmodifiableCollection(library.unique); |
if (!scanned) { |
throw new IllegalStateException("Can't list before scanning"); |
} |
|
public int foundPlugins() { |
scan(); |
return library.unique.size(); |
return Collections.unmodifiableCollection(library.byFilename.values()); |
} |
|
} |