FileDocCategorySizeDatePackage
Set.javaAPI DocAzureus 3.0.3.410289Fri Aug 18 02:41:38 BST 2006org.gudy.azureus2.ui.console.commands

Set

public class Set extends IConsoleCommand
command that allows manipulation of Azureus' runtime properties. - when called without any parameters, it lists all of the available runtime properties. - when called with 1 parameter, it shows the current value of that parameter - when called with 2 or 3 parameters, it assigns a specified value to the specified parameter name. (the third parameter forces the property to be set to a particular type, otherwise we try and guess the type by the current value)
author
Tobias Minich, Paul Duran

Fields Summary
private static final String
NULL_STRING
Constructors Summary
public Set()


	 
	
		super(new String[] {"set", "+" });
	
Methods Summary
private voiddisplayOptions(java.io.PrintStream out, org.pf.text.StringPattern sp, boolean non_defaults)

		sp.setIgnoreCase(true);
		Iterator I = COConfigurationManager.getAllowedParameters().iterator();
		Map backmap = new HashMap();
		for (Iterator iter = ExternalUIConst.parameterlegacy.entrySet().iterator(); iter.hasNext();) {
			Map.Entry entry = (Map.Entry) iter.next();
			backmap.put( entry.getValue(), entry.getKey() );
		}
		TreeSet srt = new TreeSet();
		while (I.hasNext()) {
			String internal_name = (String) I.next();
			
			String	external_name = (String) backmap.get(internal_name);
			
			if ( external_name == null ){
				
				external_name = internal_name;
			}
			if( sp.matches(external_name) )
			{
				Parameter param = Parameter.get( internal_name, external_name );
				
				if ( non_defaults ){
					
					if ( !param.isDefault()){
						
						srt.add( param.getString( true ));
					}
				}else{
					
					srt.add( param.getString( false ));
				}
			}
		}
		I = srt.iterator();
		while (I.hasNext()) {
			out.println((String) I.next());
		}
	
public voidexecute(java.lang.String commandName, org.gudy.azureus2.ui.console.ConsoleInput ci, java.util.List args)

		
		boolean		non_defaults = false;
		
		Iterator	it = args.iterator();
		
		while( it.hasNext()){
			String	arg = (String)it.next();
			if ( arg.equals( "-export" )){
				non_defaults = true;
				it.remove();
			}
		}
		if( args.isEmpty() )
		{
			displayOptions(ci.out, new StringPattern("*"), non_defaults );
			return;
		}
		String external_name = (String) args.get(0);
		String internal_name = (String) ExternalUIConst.parameterlegacy.get(external_name);
		if( internal_name == null || internal_name.length() == 0 )
		{
			internal_name = external_name;
		}
//		else
//			ci.out.println("> converting " + origParamName + " to " + parameter);
		
		Parameter param;
		switch( args.size() )
		{
			case 1:
				// allow wildcards : eg: Core* or *DHT* to shorten result list
				StringPattern sp = new StringPattern(internal_name);
				if( sp.hasWildcard() )
				{
					displayOptions(ci.out, sp, non_defaults);
				}
				else
				{
					// try to display the value of the specified parameter
					if( ! COConfigurationManager.doesParameterDefaultExist( internal_name ) )					
					{
						ci.out.println("> Command 'set': Parameter '" + external_name + "' unknown.");
						return;
					}
					param = Parameter.get(internal_name,external_name);
					
					ci.out.println( param.getString( false ) );					
				}
				break;
			case 2:
			case 3:
				String setto = (String) args.get(1);
				String type;
				if( args.size() == 2 )
				{
					// guess the parameter type by getting the current value and determining its type
					param = Parameter.get( internal_name, external_name );
					type = param.getType();
				}
				else
					type = (String) args.get(2);
				
				boolean success = false;
				if( type.equalsIgnoreCase("int") || type.equalsIgnoreCase("integer") ) {
					COConfigurationManager.setParameter( internal_name, Integer.parseInt( setto ) );
					success = true;
				}
				else if( type.equalsIgnoreCase("bool") || type.equalsIgnoreCase("boolean") ) {
					
					boolean	value;
					
					if ( setto.equalsIgnoreCase("true") || setto.equalsIgnoreCase("y") || setto.equals("1" )){
						value = true;
					}else{
						value = false;
					}
						
					COConfigurationManager.setParameter( internal_name, value );
					success = true;
				}
				else if( type.equalsIgnoreCase("float") ) {
					COConfigurationManager.setParameter( internal_name, Float.parseFloat( setto ) );
					success = true;
				}
				else if( type.equalsIgnoreCase("string") ) {
					COConfigurationManager.setParameter( internal_name, setto );
					success = true;
				}
				else if( type.equalsIgnoreCase("password") ) {
					SHA1Hasher hasher = new SHA1Hasher();
					
					byte[] password = setto.getBytes();
					
					byte[] encoded;
					
					if(password.length > 0){
						
						encoded = hasher.calculateHash(password);
						
					}else{
						
						encoded = password;
					}
					
					COConfigurationManager.setParameter( internal_name, encoded );
					
					success = true;
				}
				
				if( success ) {
					COConfigurationManager.save();
					ci.out.println("> Parameter '" + external_name + "' set to '" + setto + "'. [" + type + "]");
				}
				else ci.out.println("ERROR: invalid type given");
				
				break;
			default:
				ci.out.println("Usage: 'set \"parameter\" value type', where type = int, bool, float, string, password");
				break;
		}
	
public java.lang.StringgetCommandDescriptions()

		return("set [options] [parameter] [value]\t\t+\tSet a configuration parameter. The whitespaceless notation has to be used. If value is omitted, the current setting is shown. Parameter may be a wildcard to narrow results");
	
public voidprintHelp(java.io.PrintStream out, java.util.List args)

		out.println("> -----");
		out.println("'set' options: ");
		out.println("\t-export\t\tPrints all the options with non-defaut values.");