Coverage Report - org.boretti.drools.integration.drools4.implementation.DroolsProviderImplementation
 
Classes in this File Line Coverage Branch Coverage Complexity
DroolsProviderImplementation
100 %
49/49
76 %
23/30
4.143
 
 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.Proxy;
 23  
 
 24  
 import org.apache.log4j.Logger;
 25  
 import org.boretti.drools.integration.drools4.DroolsInterface;
 26  
 import org.boretti.drools.integration.drools4.DroolsServiceType;
 27  
 import org.boretti.drools.integration.drools4.annotations.DroolsService;
 28  
 import org.boretti.drools.integration.drools4.exceptions.NotAnInterfaceDroolsError;
 29  
 import org.boretti.drools.integration.drools4.exceptions.NotAnnotatedClassDroolsError;
 30  
 import org.boretti.drools.integration.drools4.exceptions.NotAnnotatedInterfaceDroolsError;
 31  
 import org.boretti.drools.integration.drools4.implementation.DroolsInterfaceImplementation;
 32  
 import org.boretti.drools.integration.drools4.implementation.DroolsCompiledInterfaceImplementation;
 33  
 import org.drools.RuleBase;
 34  
 
 35  
 public class DroolsProviderImplementation {
 36  
         
 37  4
         private final Logger logger = Logger.getLogger(this.getClass());
 38  
         
 39  8
         private DroolsProviderImplementation() {}
 40  
         
 41  4
         private static final DroolsProviderImplementation instance = new DroolsProviderImplementation();
 42  
         
 43  
         public static final DroolsProviderImplementation getInstance() {
 44  140
                 return instance;
 45  
         }
 46  
         
 47  
         @SuppressWarnings("unchecked")
 48  
         public <T> T getService(Class<T> clazz) {
 49  108
                 if (logger.isDebugEnabled()) logger.debug("Gettting service for "+clazz);
 50  108
                 validNullability(clazz,"Invalid interface - can't be null");
 51  104
                 if (!clazz.isInterface()) {
 52  4
                         String msg = "Not an interface "+clazz;
 53  4
                         logger.error(msg);
 54  4
                         throw new NotAnInterfaceDroolsError(msg);
 55  
                 }
 56  100
                 DroolsService ds = getAnnotation(clazz);
 57  100
                 if(ds==null) {
 58  8
                         String msg = "Invalid interface (missing annotation) "+clazz;
 59  8
                         logger.error(msg);
 60  8
                         throw new NotAnnotatedInterfaceDroolsError(msg);
 61  
                 }
 62  92
                 DroolsServiceType dst=ds.type();
 63  92
                 String resource = ds.resourceName();
 64  92
                 validNullability(dst,"Invalid type parameter of annotation - can't be null");
 65  92
                 DroolsInterfaceImplementation<T> inter=getDroolsImplementation(dst,resource,clazz);
 66  76
                 if (logger.isDebugEnabled()) logger.debug("Creating proxy using "+clazz+" from "+dst+" using resource "+resource);
 67  76
                 return (T) Proxy.newProxyInstance(clazz.getClassLoader(),new Class[] {clazz,DroolsInterface.class},new DroolsProxy<T>(clazz,inter));
 68  
         }
 69  
         
 70  
         /**
 71  
          * 
 72  
          * @param clazz
 73  
          * @return
 74  
          * @since 1.1.0
 75  
          */
 76  
         public <T> RuleBase getRuleBase(Class<T> clazz) {
 77  32
                 if (logger.isDebugEnabled()) logger.debug("Gettting service for "+clazz);
 78  32
                 validNullability(clazz,"Invalid class or interface - can't be null");
 79  32
                 DroolsService ds = getAnnotation(clazz);
 80  32
                 if(ds==null) {
 81  12
                         if (clazz.isInterface()) {
 82  8
                                 String msg = "Invalid interface (missing annotation) "+clazz;
 83  8
                                 logger.error(msg);
 84  8
                                 throw new NotAnnotatedInterfaceDroolsError(msg);
 85  
                         } else {
 86  4
                                 String msg = "Invalid class (missing annotation) "+clazz;
 87  4
                                 logger.error(msg);
 88  4
                                 throw new NotAnnotatedClassDroolsError(msg);
 89  
                         }
 90  
                 }
 91  20
                 DroolsServiceType dst=ds.type();
 92  20
                 String resource = ds.resourceName();
 93  20
                 validNullability(dst,"Invalid type parameter of annotation - can't be null");
 94  20
                 return getDroolsImplementation(dst,resource,clazz).getRuleBase();
 95  
         }
 96  
         
 97  
         private <T> DroolsInterfaceImplementation<T> getDroolsImplementation(DroolsServiceType dst,String resource,Class<T> clazz) {
 98  112
                 if (DroolsServiceType.COMPILED.equals(dst)) {
 99  16
                         if (resource==null || resource.trim().equalsIgnoreCase("")) resource = clazz.getName()+".cdrl";
 100  16
                         return new DroolsCompiledInterfaceImplementation<T>(clazz, resource);
 101  
                 } else {
 102  96
                         if (resource==null || resource.trim().equalsIgnoreCase("")) resource = clazz.getName()+".drl";
 103  96
                         return new DroolsSourceInterfaceImplementation<T>(clazz,resource);
 104  
                 }
 105  
         }
 106  
         
 107  
         private void validNullability(Object o,String msg) {
 108  252
                 if (o==null) {
 109  4
                         logger.error(msg);
 110  4
                         throw new IllegalArgumentException(msg);
 111  
                 }
 112  248
         }
 113  
         
 114  
         private <T> DroolsService getAnnotation(Class<T> clazz) {
 115  132
                 Annotation as[] = clazz.getAnnotations();
 116  140
                 for(Annotation a:as) {
 117  120
                         if (a.annotationType().equals(DroolsService.class)) return (DroolsService)a;
 118  
                 }
 119  20
                 return null;
 120  
         }
 121  
 }