001: package org.apache.ojb.broker.metadata.fieldaccess;
002:
003: /* Copyright 2003-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * 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: import java.io.Serializable;
019: import java.lang.reflect.Field;
020: import java.security.AccessController;
021: import java.security.PrivilegedAction;
022:
023: /**
024: * A {@link PersistentField} implementation using
025: * reflection to access but does cooperate with
026: * AccessController and do not suppress the java
027: * language access check.
028: *
029: * @version $Id: PersistentFieldPrivilegedImpl.java,v 1.11.2.2 2005/12/21 22:26:41 tomdz Exp $
030: * @see PersistentFieldDirectImpl
031: */
032: public class PersistentFieldPrivilegedImpl extends
033: PersistentFieldDirectImpl {
034: private static final long serialVersionUID = -6110158693763128846L;
035:
036: private SetAccessibleAction setAccessibleAction = new SetAccessibleAction();
037: private UnsetAccessibleAction unsetAccessibleAction = new UnsetAccessibleAction();
038: private static final int ACCESSIBLE_STATE_UNKOWN = 0;
039: private static final int ACCESSIBLE_STATE_FALSE = 1;
040: private static final int ACCESSIBLE_STATE_SET_TRUE = 2;
041:
042: public PersistentFieldPrivilegedImpl() {
043: }
044:
045: public PersistentFieldPrivilegedImpl(Class type, String fieldname) {
046: super (type, fieldname);
047: }
048:
049: protected Object getValueFrom(Field field, Object target) {
050: int accessibleState = ACCESSIBLE_STATE_UNKOWN;
051: Object result = null;
052: if (!field.isAccessible())
053: accessibleState = ACCESSIBLE_STATE_FALSE;
054: if (accessibleState == ACCESSIBLE_STATE_FALSE) {
055: accessibleState = ACCESSIBLE_STATE_SET_TRUE;
056: setAccessibleAction.current = field;
057: AccessController.doPrivileged(setAccessibleAction);
058: }
059: try {
060: result = super .getValueFrom(field, target);
061: } finally {
062: if (accessibleState == ACCESSIBLE_STATE_SET_TRUE) {
063: unsetAccessibleAction.current = field;
064: AccessController.doPrivileged(unsetAccessibleAction);
065: }
066: }
067: return result;
068: }
069:
070: protected void setValueFor(Field field, Object target, Object value) {
071: int accessibleState = ACCESSIBLE_STATE_UNKOWN;
072: if (!field.isAccessible())
073: accessibleState = ACCESSIBLE_STATE_FALSE;
074: if (accessibleState == ACCESSIBLE_STATE_FALSE) {
075: accessibleState = ACCESSIBLE_STATE_SET_TRUE;
076: setAccessibleAction.current = field;
077: AccessController.doPrivileged(setAccessibleAction);
078: }
079: try {
080: super .setValueFor(field, target, value);
081: } finally {
082: if (accessibleState == ACCESSIBLE_STATE_SET_TRUE) {
083: unsetAccessibleAction.current = field;
084: AccessController.doPrivileged(unsetAccessibleAction);
085: }
086: }
087: }
088:
089: /**
090: * This implementation returns always 'false'.
091: */
092: public boolean makeAccessible() {
093: return false;
094: }
095:
096: /**
097: * Always returns 'false'.
098: *
099: * @see PersistentField#usesAccessorsAndMutators
100: */
101: public boolean usesAccessorsAndMutators() {
102: return false;
103: }
104:
105: //************************************************************
106: // inner class
107: //************************************************************
108: private static class SetAccessibleAction implements
109: PrivilegedAction, Serializable {
110: static final long serialVersionUID = 8152025069698028050L;
111: transient Field current;
112:
113: public Object run() {
114: current.setAccessible(true);
115: return null;
116: }
117: }
118:
119: private static class UnsetAccessibleAction implements
120: PrivilegedAction, Serializable {
121: static final long serialVersionUID = -2284913657454430305L;
122: transient Field current;
123:
124: public Object run() {
125: current.setAccessible(false);
126: return null;
127: }
128: }
129: }
|