Coverage Report - org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldPrivilegedImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
PersistentFieldPrivilegedImpl
N/A
N/A
1.75
PersistentFieldPrivilegedImpl$1
N/A
N/A
1.75
PersistentFieldPrivilegedImpl$SetAccessibleAction
N/A
N/A
1.75
PersistentFieldPrivilegedImpl$UnsetAccessibleAction
N/A
N/A
1.75
 
 1  
 package org.apache.ojb.broker.metadata.fieldaccess;
 2  
 
 3  
 /* Copyright 2003-2005 The Apache Software Foundation
 4  
  *
 5  
  * Licensed under the Apache License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * 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  
 
 18  
 import java.io.Serializable;
 19  
 import java.lang.reflect.Field;
 20  
 import java.security.AccessController;
 21  
 import java.security.PrivilegedAction;
 22  
 
 23  
 /**
 24  
  * A {@link PersistentField} implementation using
 25  
  * reflection to access but does cooperate with
 26  
  * AccessController and do not suppress the java
 27  
  * language access check.
 28  
  *
 29  
  * @version $Id: PersistentFieldPrivilegedImpl.java,v 1.1 2007-08-24 22:17:35 ewestfal Exp $
 30  
  * @see PersistentFieldDirectImpl
 31  
  */
 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  
     /**
 102  
      * This implementation returns always 'false'.
 103  
      */
 104  
     public boolean makeAccessible()
 105  
     {
 106  
         return false;
 107  
     }
 108  
 
 109  
     /**
 110  
      * Always returns 'false'.
 111  
      *
 112  
      * @see PersistentField#usesAccessorsAndMutators
 113  
      */
 114  
     public boolean usesAccessorsAndMutators()
 115  
     {
 116  
         return false;
 117  
     }
 118  
 
 119  
     //************************************************************
 120  
     // inner class
 121  
     //************************************************************
 122  
     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  
 }