/*
 * 
 * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * The contents of this file are subject to the Sun Community Source
 * License v3.0/Jini Technology Specific Attachment v 1.0 (the
 * "License"). You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 * http://wwws.sun.com/software/jini/licensing/SCSL3_JiniTSA1.html.
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Reference Code is Jini Technology Core Platform code, v2.0. The
 * Developer of the Reference Code is Sun Microsystems, Inc.
 * 
 * Contributor(s): Sun Microsystems, Inc.
 * 
 * The contents of this file comply with the Jini Technology Core
 * Platform Compatibility Kit, v2.0A.
 * 
 * Tester(s): Sun Microsystems, Inc.
 * 
 * Test Platform(s):
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Solaris(TM)SPARC/x86
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Linux (Intel x86)
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Windows (Intel Platform)
 * 
 */

package com.sun.jini.action;

import java.security.AccessController;
import java.security.PrivilegedAction;
import net.jini.security.Security;

/**
 * A convenience class for retrieving the <code>long</code> value of a
 * system property as a privileged action.
 *
 * <p>An instance of this class can be used as the argument of {@link
 * AccessController#doPrivileged(PrivilegedAction)
 * AccessController.doPrivileged} or {@link
 * Security#doPrivileged(PrivilegedAction) Security.doPrivileged}.
 *
 * <p>The following code retrieves the <code>long</code> value of the
 * system property named <code>"prop"</code> as a privileged action.
 * Since it does not pass a default value to be used in case the
 * property <code>"prop"</code> is not defined, it has to check the
 * result for <code>null</code>:
 *
 * <pre>
 * Long tmp = (Long)
 *     Security.doPrivileged(new GetLongAction("prop"));
 * long l;
 * if (tmp != null) {
 *     l = tmp.longValue();
 * }
 * </pre>
 *
 * <p>The following code retrieves the <code>long</code> value of the
 * system property named <code>"prop"</code> as a privileged action,
 * and also passes a default value to be used in case the property
 * <code>"prop"</code> is not defined:
 *
 * <pre>
 * long l = ((Long) Security.doPrivileged(
 *                 new GetLongAction("prop"))).longValue();
 * </pre>
 *
 * @author	Sun Microsystems, Inc.
 * 
 * @see		PrivilegedAction
 * @see		AccessController
 * @see		Security
 * @since 2.0
 **/
public class GetLongAction implements PrivilegedAction {

    private final String theProp;
    private final long defaultVal;
    private final boolean defaultSet;

    /**
     * Constructor that takes the name of the system property whose
     * <code>long</code> value needs to be determined.
     *
     * @param	theProp the name of the system property
     **/
    public GetLongAction(String theProp) {
	this.theProp = theProp;
	defaultVal = 0;
	defaultSet = false;
    }

    /**
     * Constructor that takes the name of the system property and the
     * default value of that property.
     *
     * @param	theProp the name of the system property
     *
     * @param	defaultVal the default value
     **/
    public GetLongAction(String theProp, long defaultVal) {
        this.theProp = theProp;
        this.defaultVal = defaultVal;
	defaultSet = true;
    }

    /**
     * Determines the <code>long</code> value of the system property
     * whose name was specified in the constructor.  The value is
     * returned in a <code>Long</code> object.
     *
     * <p>If the system property is defined to be a value that can be
     * parsed successfully by {@link Long#decode Long.decode}, then
     * this method returns an <code>Long</code> with the parsed value.
     * Otherwise, if a default value was supplied to this object's
     * constructor, then this method returns an <code>Long</code> with
     * that default value, or else <code>null</code> is returned.
     *
     * @return	a <code>Long</code> representing the value of the
     * system property or the default value, or <code>null</code>
     **/
    public Object run() {
        Long value = Long.getLong(theProp);
        if ((value == null) && defaultSet)
	    return new Long(defaultVal);
	return value;
    }
}
