1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.luni.util;
19
20
21import java.lang.reflect.AccessibleObject;
22import java.security.Policy;
23import java.security.PrivilegedAction;
24import java.security.Security;
25
26/**
27 * Helper class to avoid multiple anonymous inner class for
28 * <code>{@link java.security.AccessController#doPrivileged(PrivilegedAction)}</code>
29 * calls.
30 */
31public class PriviAction<T> implements PrivilegedAction<T> {
32
33    private Object arg1;
34
35    private Object arg2;
36
37    private int action;
38
39    private static final int GET_SYSTEM_PROPERTY = 1;
40
41    private static final int GET_SECURITY_POLICY = 2;
42
43    private static final int SET_ACCESSIBLE = 3;
44
45    private static final int GET_SECURITY_PROPERTY = 4;
46
47    /**
48     * Creates a PrivilegedAction to get the security property with the given
49     * name.
50     *
51     * @param property
52     *            the name of the property
53     *
54     * @see Security#getProperty
55     */
56    public static PrivilegedAction<String> getSecurityProperty(String property) {
57        return new PriviAction<String>(GET_SECURITY_PROPERTY, property);
58    }
59
60    private PriviAction(int action, Object arg) {
61        this.action = action;
62        this.arg1 = arg;
63    }
64
65    /**
66     * Creates a PrivilegedAction to get the current security policy object.
67     *
68     * @see Policy#getPolicy
69     */
70    public PriviAction() {
71        action = GET_SECURITY_POLICY;
72    }
73
74    /**
75     * Creates a PrivilegedAction to disable the access checks to the given
76     * object.
77     *
78     * @param object
79     *            the object whose accessible flag will be set to
80     *            <code>true</code>
81     *
82     * @see AccessibleObject#setAccessible(boolean)
83     */
84    public PriviAction(AccessibleObject object) {
85        action = SET_ACCESSIBLE;
86        arg1 = object;
87    }
88
89    /**
90     * Creates a PrivilegedAction to return the value of the system property
91     * with the given key.
92     *
93     * @param property
94     *            the key of the system property
95     *
96     * @see System#getProperty(String)
97     */
98    public PriviAction(String property) {
99        action = GET_SYSTEM_PROPERTY;
100        arg1 = property;
101    }
102
103    /**
104     * Creates a PrivilegedAction to return the value of the system property
105     * with the given key.
106     *
107     * @param property
108     *            the key of the system property
109     * @param defaultAnswer
110     *            the return value if the system property does not exist
111     *
112     * @see System#getProperty(String, String)
113     */
114    public PriviAction(String property, String defaultAnswer) {
115        action = GET_SYSTEM_PROPERTY;
116        arg1 = property;
117        arg2 = defaultAnswer;
118    }
119
120    /**
121     * Performs the actual privileged computation as defined by the constructor.
122     *
123     * @see java.security.PrivilegedAction#run()
124     */
125    @SuppressWarnings("unchecked")
126    public T run() {
127        switch (action) {
128        case GET_SYSTEM_PROPERTY:
129            return (T)System.getProperty((String) arg1, (String) arg2);
130        case GET_SECURITY_PROPERTY:
131            return (T)Security.getProperty((String) arg1);
132        case GET_SECURITY_POLICY:
133            return (T)Policy.getPolicy();
134        case SET_ACCESSIBLE:
135            ((AccessibleObject) arg1).setAccessible(true);
136        }
137        return null;
138    }
139}
140