📄 plugininitializer.java
字号:
/*
* File : PluginInitializer.java
* Created : 2 nov. 2003 18:59:17
* By : Olivier
*
* Azureus - a Java Bittorrent client
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details ( see the LICENSE file ).
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.gudy.azureus2.pluginsimpl.local;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLConnection;
import java.util.*;
import com.aelitis.azureus.core.*;
import org.gudy.azureus2.core3.config.COConfigurationManager;
import org.gudy.azureus2.core3.download.DownloadManager;
import org.gudy.azureus2.core3.global.GlobalManager;
import org.gudy.azureus2.core3.global.GlobalManagerListener;
import org.gudy.azureus2.core3.internat.MessageText;
import org.gudy.azureus2.core3.util.*;
import org.gudy.azureus2.core3.logging.*;
import org.gudy.azureus2.plugins.*;
import org.gudy.azureus2.pluginsimpl.local.launch.PluginLauncherImpl;
import org.gudy.azureus2.pluginsimpl.local.ui.UIManagerImpl;
import org.gudy.azureus2.pluginsimpl.local.update.*;
import org.gudy.azureus2.pluginsimpl.local.utils.UtilitiesImpl;
import org.gudy.azureus2.update.UpdaterUpdateChecker;
import org.gudy.azureus2.update.UpdaterUtils;
/**
* @author Olivier
*
*/
public class
PluginInitializer
implements GlobalManagerListener, AEDiagnosticsEvidenceGenerator
{
private static final LogIDs LOGID = LogIDs.CORE;
public static final String INTERNAL_PLUGIN_ID = "<internal>";
// class name, plugin id, plugin key (key used for config props so if you change
// it you'll need to migrate the config)
// "id" is used when checking for updates
// IF YOU ADD TO THE BUILTIN PLUGINS, AMEND PluginManagerDefault appropriately!!!!
// Plugin ID constant
// class
// plugin id
// plugin key for prefixing config data
// report if not present
private String[][] builtin_plugins = {
{ PluginManagerDefaults.PID_START_STOP_RULES,
"com.aelitis.azureus.plugins.startstoprules.defaultplugin.StartStopRulesDefaultPlugin",
"azbpstartstoprules",
"",
"true" },
{ PluginManagerDefaults.PID_REMOVE_RULES,
"com.aelitis.azureus.plugins.removerules.DownloadRemoveRulesPlugin",
"azbpremovalrules",
"",
"true" },
{ PluginManagerDefaults.PID_SHARE_HOSTER,
"com.aelitis.azureus.plugins.sharing.hoster.ShareHosterPlugin",
"azbpsharehoster",
"ShareHoster",
"true" },
{ PluginManagerDefaults.PID_PLUGIN_UPDATE_CHECKER,
"org.gudy.azureus2.pluginsimpl.update.PluginUpdatePlugin",
"azbppluginupdate",
"PluginUpdate",
"true" },
{ PluginManagerDefaults.PID_CLIENT_ID,
"com.aelitis.azureus.plugins.clientid.ClientIDPlugin",
"azbpclientid",
"Client ID",
"true" },
{ PluginManagerDefaults.PID_UPNP,
"com.aelitis.azureus.plugins.upnp.UPnPPlugin",
"azbpupnp",
"UPnP",
"true" },
{ PluginManagerDefaults.PID_DHT,
"com.aelitis.azureus.plugins.dht.DHTPlugin",
"azbpdht",
"DHT",
"true" },
{ PluginManagerDefaults.PID_DHT_TRACKER,
"com.aelitis.azureus.plugins.tracker.dht.DHTTrackerPlugin",
"azbpdhdtracker",
"DHT Tracker",
"true" },
{ PluginManagerDefaults.PID_MAGNET,
"com.aelitis.azureus.plugins.magnet.MagnetPlugin",
"azbpmagnet",
"Magnet URI Handler",
"true" },
{ PluginManagerDefaults.PID_CORE_UPDATE_CHECKER,
"org.gudy.azureus2.update.CoreUpdateChecker",
"azbpcoreupdater",
"CoreUpdater",
"true" },
{ PluginManagerDefaults.PID_CORE_PATCH_CHECKER,
"org.gudy.azureus2.update.CorePatchChecker",
"azbpcorepatcher",
"CorePatcher",
"true" },
{ PluginManagerDefaults.PID_PLATFORM_CHECKER,
"org.gudy.azureus2.platform.win32.PlatformManagerUpdateChecker",
"azplatform2",
"azplatform2",
"true" },
//{ PluginManagerDefaults.PID_JPC,
// "com.aelitis.azureus.plugins.jpc.JPCPlugin",
// "azjpc",
// "azjpc",
// "false" },
{ PluginManagerDefaults.PID_EXTERNAL_SEED,
"com.aelitis.azureus.plugins.extseed.ExternalSeedPlugin",
"azextseed",
"azextseed",
"true" },
{ PluginManagerDefaults.PID_EXTERNAL_SEED,
"com.aelitis.azureus.plugins.tracker.local.LocalTrackerPlugin",
"azlocaltracker",
"azlocaltracker",
"true" },
};
// these can be removed one day
private static String[][]default_version_details =
{
{ "org.gudy.azureus2.ui.webplugin.remoteui.servlet.RemoteUIServlet",
"webui", "Swing Web Interface", "1.2.3" },
{ "org.ludo.plugins.azureus.AzureusIpFilterExporter",
"safepeer", "SafePeer", "1.2.4" },
{ "org.gudy.azureus2.countrylocator.Plugin",
"CountryLocator", "Country Locator", "1.0" },
{ "org.gudy.azureus2.ui.webplugin.remoteui.xml.server.XMLHTTPServerPlugin",
"xml_http_if", "XML over HTTP", "1.0" },
{ "org.cneclipse.bdcc.BDCCPlugin",
"bdcc", "BitTorrent IRC Bot", "2.1" },
{ "org.cneclipse.multiport.MultiPortPlugin",
"multi-ports", "Mutli-Port Trackers", "1.0" },
{ "i18nPlugin.i18nPlugin",
"i18nAZ", "i18nAZ", "1.0" },
{ "info.baeker.markus.plugins.azureus.RSSImport",
"RSSImport", "RSS Importer", "1.0" },
};
private static PluginInitializer singleton;
private static AEMonitor class_mon = new AEMonitor( "PluginInitializer");
private static List registration_queue = new ArrayList();
private AzureusCoreOperation core_operation;
private AzureusCore azureus_core;
private PluginInterfaceImpl default_plugin;
private PluginManager plugin_manager;
private ClassLoader root_class_loader = getClass().getClassLoader();
private List loaded_pi_list = new ArrayList();
private List plugins = new ArrayList();
private List plugin_interfaces = new ArrayList();
private boolean initialisation_complete;
public static PluginInitializer
getSingleton(
AzureusCore azureus_core,
AzureusCoreOperation core_operation )
{
try{
class_mon.enter();
if ( singleton == null ){
singleton = new PluginInitializer( azureus_core, core_operation );
}
return( singleton );
}finally{
class_mon.exit();
}
}
protected static void
queueRegistration(
Class _class )
{
try{
class_mon.enter();
if ( singleton == null ){
registration_queue.add( _class );
}else{
try{
singleton.initializePluginFromClass( _class, INTERNAL_PLUGIN_ID, _class.getName());
}catch(PluginException e ){
}
}
}finally{
class_mon.exit();
}
}
protected static void
queueRegistration(
Plugin plugin,
String id )
{
try{
class_mon.enter();
if ( singleton == null ){
registration_queue.add( new Object[]{ plugin, id });
}else{
try{
singleton.initializePluginFromInstance( plugin, id, plugin.getClass().getName());
}catch(PluginException e ){
}
}
}finally{
class_mon.exit();
}
}
protected
PluginInitializer(
AzureusCore _azureus_core,
AzureusCoreOperation _core_operation )
{
azureus_core = _azureus_core;
AEDiagnostics.addEvidenceGenerator( this );
azureus_core.addLifecycleListener(
new AzureusCoreLifecycleAdapter()
{
public void
componentCreated(
AzureusCore core,
AzureusCoreComponent comp )
{
if ( comp instanceof GlobalManager ){
GlobalManager gm = (GlobalManager)comp;
gm.addListener( PluginInitializer.this );
}
}
});
core_operation = _core_operation;
UpdateManagerImpl.getSingleton( azureus_core ); // initialise the update manager
plugin_manager = PluginManagerImpl.getSingleton( this );
UpdaterUtils.checkPlugin();
}
public List
loadPlugins(
AzureusCore core,
boolean bSkipAlreadyLoaded)
{
List pluginLoaded = new ArrayList();
PluginManagerImpl.setStartDetails( core );
getRootClassLoader();
// first do explicit plugins
File user_dir = FileUtil.getUserFile("plugins");
File app_dir = FileUtil.getApplicationFile("plugins");
int user_plugins = 0;
int app_plugins = 0;
if ( user_dir.exists() && user_dir.isDirectory()){
user_plugins = user_dir.listFiles().length;
}
if ( app_dir.exists() && app_dir.isDirectory()){
app_plugins = app_dir.listFiles().length;
}
// user ones first so they override app ones if present
pluginLoaded.addAll(loadPluginsFromDir(user_dir, 0, user_plugins
+ app_plugins, bSkipAlreadyLoaded));
if ( !user_dir.equals( app_dir )){
pluginLoaded.addAll(loadPluginsFromDir(app_dir, user_plugins,
user_plugins + app_plugins, bSkipAlreadyLoaded));
}
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "Loading built-in plugins"));
if (core_operation != null) {
core_operation.reportCurrentTask(MessageText.getString("splash.plugin")
+ MessageText.getString("ConfigView.pluginlist.column.type.builtIn"));
}
PluginManagerDefaults def = PluginManager.getDefaults();
for (int i=0;i<builtin_plugins.length;i++){
if ( def.isDefaultPluginEnabled( builtin_plugins[i][0])){
try{
// lazyness here, for builtin we use static load method with default plugin interface
// if we need to improve on this then we'll have to move to a system more akin to
// the dir-loaded plugins
Class cla = root_class_loader.loadClass( builtin_plugins[i][1]);
Method load_method = cla.getMethod( "load", new Class[]{ PluginInterface.class });
load_method.invoke( null, new Object[]{ getDefaultInterfaceSupport() });
Logger.log(new LogEvent(LOGID, LogEvent.LT_WARNING,
"Built-in plugin '" + builtin_plugins[i][0] + "' ok"));
}catch( NoSuchMethodException e ){
}catch( Throwable e ){
if ( builtin_plugins[i][4].equalsIgnoreCase("true" )){
Debug.printStackTrace( e );
Logger.log(new LogAlert(LogAlert.UNREPEATABLE,
"Load of built in plugin '" + builtin_plugins[i][2] + "' fails", e));
}
}
}else{
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, LogEvent.LT_WARNING,
"Built-in plugin '" + builtin_plugins[i][2] + "' is disabled"));
}
}
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "Loading dynamically registered plugins"));
for (int i=0;i<registration_queue.size();i++){
Object entry = registration_queue.get(i);
Class cla;
String id;
if ( entry instanceof Class ){
cla = (Class)entry;
id = cla.getName();
}else{
Object[] x = (Object[])entry;
Plugin plugin = (Plugin)x[0];
cla = plugin.getClass();
id = (String)x[1];
}
try{
// lazyness here, for dynamic we use static load method with default plugin interface
// if we need to improve on this then we'll have to move to a system more akin to
// the dir-loaded plugins
Method load_method = cla.getMethod( "load", new Class[]{ PluginInterface.class });
load_method.invoke( null, new Object[]{ getDefaultInterfaceSupport() });
}catch( NoSuchMethodException e ){
}catch( Throwable e ){
Debug.printStackTrace( e );
Logger.log(new LogAlert(LogAlert.UNREPEATABLE,
"Load of dynamic plugin '" + id + "' fails", e));
}
}
return pluginLoaded;
}
private void
getRootClassLoader()
{
// first do explicit plugins
File user_dir = FileUtil.getUserFile("shared");
getRootClassLoader( user_dir );
File app_dir = FileUtil.getApplicationFile("shared");
if ( !user_dir.equals( app_dir )){
getRootClassLoader( app_dir );
}
}
private void
getRootClassLoader(
File dir )
{
dir = new File( dir, "lib" );
if ( dir.exists() && dir.isDirectory()){
File[] files = dir.listFiles();
if ( files != null ){
files = PluginLauncherImpl.getHighestJarVersions( files, new String[]{ null }, new String[]{ null }, false );
for (int i=0;i<files.length;i++){
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "Share class loader extended by " + files[i].toString()));
root_class_loader =
PluginLauncherImpl.addFileToClassPath(
PluginInitializer.class.getClassLoader(),
root_class_loader, files[i] );
}
}
}
}
private List
loadPluginsFromDir(
File pluginDirectory,
int plugin_offset,
int plugin_total,
boolean bSkipAlreadyLoaded)
{
List dirLoadedPIs = new ArrayList();
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "Plugin Directory is " + pluginDirectory));
if ( !pluginDirectory.exists() ){
FileUtil.mkdirs(pluginDirectory);
}
if( pluginDirectory.isDirectory()){
File[] pluginsDirectory = pluginDirectory.listFiles();
for(int i = 0 ; i < pluginsDirectory.length ; i++) {
if( pluginsDirectory[i].getName().equals( "CVS" ) ) {
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "Skipping plugin "
+ pluginsDirectory[i].getName()));
continue;
}
if (Logger.isEnabled())
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -