pluginupdateplugin.java
来自「Azureus is a powerful, full-featured, cr」· Java 代码 · 共 1,101 行 · 第 1/2 页
JAVA
1,101 行
final int restart_type )
{
final Update update = checker.addUpdate(
update_name,
update_details,
version,
resource_downloader,
restart_type );
update.setUserObject( pi_for_update );
resource_downloader.addListener(
new ResourceDownloaderAdapter()
{
public boolean
completed(
final ResourceDownloader downloader,
InputStream data )
{
// during the update phase report any messages
// to the downloader
LoggerChannelListener list =
new LoggerChannelListener()
{
public void
messageLogged(
int type,
String content )
{
downloader.reportActivity( content );
}
public void
messageLogged(
String str,
Throwable error )
{
downloader.reportActivity( str );
}
};
try{
log.addListener(list);
installUpdate(
update,
pi_for_update,
restart_type == Update.RESTART_REQUIRED_NO,
is_jar,
version,
data );
return( true );
}finally{
log.removeListener( list );
}
}
});
}
protected void
installUpdate(
Update update,
PluginInterface plugin, // note this will be first one if > 1 defined
boolean unloadable,
boolean is_jar, // false -> zip
String version,
InputStream data )
{
log.log( LoggerChannel.LT_INFORMATION,
"Installing plugin " + plugin.getPluginID() + ", version " + version );
String target_version = version.endsWith("_CVS")?version.substring(0,version.length()-4):version;
try{
boolean update_txt_found = false;
// .jar files get copied straight in with the right version number
// .zip files need to be unzipped. There are various possibilities for
// target dir depending on the contents of the zip file. Basically we
// need to remove any zip paths to ensure it ends up in the right place
// There's also the issue of overwriting stuff like "plugin.properties"
// and any other config files....
String target = plugin.getPluginDirectoryName() + File.separator +
plugin.getPluginID() + "_" + target_version + (is_jar?".jar":".zip");
FileUtil.copyFile( data, new FileOutputStream(target));
if ( !is_jar ){
ZipInputStream zis =
new ZipInputStream(
new BufferedInputStream( new FileInputStream( target ) ));
// first look for a common dir prefix
String common_prefix = null;
try{
while( true ){
ZipEntry entry = zis.getNextEntry();
if ( entry == null ){
break;
}
String name = entry.getName();
if ( !name.endsWith("/")){
if ( common_prefix == null ){
common_prefix = name;
}else{
int len = 0;
for (int i=0;i<Math.min(common_prefix.length(), name.length());i++){
if ( common_prefix.charAt(i) == name.charAt(i)){
len++;
}else{
break;
}
}
common_prefix = common_prefix.substring(0,len);
}
}
long rem = entry.getSize();
byte[] buffer = new byte[65536];
while( rem > 0 ){
int len = zis.read( buffer, 0, buffer.length>rem?(int)rem:buffer.length);
if ( len <= 0 ){
break;
}
rem -= len;
}
if ( rem != 0 ){
throw( new Exception( "Invalid ZIP file" ));
}
}
}finally{
zis.close();
}
if ( common_prefix != null ){
int pos = common_prefix.lastIndexOf("/");
if ( pos == -1 ){
common_prefix = "";
}else{
common_prefix = common_prefix.substring(0,pos+1);
}
zis = new ZipInputStream(
new BufferedInputStream( new FileInputStream( target ) ));
try{
while( true ){
ZipEntry entry = zis.getNextEntry();
if ( entry == null ){
break;
}
String name = entry.getName();
OutputStream entry_os = null;
File initial_target = null;
File final_target = null;
boolean is_plugin_properties = false;
try{
if ( name.length() >= common_prefix.length() &&
!name.endsWith("/")){
String file_name = entry.getName().substring( common_prefix.length());
initial_target = new File( plugin.getPluginDirectoryName() + File.separator + file_name );
final_target = initial_target;
if ( initial_target.exists()){
if ( file_name.toLowerCase().endsWith(".properties") ||
file_name.toLowerCase().endsWith(".config" )){
is_plugin_properties = file_name.toLowerCase().equals("plugin.properties");
String old_file_name = file_name;
file_name = file_name + "_" + target_version;
final_target = new File( plugin.getPluginDirectoryName() + File.separator + file_name );
log.log( LoggerChannel.LT_INFORMATION,
"saving new file '" + old_file_name + "'as '" + file_name +"'" );
}else{
log.log( LoggerChannel.LT_INFORMATION,
"overwriting '" + file_name +"'" );
// back up just in case
File backup = new File( initial_target.getParentFile(), initial_target.getName() + ".bak" );
if ( backup.exists()){
backup.delete();
}
initial_target.renameTo( backup );
}
}
final_target.getParentFile().mkdirs();
entry_os = new FileOutputStream( final_target );
}
long rem = entry.getSize();
byte[] buffer = new byte[65536];
while( rem > 0 ){
int len = zis.read( buffer, 0, buffer.length>rem?(int)rem:buffer.length);
if ( len <= 0 ){
break;
}
rem -= len;
if ( entry_os != null ){
entry_os.write( buffer, 0, len );
}
}
}finally{
if ( entry_os != null ){
entry_os.close();
}
}
if ( is_plugin_properties ){
// we've got to modify the plugin.version in the existing
// file (if it exists) otherwise we keep downloading the new
// version! Or, if the key doesn't exist, add it!
// if we were smarter we could merge values from the
// old one into the new one, however this is too much like
// hard work
// hmm, we really need to at least merge in the new
// predefined values such as
// plugin.name, plugin.names
// plugin.class, plugin.classes
// plugin.langfile
Properties old_props = new Properties();
Properties new_props = new Properties();
List props_to_delete = new ArrayList();
Map props_to_replace = new HashMap();
Map props_to_insert = new HashMap();
try{
FileInputStream fis = new FileInputStream( initial_target );
old_props.load( fis );
try{
fis.close();
}catch( Throwable e ){
}
fis = new FileInputStream( final_target );
new_props.load( fis );
try{
fis.close();
}catch( Throwable e ){
}
}catch( Throwable e ){
Debug.printStackTrace( e );
}
new_props.put( "plugin.version", target_version );
String[] prop_names =
{ "plugin.name", "plugin.names",
"plugin.class", "plugin.classes",
"plugin.version",
"plugin.langfile"
};
for (int z=0;z<prop_names.length;z++){
String prop_name = prop_names[z];
String old_name = old_props.getProperty( prop_name );
String new_name = new_props.getProperty( prop_name );
if ( new_name != null ){
if ( prop_name.equals( "plugin.name")){
props_to_delete.add( "plugin.names" );
}else if ( prop_name.equals( "plugin.names")){
props_to_delete.add( "plugin.name" );
}else if ( prop_name.equals( "plugin.class")){
props_to_delete.add( "plugin.classes" );
}else if ( prop_name.equals( "plugin.classes")){
props_to_delete.add( "plugin.class" );
}
if ( old_name == null ){
props_to_insert.put( prop_name, new_name );
}else if ( !new_name.equals( old_name )){
props_to_replace.put( prop_name, new_name );
}
}
}
File tmp_file = new File(initial_target.toString() + ".tmp");
File bak_file = new File(initial_target.toString() + ".bak");
LineNumberReader lnr = null;
PrintWriter tmp = null;
try{
lnr = new LineNumberReader(new FileReader(initial_target));
tmp = new PrintWriter(new FileWriter( tmp_file ));
Iterator it = props_to_insert.keySet().iterator();
while( it.hasNext()){
String pn = (String)it.next();
String pv = (String)props_to_insert.get(pn);
log.log(" Inserting property:" + pn + "=" + pv );
tmp.println( pn + "=" + pv );
}
while(true){
String line = lnr.readLine();
if ( line == null ){
break;
}
int ep = line.indexOf('=');
if( ep != -1 ){
String pn = line.substring(0,ep).trim();
if ( props_to_delete.contains(pn)){
log.log(" Deleting property:" + pn );
}else{
String rv = (String)props_to_replace.get( pn );
if ( rv != null ){
log.log(" Replacing property:" + pn + " with " + rv );
tmp.println( pn + "=" + rv );
}else{
tmp.println( line );
}
}
}else{
tmp.println( line );
}
}
}finally{
lnr.close();
if ( tmp != null ){
tmp.close();
}
}
if ( bak_file.exists()){
bak_file.delete();
}
if ( !initial_target.renameTo( bak_file)){
throw( new IOException( "Failed to rename '" + initial_target.toString() + "' to '" + bak_file.toString() + "'" ));
}
if ( !tmp_file.renameTo( initial_target )){
bak_file.renameTo( initial_target );
throw( new IOException( "Failed to rename '" + tmp_file.toString() + "' to '" + initial_target.toString() + "'" ));
}
bak_file.delete();
}else if ( final_target != null && final_target.getName().equalsIgnoreCase( "update.txt" )){
update_txt_found = true;
LineNumberReader lnr = null;
try{
lnr = new LineNumberReader( new FileReader( final_target ));
while(true){
String line = lnr.readLine();
if (line == null ){
break;
}
log.log( LoggerChannel.LT_INFORMATION, line );
}
}catch( Throwable e ){
Debug.printStackTrace( e );
}finally{
if ( lnr != null ){
lnr.close();
}
}
}
}
}finally{
zis.close();
}
}
}
if ( unloadable ){
log.log( "Plugin initialising, please wait... " );
plugin.reload(); // this will reload all if > 1 defined
log.log( "... initialisation complete." );
}
String msg = "Version " + version + " of plugin '" +
plugin.getPluginID() + "' " +
"installed successfully";
if ( update_txt_found ){
msg += " - See update log for details";
}
log.logAlert( update_txt_found?LoggerChannel.LT_WARNING:LoggerChannel.LT_INFORMATION, msg );
}catch( Throwable e ){
String msg = "Version " + version + " of plugin '" +
plugin.getPluginID() + "' " +
"failed to install - " + (e.getMessage());
log.log( msg, e );
log.logAlert( LoggerChannel.LT_ERROR, msg );
}finally{
update.complete();
}
}
protected void
logMultiLine(
String indent,
List lines )
{
for (int i=0;i<lines.size();i++){
log.log( LoggerChannel.LT_INFORMATION, indent + (String)lines.get(i) );
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?