1 21 package oracle.toplink.essentials.internal.security; 23 24 import java.security.*; 25 import java.lang.reflect.*; 26 27 37 public class PrivilegedAccessHelper { 38 private static boolean shouldUsePrivilegedAccess = false; 39 private static boolean shouldSecurityManagerBeChecked = true; 40 41 46 private static Field findDeclaredField(Class javaClass, String fieldName) throws NoSuchFieldException { 47 try { 48 return javaClass.getDeclaredField(fieldName); 49 } catch (NoSuchFieldException ex) { 50 Class superclass = javaClass.getSuperclass(); 51 if (superclass == null) { 52 throw ex; 53 } else { 54 return findDeclaredField(superclass, fieldName); 55 } 56 } 57 } 58 59 64 private static Method findMethod(Class javaClass, String methodName, Class [] methodParameterTypes) throws NoSuchMethodException { 65 try { 66 return javaClass.getDeclaredMethod(methodName, methodParameterTypes); 67 } catch (NoSuchMethodException ex) { 68 Class superclass = javaClass.getSuperclass(); 69 if (superclass == null) { 70 throw ex; 71 } else { 72 try{ 73 return findMethod(superclass, methodName, methodParameterTypes); 74 }catch (NoSuchMethodException lastEx){ 75 throw ex; 76 } 77 } 78 } 79 } 80 81 85 public static Class getClassForName(final String className) throws ClassNotFoundException { 86 return Class.forName(className); 87 } 88 89 96 public static Class getClassForName(final String className, final boolean initialize, final ClassLoader loader) throws ClassNotFoundException { 97 return Class.forName(className, initialize, loader); 98 } 99 100 103 public static ClassLoader getClassLoaderForClass(final Class clazz) { 104 return clazz.getClassLoader(); 105 } 106 107 116 public static Constructor getConstructorFor(final Class javaClass, final Class [] args, final boolean shouldSetAccessible) throws NoSuchMethodException { 117 Constructor result = javaClass.getConstructor(args); 118 if (shouldSetAccessible) { 119 result.setAccessible(true); 120 } 121 return result; 122 } 123 124 127 public static ClassLoader getContextClassLoader(final Thread thread) { 128 return thread.getContextClassLoader(); 129 } 130 131 141 public static Constructor getDeclaredConstructorFor(final Class javaClass, final Class [] args, final boolean shouldSetAccessible) throws NoSuchMethodException { 142 Constructor result = javaClass.getDeclaredConstructor(args); 143 if (shouldSetAccessible) { 144 result.setAccessible(true); 145 } 146 return result; 147 } 148 149 158 public static Field getField(final Class javaClass, final String fieldName, final boolean shouldSetAccessible) throws NoSuchFieldException { 159 Field field = (Field)findDeclaredField(javaClass, fieldName); 160 if (shouldSetAccessible) { 161 field.setAccessible(true); 162 } 163 return field; 164 } 165 166 175 public static Field getDeclaredField(final Class javaClass, final String fieldName, final boolean shouldSetAccessible) throws NoSuchFieldException { 176 Field field = javaClass.getDeclaredField(fieldName); 177 if (shouldSetAccessible) { 178 field.setAccessible(true); 179 } 180 return field; 181 } 182 183 188 public static Field[] getDeclaredFields(final Class clazz) { 189 return clazz.getDeclaredFields(); 190 } 191 192 201 public static Method getDeclaredMethod(final Class clazz, final String methodName, final Class [] methodParameterTypes) throws NoSuchMethodException { 202 return clazz.getDeclaredMethod(methodName, methodParameterTypes); 203 } 204 205 217 public static Method getMethod(final Class javaClass, final String methodName, final Class [] methodParameterTypes, final boolean shouldSetAccessible) throws NoSuchMethodException { 218 Method method = findMethod(javaClass, methodName, methodParameterTypes); 219 if (shouldSetAccessible) { 220 method.setAccessible(true); 221 } 222 return method; 223 } 224 225 230 public static Method[] getDeclaredMethods(final Class clazz) { 231 return clazz.getDeclaredMethods(); 232 } 233 234 238 public static Class getFieldType(final Field field) { 239 return field.getType(); 240 } 241 242 246 public static String getLineSeparator() { 247 if (shouldUsePrivilegedAccess()) { 248 return (String )AccessController.doPrivileged(new PrivilegedAction() { 249 public Object run() { 250 return System.getProperty("file.separator"); 251 } 252 }); 253 } else { 254 return oracle.toplink.essentials.internal.helper.Helper.cr(); 255 } 256 } 257 258 262 public static Class [] getMethodParameterTypes(final Method method) { 263 return method.getParameterTypes(); 264 } 265 266 270 public static Class getMethodReturnType(final Method method) { 271 return method.getReturnType(); 272 } 273 274 279 public static Method[] getMethods(final Class clazz) { 280 return clazz.getMethods(); 281 } 282 283 286 public static Object getValueFromField(final Field field, final Object object) throws IllegalAccessException { 287 return field.get(object); 288 } 289 290 294 public static Object invokeConstructor(final Constructor constructor, final Object [] args) throws IllegalAccessException , InvocationTargetException, InstantiationException { 295 return constructor.newInstance(args); 296 } 297 298 302 public static Object invokeMethod(final Method method, final Object object, final Object [] parameters) throws IllegalAccessException , InvocationTargetException { 303 if (!method.isAccessible()) { 305 method.setAccessible(true); 306 } 307 return method.invoke(object, parameters); 308 } 309 310 314 public static Object newInstanceFromClass(final Class clazz) throws IllegalAccessException , InstantiationException { 315 return clazz.newInstance(); 316 } 317 318 322 public static void setValueInField(final Field field, final Object object, final Object value) throws IllegalAccessException { 323 field.set(object, value); 324 } 325 326 337 public static boolean shouldUsePrivilegedAccess() { 338 if (shouldSecurityManagerBeChecked) { 340 shouldSecurityManagerBeChecked = false; 341 342 Boolean privilegedPropertySet = (Boolean )AccessController.doPrivileged(new PrivilegedAction() { 343 public Object run() { 344 boolean propertySet; 345 346 String usePrivileged = System.getProperty("oracle.j2ee.toplink.security.usedoprivileged"); 348 String oc4jUsePrivileged = System.getProperty("oracle.j2ee.security.usedoprivileged"); 349 propertySet = (((usePrivileged != null) && usePrivileged.equalsIgnoreCase("false")) || ((oc4jUsePrivileged != null) && oc4jUsePrivileged.equalsIgnoreCase("false"))); 350 return new Boolean (propertySet); 351 } 352 }); 353 if (privilegedPropertySet.booleanValue()) { 354 shouldUsePrivilegedAccess = false; 355 } else { 356 shouldUsePrivilegedAccess = (System.getSecurityManager() != null); 357 } 358 } 359 return shouldUsePrivilegedAccess; 360 } 361 } 362 | Popular Tags |