📄 pluginupdateplugin.java
字号:
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.equals( "azureus.sig") || name.endsWith("/"))){
boolean skip_file = false;
String file_name = entry.getName().substring( common_prefix.length());
if ( selected_platform != null ){
if ( file_name.indexOf( "platform/" ) != -1 ){
String bit_to_remove = "platform/" + selected_platform;
int pp = file_name.indexOf( bit_to_remove );
if ( pp != -1 ){
file_name = file_name.substring(0,pp) + file_name.substring(pp+bit_to_remove.length()+1);
}else{
// stuff from other platform, ignore it
skip_file = true;
}
}
}
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{
// if it is a versioned artifact then we don't need to do anything as the update contains
// the same version of the file
if ( isVersioned( file_name )){
log.log( LoggerChannel.LT_INFORMATION,
"Version '" + file_name +"' already present, skipping" );
skip_file = true;
}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();
}
if ( !initial_target.renameTo( backup )){
log.log( LoggerChannel.LT_INFORMATION,
" failed to backup '" + file_name +"', deferring until restart" );
if ( installer == null ){
update.setRestartRequired( Update.RESTART_REQUIRED_YES );
installer = update.getCheckInstance().createInstaller();
}
File tmp = new File( initial_target.getParentFile(), initial_target.getName() + ".tmp" );
tmp.delete();
installer.addMoveAction( tmp.getAbsolutePath(), initial_target.getAbsolutePath());
final_target = tmp;
}
}
}
}
if ( !skip_file ){
FileUtil.mkdirs(final_target.getParentFile());
entry_os = new FileOutputStream( final_target );
}
}
byte[] buffer = new byte[65536];
while( true ){
int len = zis.read( buffer );
if ( len <= 0 ){
break;
}
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.logAlertRepeatable( 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.logAlertRepeatable( LoggerChannel.LT_ERROR, msg );
}finally{
update.complete();
}
}
protected boolean
isVersioned(
String name )
{
// assumption: versioned names are of the form <prefix>_<version>.<extension>
// where version is mixture of digits and .s
int pos = name.lastIndexOf('_');
if ( pos == -1 || name.endsWith("_")){
return( false );
}
// remove everything up to _
String rem = name.substring(pos+1);
pos = rem.lastIndexOf( '.' );
// remove extension (e.g. .jar)
if ( pos != -1 ){
rem = rem.substring(0,pos);
}
for (int i=0;i<rem.length();i++){
char c = rem.charAt(i);
if ( c != '.' && !Character.isDigit( c )){
return( false );
}
}
return( true );
}
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -