Coverage Report - org.boretti.drools.integration.drools4.implementation.DroolsProxy
 
Classes in this File Line Coverage Branch Coverage Complexity
DroolsProxy
95 %
59/62
87 %
51/58
9.75
 
 1  
 /*
 2  
     Drools4 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.drools4.implementation;
 20  
 
 21  
 import java.lang.annotation.Annotation;
 22  
 import java.lang.reflect.InvocationHandler;
 23  
 import java.lang.reflect.Method;
 24  
 import java.util.ArrayList;
 25  
 import java.util.Collection;
 26  
 import java.util.Iterator;
 27  
 import java.util.List;
 28  
 
 29  
 import org.apache.log4j.Logger;
 30  
 import org.boretti.drools.integration.drools4.DroolsInterface;
 31  
 import org.boretti.drools.integration.drools4.DroolsParameterHandling;
 32  
 import org.boretti.drools.integration.drools4.DroolsSessionType;
 33  
 import org.boretti.drools.integration.drools4.annotations.DroolsMethod;
 34  
 import org.boretti.drools.integration.drools4.annotations.DroolsParameter;
 35  
 
 36  
 
 37  
 /**
 38  
  * @author mbo
 39  
  * 
 40  
  */
 41  
 class DroolsProxy<T> implements InvocationHandler {
 42  
         
 43  76
         private final Logger logger = Logger.getLogger(this.getClass());
 44  
                         
 45  
         private List<Object> produceFact(Method m,Object[] args) {
 46  140
                 List<Object> fact = new ArrayList<Object>();
 47  140
                 if (args==null) return fact;
 48  72
                 if (args.length==0) return fact;
 49  72
                 Annotation[][] ass = m.getParameterAnnotations();
 50  152
                 for(int i=0;i<args.length;i++) {
 51  80
                         Annotation[] as = ass[i];
 52  80
                         DroolsParameterHandling handling = DroolsParameterHandling.NONE;
 53  80
                         for(Annotation a:as) {
 54  28
                                 if (a.annotationType().equals(DroolsParameter.class)) {
 55  28
                                         handling = ((DroolsParameter)a).handling();
 56  28
                                         break;
 57  
                                 }
 58  
                         }
 59  80
                         if (handling==null) handling = DroolsParameterHandling.NONE;
 60  80
                         if (DroolsParameterHandling.NONE.equals(handling)) {
 61  52
                                 fact.add(args[i]);
 62  28
                         } else if (DroolsParameterHandling.IGNORED.equals(handling)) {
 63  24
                         } else if (DroolsParameterHandling.TOFLAT1.equals(handling)) {
 64  16
                                 if (args[i] instanceof Object[]) for(Object o:(Object[])args[i]) fact.add(o);
 65  8
                                 else if (args[i] instanceof Collection) for(Object o:(Collection<?>)args[i]) fact.add(o);
 66  4
                                 else fact.add(args[i]);
 67  8
                         } else if (DroolsParameterHandling.TOFLATALL.equals(handling)) {
 68  8
                                 addFlat(fact,args[i]);
 69  
                         } 
 70  
                 }
 71  72
                 return fact;
 72  
         }
 73  
         
 74  
         private void addFlat(List<Object> fact,Object args) {
 75  40
                 if (args instanceof Object[]) for(Object o:(Object[])args) addFlat(fact,o);
 76  28
                 else if (args instanceof Collection) for(Object o:(Collection<?>)args) addFlat(fact,o);
 77  20
                 else fact.add(args);
 78  40
         }
 79  
 
 80  
         /*
 81  
          * (non-Javadoc)
 82  
          * 
 83  
          * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object,
 84  
          *      java.lang.reflect.Method, java.lang.Object[])
 85  
          */
 86  
         @Override
 87  
         public Object invoke(Object proxy, Method method, Object[] args)
 88  
                         throws Throwable {
 89  144
                 if (method.getDeclaringClass().equals(clazz)) {
 90  140
                         DroolsMethod annotation = null;
 91  140
                         DroolsSessionType session = DroolsSessionType.NONE;
 92  
                         try {
 93  140
                                 annotation = method.getAnnotation(DroolsMethod.class);
 94  140
                         } catch (NullPointerException ex) {}
 95  140
                         List<Object> fact = produceFact(method,args);
 96  140
                         Class<?> c = method.getReturnType();
 97  140
                         if (annotation!=null && annotation.session()!=null) session = annotation.session();
 98  140
                         Iterator<?> result=null;
 99  140
                         if (DroolsSessionType.NONE.equals(session)) {
 100  36
                                 result = inter.runStatelessSession(fact);
 101  104
                         } else if (DroolsSessionType.NEW.equals(session)) {
 102  20
                                 if (inter.getCurrentSession()!=null) {
 103  4
                                         inter.getCurrentSession().halt();
 104  4
                                         inter.setCurrentSession(null);
 105  
                                 }
 106  20
                                 result=inter.runStatefulSession(fact, false);
 107  84
                         } else if (DroolsSessionType.END.equals(session)) {
 108  20
                                 result=inter.runStatefulSession(fact, true);
 109  16
                                 inter.getCurrentSession().halt();
 110  16
                                 inter.setCurrentSession(null);
 111  64
                         } else if (DroolsSessionType.JOIN.equals(session)) {
 112  32
                                 if (inter.getCurrentSession()==null) result = inter.runStatelessSession(fact);
 113  20
                                 else result = inter.runStatefulSession(fact, true);
 114  32
                         } else if (DroolsSessionType.REQUIRE.equals(session)) {
 115  32
                                 result = inter.runStatefulSession(fact, true);
 116  
                         }
 117  128
                         return inter.getResult(c, result);
 118  4
                 } else if (method.getDeclaringClass().equals(DroolsInterface.class)) {
 119  4
                         return method.invoke(inter, args);
 120  
                 }
 121  0
                 String msg = "Unable to handle the received parameter";
 122  0
                 logger.error(msg);
 123  0
                 throw new IllegalArgumentException(msg);
 124  
         }
 125  
         
 126  
         private Class<T> clazz;
 127  
         
 128  
         private DroolsInterfaceImplementation<T> inter;
 129  
         
 130  76
         public DroolsProxy(Class<T> clazz,DroolsInterfaceImplementation<T> inter) {
 131  76
                 this.clazz=clazz;
 132  76
                 this.inter=inter;
 133  76
         }
 134  
 
 135  
 }