Coverage Report - org.jtheque.core.spring.factory.LazyProxyHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
LazyProxyHandler
0 %
0/14
0 %
0/6
2.333
LazyProxyHandler$1
0 %
0/3
N/A
2.333
 
 1  
 package org.jtheque.core.spring.factory;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import org.jtheque.core.managers.Managers;
 20  
 import org.jtheque.core.managers.beans.IBeansManager;
 21  
 import org.jtheque.core.managers.log.ILoggingManager;
 22  
 
 23  
 import javax.swing.SwingUtilities;
 24  
 import java.lang.reflect.InvocationHandler;
 25  
 import java.lang.reflect.InvocationTargetException;
 26  
 import java.lang.reflect.Method;
 27  
 
 28  
 /**
 29  
  * An invocation handler to disable the manager.
 30  
  *
 31  
  * @author Baptiste Wicht
 32  
  */
 33  0
 final class LazyProxyHandler implements InvocationHandler {
 34  
     private final String beanName;
 35  
     private final boolean swing;
 36  
 
 37  
     private Object instance;
 38  
 
 39  
     /**
 40  
      * Construct a new LazyProxyHandler.
 41  
      *
 42  
      * @param beanName The name of the bean.
 43  
      * @param swing A boolean tag indicating if the bean is swing bean.
 44  
      */
 45  
     LazyProxyHandler(String beanName, boolean swing) {
 46  0
         super();
 47  
 
 48  0
         this.beanName = beanName;
 49  0
         this.swing = swing;
 50  0
     }
 51  
 
 52  
     @Override
 53  
     public Object invoke(Object proxy, Method method, Object[] args) throws InvocationTargetException, IllegalAccessException {
 54  0
         if (instance == null) {
 55  0
             Managers.getManager(ILoggingManager.class).getLogger(getClass()).debug("Init {} due to call to {}", beanName, method.toGenericString());
 56  
 
 57  0
             if(swing && !SwingUtilities.isEventDispatchThread()){
 58  
                 try {
 59  0
                     SwingUtilities.invokeAndWait(new Runnable(){
 60  
                         @Override
 61  
                         public void run() {
 62  0
                             instance = Managers.getManager(IBeansManager.class).getBean(beanName);
 63  0
                         }
 64  
                     });
 65  0
                 } catch (InterruptedException e) {
 66  0
                     Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e);
 67  0
                 }
 68  
             } else {
 69  0
                 instance = Managers.getManager(IBeansManager.class).getBean(beanName);
 70  
             }
 71  
         }
 72  
 
 73  0
         return method.invoke(instance, args);
 74  
     }
 75  
 }