Coverage Report - org.jtheque.core.spring.processors.LoggerPostProcessor
 
Classes in this File Line Coverage Branch Coverage Complexity
LoggerPostProcessor
0 %
0/13
0 %
0/4
2
LoggerPostProcessor$1
N/A
N/A
2
LoggerPostProcessor$MakeFieldAccessible
0 %
0/6
N/A
2
 
 1  
 package org.jtheque.core.spring.processors;
 2  
 
 3  
 import org.jtheque.core.managers.Managers;
 4  
 import org.jtheque.core.managers.log.IJThequeLogger;
 5  
 import org.jtheque.core.managers.log.ILoggingManager;
 6  
 import org.jtheque.core.managers.log.Logger;
 7  
 import org.springframework.beans.factory.BeanInitializationException;
 8  
 import org.springframework.beans.factory.config.BeanPostProcessor;
 9  
 
 10  
 import java.lang.reflect.Field;
 11  
 import java.security.AccessController;
 12  
 import java.security.PrivilegedAction;
 13  
 
 14  
 /*
 15  
  * This file is part of JTheque.
 16  
  *
 17  
  * JTheque is free software: you can redistribute it and/or modify
 18  
  * it under the terms of the GNU General Public License as published by
 19  
  * the Free Software Foundation, either version 3 of the License.
 20  
  *
 21  
  * JTheque is distributed in the hope that it will be useful,
 22  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 23  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 24  
  * GNU General Public License for more details.
 25  
  *
 26  
  * You should have received a copy of the GNU General Public License
 27  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 28  
  */
 29  
 
 30  
 /**
 31  
  * A bean post process to process @Logger and to inject loggers to the classes.
 32  
  *
 33  
  * @author Baptiste Wicht
 34  
  */
 35  0
 public final class LoggerPostProcessor implements BeanPostProcessor {
 36  
     @Override
 37  
     public Object postProcessAfterInitialization(Object bean, String beanName) {
 38  0
         return bean;
 39  
     }
 40  
 
 41  
     @Override
 42  
     public Object postProcessBeforeInitialization(Object bean, String beanName) {
 43  0
         Class<?> clazz = bean.getClass();
 44  
 
 45  0
         for (final Field field : clazz.getDeclaredFields()) {
 46  0
             if (field.getAnnotation(Logger.class) != null) {
 47  0
                AccessController.doPrivileged(new MakeFieldAccessible(field));
 48  
 
 49  0
                 IJThequeLogger logger = Managers.getManager(ILoggingManager.class).getLogger(clazz);
 50  
 
 51  
                 try {
 52  0
                     field.set(bean, logger);
 53  0
                 } catch (IllegalAccessException e) {
 54  0
                     throw new BeanInitializationException("Unable to access field " + field.getName(), e);
 55  0
                 }
 56  
             }
 57  
         }
 58  
 
 59  0
         return bean;
 60  
     }
 61  
 
 62  
     /**
 63  
      * A privileged action to make a field accessible.
 64  
      *
 65  
      * @author Baptiste Wicht
 66  
      */
 67  0
     private static final class MakeFieldAccessible implements PrivilegedAction<Object> {
 68  
         private final Field field;
 69  
 
 70  
         /**
 71  
          * Construct a new MakeFieldAccessible.
 72  
          *
 73  
          * @param field The field to make accessible. 
 74  
          */
 75  
         private MakeFieldAccessible(Field field) {
 76  0
             super();
 77  
             
 78  0
             this.field = field;
 79  0
         }
 80  
 
 81  
         @Override
 82  
         public Object run() {
 83  0
             field.setAccessible(true);
 84  
 
 85  0
             return null;
 86  
         }
 87  
     }
 88  
 }