1 /*
2  * Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package sun.security.action;
27 
28 /**
29  * A convenience class for retrieving the <code>Long</code> value of a system
30  * property as a privileged action.
31  *
32  * <p>An instance of this class can be used as the argument of
33  * <code>AccessController.doPrivileged</code>.
34  *
35  * <p>The following code retrieves the <code>Long</code> value of the system
36  * property named <code>"prop"</code> as a privileged action. Since it does
37  * not pass a default value to be used in case the property
38  * <code>"prop"</code> is not defined, it has to check the result for
39  * <code>null</code>:
40  *
41  * <pre>
42  * Long tmp = java.security.AccessController.doPrivileged
43  *     (new sun.security.action.GetLongAction("prop"));
44  * long l;
45  * if (tmp != null) {
46  *     l = tmp.longValue();
47  * }
48  * </pre>
49  *
50  * <p>The following code retrieves the <code>Long</code> value of the system
51  * property named <code>"prop"</code> as a privileged action, and also passes
52  * a default value to be used in case the property <code>"prop"</code> is not
53  * defined:
54  *
55  * <pre>
56  * long l = java.security.AccessController.doPrivileged
57  *      (new GetLongAction("prop")).longValue();
58  * </pre>
59  *
60  * @author Roland Schemers
61  * @see java.security.PrivilegedAction
62  * @see java.security.AccessController
63  * @since 1.2
64  */
65 
66 public class GetLongAction implements java.security.PrivilegedAction<Long> {
67     private String theProp;
68     private long defaultVal;
69     private boolean defaultSet = false;
70 
71     /**
72      * Constructor that takes the name of the system property whose
73      * <code>Long</code> value needs to be determined.
74      *
75      * @param theProp the name of the system property.
76      */
GetLongAction(String theProp)77     public GetLongAction(String theProp) {
78         this.theProp = theProp;
79     }
80 
81     /**
82      * Constructor that takes the name of the system property and the default
83      * value of that property.
84      *
85      * @param theProp the name of the system property.
86      * @param defaultVal the default value.
87      */
GetLongAction(String theProp, long defaultVal)88     public GetLongAction(String theProp, long defaultVal) {
89         this.theProp = theProp;
90         this.defaultVal = defaultVal;
91         this.defaultSet = true;
92     }
93 
94     /**
95      * Determines the <code>Long</code> value of the system property whose
96      * name was specified in the constructor.
97      *
98      * <p>If there is no property of the specified name, or if the property
99      * does not have the correct numeric format, then a <code>Long</code>
100      * object representing the default value that was specified in the
101      * constructor is returned, or <code>null</code> if no default value was
102      * specified.
103      *
104      * @return the <code>Long</code> value of the property.
105      */
run()106     public Long run() {
107         Long value = Long.getLong(theProp);
108         if ((value == null) && defaultSet)
109             return defaultVal;
110         return value;
111     }
112 }
113