📄 pluginupdateplugin.java
字号:
/*
* Created on 28-Apr-2004
* Created by Paul Gardner
* Copyright (C) 2004, 2005, 2006 Aelitis, All Rights Reserved.
*
* 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, or (at your option) any later version.
* 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.
* 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.
*
* AELITIS, SAS au capital de 46,603.30 euros
* 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
*
*/
package org.gudy.azureus2.pluginsimpl.update;
/**
* @author parg
*
*/
import java.util.*;
import java.util.zip.*;
import java.net.URL;
import java.io.*;
import org.gudy.azureus2.core3.html.HTMLUtils;
import org.gudy.azureus2.core3.internat.MessageText;
import org.gudy.azureus2.core3.util.*;
import org.gudy.azureus2.plugins.*;
import org.gudy.azureus2.plugins.installer.PluginInstaller;
import org.gudy.azureus2.plugins.installer.StandardPlugin;
import org.gudy.azureus2.plugins.logging.*;
import org.gudy.azureus2.plugins.update.*;
import org.gudy.azureus2.plugins.utils.resourcedownloader.*;
import org.gudy.azureus2.plugins.ui.*;
import org.gudy.azureus2.plugins.ui.config.ConfigSection;
import org.gudy.azureus2.plugins.ui.model.*;
import org.gudy.azureus2.pluginsimpl.*;
import org.gudy.azureus2.pluginsimpl.update.sf.*;
import org.gudy.azureus2.update.CorePatchChecker;
import com.aelitis.azureus.core.versioncheck.VersionCheckClient;
public class
PluginUpdatePlugin
implements Plugin
{
private static final String PLUGIN_CONFIGSECTION_ID = "plugins.update";
public static final int RD_SIZE_RETRIES = 3;
public static final int RD_SIZE_TIMEOUT = 10000;
protected PluginInterface plugin_interface;
protected SFPluginDetailsLoader loader;
protected LoggerChannel log;
private String last_id_info = "";
public void
initialize(
PluginInterface _plugin_interface )
{
plugin_interface = _plugin_interface;
plugin_interface.getPluginProperties().setProperty( "plugin.version", "1.0" );
plugin_interface.getPluginProperties().setProperty( "plugin.name", "Plugin Updater" );
log = plugin_interface.getLogger().getChannel("Plugin Update");
UIManager ui_manager = plugin_interface.getUIManager();
final BasicPluginViewModel model =
ui_manager.createBasicPluginViewModel(
"Plugin Update");
final PluginConfig plugin_config = plugin_interface.getPluginconfig();
boolean enabled = plugin_config.getPluginBooleanParameter( "enable.update", true );
model.setConfigSectionID(PLUGIN_CONFIGSECTION_ID);
model.getStatus().setText( enabled?"Running":"Optional checks disabled" );
model.getActivity().setVisible( false );
model.getProgress().setVisible( false );
log.addListener(
new LoggerChannelListener()
{
public void
messageLogged(
int type,
String message )
{
model.getLogArea().appendText( message+"\n");
}
public void
messageLogged(
String str,
Throwable error )
{
model.getLogArea().appendText( error.toString()+"\n");
}
});
loader = SFPluginDetailsLoaderFactory.getSingleton();
loader.addListener(
new SFPluginDetailsLoaderListener()
{
public void
log(
String str )
{
log.log( LoggerChannel.LT_INFORMATION, "[" + str + "]" );
}
});
BasicPluginConfigModel config = ui_manager.createBasicPluginConfigModel(ConfigSection.SECTION_PLUGINS, PLUGIN_CONFIGSECTION_ID);
config.addBooleanParameter2( "enable.update", "Plugin.pluginupdate.enablecheck", true );
UpdateManager update_manager = plugin_interface.getUpdateManager();
update_manager.addListener(
new UpdateManagerListener()
{
public void
checkInstanceCreated(
UpdateCheckInstance inst )
{
SFPluginDetailsLoaderFactory.getSingleton().reset();
}
});
final PluginManager plugin_manager = plugin_interface.getPluginManager();
PluginInterface[] plugins = plugin_manager.getPlugins();
int mandatory_count = 0;
int non_mandatory_count = 0;
for (int i=0;i<plugins.length;i++){
PluginInterface pi = plugins[i];
boolean pi_mandatory = pi.isMandatory();
if ( pi_mandatory ){
mandatory_count++;
}else{
non_mandatory_count++;
}
}
final int f_non_mandatory_count = non_mandatory_count;
final int f_mandatory_count = mandatory_count;
update_manager.registerUpdatableComponent(
new UpdatableComponent()
{
public String
getName()
{
return( "Non-mandatory plugins" );
}
public int
getMaximumCheckTime()
{
return( f_non_mandatory_count * (( RD_SIZE_RETRIES * RD_SIZE_TIMEOUT )/1000));
}
public void
checkForUpdate(
UpdateChecker checker )
{
if ( checkForUpdateSupport( checker, null, false ) == 0 ){
String[] rps = VersionCheckClient.getSingleton(). getRecommendedPlugins();
boolean found_one = false;
for (int i=0;i<rps.length;i++){
String rp_id = rps[i];
if ( plugin_manager.getPluginInterfaceByID( rp_id ) != null ){
// already installed
continue;
}
final String config_key = "recommended.processed." + rp_id;
if ( !plugin_config.getPluginBooleanParameter( config_key, false )){
try{
final PluginInstaller installer = plugin_interface.getPluginManager().getPluginInstaller();
StandardPlugin[] sps = installer.getStandardPlugins();
for (int j=0;j<sps.length;j++){
final StandardPlugin sp = sps[j];
if ( sp.getId().equals( rp_id )){
found_one = true;
checker.getCheckInstance().addListener(
new UpdateCheckInstanceListener()
{
public void
cancelled(
UpdateCheckInstance instance )
{
}
public void
complete(
UpdateCheckInstance instance )
{
if ( instance.getUpdates().length == 0 ){
installRecommendedPlugin( installer, sp );
plugin_config.setPluginParameter( config_key, true );
}
}
});
break;
}
}
}catch( Throwable e ){
}
}
if ( found_one ){
break;
}
}
}
}
}, false );
update_manager.registerUpdatableComponent(
new UpdatableComponent()
{
public String
getName()
{
return( "Mandatory plugins" );
}
public int
getMaximumCheckTime()
{
return( f_mandatory_count * (( RD_SIZE_RETRIES * RD_SIZE_TIMEOUT )/1000));
}
public void
checkForUpdate(
UpdateChecker checker )
{
checkForUpdateSupport( checker, null, true );
}
}, true );
update_manager.addListener(
new UpdateManagerListener()
{
public void
checkInstanceCreated(
UpdateCheckInstance instance )
{
log.log( LoggerChannel.LT_INFORMATION, "**** Update check starts ****" );
}
});
}
protected void
installRecommendedPlugin(
PluginInstaller installer,
StandardPlugin plugin )
{
try{
installer.requestInstall( MessageText.getString("plugin.installer.recommended.plugin"), plugin );
}catch( Throwable e ){
log.log(e);
}
}
public UpdatableComponent
getCustomUpdateableComponent(
final String id,
final boolean mandatory )
{
return(
new UpdatableComponent()
{
public String
getName()
{
return( "Installation of '" + id + "'" );
}
public int
getMaximumCheckTime()
{
return( ( RD_SIZE_RETRIES * RD_SIZE_TIMEOUT )/1000 );
}
public void
checkForUpdate(
UpdateChecker checker )
{
checkForUpdateSupport( checker, new String[]{ id }, mandatory );
}
});
}
protected int
checkForUpdateSupport(
UpdateChecker checker,
String[] ids_to_check, // explicit ids or null for all
boolean mandatory )
{
int num_updates_found = 0;
try{
if ( (!mandatory) &&
(ids_to_check == null ) && // allow custom actions through
(!plugin_interface.getPluginconfig().getPluginBooleanParameter( "enable.update", true ))){
return( num_updates_found );
}
PluginInterface[] plugins = plugin_interface.getPluginManager().getPlugins();
List plugins_to_check = new ArrayList();
List plugins_to_check_ids = new ArrayList();
Map plugins_to_check_unloadable = new HashMap();
Map plugins_to_check_names = new HashMap();
for (int i=0;i<plugins.length;i++){
PluginInterface pi = plugins[i];
if ( pi.isDisabled()){
continue;
}
String mand = pi.getPluginProperties().getProperty( "plugin.mandatory");
boolean pi_mandatory = mand != null && mand.trim().toLowerCase().equals("true");
if ( pi_mandatory != mandatory ){
continue;
}
String id = pi.getPluginID();
String version = pi.getPluginVersion();
String name = pi.getPluginName();
if ( ids_to_check != null ){
boolean id_selected = false;
for (int j=0;j<ids_to_check.length;j++){
if ( ids_to_check[j].equals( id )){
id_selected = true;
break;
}
}
if ( !id_selected ){
continue;
}
}
if ( version != null ){
if ( plugins_to_check_ids.contains( id )){
String s = (String)plugins_to_check_names.get(id);
if ( !name.equals( id )){
plugins_to_check_names.put( id, s+","+name);
}
Boolean old_unloadable = (Boolean)plugins_to_check_unloadable.get(id);
plugins_to_check_unloadable.put(id,new Boolean(pi.isUnloadable() && old_unloadable.booleanValue()));
}else{
plugins_to_check_ids.add( id );
plugins_to_check.add( pi );
plugins_to_check_names.put( id, name.equals(id)?"":name);
plugins_to_check_unloadable.put( id, new Boolean( pi.isUnloadable()));
}
}
String location = pi.getPluginDirectoryName();
log.log( LoggerChannel.LT_INFORMATION, (mandatory?"*":"-") + pi.getPluginName() + ", id = " + id + (version==null?"":(", version = " + pi.getPluginVersion())) + (location==null?"":( ", loc = " + location)));
}
String[] ids = loader.getPluginIDs();
String id_info = "";
for (int i=0;i<ids.length;i++){
String id = ids[i];
SFPluginDetails details = loader.getPluginDetails( id );
id_info += (i==0?"":",") + ids[i] + "=" + details.getVersion()+"/"+details.getCVSVersion();
}
if ( !id_info.equals( last_id_info )){
last_id_info = id_info;
log.log( LoggerChannel.LT_INFORMATION, "Downloaded plugin info = " + id_info );
}
for ( int i=0;i<plugins_to_check.size();i++){
if ( checker.getCheckInstance().isCancelled()){
throw( new Exception( "Update check cancelled" ));
}
final PluginInterface pi_being_checked = (PluginInterface)plugins_to_check.get(i);
final String plugin_id = pi_being_checked.getPluginID();
checker.reportProgress( "Loading details for " + plugin_id + "/" + pi_being_checked.getPluginName());
boolean found = false;
for (int j=0;j<ids.length;j++){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -