FileDocCategorySizeDatePackage
FilePluginInstallerImpl.javaAPI DocAzureus 3.0.3.48982Mon Jul 02 14:00:22 BST 2007org.gudy.azureus2.pluginsimpl.local.installer

FilePluginInstallerImpl

public class FilePluginInstallerImpl extends Object implements org.gudy.azureus2.plugins.installer.FilePluginInstaller, InstallablePluginImpl
author
parg

Fields Summary
protected PluginInstallerImpl
installer
protected File
file
protected String
id
protected String
version
protected boolean
is_jar
Constructors Summary
protected FilePluginInstallerImpl(PluginInstallerImpl _installer, File _file)

		installer	= _installer;
		file		= _file;

		String	name = file.getName();
		
		int	pos = name.lastIndexOf( "." );
		
		boolean	ok = false;
		
		if ( pos != -1 ){
			
			String	prefix = name.substring(0,pos);
			String	suffix = name.substring(pos+1);
			
			if ( 	suffix.toLowerCase().equals( "jar") ||
					suffix.toLowerCase().equals( "zip" )){
		
				is_jar		= suffix.toLowerCase().equals( "jar");

					// See if we can get at the plugin.properties in the file
				
				Properties	properties	= null;
				
				ZipInputStream	zis = null;
				
				try{
					zis = 
						new ZipInputStream( 
								new BufferedInputStream( new FileInputStream( file ) ));
					
					
						while( properties == null ){
							
							ZipEntry	entry = zis.getNextEntry();
								
							if ( entry == null ){
								
								break;
							}
						
							String	zip_name = entry.getName().toLowerCase();
						
							// System.out.println( "zis1:" + zip_name );
							
							if ( zip_name.equals( "plugin.properties" ) || zip_name.endsWith( "/plugin.properties")){
								
								properties	= new Properties();
								
								properties.load( zis );
																
							}else if ( zip_name.endsWith( ".jar" )){
								
								ZipInputStream	zis2 = new ZipInputStream( zis );
								
								while( properties == null ){
									
									ZipEntry	entry2 = zis2.getNextEntry();
										
									if ( entry2 == null ){
										
										break;
									}
								
									String	zip_name2 = entry2.getName().toLowerCase();
							
									// System.out.println( "    zis2:" + zip_name2 );
									
									if ( zip_name2.equals( "plugin.properties" )){
										
										properties	= new Properties();
										
										properties.load( zis2 );
			
									}
								}
							}
						}
				}catch( Throwable e ){
					
					throw( new PluginException( "Failed to read plugin file", e ));
				
				}finally{
				
					if ( zis != null ){
						
						try{
							zis.close();
							
						}catch( Throwable e ){
							
							Debug.printStackTrace(e);
						}
					}
				}
					
				if ( properties == null ){
					
						// one valid possibility here, this is a built-in plugin. this doesn't have
						// a plugin.properties
										
					pos = prefix.lastIndexOf("_");
					
					if ( pos != -1 ){
					
						id 			= prefix.substring(0,pos);							
						version		= prefix.substring(pos+1);
					
						PluginInterface pi = installer.getPluginManager().getPluginInterfaceByID( id );

						ok =  	pi != null &&
								(	pi.getPluginDirectoryName() == null || 
								    pi.getPluginDirectoryName().length() == 0 ); 
					}
					
					if ( !ok ){
					
						throw( new PluginException( "Mandatory file 'plugin.properties' not found in plugin file" ));
					}
				}else{
				
					// unfortunately plugin.id isn't mandatory for the properties, and neither is plugin.version
				
					id		= properties.getProperty( "plugin.id" );
					version	= properties.getProperty( "plugin.version" );
				}
				
				if ( id == null ){
					
					// see if plugin is already loaded, if so we can get the id from it
					
					String	plugin_class = properties.getProperty("plugin.class");
					
					if ( plugin_class == null ){
						
						String	plugin_classes = properties.getProperty( "plugin.classes" );
						
						if ( plugin_classes != null ){
							
							int	semi_pos = plugin_classes.indexOf(";");
							
							if ( semi_pos == -1 ){
								
								plugin_class	= plugin_classes;
								
							}else{
								
								plugin_class = plugin_classes.substring( 0, semi_pos );
							}
						}
					}
					
					if ( plugin_class != null ){
						
						try{
							PluginInterface pi = installer.getPluginManager().getPluginInterfaceByClass( plugin_class );
							
							if ( pi != null ){
								
								id	= pi.getPluginID();
							}
						}catch( Throwable ignore ){
							
						}
					}
				}
				
				pos = prefix.lastIndexOf("_");
		
				if ( pos != -1 ){
		
					id 			= id==null?prefix.substring(0,pos):id;

						// see if we can normalise the ID based on SF values
					
					try{
						SFPluginDetailsLoader	loader = SFPluginDetailsLoaderFactory.getSingleton();
					
						String[]	ids = loader.getPluginIDs();
						
						for (int i=0;i<ids.length;i++){
							
							if ( ids[i].equalsIgnoreCase(id)){
								
								id = ids[i];
								
								break;
							}
						}
					}catch( Throwable e ){
						
						Debug.printStackTrace(e);
					}

					version		= version == null?prefix.substring(pos+1):version;

				}
				
				ok	= id != null && version != null;
			}
		}
		
		if ( !ok ){
			
			throw( new PluginException( "Invalid plugin file name: must be of form <pluginid>_<version>.[jar|zip]" ));
		}
	
Methods Summary
public voidaddUpdate(org.gudy.azureus2.plugins.update.UpdateCheckInstance inst, org.gudy.azureus2.pluginsimpl.update.PluginUpdatePlugin plugin_update_plugin, org.gudy.azureus2.plugins.Plugin plugin, org.gudy.azureus2.plugins.PluginInterface plugin_interface)

		inst.addUpdatableComponent(
				new UpdatableComponent()
				{
					public String
					getName()
					{
						return( file.getName());
					}
				
					public int
					getMaximumCheckTime()
					{
						return( 0 );
					}
					
					public void
					checkForUpdate(
						UpdateChecker	checker )
					{
						try{
							ResourceDownloader rd = 
								plugin_interface.getUtilities().getResourceDownloaderFactory().create( file );
							
							plugin_update_plugin.addUpdate(
								plugin_interface,
								checker,
								getName(),
								new String[]{"Installation from file: " + file.toString()},
								version,
								rd,
								is_jar,
								plugin_interface.isUnloadable()?Update.RESTART_REQUIRED_NO:Update.RESTART_REQUIRED_YES,
								false );
								
						}finally{
							
							checker.completed();
						}
							
					}
				}, false );
	
public org.gudy.azureus2.plugins.PluginInterfacegetAlreadyInstalledPlugin()

		return( installer.getAlreadyInstalledPlugin( getId()));
	
public java.lang.StringgetDescription()

		return( file.toString());
	
public java.io.FilegetFile()

		return( file );
	
public java.lang.StringgetId()

		return( id );
	
public org.gudy.azureus2.plugins.installer.PluginInstallergetInstaller()

		return( installer );
	
public java.lang.StringgetName()

		return( "" );
	
public java.lang.StringgetRelativeURLBase()

		return( "" );
	
public java.lang.StringgetVersion()

		return( version );
	
public voidinstall(boolean shared)

		installer.install( this, shared );
	
public voiduninstall()

		installer.uninstall( this );