001    /*
002    
003            $Id: PluginManager.java,v 1.4 2003/05/08 17:12:31 ndulgheru Exp $
004    
005    */
006    
007    package sharpster.common;
008    
009    import java.io.File;
010    import java.util.LinkedList;
011    import java.util.ListIterator;
012    
013    import sharpster.common.FileCollection;
014    import sharpster.common.SharedFile;
015    import sharpster.common.PluginLoader;
016    import sharpster.common.PartOfFilePlugin;
017    import sharpster.common.PluginData;
018    
019    public class PluginManager {
020        /**
021         * A list holding all loaded plugins
022         */
023        protected LinkedList plugins;
024    
025        /**
026         * Constructor
027         */
028        public PluginManager() {
029            plugins = new LinkedList();
030        } 
031    
032        /**
033         * Loads all plug-ins from the given directory
034         * (Loads all classes ending with Plugin.class).
035         */
036        public boolean loadPlugins(String directory) { 
037            File path = new File(directory);
038            
039            if(!path.isDirectory()) return false;
040            File[] files = path.listFiles();
041            
042            plugins.clear();
043            
044            for(int i=0;i<files.length;i++) {
045                if(files[i].getName().matches(".*(Plugin.class)")) {
046                    File parent = files[i].getParentFile();
047                    String name = files[i].getName();
048                    name = name.substring(0,name.length()-6);
049                    
050                    try {
051                        //System.out.println(name);
052                        PartOfFilePlugin plugin = (PartOfFilePlugin)
053                            PluginLoader.load(name, parent.getAbsolutePath());
054                        if(plugin != null) {
055                            plugins.add(plugin);
056                        }
057                    }
058                    catch(Exception e) {
059                    }
060                }
061            }
062            return true;
063        }
064    
065        /**
066         * Returns an array of available plug-in names.
067         */
068        public String[] getPluginNames() { 
069            String[] names = new String[plugins.size()];
070            ListIterator i;
071            int j=0;
072            for(i=plugins.listIterator();i.hasNext();) {
073                    names[j++] = ((PartOfFilePlugin)i.next()).getPluginName();
074            }
075            return names;
076        }
077    
078        /**
079         * Returns a plug-in object given a specific plug-in name
080         */
081        public PartOfFilePlugin getPluginFromName(String pluginName) {
082            PartOfFilePlugin retval = null;
083            for(int i=0; i<plugins.size(); i++) {
084                retval = (PartOfFilePlugin)plugins.get(i);
085                if(pluginName.equals(retval.getPluginName()))
086                    return retval;
087            }
088            return null;
089        }
090    
091        /**
092         * Returns a description about the given plug-in name.
093         */
094        public String getPluginDescription(String name) { 
095            PartOfFilePlugin plugin = getPluginFromName(name);
096            if(plugin != null) {
097                return plugin.getPluginDescription();
098            }
099            return new String();
100        }
101    }
102    
103    
104    
105    
106