FileDocCategorySizeDatePackage
PluginConfigImpl.javaAPI DocAzureus 3.0.3.422687Mon May 07 10:16:30 BST 2007org.gudy.azureus2.pluginsimpl.local

PluginConfigImpl

public class PluginConfigImpl extends Object implements org.gudy.azureus2.plugins.PluginConfig
author
Eric Allen To change the template for this generated type comment go to Window - Preferences - Java - Code Generation - Code and Comments

Fields Summary
protected static Map
external_to_internal_key_map
private static Map
fake_values_when_disabled
private static int
fake_values_ref_count
private org.gudy.azureus2.plugins.PluginInterface
plugin_interface
private String
key
Constructors Summary
public PluginConfigImpl(org.gudy.azureus2.plugins.PluginInterface _plugin_interface, String _key)

		plugin_interface	= _plugin_interface;
		
		key = _key + ".";
	
Methods Summary
public voidaddListener(org.gudy.azureus2.plugins.PluginConfigListener l)

		COConfigurationManager.addListener(
			new COConfigurationListener()
			{
				public void
				configurationSaved()
				{
					l.configSaved();
				}
			});
	
public booleangetBooleanParameter(java.lang.String name)

		return getDefaultedBooleanParameter(name, true);
	
public booleangetBooleanParameter(java.lang.String name, boolean default_value)

		return getBooleanParameter(name, default_value, true, false);
	
private booleangetBooleanParameter(java.lang.String name, boolean _default, boolean map_name, boolean set_default)

		Object	obj = getFakeValueWhenDisabled(key,name);
		if ( obj != null ){
			return(((Boolean)obj).booleanValue());
		}
		if (map_name) {name = mapKeyName(name, false);}
		if (set_default) {COConfigurationManager.setBooleanDefault(name, _default);}
		else if (!hasParameter(name)) {return _default;}
		return COConfigurationManager.getBooleanParameter(name);
	
public byte[]getByteParameter(java.lang.String name)

		return getDefaultedByteParameter(name, true);
	
public byte[]getByteParameter(java.lang.String name, byte[] default_value)

		return getByteParameter(name, default_value, true, false);
	
private byte[]getByteParameter(java.lang.String name, byte[] _default, boolean map_name, boolean set_default)

		Object	obj = getFakeValueWhenDisabled(key,name);
		if ( obj != null ){
			return((byte[])obj);
		}
		if (map_name) {name = mapKeyName(name, false);}
		if (set_default) {COConfigurationManager.setByteDefault(name, _default);}
		else if (!hasParameter(name)) {return _default;}
		return COConfigurationManager.getByteParameter(name);
	
private booleangetDefaultedBooleanParameter(java.lang.String name, boolean map_name)

		Object	obj = getFakeValueWhenDisabled(key,name);
		if ( obj != null ){
			return(((Boolean)obj).booleanValue());
		}
		return getBooleanParameter(name, ConfigurationDefaults.def_boolean == 1, map_name, false);
	
private byte[]getDefaultedByteParameter(java.lang.String name, boolean map_name)

		return getByteParameter(name, ConfigurationDefaults.def_bytes, map_name, false);
	
private floatgetDefaultedFloatParameter(java.lang.String name, boolean map_name)

		return getFloatParameter(name, ConfigurationDefaults.def_float, map_name, false);
	
private intgetDefaultedIntParameter(java.lang.String name, boolean map_name)

		return getIntParameter(name, ConfigurationDefaults.def_int, map_name, false);
	
private longgetDefaultedLongParameter(java.lang.String name, boolean map_name)

		return getLongParameter(name, ConfigurationDefaults.def_long, map_name, false);
	
private java.lang.StringgetDefaultedStringParameter(java.lang.String name, boolean map_name)

		return getStringParameter(name, ConfigurationDefaults.def_String, map_name, false);
	
private static java.lang.ObjectgetFakeValueWhenDisabled(java.lang.String key, java.lang.String name)

		if ( name.startsWith(key)){
			return( null );
		}
		
		synchronized( PluginConfigImpl.class ){
			
			if ( fake_values_when_disabled != null ){
				
				return( fake_values_when_disabled.get( name ));
			}
		}
		
		return( null );
	
private floatgetFloatParameter(java.lang.String name, float _default, boolean map_name, boolean set_default)

		Object	obj = getFakeValueWhenDisabled(key,name);
		if ( obj != null ){
			return(((Float)obj).floatValue());
		}
		if (map_name) {name = mapKeyName(name, false);}
		if (set_default) {COConfigurationManager.setFloatDefault(name, _default);}
		else if (!hasParameter(name)) {return _default;}
		return COConfigurationManager.getFloatParameter(name);
	
public floatgetFloatParameter(java.lang.String name)

		return getDefaultedFloatParameter(name, true);
	
public floatgetFloatParameter(java.lang.String name, float default_value)

		return getFloatParameter(name, default_value, true, false);
	
private intgetIntParameter(java.lang.String name, int _default, boolean map_name, boolean set_default)

		Object	obj = getFakeValueWhenDisabled(key,name);
		if ( obj != null ){
			return(((Long)obj).intValue());
		}
		if (map_name) {name = mapKeyName(name, false);}
		if (set_default) {COConfigurationManager.setIntDefault(name, _default);}
		else if (!hasParameter(name)) {return _default;}
		return COConfigurationManager.getIntParameter(name);
	
public intgetIntParameter(java.lang.String name)

		return getDefaultedIntParameter(name, true);
	
public intgetIntParameter(java.lang.String name, int default_value)

		return getIntParameter(name, default_value, true, false);
	
private longgetLongParameter(java.lang.String name, long _default, boolean map_name, boolean set_default)

		Object	obj = getFakeValueWhenDisabled(key,name);
		if ( obj != null ){
			return(((Long)obj).longValue());
		}
		if (map_name) {name = mapKeyName(name, false);}
		if (set_default) {COConfigurationManager.setLongDefault(name, _default);}
		else if (!hasParameter(name)) {return _default;}
		return COConfigurationManager.getLongParameter(name);
	
public longgetLongParameter(java.lang.String name)

		return getDefaultedLongParameter(name, true);
	
public longgetLongParameter(java.lang.String name, long default_value)

		return getLongParameter(name, default_value, true, false);
	
public org.gudy.azureus2.plugins.config.ConfigParametergetParameter(java.lang.String key)

		return( new ConfigParameterImpl( mapKeyName(key, false)));
	
public booleangetPluginBooleanParameter(java.lang.String name)

		return getDefaultedBooleanParameter(this.key + name, false);
	
public booleangetPluginBooleanParameter(java.lang.String name, boolean default_value)

		return getBooleanParameter(this.key + name, default_value, false, true);
	
public byte[]getPluginByteParameter(java.lang.String name)

		return getDefaultedByteParameter(this.key + name, false);
	
public byte[]getPluginByteParameter(java.lang.String name, byte[] default_value)

		return getByteParameter(this.key + name, default_value, false, true);
	
public java.lang.StringgetPluginConfigKeyPrefix()

		return( key );
	
public floatgetPluginFloatParameter(java.lang.String name)

		return getDefaultedFloatParameter(this.key + name, false);
	
public floatgetPluginFloatParameter(java.lang.String name, float default_value)

		return getFloatParameter(this.key + name, default_value, false, true);
	
public intgetPluginIntParameter(java.lang.String name)

		return getDefaultedIntParameter(this.key + name, false);
	
public intgetPluginIntParameter(java.lang.String name, int default_value)

		return getIntParameter(this.key + name, default_value, false, true);
	
public java.util.ListgetPluginListParameter(java.lang.String key, java.util.List default_value)

		return COConfigurationManager.getListParameter(this.key+key, default_value); 
	 
public longgetPluginLongParameter(java.lang.String name)

		return getDefaultedLongParameter(this.key + name, false);
	
public longgetPluginLongParameter(java.lang.String name, long default_value)

		return getLongParameter(this.key + name, default_value, false, true);
	
public java.util.MapgetPluginMapParameter(java.lang.String key, java.util.Map default_value)

		return COConfigurationManager.getMapParameter(this.key+key, default_value); 
	 
public org.gudy.azureus2.plugins.config.ConfigParametergetPluginParameter(java.lang.String key)

		return( new ConfigParameterImpl( this.key+key ));
	
public java.lang.StringgetPluginStringParameter(java.lang.String name)

		return getDefaultedStringParameter(this.key + name, false);
	
public java.lang.StringgetPluginStringParameter(java.lang.String name, java.lang.String default_value)

    	return getStringParameter(this.key + name, default_value, false, true);
    
public java.io.FilegetPluginUserFile(java.lang.String name)

		
		String	dir = plugin_interface.getUtilities().getAzureusUserDir();
		
		File	file = new File( dir, "plugins" );

		String	p_dir = plugin_interface.getPluginDirectoryName();
		
		if ( p_dir.length() != 0 ){
			
			int	lp = p_dir.lastIndexOf(File.separatorChar);
			
			if ( lp != -1 ){
				
				p_dir = p_dir.substring(lp+1);
			}
			
			file = new File( file, p_dir );
			
		}else{
			
			String	id = plugin_interface.getPluginID();
			
			if ( id.length() > 0 && !id.equals( PluginInitializer.INTERNAL_PLUGIN_ID )){
			
				file = new File( file, id );
				
			}else{
				
				throw( new RuntimeException( "Plugin was not loaded from a directory" ));
			}
		}
	
		
		FileUtil.mkdirs(file);
		
		return( new File( file, name ));
	
private java.lang.StringgetStringParameter(java.lang.String name, java.lang.String _default, boolean map_name, boolean set_default)

		Object	obj = getFakeValueWhenDisabled(key,name);
		if ( obj != null ){
			return((String)obj);
		}
		if (map_name) {name = mapKeyName(name, false);}
		if (set_default) {COConfigurationManager.setStringDefault(name, _default);}
		else if (!hasParameter(name)) {return _default;}
		return COConfigurationManager.getStringParameter(name);
	
public java.lang.StringgetStringParameter(java.lang.String name)

		return getDefaultedStringParameter(name, true);
	
public java.lang.StringgetStringParameter(java.lang.String name, java.lang.String default_value)

    	return getStringParameter(name, default_value, true, false);
    
public booleangetUnsafeBooleanParameter(java.lang.String name)

		return getDefaultedBooleanParameter(name, false);
	
public booleangetUnsafeBooleanParameter(java.lang.String name, boolean default_value)

		return getBooleanParameter(name, default_value, false, false);
	
public byte[]getUnsafeByteParameter(java.lang.String name)

		return getDefaultedByteParameter(name, false);
	
public byte[]getUnsafeByteParameter(java.lang.String name, byte[] default_value)

		return getByteParameter(name, default_value, false, false);
	
public floatgetUnsafeFloatParameter(java.lang.String name)

		return getDefaultedFloatParameter(name, false);
	
public floatgetUnsafeFloatParameter(java.lang.String name, float default_value)

		return getFloatParameter(name, default_value, false, false);
	
public intgetUnsafeIntParameter(java.lang.String name)

		return getDefaultedIntParameter(name, false);
	
public intgetUnsafeIntParameter(java.lang.String name, int default_value)

		return getIntParameter(name, default_value, false, false);
	
public longgetUnsafeLongParameter(java.lang.String name)

		return getDefaultedLongParameter(name, false);
	
public longgetUnsafeLongParameter(java.lang.String name, long default_value)

		return getLongParameter(name, default_value, false, false);
	
public java.util.MapgetUnsafeParameterList()

		  Set params = COConfigurationManager.getAllowedParameters();
		  
		  Iterator	it = params.iterator();
		  
		  Map	result = new HashMap();
		  
		  while( it.hasNext()){
			  
			  try{
				  String	name = (String)it.next();
				  
				  Object val = COConfigurationManager.getParameter( name );
				  
				  if ( val instanceof String || val instanceof Long ){
					  
				  }else if ( val instanceof byte[]){
					  
					  val = new String((byte[])val, "UTF-8" );
					  
				  }else if ( val instanceof Integer ){
					  
					  val = new Long(((Integer)val).intValue());
	
				  }else if ( val instanceof List ){
					  
					  val = null;
					  
				  }else if ( val instanceof Map ){
					  
					  val = null;
					  
				  }else if ( val instanceof Boolean ){
					  
					  val = new Long(((Boolean)val).booleanValue()?1:0);
					  
				  }else if ( val instanceof Float || val instanceof Double ){
					  
					  val = val.toString();
				  }
				  
				  if ( val != null ){
					 
					  result.put( name, val );
				  }
			  }catch( Throwable e ){
				  
				  Debug.printStackTrace(e);
			  }
		  }
		  
		  return( result );
	  
public java.lang.StringgetUnsafeStringParameter(java.lang.String name)

		return getDefaultedStringParameter(name, false);
	
public java.lang.StringgetUnsafeStringParameter(java.lang.String name, java.lang.String default_value)

    	return getStringParameter(name, default_value, false, false);
    
public booleanhasParameter(java.lang.String param_name)

		// Don't see any reason why a plugin should care whether it is looking
		// at a system default setting or not, so we'll do an implicit check.
		return COConfigurationManager.hasParameter(param_name, false);
	
public booleanhasPluginParameter(java.lang.String param_name)

		// We should not have default settings for plugins in configuration
		// defaults, so we don't bother doing an implicit check.
		return COConfigurationManager.hasParameter(this.key + param_name, true);
	
public booleanisNewInstall()

		return( COConfigurationManager.isNewInstall());
	
private java.lang.StringmapKeyName(java.lang.String key, boolean for_set)

		String result = (String)external_to_internal_key_map.get(key);
		if (result == null) {
			if (for_set) {
				throw new RuntimeException("No permission to set the value of core parameter: " + key);
			}
			else {
				return key;
			}
		}
		return result;
	
public booleanremovePluginParameter(java.lang.String key)

		return COConfigurationManager.removeParameter(this.key + key);
	
public voidsave()

		COConfigurationManager.save();
	
public voidsetBooleanParameter(java.lang.String name, boolean value)

		if ( setFakeValueWhenDisabled(key, name, new Boolean( value))){
			return;
		}
    	COConfigurationManager.setParameter(mapKeyName(name, true), value);
    
public voidsetByteParameter(java.lang.String name, byte[] value)

		if ( setFakeValueWhenDisabled(key, name, value )){
			return;
		}
    	COConfigurationManager.setParameter(mapKeyName(name, true), value);
    
public static voidsetEnablePluginCoreConfigChange(boolean enabled)

		synchronized( PluginConfigImpl.class ){

			if ( enabled ){
				
				fake_values_ref_count--;
				
				if ( fake_values_ref_count == 0 ){
					
						// TODO: we could try and recover the faked values at this point
					
					fake_values_when_disabled = null;
				}				

			}else{
				fake_values_ref_count++;

				if ( fake_values_ref_count == 1 ){
					
					fake_values_when_disabled = new HashMap();
				}
			}
		}
	
private static booleansetFakeValueWhenDisabled(java.lang.String key, java.lang.String name, java.lang.Object value)

		if ( name.startsWith(key)){
			return( false );
		}
		
		synchronized( PluginConfigImpl.class ){
			
			if ( fake_values_when_disabled != null ){
				
				fake_values_when_disabled.put( name, value );
				
				return( true );
			}
		}
		
		return( false );
	
public voidsetFloatParameter(java.lang.String name, float value)

		if ( setFakeValueWhenDisabled(key, name, new Float( value))){
			return;
		}
    	COConfigurationManager.setParameter(mapKeyName(name, true), value);
    
public voidsetIntParameter(java.lang.String name, int value)

		if ( setFakeValueWhenDisabled(key, name, new Long( value))){
			return;
		}
    	COConfigurationManager.setParameter(mapKeyName(name, true), value);
    
public voidsetLongParameter(java.lang.String name, long value)

		if ( setFakeValueWhenDisabled(key, name, new Long( value))){
			return;
		}
    	COConfigurationManager.setParameter(mapKeyName(name, true), value);
    
public voidsetPluginConfigKeyPrefix(java.lang.String _key)

		if (_key.length() > 0 || plugin_interface.isBuiltIn()) {
			key = _key;
		} else {
			throw (new RuntimeException("Can't set Plugin Config Key Prefix to '"
					+ _key + "'"));
		}
	
public voidsetPluginListParameter(java.lang.String key, java.util.List value)

		 COConfigurationManager.setParameter(this.key+key, value);
	 
public voidsetPluginMapParameter(java.lang.String key, java.util.Map value)

		 COConfigurationManager.setParameter(this.key+key, value);
	 
public voidsetPluginParameter(java.lang.String name, boolean value)

    	COConfigurationManager.setParameter(this.key + name, value);
    
public voidsetPluginParameter(java.lang.String name, byte[] value)

    	COConfigurationManager.setParameter(this.key + name, value);
    
public voidsetPluginParameter(java.lang.String name, float value)

    	COConfigurationManager.setParameter(this.key + name, value);
    
public voidsetPluginParameter(java.lang.String name, int value)

    	COConfigurationManager.setParameter(this.key + name, value);
    
public voidsetPluginParameter(java.lang.String name, long value)

    	COConfigurationManager.setParameter(this.key + name, value);
    
public voidsetPluginParameter(java.lang.String name, java.lang.String value)

    	COConfigurationManager.setParameter(this.key + name, value);
    
public voidsetPluginParameter(java.lang.String key, int value, boolean global)

		COConfigurationManager.setParameter(this.key+key, value);
		if (global) {
			MagnetURIHandler.getSingleton().addInfo(this.key+key, value);
		}
	 
public voidsetStringParameter(java.lang.String name, java.lang.String value)

		if ( setFakeValueWhenDisabled(key, name, value)){
			return;
		}
    	COConfigurationManager.setParameter(mapKeyName(name, true), value);
    
public voidsetUnsafeBooleanParameter(java.lang.String name, boolean value)

		if ( setFakeValueWhenDisabled(key, name, new Boolean( value))){
			return;
		}
    	COConfigurationManager.setParameter(name, value);
    
public voidsetUnsafeByteParameter(java.lang.String name, byte[] value)

		if ( setFakeValueWhenDisabled(key, name, value)){
			return;
		}
    	COConfigurationManager.setParameter(name, value);
    
public voidsetUnsafeFloatParameter(java.lang.String name, float value)

		if ( setFakeValueWhenDisabled(key, name, new Float( value))){
			return;
		}
    	COConfigurationManager.setParameter(name, value);
    
public voidsetUnsafeIntParameter(java.lang.String name, int value)

		if ( setFakeValueWhenDisabled(key, name, new Long( value))){
			return;
		}
    	COConfigurationManager.setParameter(name, value);
    
public voidsetUnsafeLongParameter(java.lang.String name, long value)

		if ( setFakeValueWhenDisabled(key, name, new Long( value))){
			return;
		}
    	COConfigurationManager.setParameter(name, value);
    
public voidsetUnsafeStringParameter(java.lang.String name, java.lang.String value)

		if ( setFakeValueWhenDisabled(key, name, value )){
			return;
		}
    	COConfigurationManager.setParameter(name, value);