Search in sources :

Example 1 with InvalidPluginException

use of org.bukkit.plugin.InvalidPluginException in project Bukkit by Bukkit.

the class JavaPluginLoader method loadPlugin.

public Plugin loadPlugin(final File file) throws InvalidPluginException {
    Validate.notNull(file, "File cannot be null");
    if (!file.exists()) {
        throw new InvalidPluginException(new FileNotFoundException(file.getPath() + " does not exist"));
    }
    final PluginDescriptionFile description;
    try {
        description = getPluginDescription(file);
    } catch (InvalidDescriptionException ex) {
        throw new InvalidPluginException(ex);
    }
    final File parentFile = file.getParentFile();
    final File dataFolder = new File(parentFile, description.getName());
    @SuppressWarnings("deprecation") final File oldDataFolder = new File(parentFile, description.getRawName());
    // Found old data folder
    if (dataFolder.equals(oldDataFolder)) {
    // They are equal -- nothing needs to be done!
    } else if (dataFolder.isDirectory() && oldDataFolder.isDirectory()) {
        server.getLogger().warning(String.format("While loading %s (%s) found old-data folder: `%s' next to the new one `%s'", description.getFullName(), file, oldDataFolder, dataFolder));
    } else if (oldDataFolder.isDirectory() && !dataFolder.exists()) {
        if (!oldDataFolder.renameTo(dataFolder)) {
            throw new InvalidPluginException("Unable to rename old data folder: `" + oldDataFolder + "' to: `" + dataFolder + "'");
        }
        server.getLogger().log(Level.INFO, String.format("While loading %s (%s) renamed data folder: `%s' to `%s'", description.getFullName(), file, oldDataFolder, dataFolder));
    }
    if (dataFolder.exists() && !dataFolder.isDirectory()) {
        throw new InvalidPluginException(String.format("Projected datafolder: `%s' for %s (%s) exists and is not a directory", dataFolder, description.getFullName(), file));
    }
    for (final String pluginName : description.getDepend()) {
        if (loaders == null) {
            throw new UnknownDependencyException(pluginName);
        }
        PluginClassLoader current = loaders.get(pluginName);
        if (current == null) {
            throw new UnknownDependencyException(pluginName);
        }
    }
    final PluginClassLoader loader;
    try {
        loader = new PluginClassLoader(this, getClass().getClassLoader(), description, dataFolder, file);
    } catch (InvalidPluginException ex) {
        throw ex;
    } catch (Throwable ex) {
        throw new InvalidPluginException(ex);
    }
    loaders.put(description.getName(), loader);
    return loader.plugin;
}
Also used : UnknownDependencyException(org.bukkit.plugin.UnknownDependencyException) InvalidPluginException(org.bukkit.plugin.InvalidPluginException) FileNotFoundException(java.io.FileNotFoundException) PluginDescriptionFile(org.bukkit.plugin.PluginDescriptionFile) InvalidDescriptionException(org.bukkit.plugin.InvalidDescriptionException) JarFile(java.util.jar.JarFile) File(java.io.File) PluginDescriptionFile(org.bukkit.plugin.PluginDescriptionFile)

Example 2 with InvalidPluginException

use of org.bukkit.plugin.InvalidPluginException in project Dragonet-Legacy by DragonetMC.

the class MixedPluginManager method loadPlugins.

/**
     * Loads the array of plugins
     *
     * @param files Array of plugin files to load
     * @param sourceFolder Containing folder path name string for error messages
     * @return
     */
public Plugin[] loadPlugins(File[] files, String sourceFolder) {
    List<Plugin> result = new ArrayList<Plugin>();
    Set<Pattern> filters = fileAssociations.keySet();
    Map<String, File> plugins = new HashMap<String, File>();
    Set<String> loadedPlugins = new HashSet<String>();
    Map<String, Collection<String>> dependencies = new HashMap<String, Collection<String>>();
    Map<String, Collection<String>> softDependencies = new HashMap<String, Collection<String>>();
    // This is where it figures out all possible plugins
    for (File file : files) {
        // Skip plugins in the ignore set
        try {
            URL url = file.toURI().toURL();
            if (ignoreURLs != null && ignoreURLs.contains(file.toURI().toURL())) {
                continue;
            }
        } catch (MalformedURLException ex) {
        // ignore
        }
        PluginLoader loader = null;
        for (Pattern filter : filters) {
            Matcher match = filter.matcher(file.getName());
            if (match.find()) {
                loader = fileAssociations.get(filter);
            }
        }
        if (loader == null)
            continue;
        PluginDescriptionFile description = null;
        try {
            description = loader.getPluginDescription(file);
            String name = description.getName();
            if (name.equalsIgnoreCase("bukkit") || name.equalsIgnoreCase("minecraft") || name.equalsIgnoreCase("mojang")) {
                server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + sourceFolder + "': Restricted Name");
                continue;
            } else if (description.getRawName().indexOf(' ') != -1) {
                server.getLogger().warning(String.format("Plugin `%s' uses the space-character (0x20) in its name `%s' - this is discouraged", description.getFullName(), description.getRawName()));
            }
        } catch (InvalidDescriptionException ex) {
            server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + sourceFolder + "'", ex);
            continue;
        }
        File replacedFile = plugins.put(description.getName(), file);
        if (replacedFile != null) {
            server.getLogger().severe(String.format("Ambiguous plugin name `%s' for files `%s' and `%s' in `%s'", description.getName(), file.getPath(), replacedFile.getPath(), sourceFolder));
        }
        Collection<String> softDependencySet = description.getSoftDepend();
        if (softDependencySet != null && !softDependencySet.isEmpty()) {
            if (softDependencies.containsKey(description.getName())) {
                // Duplicates do not matter, they will be removed together if applicable
                softDependencies.get(description.getName()).addAll(softDependencySet);
            } else {
                softDependencies.put(description.getName(), new LinkedList<String>(softDependencySet));
            }
        }
        Collection<String> dependencySet = description.getDepend();
        if (dependencySet != null && !dependencySet.isEmpty()) {
            dependencies.put(description.getName(), new LinkedList<String>(dependencySet));
        }
        Collection<String> loadBeforeSet = description.getLoadBefore();
        if (loadBeforeSet != null && !loadBeforeSet.isEmpty()) {
            for (String loadBeforeTarget : loadBeforeSet) {
                if (softDependencies.containsKey(loadBeforeTarget)) {
                    softDependencies.get(loadBeforeTarget).add(description.getName());
                } else {
                    // softDependencies is never iterated, so 'ghost' plugins aren't an issue
                    Collection<String> shortSoftDependency = new LinkedList<String>();
                    shortSoftDependency.add(description.getName());
                    softDependencies.put(loadBeforeTarget, shortSoftDependency);
                }
            }
        }
    }
    while (!plugins.isEmpty()) {
        boolean missingDependency = true;
        Iterator<String> pluginIterator = plugins.keySet().iterator();
        while (pluginIterator.hasNext()) {
            String plugin = pluginIterator.next();
            if (dependencies.containsKey(plugin)) {
                Iterator<String> dependencyIterator = dependencies.get(plugin).iterator();
                while (dependencyIterator.hasNext()) {
                    String dependency = dependencyIterator.next();
                    // Dependency loaded
                    if (loadedPlugins.contains(dependency)) {
                        dependencyIterator.remove();
                    // We have a dependency not found
                    } else if (!plugins.containsKey(dependency)) {
                        missingDependency = false;
                        File file = plugins.get(plugin);
                        pluginIterator.remove();
                        softDependencies.remove(plugin);
                        dependencies.remove(plugin);
                        server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + sourceFolder + "'", new UnknownDependencyException(dependency));
                        break;
                    }
                }
                if (dependencies.containsKey(plugin) && dependencies.get(plugin).isEmpty()) {
                    dependencies.remove(plugin);
                }
            }
            if (softDependencies.containsKey(plugin)) {
                Iterator<String> softDependencyIterator = softDependencies.get(plugin).iterator();
                while (softDependencyIterator.hasNext()) {
                    String softDependency = softDependencyIterator.next();
                    // Soft depend is no longer around
                    if (!plugins.containsKey(softDependency)) {
                        softDependencyIterator.remove();
                    }
                }
                if (softDependencies.get(plugin).isEmpty()) {
                    softDependencies.remove(plugin);
                }
            }
            if (!(dependencies.containsKey(plugin) || softDependencies.containsKey(plugin)) && plugins.containsKey(plugin)) {
                // We're clear to load, no more soft or hard dependencies left
                File file = plugins.get(plugin);
                pluginIterator.remove();
                missingDependency = false;
                try {
                    result.add(loadPlugin(file));
                    loadedPlugins.add(plugin);
                    continue;
                } catch (InvalidPluginException ex) {
                    server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + sourceFolder + "'", ex);
                }
            }
        }
        if (missingDependency) {
            // We now iterate over plugins until something loads
            // This loop will ignore soft dependencies
            pluginIterator = plugins.keySet().iterator();
            while (pluginIterator.hasNext()) {
                String plugin = pluginIterator.next();
                if (!dependencies.containsKey(plugin)) {
                    softDependencies.remove(plugin);
                    missingDependency = false;
                    File file = plugins.get(plugin);
                    pluginIterator.remove();
                    try {
                        result.add(loadPlugin(file));
                        loadedPlugins.add(plugin);
                        break;
                    } catch (InvalidPluginException ex) {
                        server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + sourceFolder + "'", ex);
                    }
                }
            }
            // We have no plugins left without a depend
            if (missingDependency) {
                softDependencies.clear();
                dependencies.clear();
                Iterator<File> failedPluginIterator = plugins.values().iterator();
                while (failedPluginIterator.hasNext()) {
                    File file = failedPluginIterator.next();
                    failedPluginIterator.remove();
                    server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + sourceFolder + "': circular dependency detected");
                }
            }
        }
    }
    return result.toArray(new Plugin[result.size()]);
}
Also used : MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) WeakHashMap(java.util.WeakHashMap) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) PluginDescriptionFile(org.bukkit.plugin.PluginDescriptionFile) URL(java.net.URL) InvalidPluginException(org.bukkit.plugin.InvalidPluginException) PluginLoader(org.bukkit.plugin.PluginLoader) HashSet(java.util.HashSet) Pattern(java.util.regex.Pattern) InvalidDescriptionException(org.bukkit.plugin.InvalidDescriptionException) LinkedList(java.util.LinkedList) UnknownDependencyException(org.bukkit.plugin.UnknownDependencyException) Collection(java.util.Collection) File(java.io.File) PluginDescriptionFile(org.bukkit.plugin.PluginDescriptionFile) Plugin(org.bukkit.plugin.Plugin)

Aggregations

File (java.io.File)2 InvalidDescriptionException (org.bukkit.plugin.InvalidDescriptionException)2 InvalidPluginException (org.bukkit.plugin.InvalidPluginException)2 PluginDescriptionFile (org.bukkit.plugin.PluginDescriptionFile)2 UnknownDependencyException (org.bukkit.plugin.UnknownDependencyException)2 FileNotFoundException (java.io.FileNotFoundException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 WeakHashMap (java.util.WeakHashMap)1 JarFile (java.util.jar.JarFile)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Plugin (org.bukkit.plugin.Plugin)1 PluginLoader (org.bukkit.plugin.PluginLoader)1