FileDocCategorySizeDatePackage
SystemProperties.javaAPI DocAzureus 3.0.3.410054Wed Apr 25 18:52:40 BST 2007org.gudy.azureus2.core3.util

SystemProperties

public class SystemProperties extends Object
Utility class to manage system-dependant information.

Fields Summary
private static final LogIDs
LOGID
public static final String
SYS_PROP_CONFIG_OVERRIDE
public static final String
SEP
Path separator charactor.
public static String
APPLICATION_NAME
private static String
APPLICATION_ID
private static String
APPLICATION_ENTRY_POINT
private static final String
WIN_DEFAULT
private static final String
OSX_DEFAULT
private static String
user_path
private static String
app_path
Constructors Summary
Methods Summary
public static java.lang.StringgetApplicationEntryPoint()

		return( APPLICATION_ENTRY_POINT );
	
public static java.lang.StringgetApplicationIdentifier()

		return( APPLICATION_ID );
	
public static java.lang.StringgetApplicationName()

		return( APPLICATION_NAME );
	
public static java.lang.StringgetApplicationPath()
Returns the full path to the directory where Azureus is installed and running from.

	  if ( app_path != null ){
		  
		  return( app_path );
	  }
	  
	  String temp_app_path = System.getProperty("azureus.install.path", System.getProperty("user.dir"));
    
	  if ( !temp_app_path.endsWith(SEP)){
		  
		  temp_app_path += SEP;
	  }

	  app_path = temp_app_path;
	  
	  return( app_path );
  
public static java.lang.StringgetAzureusJarPath()

	  String	str = getApplicationPath();

	  if( Constants.isOSX ){

		  str += SystemProperties.getApplicationName() + ".app/Contents/Resources/Java/";
	  }

	  return( str + "Azureus2.jar" );			
  
public static java.lang.StringgetDocPath()

		PlatformManager platformManager = PlatformManagerFactory.getPlatformManager();
		File fDocPath = null;
		try {
			fDocPath = platformManager.getLocation(PlatformManager.LOC_DOCUMENTS);
		} catch (PlatformManagerException e) {
		}
		if (fDocPath == null) {
			// should never happen.. but if we are missing a dll..
			fDocPath = new File(getUserPath(), "Documents");
		}

		return fDocPath.getAbsolutePath();
  
public static java.lang.StringgetEnvironmentalVariable(java.lang.String _var)
Will attempt to retrieve an OS-specific environmental var.

  	Process p = null;
  	Properties envVars = new Properties();
  	Runtime r = Runtime.getRuntime();
    BufferedReader br = null;

    	// this approach doesn't work at all on Windows 95/98/ME - it just hangs
    	// so get the hell outta here!
    
    if ( Constants.isWindows9598ME ){
    	
    	return( "" );
    }
    
    try {
    	if ( Constants.isWindows ) {
    		p = r.exec( "cmd.exe /c set" );
    	}
    	else { //we assume unix
    		p = r.exec( "env" );
    	}
    
    	String system_encoding = LocaleUtil.getSingleton().getSystemEncoding();
    	
    	if (Logger.isEnabled())
				Logger.log(new LogEvent(LOGID,
						"SystemProperties::getEnvironmentalVariable - " + _var
								+ ", system encoding = " + system_encoding));

    	br = new BufferedReader( new InputStreamReader( p.getInputStream(), system_encoding), 8192);
    	String line;
    	while( (line = br.readLine()) != null ) {
    		int idx = line.indexOf( '=" );
    		if (idx >= 0) {
      		String key = line.substring( 0, idx );
      		String value = line.substring( idx+1 );
      		envVars.setProperty( key, value );
      	}
    	}
      br.close();
    }
    catch (Throwable t) {
      if (br != null) try {  br.close();  } catch (Exception ingore) {}
    }
    
    return envVars.getProperty( _var, "" );
  
public static java.lang.StringgetUserPath()
Returns the full path to the user's home azureus directory. Under unix, this is usually ~/.azureus/ Under Windows, this is usually .../Documents and Settings/username/Application Data/Azureus/ Under OSX, this is usually /Users/username/Library/Application Support/Azureus/

		if (user_path != null) {
			return user_path;
		}

		// WATCH OUT!!!! possible recursion here if logging is changed so that it messes with
		// config initialisation - that's why we don't assign the user_path variable until it
		// is complete - an earlier bug resulted in us half-assigning it and using it due to 
		// recursion. At least with this approach we'll get (worst case) stack overflow if
		// a similar change is made, and we'll spot it!!!!

		// Super Override -- no AZ_DIR or xxx_DEFAULT added at all.

		String temp_user_path = System.getProperty(SYS_PROP_CONFIG_OVERRIDE);

		try {
			if (temp_user_path != null) {

				if (!temp_user_path.endsWith(SEP)) {

					temp_user_path += SEP;
				}

				File dir = new File(temp_user_path);

				if (!dir.exists()) {
					FileUtil.mkdirs(dir);
				}

				if (Logger.isEnabled())
					Logger.log(new LogEvent(LOGID,
							"SystemProperties::getUserPath(Custom): user_path = "
									+ temp_user_path));

				return temp_user_path;
			}

			// No override, get it from platform manager

			PlatformManager platformManager = PlatformManagerFactory.getPlatformManager();
			try {
				temp_user_path = platformManager.getLocation(
						PlatformManager.LOC_USER_DATA).getPath() + SEP;

				if (Logger.isEnabled()) {
					Logger.log(new LogEvent(LOGID,
							"SystemProperties::getUserPath: user_path = " + temp_user_path));
				}
			} catch (Exception e) {
				if (Logger.isEnabled()) {
					Logger.log(new LogEvent(LOGID,
							"Unable to retrieve user config path from "
									+ platformManager.getClass().getName()
									+ "Make sure aereg.dll is present."));
				}
			}

			// If platform failed, try some hackery
			if (temp_user_path == null) {
				String userhome = System.getProperty("user.home");

				if (Constants.isWindows) {
					temp_user_path = getEnvironmentalVariable("APPDATA");

					if (temp_user_path != null && temp_user_path.length() > 0) {
						if (Logger.isEnabled())
							Logger.log(new LogEvent(LOGID,
									"Using user config path from APPDATA env var instead: "
											+ temp_user_path));
					} else {
						temp_user_path = userhome + SEP + WIN_DEFAULT;
						if (Logger.isEnabled())
							Logger.log(new LogEvent(LOGID,
									"Using user config path from java user.home var instead: "
											+ temp_user_path));
					}

					temp_user_path = temp_user_path + SEP + APPLICATION_NAME + SEP;

					if (Logger.isEnabled())
						Logger.log(new LogEvent(LOGID,
								"SystemProperties::getUserPath(Win): user_path = "
										+ temp_user_path));

				} else if (Constants.isOSX) {
					temp_user_path = userhome + SEP + OSX_DEFAULT + SEP
							+ APPLICATION_NAME + SEP;

					if (Logger.isEnabled())
						Logger.log(new LogEvent(LOGID,
								"SystemProperties::getUserPath(Mac): user_path = "
										+ temp_user_path));

				} else {
					// unix type
					temp_user_path = userhome + SEP + "."
							+ APPLICATION_NAME.toLowerCase() + SEP;

					if (Logger.isEnabled())
						Logger.log(new LogEvent(LOGID,
								"SystemProperties::getUserPath(Unix): user_path = "
										+ temp_user_path));
				}
			}

			//if the directory doesn't already exist, create it
			File dir = new File(temp_user_path);
			if (!dir.exists()) {
				FileUtil.mkdirs(dir);
			}

			return temp_user_path;
		} finally {

			user_path = temp_user_path;
		}
	
public static booleanisJavaWebStartInstance()
Returns whether or not this running instance was started via Java's Web Start system.

    try {
      String java_ws_prop = System.getProperty("azureus.javaws");
      return ( java_ws_prop != null && java_ws_prop.equals( "true" ) );
    }
    catch (Throwable e) {
      //we can get here if running in an applet, as we have no access to system props
      return false;
    }
  
public static voidsetApplicationEntryPoint(java.lang.String entry_point)

		if ( entry_point != null && entry_point.trim().length() > 0 ){

			APPLICATION_ENTRY_POINT	= entry_point.trim();
		}
	
public static voidsetApplicationIdentifier(java.lang.String application_id)

		if ( application_id != null && application_id.trim().length() > 0 ){
			
			APPLICATION_ID			= application_id.trim();
		}
	
public static voidsetApplicationName(java.lang.String name)

  	
  	
	  
	
				 
	
		if ( name != null && name.trim().length() > 0 ){
			
			name	= name.trim();
			
			if ( user_path != null ){
				
				if ( !name.equals( APPLICATION_NAME )){
					
					System.out.println( "**** SystemProperties::setApplicationName called too late! ****" );
				}
			}
			
			APPLICATION_NAME			= name;
		}