FileDocCategorySizeDatePackage
SFPluginDetailsLoaderImpl.javaAPI DocAzureus 3.0.3.412394Sat Sep 08 19:30:42 BST 2007org.gudy.azureus2.pluginsimpl.update.sf.impl2

SFPluginDetailsLoaderImpl

public class SFPluginDetailsLoaderImpl extends Object implements ResourceDownloaderListener, SFPluginDetailsLoader
author
parg

Fields Summary
private static final LogIDs
LOGID
private static final String
site_prefix
private static String
base_url_params
private static String
page_url
private static SFPluginDetailsLoaderImpl
singleton
private static org.gudy.azureus2.core3.util.AEMonitor
class_mon
private static final int
RELOAD_MIN_TIME
protected boolean
plugin_ids_loaded
protected long
plugin_ids_loaded_at
protected List
plugin_ids
protected Map
plugin_map
protected List
listeners
protected ResourceDownloaderFactory
rd_factory
protected org.gudy.azureus2.core3.util.AEMonitor
this_mon
Constructors Summary
protected SFPluginDetailsLoaderImpl()


	
	
	
		reset();
	
Methods Summary
public voidaddListener(SFPluginDetailsLoaderListener l)

		listeners.add( l );
	
public booleancompleted(ResourceDownloader downloader, java.io.InputStream data)

		return( true );
	
protected voiddumpTables(java.lang.String indent, HTMLTable[] tables)

		for (int i=0;i<tables.length;i++){
			
			HTMLTable	tab = tables[i];
			
			System.out.println( indent + "tab:" + tab.getContent());
			
			HTMLTableRow[] rows = tab.getRows();
			
			for (int j=0;j<rows.length;j++){
				
				HTMLTableRow	row = rows[j];
				
				System.out.println( indent + "  row[" + j + "]: " + rows[j].getContent());
				
				HTMLTableCell[]	cells = row.getCells();
				
				for (int k=0;k<cells.length;k++){
					
					System.out.println( indent + "    cell[" + k + "]: " + cells[k].getContent());
					
				}
			}
			
			dumpTables( indent + "  ", tab.getTables());
		}
	
public voidfailed(ResourceDownloader downloader, ResourceDownloaderException e)

		informListeners( "Error: " + e.getMessage());
	
public SFPluginDetailsgetPluginDetails(java.lang.String name)

		try{
			this_mon.enter();
		
				// make sure details are loaded
			
			getPluginIDs();
			
			SFPluginDetails details = (SFPluginDetails)plugin_map.get(name.toLowerCase()); 
			
			if ( details == null ){
				
				throw( new SFPluginDetailsException( "Plugin '" + name + "' not found" ));
			}
			
			return( details );
			
		}finally{
			this_mon.exit();
		}
	
public SFPluginDetails[]getPluginDetails()

		String[]	plugin_ids = getPluginIDs();
		
		SFPluginDetails[]	res = new SFPluginDetails[plugin_ids.length];
	
		for (int i=0;i<plugin_ids.length;i++){
			
			res[i] = getPluginDetails(plugin_ids[i]);
		}
		
		return( res );
	
public java.lang.String[]getPluginIDs()

		try{
			this_mon.enter();
		
			if ( !plugin_ids_loaded ){
				
				loadPluginList();
			}
			
			String[]	res = new String[plugin_ids.size()];
			
			plugin_ids.toArray( res );
			
			return( res );
			
		}finally{
			
			this_mon.exit();
		}
	
protected java.lang.StringgetRelativeURLBase()

		return( site_prefix );
	
public static SFPluginDetailsLoadergetSingleton()

	
	  
	
	
		try{
			class_mon.enter();
		
			if ( singleton == null ){
				
				singleton	= new SFPluginDetailsLoaderImpl();
			}
			
			return( singleton );
		}finally{
			
			class_mon.exit();
		}
	
protected voidinformListeners(java.lang.String log)

		for (int i=0;i<listeners.size();i++){
			
			((SFPluginDetailsLoaderListener)listeners.get(i)).log( log );
		}
	
protected voidloadPluginDetails(SFPluginDetailsImpl details)

		try{
			ResourceDownloader p_dl = rd_factory.create( new URL( site_prefix + "plugin_details.php?plugin=" + details.getId() + "&" + base_url_params ));
		
			p_dl = rd_factory.getRetryDownloader( p_dl, 5 );
		
			p_dl.addListener( this );

			HTMLPage	plugin_page = HTMLPageFactory.loadPage( p_dl.download());
			
			if ( !processPluginPage( details, plugin_page )){
							
				throw( new SFPluginDetailsException( "Plugin details load fails for '" + details.getId() + "': data not found" ));
			}
					
		}catch( Throwable e ){
			
			Debug.printStackTrace( e );
			
			throw( new SFPluginDetailsException( "Plugin details load fails", e ));
		}
	
protected voidloadPluginList()

		try{			
			ResourceDownloader dl = rd_factory.create( new URL(page_url));
			
			dl = rd_factory.getRetryDownloader( dl, 5 );
			
			dl.addListener( this );
			
			Properties	details = new Properties();
			
			InputStream is = dl.download();
			
			details.load( is );
			
			is.close();
			
			Iterator it = details.keySet().iterator();
			
			while( it.hasNext()){
				
				String	plugin_id 	= (String)it.next();
				
				String	data			= (String)details.get(plugin_id);

				int	pos = 0;
				
				List	bits = new ArrayList();
				
				while( pos < data.length()){
					
					int	p1 = data.indexOf(';",pos);
					
					if ( p1 == -1 ){
						
						bits.add( data.substring(pos).trim());
					
						break;
					}else{
						
						bits.add( data.substring(pos,p1).trim());
						
						pos = p1+1;
					}
				}
				
				if (bits.size() < 3) {
					Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR,
							"SF loadPluginList failed for plugin '" + plugin_id
									+ "'.  Details array is " + bits.size() + " (3 min)"));
				} else {
					String version = (String) bits.get(0);
					String cvs_version = (String) bits.get(1);
					String name = (String) bits.get(2);
					String category = "";

					if (bits.size() > 3) {
						category = (String) bits.get(3);
					}

					plugin_ids.add(plugin_id);

					plugin_map.put(plugin_id.toLowerCase(), new SFPluginDetailsImpl(this,
							plugin_id, version, cvs_version, name, category));
				}
			}
			
			plugin_ids_loaded	= true;
			
			plugin_ids_loaded_at	= SystemTime.getCurrentTime();
			
		}catch( Throwable e ){
			
			Debug.printStackTrace( e );
			
			throw( new SFPluginDetailsException( "Plugin list load failed", e ));
		}
	
protected booleanprocessPluginPage(SFPluginDetailsImpl details, HTMLPage page)

		HTMLTable[]	tables = page.getTables();
		
		// dumpTables("", tables );
		
		return( processPluginPage( details, tables ));
	
protected booleanprocessPluginPage(SFPluginDetailsImpl details, HTMLTable[] tables)

		for (int i=0;i<tables.length;i++){
			
			HTMLTable	table = tables[i];
			
			HTMLTableRow[]	rows = table.getRows();
		
			if ( rows.length == 10 ){
				
				HTMLTableCell[]	cells = rows[0].getCells();
				
				if ( cells.length == 6 &&
						cells[0].getContent().trim().equals("Name") &&
						cells[5].getContent().trim().equals("Contact")){
				
					
					// got the plugin details table
				
					HTMLTableCell[]	detail_cells = rows[2].getCells();
					
					//String	plugin_name			= detail_cells[0].getContent();
					//String	plugin_version		= detail_cells[1].getContent();
					String	plugin_auth			= detail_cells[4].getContent();
					
					String[]	dl_links = detail_cells[2].getLinks();
					
					String	plugin_download;
					
					if ( dl_links.length == 0 ){
						
						plugin_download	= "<unknown>";
						
					}else{
						
						plugin_download = site_prefix + dl_links[0];
					}
					
					HTMLTableCell[]	cvs_detail_cells = rows[3].getCells();

					// String	plugin_cvs_version		= cvs_detail_cells[1].getContent();

					String[]	cvs_dl_links 		= cvs_detail_cells[2].getLinks();
					
					String	plugin_cvs_download;
					
					if ( cvs_dl_links.length == 0 ){
						
						plugin_cvs_download	= "<unknown>";
						
					}else{
						
						plugin_cvs_download = site_prefix + cvs_dl_links[0];
					}
					
					String info_url = null;
					if (rows[9].getCells().length > 1) {
						info_url = rows[9].getCells()[1].getContent();
					}


					// System.out.println( "got plugin:" + plugin_name + "/" + plugin_version + "/" + plugin_download + "/" + plugin_auth );
					
					details.setDetails(
									plugin_download,
									plugin_auth,
									plugin_cvs_download,
									rows[6].getCells()[0].getContent(),
									rows[9].getCells()[0].getContent(),
									info_url);
					
					return( true );
				}
			}
			
			HTMLTable[]	sub_tables = table.getTables();
			
			boolean	res = processPluginPage( details, sub_tables );
			
			if( res ){
				
				return( res );
			}
		}
		
		return( false );
	
public voidremoveListener(SFPluginDetailsLoaderListener l)

		listeners.remove(l);
	
public voidreportActivity(ResourceDownloader downloader, java.lang.String activity)

		informListeners( activity );
	
public voidreportAmountComplete(ResourceDownloader downloader, long amount)

	
public voidreportPercentComplete(ResourceDownloader downloader, int percentage)

	
public voidreset()

		try{
			this_mon.enter();
		
			long	now = SystemTime.getCurrentTime();
			
				// handle backward time changes
			
			if ( now < plugin_ids_loaded_at ){
				
				plugin_ids_loaded_at	= 0;
			}
			
			if ( now - plugin_ids_loaded_at > RELOAD_MIN_TIME ){
				
				if (Logger.isEnabled())
					Logger.log(new LogEvent(LOGID,
							"SFPluginDetailsLoader: resetting values"));
				
				plugin_ids_loaded	= false;
			
				plugin_ids		= new ArrayList();
				plugin_map			= new HashMap();
				
			}else{
				if (Logger.isEnabled())
					Logger.log(new LogEvent(LOGID, LogEvent.LT_WARNING,
							"SFPluginDetailsLoader: not resetting, " + "cache still valid"));
			}
			
		}finally{
			
			this_mon.exit();
		}