001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.util;
019:
020: import java.lang.reflect.AccessibleObject;
021: import java.security.Policy;
022: import java.security.PrivilegedAction;
023: import java.security.Security;
024:
025: /**
026: * Helper class to avoid multiple anonymous inner class for
027: * <code>{@link java.security.AccessController#doPrivileged(PrivilegedAction)}</code>
028: * calls.
029: */
030: public class PriviAction<T> implements PrivilegedAction<T> {
031:
032: private Object arg1;
033:
034: private Object arg2;
035:
036: private int action;
037:
038: private static final int GET_SYSTEM_PROPERTY = 1;
039:
040: private static final int GET_SECURITY_POLICY = 2;
041:
042: private static final int SET_ACCESSIBLE = 3;
043:
044: private static final int GET_SECURITY_PROPERTY = 4;
045:
046: /**
047: * Creates a PrivilegedAction to get the security property with the given
048: * name.
049: *
050: * @param property
051: * the name of the property
052: *
053: * @see Security#getProperty
054: */
055: public static PrivilegedAction<String> getSecurityProperty(
056: String property) {
057: return new PriviAction<String>(GET_SECURITY_PROPERTY, property);
058: }
059:
060: private PriviAction(int action, Object arg) {
061: this .action = action;
062: this .arg1 = arg;
063: }
064:
065: /**
066: * Creates a PrivilegedAction to get the current security policy object.
067: *
068: * @see Policy#getPolicy
069: */
070: public PriviAction() {
071: action = GET_SECURITY_POLICY;
072: }
073:
074: /**
075: * Creates a PrivilegedAction to disable the access checks to the given
076: * object.
077: *
078: * @param object
079: * the object whose accessible flag will be set to
080: * <code>true</code>
081: *
082: * @see AccessibleObject#setAccessible(boolean)
083: */
084: public PriviAction(AccessibleObject object) {
085: action = SET_ACCESSIBLE;
086: arg1 = object;
087: }
088:
089: /**
090: * Creates a PrivilegedAction to return the value of the system property
091: * with the given key.
092: *
093: * @param property
094: * the key of the system property
095: *
096: * @see System#getProperty(String)
097: */
098: public PriviAction(String property) {
099: 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: }
|