1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
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.InputStreamReader; |
24 | |
|
25 | |
import org.apache.log4j.Logger; |
26 | |
import org.boretti.drools.integration.drools5.exceptions.CompilationDroolsError; |
27 | |
import org.boretti.drools.integration.drools5.exceptions.DroolsError; |
28 | |
import org.boretti.drools.integration.drools5.exceptions.IODroolsError; |
29 | |
import org.boretti.drools.integration.drools5.exceptions.XMLDroolsError; |
30 | |
import org.drools.RuleBase; |
31 | |
import org.drools.RuleBaseFactory; |
32 | |
import org.drools.compiler.PackageBuilder; |
33 | |
import org.drools.compiler.PackageBuilderConfiguration; |
34 | |
import org.xml.sax.SAXParseException; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
class DroolsXMLInterfaceImplementation<T> extends |
41 | |
DroolsInterfaceImplementation<T> { |
42 | |
|
43 | 2 | private static final Logger logger = Logger.getLogger(DroolsXMLInterfaceImplementation.class); |
44 | |
|
45 | |
public DroolsXMLInterfaceImplementation(Class<T> clazz, |
46 | |
String resourceName) { |
47 | 6 | super(clazz, generateRuleBase(clazz, resourceName)); |
48 | 2 | } |
49 | |
|
50 | |
private static final RuleBase generateRuleBase(Class<?> clazz,String resourceName) { |
51 | 6 | RuleBase ruleBase = RuleBaseFactory.newRuleBase(); |
52 | |
try { |
53 | 6 | InputStream is = clazz.getResourceAsStream(resourceName); |
54 | 6 | if (is==null) { |
55 | 2 | String msg = "IO Exception, resource not found"; |
56 | 2 | logger.error(msg); |
57 | 2 | throw new IODroolsError(msg); |
58 | |
} |
59 | 4 | InputStreamReader isr = new InputStreamReader(is); |
60 | 4 | PackageBuilderConfiguration conf = new PackageBuilderConfiguration(); |
61 | 4 | conf.setClassLoader(clazz.getClassLoader()); |
62 | 4 | PackageBuilder builder = new PackageBuilder(conf); |
63 | 4 | builder.addPackageFromXml(isr); |
64 | 2 | if (builder.getErrors()!=null) { |
65 | 2 | if (builder.getErrors().getErrors()!=null) { |
66 | 2 | if (builder.getErrors().getErrors().length>0) { |
67 | 0 | StringBuffer sp = new StringBuffer(); |
68 | 0 | for(org.drools.compiler.DroolsError er :builder.getErrors().getErrors()) { |
69 | 0 | logger.error("Drools compilation error :"+er.getMessage()); |
70 | 0 | sp.append(er.getMessage()+";\n"); |
71 | |
} |
72 | 0 | String msg = "Invalid Drools :"+sp.toString(); |
73 | 0 | logger.error(msg); |
74 | 0 | throw new CompilationDroolsError(msg,builder.getErrors()); |
75 | |
} |
76 | |
} |
77 | |
} |
78 | 2 | isr.close(); |
79 | 2 | ruleBase.addPackage(builder.getPackage()); |
80 | 0 | } catch (IOException e) { |
81 | 0 | String msg = "IO Exception "+e.getMessage(); |
82 | 0 | logger.error(msg); |
83 | 0 | throw new IODroolsError(msg,e); |
84 | 2 | } catch (Exception e) { |
85 | 2 | if (e instanceof SAXParseException) { |
86 | 0 | String msg = "XML Parsing Exception "+e.getMessage(); |
87 | 0 | logger.error(msg); |
88 | 0 | throw new XMLDroolsError(msg,e); |
89 | |
} |
90 | 2 | String msg = "Exception "+e.getMessage(); |
91 | 2 | logger.error(msg); |
92 | 2 | throw new DroolsError(msg,e); |
93 | 2 | } |
94 | 2 | return ruleBase; |
95 | |
} |
96 | |
|
97 | |
} |