Coverage Report - org.boretti.drools.integration.drools4.implementation.DroolsProviderImplementation
 
Classes in this File Line Coverage Branch Coverage Complexity
DroolsProviderImplementation
92 %
38/41
71 %
20/28
7
 
 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.NotAnnotatedInterfaceDroolsError;
 30  
 import org.boretti.drools.integration.drools4.implementation.DroolsInterfaceImplementation;
 31  
 import org.boretti.drools.integration.drools4.implementation.DroolsCompiledInterfaceImplementation;
 32  
 
 33  
 public class DroolsProviderImplementation {
 34  
         
 35  1
         private final Logger logger = Logger.getLogger(this.getClass());
 36  
         
 37  2
         private DroolsProviderImplementation() {}
 38  
         
 39  1
         private static final DroolsProviderImplementation instance = new DroolsProviderImplementation();
 40  
         
 41  
         public static final DroolsProviderImplementation getInstance() {
 42  27
                 return instance;
 43  
         }
 44  
         
 45  
         @SuppressWarnings("unchecked")
 46  
         public <T> T getService(Class<T> clazz) {
 47  27
                 if (logger.isDebugEnabled()) logger.debug("Gettting service for "+clazz);
 48  27
                 if (clazz==null) {
 49  1
                         String msg = "Received class is null";
 50  1
                         logger.error(msg);
 51  1
                         throw new IllegalArgumentException(msg);
 52  
                 }
 53  26
                 if (!clazz.isInterface()) {
 54  1
                         String msg = "Not an interface "+clazz;
 55  1
                         logger.error(msg);
 56  1
                         throw new NotAnInterfaceDroolsError(msg);
 57  
                 }
 58  25
                 Annotation as[] = clazz.getAnnotations();
 59  25
                 boolean valid=false;
 60  25
                 DroolsServiceType dst = null;
 61  25
                 String resource = null;
 62  26
                 for(Annotation a:as) {
 63  24
                         if (a.annotationType().equals(DroolsService.class)) {
 64  23
                                 valid=true;
 65  23
                                 DroolsService ds = (DroolsService)a;
 66  23
                                 dst=ds.type();
 67  23
                                 resource = ds.resourceName();
 68  23
                                 break;
 69  
                         }
 70  
                 }
 71  25
                 if(!valid) {
 72  2
                         String msg = "Invalid interface (missing annotation) "+clazz;
 73  2
                         logger.error(msg);
 74  2
                         throw new NotAnnotatedInterfaceDroolsError(msg);
 75  
                 }
 76  23
                 DroolsInterfaceImplementation<T> inter=null;
 77  23
                 if (dst==null) {
 78  0
                         String msg = "Type must not be null";
 79  0
                         logger.error(msg);
 80  0
                         throw new IllegalArgumentException(msg);
 81  
                 }
 82  23
                 if (DroolsServiceType.COMPILED.equals(dst)) {
 83  2
                         if (resource==null || resource.trim().equalsIgnoreCase("")) resource = clazz.getName()+".cdrl";
 84  2
                          inter = new DroolsCompiledInterfaceImplementation<T>(clazz, resource);
 85  21
                 } else if (DroolsServiceType.SOURCE.equals(dst)) {
 86  21
                         if (resource==null || resource.trim().equalsIgnoreCase("")) resource = clazz.getName()+".drl";
 87  21
                         inter = new DroolsSourceInterfaceImplementation<T>(clazz,resource);
 88  
                 }
 89  19
                 if (logger.isDebugEnabled()) logger.debug("Creating proxy using "+clazz+" from "+dst+" using resource "+resource);
 90  19
                 return (T) Proxy.newProxyInstance(clazz.getClassLoader(),new Class[] {clazz,DroolsInterface.class},new DroolsProxy<T>(clazz,inter));
 91  
         }
 92  
 }