

package org.apache.yoko.rmi.util;

import java.security.PrivilegedAction;

/**
 * Simple utility class for retrieving a system property
 * value using the AccessController.
 */
public class GetSystemPropertyAction implements PrivilegedAction<String> {
    // property name to retrieve
    String name;
    // potential default value
    String defaultValue = null;

    /**
     * Retrive a value using the name with no default value.
     *
     * @param name   The property name.
     */
    public GetSystemPropertyAction(String name) {
        this.name = name;
        this.defaultValue = null;
    }

    /**
     * Retrieve a property using a name and a specified default value.
     *
     * @param name   The property name.
     * @param defaultValue
     *               The default value if the property has not been set.
     */
    public GetSystemPropertyAction(String name, String defaultValue) {
        this.name = name;
        this.defaultValue = defaultValue;
    }

    /**
     * Perform the AccessController action of retrieving the system property.
     *
     * @return The retrieved property.  Returns either null or the
     *         specified default value if this has not been set.
     */
    public String run() {
        if (defaultValue == null) {
            return System.getProperty(name);
        }
        else {
            return System.getProperty(name, defaultValue);
        }
    }
}

