[Jtreeview-cvs] jtreeview commit notification
Brought to you by:
alokito
From: Ray <rq...@us...> - 2006-08-18 06:50:21
|
Update of /cvsroot/jtreeview/LinkedView/src/edu/stanford/genetics/treeview/plugin In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv2394/src/edu/stanford/genetics/treeview/plugin Modified Files: MenuHelpPluginsFrame.java Log Message: bug1540281 - Now you can point treeview to plugin jar files and dynamically load them. 1. Updated bulk of plugin functionality code. 2. Added tv_plugins.cd, a textfile listing the main plugin classes, to each current plugin. 3. Updated the Debug class. Index: MenuHelpPluginsFrame.java =================================================================== RCS file: /cvsroot/jtreeview/LinkedView/src/edu/stanford/genetics/treeview/plugin/MenuHelpPluginsFrame.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** MenuHelpPluginsFrame.java 16 Aug 2006 19:13:48 -0000 1.1 --- MenuHelpPluginsFrame.java 18 Aug 2006 06:50:18 -0000 1.2 *************** *** 5,15 **** import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileFilter; import java.io.IOException; ! import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.util.jar.JarFile; import java.util.zip.ZipEntry; --- 5,19 ---- import java.awt.event.ActionEvent; import java.awt.event.ActionListener; + import java.io.BufferedReader; import java.io.File; import java.io.FileFilter; import java.io.IOException; ! import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; + import java.util.ArrayList; + import java.util.Enumeration; + import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.zip.ZipEntry; *************** *** 23,114 **** import edu.stanford.genetics.treeview.PluginFactory; import edu.stanford.genetics.treeview.TreeViewFrame; public class MenuHelpPluginsFrame extends JFrame { ! static String s_pluginclassfile = "tv_plugin.cd"; ! private File[] readdir(String s_dir) { ! File f_dir = new File(s_dir); ! FileFilter fileFilter = new FileFilter() { ! public boolean accept(File file) { ! return file.getName().endsWith("jar"); ! } ! }; ! return f_dir.listFiles(fileFilter); ! } ! private void loadPlugins(File[] f_jars) { ! String s_loaded = ""; ! String s_notloaded = ""; ! for (int i = 0 ; i < f_jars.length; i++) { ! System.out.println("loadPlugins: " + f_jars[i].getName()); /* ! try { ! // Load jarfile ! URLClassLoader urlcl = new URLClassLoader( ! new URL[] {f_jars[i].toURL()}, ! ClassLoader.getSystemClassLoader()); ! // Read textfile with Class declarations ! JarFile jf = new JarFile(f_jars[i]); ! ZipEntry ze = jf.getEntry(s_pluginclassfile); ! InputStream is = jf.getInputStream(ze); ! byte[] byte_classname; ! // if (is.read(byte_classname)) { ! // Load classes } catch (MalformedURLException e) { // TODO Auto-generated catch block ! e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block ! e.printStackTrace(); } - */ } ! ! JOptionPane.showMessageDialog(this, ! "Loaded plugins: " + s_loaded + ! "\nUnable to load: " +s_notloaded + "\n"); ! //Class c = urlcl.loadClass(classname + ".P" + classname.substring(1, classname.length())); ! //PluginManager.printPlugins(); } - public MenuHelpPluginsFrame(String string, PluginFactory [] plugins) { - this.setTitle(string); - - JLabel label = new JLabel(""); if (plugins == null || plugins.length == 0) { ! label.setText("No Plugins Found"); } else { ! label.setText("<html><br><ol>"); ! for (int i = 0 ; i < plugins.length; i++) { ! label.setText(label.getText() + "<li>" + plugins[i].getPluginName()); ! } ! label.setText(label.getText() + "</ol><br></html>"); } ! label.setPreferredSize(new Dimension(175, 20*plugins.length)); ! this.getContentPane().add(label, BorderLayout.NORTH); ! final JTextField tf_dir = new JTextField(20); ! File f_currdir = new File ("."); try { ! tf_dir.setText(f_currdir.getCanonicalPath()+"\\plugins\\"); } catch (IOException e) { tf_dir.setText("Unable to read default plugins directory."); } this.getContentPane().add(tf_dir, BorderLayout.WEST); - - JButton b_scan= new JButton("Scan new plugins"); - this.getContentPane().add(b_scan, BorderLayout.EAST); b_scan.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { File[] files = readdir(tf_dir.getText()); if (files == null || files.length == 0) { ! tf_dir.setText("Directory contains no plugins"); } else { loadPlugins(files); } } }); ! this.pack(); ! } } \ No newline at end of file --- 27,217 ---- import edu.stanford.genetics.treeview.PluginFactory; import edu.stanford.genetics.treeview.TreeViewFrame; + import edu.stanford.genetics.treeview.app.TreeViewApp; + import edu.stanford.genetics.treeview.core.Debug; public class MenuHelpPluginsFrame extends JFrame { ! private static String s_pluginclassfile = "tv_plugins.cd"; ! ! private TreeViewFrame tvf = null; ! ! private JTextField tf_dir = new JTextField(20); ! ! private JLabel l_pluginlist = new JLabel(""); ! private File[] readdir(String s_dir) { ! File f_dir = new File(s_dir); ! FileFilter fileFilter = new FileFilter() { ! public boolean accept(File file) { ! return file.getName().endsWith("jar"); ! } ! }; ! return f_dir.listFiles(fileFilter); ! } ! ! /* ! * EFFECTS: Reads the file <s_pluginclassfile> from a jar RETURNS: string ! * array of all classes to be declared NOTE: This method should probably be ! * moved into a PluginManager class ! */ ! private ArrayList getClassDeclarations(JarFile jf) ! throws NullPointerException, IOException { ! ! ZipEntry ze = null; ! try { /* ! * See if class declarations file exists ! */ ! Enumeration e = jf.entries(); ! JarEntry je = null; ! String classfile = null; ! for (; e.hasMoreElements();) { ! je = (JarEntry) e.nextElement(); ! if (je.toString().contains(s_pluginclassfile)) { ! classfile = je.toString(); ! } ! } ! ze = jf.getEntry(classfile); ! } catch (NullPointerException e) { ! Debug.print("JarFile has no tv_plugins.cd", (Object) jf.getName()); ! throw e; ! } catch (RuntimeException e) { ! // TODO Auto-generated catch block ! e.printStackTrace(); ! } + /* + * Classfile exists (otherwise exception thrown) Read classes into array + * list + */ + BufferedReader br = new BufferedReader(new InputStreamReader(jf + .getInputStream(ze))); + ArrayList<String> al = new ArrayList<String>(); + String s = null; + while ((s = br.readLine()) != null) { + Debug.print("Class Declarations File", (Object) s); + al.add(s); + } + return al; + } + + public void loadPlugins(File[] f_jars) { + boolean b_loadedPlugin = false; + String s_loadedPlugins = ""; + String s_notloadedPlugins = ""; + + for (int i = 0; i < f_jars.length; i++) { + try { + Debug.print("Plugin Name Url", (Object) f_jars[i].toURL()); + ArrayList al_classnames = getClassDeclarations(new JarFile( + f_jars[i])); + for (int j = 0; j < al_classnames.size(); j++) { + URLClassLoader urlcl = new URLClassLoader( + new URL[] { f_jars[i].toURL() }, ClassLoader + .getSystemClassLoader()); + if (!tvf.getApp().pluginExists( + (String) al_classnames.get(j))) { + Class c = urlcl + .loadClass((String) al_classnames.get(j)); + /* + * XXX: Supposedly, loadClass should call the static + * initializer for the class, but I'm finding it doesn't + * so I'm forcing an instantiation with new Instance. + */ + PluginFactory pp = (PluginFactory) c.newInstance(); + Debug.print("Plugin Class Loaded", + (Object) al_classnames.get(j)); + b_loadedPlugin |= true; + } else { + b_loadedPlugin |= false; + } + } + if (b_loadedPlugin) { + s_loadedPlugins += "<li>" + f_jars[i].getName() + "</li>"; + } else { + s_loadedPlugins += "<li>" + f_jars[i].getName() + "*</li>"; + } + } catch (NullPointerException e) { + s_notloadedPlugins += "<li>" + f_jars[i].getName() + "</li>"; + Debug.print(e); } catch (MalformedURLException e) { // TODO Auto-generated catch block ! Debug.print(e); } catch (IOException e) { // TODO Auto-generated catch block ! Debug.print(e); ! } catch (ClassNotFoundException e) { ! s_notloadedPlugins += "<li>" + f_jars[i].getName() + "</li>"; ! Debug.print(e); ! } catch (InstantiationException e) { ! // TODO Auto-generated catch block ! Debug.print(e); ! } catch (IllegalAccessException e) { ! // TODO Auto-generated catch block ! Debug.print(e); } } ! JOptionPane.showmesshowMessageDialog(this, ! "<html> Found jar files: <ol>" + s_loadedPlugins ! + "<br>* Already loaded</ol> <p>Unable to load: <ol>" ! + s_notloadedPlugins + "</ol></html>"); } + /* + * EFFECTS: Sets <l_pluginslist> text to currently loaded plugins RETURNS: # + * of plugins loaded + */ + private int setLabelText(TreeViewApp app) { + final PluginFactory[] plugins = app.getPluginFactories(); + String s = null; + int height = 0; if (plugins == null || plugins.length == 0) { ! s = "No Plugins Found"; ! height = 1; } else { ! s = "<html><br><ol>"; ! for (int i = 0; i < plugins.length; i++) { ! s += "<li>" + plugins[i].getPluginName(); ! } ! s += "</ol><br></html>"; ! height = plugins.length; ! Debug.print("LabelHeight: ", (Object) height); } ! l_pluginlist.setText(s); ! return height; ! } ! public MenuHelpPluginsFrame(String string, final TreeViewFrame frame) { ! this.setTitle(string); ! tvf = frame; ! ! setLabelText(tvf.getApp()); ! this.getContentPane().add(l_pluginlist, BorderLayout.NORTH); ! ! File f_currdir = new File("."); try { ! tf_dir.setText(f_currdir.getCanonicalPath() + "\\plugins\\"); } catch (IOException e) { tf_dir.setText("Unable to read default plugins directory."); } this.getContentPane().add(tf_dir, BorderLayout.WEST); + JButton b_scan = new JButton("Scan new plugins"); + this.getContentPane().add(b_scan, BorderLayout.EAST); b_scan.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { File[] files = readdir(tf_dir.getText()); if (files == null || files.length == 0) { ! tf_dir.setText("Directory contains no plugins"); } else { loadPlugins(files); } + tvf.getApp().pluginAssignConfigNodes(); + setLabelText(tvf.getApp()); + MenuHelpPluginsFrame.this.pack(); + tvf.rebuildMainPanelMenu(); } }); ! this.pack(); ! this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); ! } } \ No newline at end of file |