package mit.util ;

public class RuntimeContext
{
	private static java.util.Hashtable contexts = new java.util.Hashtable() ;

	public static boolean contains( Object key )
	{
		try
		{
			if( null != key )
			{
				Runtime runtime = Runtime.getRuntime() ;
				if( contexts.containsKey( runtime ) )
				{
					java.util.Hashtable context
						= ( java.util.Hashtable ) contexts.get( runtime ) ;
					return( context.containsKey( key ) ) ;
				}
			}
		}
		catch( Exception ex )
		{
		}
		return false ;
	}

	public static void setProperty( Object key , Object value )
	{
		try
		{
			if( null != key )
			{
				Runtime runtime = Runtime.getRuntime() ;
				if( ! contexts.containsKey( runtime ) )
				{
					contexts.put( runtime , new java.util.Hashtable() ) ;
				}
				java.util.Hashtable context
					= ( java.util.Hashtable ) contexts.get( runtime ) ;
				if( null != value )
				{
					context.put( key , value ) ;

				}
				else
				{
					context.remove( key ) ;
				}
			}
		}
		catch( Exception ex )
		{
			ex.printStackTrace() ;
		}
	}

	public static Object getProperty( Object key )
	{
		try
		{
			if( null != key )
			{
				Runtime runtime = Runtime.getRuntime() ;
				if( contexts.containsKey( runtime) )
				{
					java.util.Hashtable context
						= ( java.util.Hashtable ) contexts.get( runtime ) ;
					return( context.get( key ) ) ;
				}
			}
		}
		catch( Exception ex )
		{
			ex.printStackTrace() ;
		}
		return null ;
	}

	public static java.util.Enumeration getPropertyNames()
	{
		try
		{
			Runtime runtime = Runtime.getRuntime() ;
			if( ! contexts.containsKey( runtime ) )
			{
				java.util.Hashtable context
					= ( java.util.Hashtable ) contexts.get( runtime) ;
				return( context.keys() ) ;
			}
		}
		catch( Exception ex )
		{
			ex.printStackTrace() ;
		}
		return null ;
	}

	public static Object removeProperty( String key )
	{
		try
		{
			if( null != key )
			{
				Runtime runtime = Runtime.getRuntime() ;
				if( ! contexts.containsKey( runtime ) )
				{
					java.util.Hashtable context
						= ( java.util.Hashtable ) contexts.get( runtime) ;
					if( context.containsKey( key ) )
					{
						Object value = context.get( key ) ;
						if( value instanceof java.sql.Connection )
						{
							( ( java.sql.Connection ) value ).close() ;
						}
						else if( value instanceof java.sql.ResultSet )
						{
							( ( java.sql.ResultSet ) value ).close() ;
						}
						Object obj = context.remove( key ) ;
						System.gc() ;
						return( obj ) ;
					}
				}
			}
		}
		catch( Exception ex )
		{
			ex.printStackTrace() ;
		}
		return null ;
	}

}