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.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 | |
|
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 | |
} |