sandeepsuvit / jspf

Automatically exported from code.google.com/p/jspf
0 stars 0 forks source link

JSPF should support dependencies. #18

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago

Mail from one of our users:

I use plugins in a Web application. Each of my plugins has its own directory. I 
load my plugins individually by calling PluginManager#addPluginsFrom(URI). It 
works like a charm. Sometimes, my plugins require their own Jar files 
(dependencies) to work properly. I would like to have a "lib" subdirectory in a 
plugin's directory where I would put the said jars. At the moment, I have to 
put the dependencies in my default classpath, which is annoying. What do I need 
to do so that the plugin's ClassLoader recognizes its dependencies?

Original issue reported on code.google.com by r.biedert on 13 Sep 2010 at 8:19

GoogleCodeExporter commented 9 years ago
And Vincent's proposed solution:

I created a class named ClassPathManagerUtils (attached file) in the same 
package as ClassPathManager (to access its classWorld variable, which has 
default scope).
Not directly related, but somewhat useful... I created a custom ClassLoader 
(PluginAwareClassLoader, attached file) that asks all the plugin ClassLoaders 
for a class/resource instead of throwing a ClassNotFoundException right away. I 
came up with this solution when faced with the following problem : 

http://blogs.sun.com/adventures/entry/desrializing_objects_custom_class_loaders

Here is a code excerpt of my plugin initialization :
{{{
    private static void initPluginManager() {
        if (pm == null) {
            pm = PluginManagerFactory.createPluginManager();
            File pluginsDir = getPluginsDir();
            File[] pluginDirs = pluginsDir.listFiles((FileFilter) DirectoryFileFilter.DIRECTORY);
            for (File pluginDir : pluginDirs) {
                // Plugin root dir jars
                Collection<File> pluginJarFiles = FileUtils.listFiles(pluginDir, new String[] { "jar" },
                    false);
                // Accept only one root dir jar
                File pluginJarFile = pluginJarFiles.isEmpty() ? null : pluginJarFiles.iterator().next();
                if (pluginJarFile != null) {
                    URI pluginJarFileURI = pluginJarFile.toURI();
                    pm.addPluginsFrom(pluginJarFileURI);

                    File pluginLibDir = new File(pluginDir, "lib");
                    if (pluginLibDir.exists() && pluginLibDir.isDirectory()) {
                        PluginManagerImpl pmImpl = (PluginManagerImpl) pm;
                        <File> pluginDependencies = FileUtils.listFiles(pluginLibDir, new String[] { "jar" }, false);
                        ClassPathManagerUtils.addJarDependencies(pmImpl.getClassPathManager(), pluginJarFile, pluginDependencies);
                    }
                }
            } 
        }
    }
}}}

Original comment by r.biedert on 13 Sep 2010 at 8:36

GoogleCodeExporter commented 9 years ago
Ralf,
Can you please publish proposed class (ClassPathManagerUtils)?

Original comment by McSe...@gmail.com on 22 Sep 2010 at 8:54

GoogleCodeExporter commented 9 years ago
Attached files. Give me some time to re-consider this report, it is related to 
Issue #19. 

Original comment by r.biedert on 23 Sep 2010 at 8:59

Attachments:

GoogleCodeExporter commented 9 years ago
Hopefully fixed (please get 0.9.1a from the downloads and test!). Also see 
http://code.google.com/p/jspf/wiki/FAQ -> Multiplugins.

Original comment by r.biedert on 23 Sep 2010 at 3:42