View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.gui.plugin;
21  
22  import java.awt.Component;
23  
24  import org.apache.log4j.Logger;
25  import org.xnap.XNap;
26  import org.xnap.gui.Dialogs;
27  import org.xnap.gui.ErrorDialog;
28  import org.xnap.gui.StatusBar;
29  import org.xnap.plugin.PluginInfo;
30  import org.xnap.plugin.PluginInitializeException;
31  import org.xnap.plugin.PluginManager;
32  
33  public class PluginHelper {
34  
35      //--- Constant(s) ---
36  
37      private static Logger logger = Logger.getLogger(PluginHelper.class);
38  
39      //--- Data field(s) ---
40  
41  	public static boolean disablePlugin(Component parent, PluginInfo info)
42  	{
43  		if (!info.isLoaded()) {
44  			// a new PluginInfo has been selected from the list
45  			return false;
46  		}
47  		
48  		if (!info.canDisable()) {
49  			info.setEnableOnStartup(false);
50  			Dialogs.error
51  				(parent, 
52  				 XNap.tr("Can not disable plugin. Restart XNap to disable plugin."));
53  			return false;
54  		}
55  
56  		try {
57  			StatusBar.setText(XNap.tr("Disabling plugin") + "...");
58  			PluginManager.getInstance().setEnabled
59  				(info.getPlugin(), false);
60  			return true;
61  		}
62  		catch (Throwable e) {
63  			logger.error("could not disable plugin", e);
64  			ErrorDialog.show(parent, XNap.tr("Could not disable plugin."), e);
65  			return false;
66  		}
67  		finally {
68  			StatusBar.clear();
69  		}
70  	}
71  
72  	public static boolean enablePlugin(Component parent, PluginInfo info)
73  	{
74  		try {
75  			StatusBar.setText(XNap.tr("Loading plugin") + "...");
76  			if (info.isLoaded() || load(parent, info)) {
77  				// load was successful
78  				try {
79  					StatusBar.setText(XNap.tr("Enabling plugin") + "...");
80  					PluginManager.getInstance().setEnabled
81  						(info.getPlugin(), true);
82  					return true;
83  				}
84  				catch (PluginInitializeException e) {
85  					logger.error("could not enable plugin", e);
86  					Dialogs.error(parent, 
87  								  XNap.tr("Error while initializing plugin: {0}",
88  										  e.getLocalizedMessage()));
89  					
90  				}
91  				catch (ClassNotFoundException e) {
92  					logger.error("could not enable plugin", e);
93  					// FIX: print more verbose error message
94  					Dialogs.error(parent, 
95  								  XNap.tr("A required class is missing: {0}",
96  										  e.getLocalizedMessage()));
97  				}
98  				catch (Throwable e) {
99  					logger.error("could not enable plugin", e);
100 					ErrorDialog.show
101 						(parent, XNap.tr("Could not enable plugin."), e);
102 				}
103 			}
104 			return false;
105 		}
106 		finally {
107 			StatusBar.clear();
108 		}
109 	}
110  
111    /***
112      * Loads <code>info</code>. Displays a message box, if load fails.
113      *
114 	 * @param c the parent component for the error dialog
115 	 * @param info the info to load
116      * @return true, if successful; false, if plugin class could not be loaded
117      * @see PluginManager
118      */
119     public static boolean load(Component c, PluginInfo info)
120     {
121 		try {
122 			PluginManager.getInstance().load(info);
123 			return true;
124 		}
125 		catch (ClassNotFoundException e) {
126 			logger.error("could not load plugin", e);
127 			// FIX: print more verbose error message
128 			Dialogs.error(c, XNap.tr("A required class is missing: {0}",
129 									 e.getLocalizedMessage()));
130 		}
131 		catch (Throwable e) {
132 			logger.error("could not load " + info.getClassName(), e);
133 			ErrorDialog.show(c, XNap.tr("Could not load plugin: {0}.",
134 										e.getLocalizedMessage()), e);
135 		}
136 
137 		return false;
138     }
139 
140 }