FileDocCategorySizeDatePackage
UtilitiesImpl.javaAPI DocAzureus 3.0.3.416221Thu Jun 28 16:08:26 BST 2007org.gudy.azureus2.pluginsimpl.local.utils

UtilitiesImpl

public class UtilitiesImpl extends Object implements Utilities
author
parg

Fields Summary
private static InetAddress
last_public_ip_address
private static long
last_public_ip_address_time
private com.aelitis.azureus.core.AzureusCore
core
private PluginInterface
pi
private static ThreadLocal
tls
Constructors Summary
public UtilitiesImpl(com.aelitis.azureus.core.AzureusCore _core, PluginInterface _pi)

		
		
	
	
					
				 
	
		core	= _core;
		pi		= _pi;
	
Methods Summary
public java.nio.ByteBufferallocateDirectByteBuffer(int size)

		return( DirectByteBufferPool.getBuffer( DirectByteBuffer.AL_EXTERNAL,size ).getBuffer(DirectByteBuffer.SS_EXTERNAL));
	
public PooledByteBufferallocatePooledByteBuffer(int length)

		return( new PooledByteBufferImpl( length ));
	
public PooledByteBufferallocatePooledByteBuffer(byte[] data)

		return( new PooledByteBufferImpl( data ));
	
public PooledByteBufferallocatePooledByteBuffer(java.util.Map map)

		return( new PooledByteBufferImpl( BEncoder.encode( map )));
	
public intcompareVersions(java.lang.String v1, java.lang.String v2)

		return Constants.compareVersions( v1, v2 );
	
public AggregatedDispatchercreateAggregatedDispatcher(long idle_dispatch_time, long max_queue_size)

		return( 
			new AggregatedDispatcher()
			{
				private AggregatedList	list = 
					createAggregatedList(
						new AggregatedListAcceptor()
						{
							public void
							accept(
								List		l )
							{
								for (int i=0;i<l.size();i++){
									
									try{
										((Runnable)l.get(i)).run();
										
									}catch( Throwable e ){
										
										Debug.printStackTrace(e);
									}
								}
							}
						},
						idle_dispatch_time,
						max_queue_size );
				
				public void
				add(
					Runnable	runnable )
				{
					list.add( runnable );
				}
				
				public Runnable
				remove(
					Runnable	runnable )
				{
					return((Runnable)list.remove( runnable ));
				}
				
				public void
				destroy()
				{
					list.destroy();
				}
			});
	
public AggregatedListcreateAggregatedList(AggregatedListAcceptor acceptor, long idle_dispatch_time, long max_queue_size)

		return( 
			new AggregatedList()
			{
				AEMonitor	timer_mon	= new AEMonitor( "aggregatedList" );
				
				Timer		timer = new Timer( "AggregatedList" );
				TimerEvent	event;
				
				List		list	= new ArrayList();
				
				public void
				add(
					Object	obj )
				{
					
					List	dispatch_now = null;
					
					try{
						timer_mon.enter();
						
							// if the list is full kick off a dispatch and reset the list
						
						if (	max_queue_size > 0 &&
								max_queue_size	== list.size()){
							
							dispatch_now = list;
							
							list	= new ArrayList();
							
						}
							
						list.add( obj );
						
							// set up a timer to wakeup in required time period 
						
						long	now = SystemTime.getCurrentTime();
					
						if ( event != null ){
							
							event.cancel();
						}
						
						event = 
							timer.addEvent( 
									now + idle_dispatch_time,
									new TimerEventPerformer()
									{
										public void
										perform(
											TimerEvent	event )
										{
											dispatch();
										}
									});
					}finally{
						
						timer_mon.exit();
					}
					
					if ( dispatch_now != null ){
						
						dispatch( dispatch_now );
					}
				}

				public Object
				remove(
					Object	obj )
				{
					Object	res = null;
					
					try{
						timer_mon.enter();
					
						res = list.remove( obj )?obj:null;
							
						if ( res != null ){
							
							long	now = SystemTime.getCurrentTime();
							
							if ( event != null ){
								
								event.cancel();
							}
								
							if ( list.size() == 0 ){
								
								event	= null;
								
							}else{
								
								event = 
									timer.addEvent( 
											now + idle_dispatch_time,
											new TimerEventPerformer()
											{
												public void
												perform(
													TimerEvent	event )
												{
													dispatch();
												}
											});
							}
						}
					}finally{
						
						timer_mon.exit();
					}
					
					return( res );
				}
				
				protected void
				dispatch()
				{
					List	dispatch_list;
					
					try{
						timer_mon.enter();
					
						dispatch_list	= list;
						
						list	= new ArrayList();
						
					}finally{
						
						timer_mon.exit();
					}
					
					dispatch( dispatch_list );
				}

				protected void
				dispatch(
					List		l )
				{
					if ( l.size() > 0 ){
						
						try{
							acceptor.accept( l );
							
						}catch( Throwable e ){
							
							Debug.printStackTrace(e);
						}
					}
				}
				
				public void
				destroy()
				{
					dispatch();
					
					timer.destroy();
				}
			});
	
public voidcreateProcess(java.lang.String command_line)

	    try{
	    		// we need to spawn without inheriting handles
	    	
	    	PlatformManager pm = PlatformManagerFactory.getPlatformManager();
	    	
	    	pm.createProcess( command_line, false );
	    	    	
	    }catch(Throwable e) {
	    	
	        Debug.printStackTrace(e);
	        
	        try{
	        	Runtime.getRuntime().exec( command_line );
	        	
	        }catch( Throwable f ){
	        	
	        	throw( new PluginException("Failed to create process", f ));
	        }
	    }
	
public voidcreateThread(java.lang.String name, java.lang.Runnable target)

		Thread t = 
			new AEThread( pi.getPluginName() + "::" + name )
			{
				public void
				runSupport()
				{
					setPluginThreadContext( pi );
					
					target.run();
				}
			};
	
		t.setDaemon(true);
		
		t.start();
	
public UTTimercreateTimer(java.lang.String name)

		return( new UTTimerImpl( pi, name, false ));
	
public UTTimercreateTimer(java.lang.String name, boolean lightweight)

		return( new UTTimerImpl( pi, name, lightweight ));
	
public UTTimercreateTimer(java.lang.String name, int priority)

		return( new UTTimerImpl( pi, name, priority ));
	
public ByteArrayWrappercreateWrapper(byte[] data)

		return( new HashWrapper( data ));
	
public voidfreeDirectByteBuffer(java.nio.ByteBuffer buffer)

    
		//DirectByteBufferPool.freeBuffer( buffer );
	
public java.lang.StringgetAzureusProgramDir()

		String	res = SystemProperties.getApplicationPath();
		
		if ( res.endsWith(File.separator )){
			
			res = res.substring(0,res.length()-1);
		}
		
		return( res );	
	
public java.lang.StringgetAzureusUserDir()

		String	res = SystemProperties.getUserPath();
		
		if ( res.endsWith(File.separator )){
			
			res = res.substring(0,res.length()-1);
		}
		
		return( res );
	
public longgetCurrentSystemTime()

    return SystemTime.getCurrentTime();
  
public FormattersgetFormatters()

		return( new FormattersImpl());
	
public java.io.InputStreamgetImageAsStream(java.lang.String image_name)

		return( UtilitiesImpl.class.getClassLoader().getResourceAsStream("org/gudy/azureus2/ui/icons/" + image_name));
	
public LocaleUtilitiesgetLocaleUtilities()

		return( new LocaleUtilitiesImpl( pi ));
	
public MonitorgetMonitor()

      return new MonitorImpl( pi );
    
public static PluginInterfacegetPluginThreadContext()

		return((PluginInterface)tls.get());
	
public java.net.InetAddressgetPublicAddress(boolean v6)

		if ( v6 ){
			
			String	vc_ip = VersionCheckClient.getSingleton().getExternalIpAddress( false, true );
			
			if ( vc_ip != null && vc_ip.length() > 0 ){
				
				try{
					return( InetAddress.getByName( vc_ip ));
					
				}catch( Throwable e ){
					
					Debug.printStackTrace( e );
				}
			}
			
			return( null );
		}else{
			
			return( getPublicAddress());
		}
	
public java.net.InetAddressgetPublicAddress()

		long	now = SystemTime.getCurrentTime();
		
		if ( now < last_public_ip_address_time ){
			
			last_public_ip_address_time	 = now;
			
		}else{
		
			if ( last_public_ip_address != null && now - last_public_ip_address_time < 15*60*1000 ){
				
				return( last_public_ip_address );
			}
		}
		
		InetAddress res	= null;
		
		try{
			
			String	vc_ip = VersionCheckClient.getSingleton().getExternalIpAddress( false, false );
			
			if ( vc_ip != null && vc_ip.length() > 0 ){
								
				res = InetAddress.getByName( vc_ip );
				
			}else{
			
				ExternalIPChecker	checker = ExternalIPCheckerFactory.create();
				
				ExternalIPCheckerService[]	services = checker.getServices();
				
				final String[]	ip = new String[]{ null };
				
				for (int i=0;i<services.length && ip[0] == null;i++){
					
					final ExternalIPCheckerService	service = services[i];
					
					if ( service.supportsCheck()){
	
						final AESemaphore	sem = new AESemaphore("Utilities:getExtIP");
						
						ExternalIPCheckerServiceListener	listener = 
							new ExternalIPCheckerServiceListener()
							{
								public void
								checkComplete(
									ExternalIPCheckerService	_service,
									String						_ip )
								{
									ip[0]	= _ip;
									
									sem.release();
								}
									
								public void
								checkFailed(
									ExternalIPCheckerService	_service,
									String						_reason )
								{
									sem.release();
								}
									
								public void
								reportProgress(
									ExternalIPCheckerService	_service,
									String						_message )
								{
								}
							};
							
						services[i].addListener( listener );
						
						try{
							
							services[i].initiateCheck( 60000 );
							
							sem.reserve( 60000 );
							
						}finally{
							
							services[i].removeListener( listener );
						}
					}
					
						
					if ( ip[0] != null ){
							
						res = InetAddress.getByName( ip[0] );
						
						break;
					}
				}
			}
		}catch( Throwable e ){
			
			Debug.printStackTrace( e );
		}
		
		if ( res == null ){
			
				// if we failed then use any prior value if we've got one
			
			res	= last_public_ip_address;
			
		}else{
			
			last_public_ip_address		= res;
			
			last_public_ip_address_time	= now;
		}
		
		return( res );
	
public org.gudy.azureus2.plugins.utils.xml.rss.RSSFeedgetRSSFeed(java.net.URL feed_location)

		return( getRSSFeed( getResourceDownloaderFactory().create( feed_location )));
	
public org.gudy.azureus2.plugins.utils.xml.rss.RSSFeedgetRSSFeed(ResourceDownloader feed_location)

		return( new RSSFeedImpl( this, feed_location ));
	
public ResourceDownloaderFactorygetResourceDownloaderFactory()

		return( ResourceDownloaderFactoryImpl.getSingleton());
	
public org.gudy.azureus2.plugins.utils.resourceuploader.ResourceUploaderFactorygetResourceUploaderFactory()

		return( ResourceUploaderFactoryImpl.getSingleton());
	
public org.gudy.azureus2.plugins.utils.security.SESecurityManagergetSecurityManager()

		return( new SESecurityManagerImpl( core ));
	
public SemaphoregetSemaphore()

		return( new SemaphoreImpl( pi ));
	
public org.gudy.azureus2.plugins.utils.xml.simpleparser.SimpleXMLParserDocumentFactorygetSimpleXMLParserDocumentFactory()

		return( new SimpleXMLParserDocumentFactoryImpl());
	
public booleanisCVSVersion()

		return( Constants.isCVSVersion());
	
public booleanisFreeBSD()

		return( Constants.isFreeBSD );
	
public booleanisLinux()

		return( Constants.isLinux );
	
public booleanisOSX()

		return( Constants.isOSX );
	
public booleanisSolaris()

		return( Constants.isSolaris );
	
public booleanisUnix()

		return( Constants.isUnix );
	
public booleanisWindows()

		return( Constants.isWindows );
	
public java.lang.StringnormaliseFileName(java.lang.String f_name)

		return FileUtil.convertOSSpecificChars(f_name);
	
public java.util.MapreadResilientBEncodedFile(java.io.File parent_dir, java.lang.String file_name, boolean use_backup)

		return( FileUtil.readResilientFile( parent_dir, file_name, use_backup ));
	
public java.lang.StringreverseDNSLookup(java.net.InetAddress address)

		final AESemaphore	sem = new AESemaphore("Utilities:reverseDNS");
		
		final String[]	res = { null };
		
		IPToHostNameResolver.addResolverRequest(
					address.getHostAddress(),
					new IPToHostNameResolverListener()
					{
						public void 
						IPResolutionComplete(
							String 		result,
							boolean 	succeeded )
						{
							if ( succeeded ){
								
								res[0] = result;
							}

							sem.release();
						}
					});
		
		sem.reserve( 60000 );
		
		return( res[0] );
	
public static final voidsetPluginThreadContext(PluginInterface pi)

		tls.set( pi );
	
public voidwriteResilientBEncodedFile(java.io.File parent_dir, java.lang.String file_name, java.util.Map data, boolean use_backup)

		FileUtil.writeResilientFile( parent_dir, file_name, data, use_backup );