Coverage Report - org.boretti.drools.integration.drools5.implementation.DroolsCompiledInterfaceImplementation
 
Classes in this File Line Coverage Branch Coverage Complexity
DroolsCompiledInterfaceImplementation
48 %
13/27
100 %
2/2
5
 
 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.implementation;
 20  
 
 21  
 import java.io.IOException;
 22  
 import java.io.InputStream;
 23  
 import java.io.ObjectInputStream;
 24  
 
 25  
 import org.apache.log4j.Logger;
 26  
 import org.boretti.drools.integration.drools5.exceptions.ClassCastDroolsError;
 27  
 import org.boretti.drools.integration.drools5.exceptions.DroolsError;
 28  
 import org.boretti.drools.integration.drools5.exceptions.IODroolsError;
 29  
 import org.drools.RuleBase;
 30  
 import org.drools.RuleBaseFactory;
 31  
 import org.drools.rule.Package;
 32  
 
 33  
 /**
 34  
  * @author mbo
 35  
  * 
 36  
  */
 37  
 class DroolsCompiledInterfaceImplementation<T> extends
 38  
                 DroolsInterfaceImplementation<T> {
 39  
         
 40  2
         private static final Logger logger = Logger.getLogger(DroolsCompiledInterfaceImplementation.class);
 41  
 
 42  
         public DroolsCompiledInterfaceImplementation(Class<T> clazz,
 43  
                         String resourceName) {
 44  8
                 super(clazz, generateRuleBase(clazz, resourceName));
 45  0
         }
 46  
 
 47  
         private static final RuleBase generateRuleBase(Class<?> clazz,String resourceName) {
 48  8
                 RuleBase ruleBase = RuleBaseFactory.newRuleBase();
 49  
                 try {
 50  8
                         InputStream is = clazz.getResourceAsStream(resourceName);
 51  8
                         if (is==null) {
 52  4
                                 String msg = "IO Exception, resource not found";
 53  4
                                 logger.error(msg);
 54  4
                                 throw new IODroolsError(msg);
 55  
                         }
 56  4
                         ObjectInputStream ois = new ObjectInputStream(is);
 57  0
                         Package packageDrools = (Package) ois.readObject();
 58  0
                         ruleBase.addPackage(packageDrools);
 59  0
                         ois.close();
 60  4
                 } catch (IOException e) {
 61  4
                         String msg = "IO Exception "+e.getMessage();
 62  4
                         logger.error(msg);
 63  4
                         throw new IODroolsError(msg,e);
 64  0
                 } catch (ClassNotFoundException e) {
 65  0
                         String msg = "Class Cast Exception "+e.getMessage();
 66  0
                         logger.error(msg);
 67  0
                         throw new ClassCastDroolsError(msg,e);
 68  0
                 } catch (Exception e) {
 69  0
                         String msg = "Exception "+e.getMessage();
 70  0
                         logger.error(msg);
 71  0
                         throw new DroolsError(msg,e);
 72  0
                 }
 73  0
                 return ruleBase;
 74  
         }
 75  
 
 76  
 }