/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.mx.util;

import java.security.AccessController;
import java.security.PrivilegedAction;

/** System property access utilties that encapsulate the
 * AccessController.doPrivileged calls required when running with a
 * security manager. Use to access system properties when the callers
 * permissions should not dictate whether or not access is allowed.
 *
 * @author Scott.Stark@jboss.org
 * @version $Revision: 1.1.4.1 $
 */
public class PropertyAccess
{
   static class PropertyReadAction implements PrivilegedAction
   {
      private String name;
      private String defaultValue;
      PropertyReadAction(String name, String defaultValue)
      {
         this.name = name;
         this.defaultValue = defaultValue;
      }
      public Object run()
      {
         return System.getProperty(name, defaultValue);
      }
   }
   static class PropertyWriteAction implements PrivilegedAction
   {
      private String name;
      private String value;
      PropertyWriteAction(String name, String value)
      {
         this.name = name;
         this.value = value;
      }
      public Object run()
      {
         return System.setProperty(name, value);
      }
   }

   public static String getProperty(String name)
   {
      return getProperty(name, null);
   }

   public static String getProperty(String name, String defaultValue)
   {
      PrivilegedAction action = new PropertyReadAction(name, defaultValue);
      String property = (String) AccessController.doPrivileged(action);
      return property;
   }

   public static String setProperty(String name, String value)
   {
      PrivilegedAction action = new PropertyWriteAction(name, value);
      String property = (String) AccessController.doPrivileged(action);
      return property;
   }
   
}