Coverage Report - org.boretti.drools.integration.drools5.DroolsBeanFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
DroolsBeanFactory
100 %
25/25
64 %
9/14
2.6
 
 1  
 /*
 2  
     Drools5 Integration Helper
 3  
     Copyright (C) 2009  Mathieu Boretti mathieu.boretti@gmail.com
 4  
 
 5  
     This program is free software: you can redistribute it and/or modify
 6  
     it under the terms of the GNU General Public License as published by
 7  
     the Free Software Foundation, either version 3 of the License, or
 8  
     (at your option) any later version.
 9  
 
 10  
     This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 17  
 
 18  
  */
 19  
 package org.boretti.drools.integration.drools5;
 20  
 
 21  
 import java.lang.reflect.Method;
 22  
 
 23  
 import org.apache.log4j.Logger;
 24  
 import org.springframework.beans.BeansException;
 25  
 import org.springframework.beans.factory.BeanFactory;
 26  
 import org.springframework.beans.factory.BeanFactoryAware;
 27  
 import org.springframework.beans.factory.FactoryBean;
 28  
 import org.springframework.beans.factory.ListableBeanFactory;
 29  
 import org.springframework.beans.factory.annotation.Autowired;
 30  
 
 31  
 /**
 32  
  * This class provide a way to interface with spring.
 33  
  * Use
 34  
  * <pre>
 35  
  * &lt;bean id="XXX"
 36  
  *         class="org.boretti.drools.integration.drools5.DroolsBeanFactory"&gt;
 37  
  *                &lt;property name="droolsInterface" value="interface name"/&gt;
 38  
  * &lt;/bean&gt;
 39  
  * </pre>
 40  
  * To use it. The passed interface must have been annotated with the
 41  
  * right annotation.
 42  
  * @author mbo
 43  
  *
 44  
  */
 45  2
 public class DroolsBeanFactory implements FactoryBean,BeanFactoryAware {
 46  
         
 47  2
         private final Logger logger = Logger.getLogger(DroolsBeanFactory.class);
 48  
 
 49  
         private Class<?> droolsInterface;
 50  
         
 51  
         private BeanFactory beanFactory;
 52  
         
 53  2
         private DroolsProvider droolsProvider = new DroolsProvider();
 54  
         
 55  
         @Override
 56  
         public Object getObject() throws Exception {
 57  4
                 Object o = droolsProvider.getService(droolsInterface);
 58  16
                 for(Method m:droolsInterface.getMethods()) {
 59  12
                         Autowired a = null;
 60  
                         try {
 61  12
                                 a=m.getAnnotation(Autowired.class);
 62  12
                         } catch (NullPointerException ex) {}
 63  12
                         if (a!=null) {
 64  4
                                 String methodName = m.getName();
 65  4
                                 if (methodName.startsWith("set") && m.getParameterTypes().length==1) {
 66  4
                                         String fieldName = methodName.substring(3,4).toLowerCase()+methodName.substring(4); 
 67  4
                                         if (beanFactory instanceof ListableBeanFactory) {
 68  4
                                                 ListableBeanFactory lbf = (ListableBeanFactory)beanFactory;
 69  4
                                                 String names[] = lbf.getBeanNamesForType(m.getParameterTypes()[0]);
 70  4
                                                 if (names.length>0) {
 71  4
                                                         if (logger.isDebugEnabled()) logger.debug("Injection into "+fieldName);
 72  4
                                                         m.invoke(o, lbf.getBean(names[0]));
 73  
                                                 }
 74  
                                         }
 75  
                                 }
 76  
                         }
 77  
                 }
 78  4
                 return o;
 79  
         }
 80  
 
 81  
         @Override
 82  
         public Class<?> getObjectType() {
 83  8
                 return droolsInterface;
 84  
         }
 85  
 
 86  
         @Override
 87  
         public boolean isSingleton() {
 88  4
                 return false;
 89  
         }
 90  
 
 91  
         public void setDroolsInterface(Class<?> droolsInterface) {
 92  2
                 this.droolsInterface = droolsInterface;
 93  2
         }
 94  
 
 95  
         @Override
 96  
         public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
 97  2
                 this.beanFactory=beanFactory;
 98  2
         }
 99  
 
 100  
 
 101  
 }