1 package org.apache.ojb.broker.metadata.fieldaccess; 2 3 17 18 import java.io.Serializable ; 19 import java.lang.reflect.Field ; 20 import java.security.AccessController ; 21 import java.security.PrivilegedAction ; 22 23 32 public class PersistentFieldPrivilegedImpl extends PersistentFieldDirectImpl 33 { 34 private static final long serialVersionUID = -6110158693763128846L; 35 36 private SetAccessibleAction setAccessibleAction = new SetAccessibleAction(); 37 private UnsetAccessibleAction unsetAccessibleAction = new UnsetAccessibleAction(); 38 private static final int ACCESSIBLE_STATE_UNKOWN = 0; 39 private static final int ACCESSIBLE_STATE_FALSE = 1; 40 private static final int ACCESSIBLE_STATE_SET_TRUE = 2; 41 42 public PersistentFieldPrivilegedImpl() 43 { 44 } 45 46 public PersistentFieldPrivilegedImpl(Class type, String fieldname) 47 { 48 super(type, fieldname); 49 } 50 51 protected Object getValueFrom(Field field, Object target) 52 { 53 int accessibleState = ACCESSIBLE_STATE_UNKOWN; 54 Object result = null; 55 if (!field.isAccessible()) accessibleState = ACCESSIBLE_STATE_FALSE; 56 if (accessibleState == ACCESSIBLE_STATE_FALSE) 57 { 58 accessibleState = ACCESSIBLE_STATE_SET_TRUE; 59 setAccessibleAction.current = field; 60 AccessController.doPrivileged(setAccessibleAction); 61 } 62 try 63 { 64 result = super.getValueFrom(field, target); 65 } 66 finally 67 { 68 if (accessibleState == ACCESSIBLE_STATE_SET_TRUE) 69 { 70 unsetAccessibleAction.current = field; 71 AccessController.doPrivileged(unsetAccessibleAction); 72 } 73 } 74 return result; 75 } 76 77 protected void setValueFor(Field field, Object target, Object value) 78 { 79 int accessibleState = ACCESSIBLE_STATE_UNKOWN; 80 if (!field.isAccessible()) accessibleState = ACCESSIBLE_STATE_FALSE; 81 if (accessibleState == ACCESSIBLE_STATE_FALSE) 82 { 83 accessibleState = ACCESSIBLE_STATE_SET_TRUE; 84 setAccessibleAction.current = field; 85 AccessController.doPrivileged(setAccessibleAction); 86 } 87 try 88 { 89 super.setValueFor(field, target, value); 90 } 91 finally 92 { 93 if (accessibleState == ACCESSIBLE_STATE_SET_TRUE) 94 { 95 unsetAccessibleAction.current = field; 96 AccessController.doPrivileged(unsetAccessibleAction); 97 } 98 } 99 } 100 101 104 public boolean makeAccessible() 105 { 106 return false; 107 } 108 109 114 public boolean usesAccessorsAndMutators() 115 { 116 return false; 117 } 118 119 private static class SetAccessibleAction implements PrivilegedAction , Serializable 123 { 124 static final long serialVersionUID = 8152025069698028050L; 125 transient Field current; 126 127 public Object run() 128 { 129 current.setAccessible(true); 130 return null; 131 } 132 } 133 134 private static class UnsetAccessibleAction implements PrivilegedAction , Serializable 135 { 136 static final long serialVersionUID = -2284913657454430305L; 137 transient Field current; 138 139 public Object run() 140 { 141 current.setAccessible(false); 142 return null; 143 } 144 } 145 } 146 | Popular Tags |