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.drools.RuleBase; |
30 | |
import org.drools.RuleBaseFactory; |
31 | |
import org.drools.compiler.PackageBuilder; |
32 | |
import org.drools.compiler.PackageBuilderConfiguration; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
class DroolsSourceInterfaceImplementation<T> extends |
39 | |
DroolsInterfaceImplementation<T> { |
40 | |
|
41 | 2 | private static final Logger logger = Logger.getLogger(DroolsSourceInterfaceImplementation.class); |
42 | |
|
43 | |
public DroolsSourceInterfaceImplementation(Class<T> clazz, |
44 | |
String resourceName) { |
45 | 120 | super(clazz, generateRuleBase(clazz, resourceName)); |
46 | 112 | } |
47 | |
|
48 | |
private static final RuleBase generateRuleBase(Class<?> clazz,String resourceName) { |
49 | 120 | RuleBase ruleBase = RuleBaseFactory.newRuleBase(); |
50 | |
try { |
51 | 120 | InputStream is = clazz.getResourceAsStream(resourceName); |
52 | 120 | if (is==null) { |
53 | 4 | String msg = "IO Exception, resource not found"; |
54 | 4 | logger.error(msg); |
55 | 4 | throw new IODroolsError(msg); |
56 | |
} |
57 | 116 | InputStreamReader isr = new InputStreamReader(is); |
58 | 116 | PackageBuilderConfiguration conf = new PackageBuilderConfiguration(); |
59 | 116 | conf.setClassLoader(clazz.getClassLoader()); |
60 | 116 | PackageBuilder builder = new PackageBuilder(conf); |
61 | 116 | builder.addPackageFromDrl(isr); |
62 | 116 | if (builder.getErrors()!=null) { |
63 | 116 | if (builder.getErrors().getErrors()!=null) { |
64 | 116 | if (builder.getErrors().getErrors().length>0) { |
65 | 4 | StringBuffer sp = new StringBuffer(); |
66 | 8 | for(org.drools.compiler.DroolsError er :builder.getErrors().getErrors()) { |
67 | 4 | logger.error("Drools compilation error :"+er.getMessage()); |
68 | 4 | sp.append(er.getMessage()+";\n"); |
69 | |
} |
70 | 4 | String msg = "Invalid Drools :"+sp.toString(); |
71 | 4 | logger.error(msg); |
72 | 4 | throw new CompilationDroolsError(msg,builder.getErrors()); |
73 | |
} |
74 | |
} |
75 | |
} |
76 | 112 | isr.close(); |
77 | 112 | ruleBase.addPackage(builder.getPackage()); |
78 | 0 | } catch (IOException e) { |
79 | 0 | String msg = "IO Exception "+e.getMessage(); |
80 | 0 | logger.error(msg); |
81 | 0 | throw new IODroolsError(msg,e); |
82 | |
} |
83 | |
|
84 | |
|
85 | |
|
86 | 0 | catch (Exception e) { |
87 | 0 | String msg = "Exception "+e.getMessage(); |
88 | 0 | logger.error(msg); |
89 | 0 | throw new DroolsError(msg,e); |
90 | 112 | } |
91 | 112 | return ruleBase; |
92 | |
} |
93 | |
|
94 | |
} |