1 |
| package org.drools.semantics.java; |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| import java.io.IOException; |
44 |
| import java.util.HashMap; |
45 |
| import java.util.HashSet; |
46 |
| import java.util.Iterator; |
47 |
| import java.util.Set; |
48 |
| |
49 |
| import org.codehaus.janino.ByteArrayClassLoader; |
50 |
| import org.codehaus.janino.Scanner; |
51 |
| import org.codehaus.janino.Java.CompileException; |
52 |
| import org.codehaus.janino.Parser.ParseException; |
53 |
| import org.codehaus.janino.Scanner.ScanException; |
54 |
| import org.drools.rule.RuleSet; |
55 |
| import org.drools.spi.Functions; |
56 |
| import org.drools.spi.ImportEntry; |
57 |
| import org.drools.spi.Importer; |
58 |
| import org.drools.spi.RuleBaseContext; |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
| public class JavaFunctions |
68 |
| implements |
69 |
| Functions |
70 |
| { |
71 |
| |
72 |
| private String text; |
73 |
| |
74 |
| private transient Class functionsClass; |
75 |
| |
76 |
| private RuleSet ruleSet; |
77 |
| |
78 |
| private String className; |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
| private static final String LINE_SEPARATOR = System.getProperty( "line.separator" ); |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
1
| public JavaFunctions(RuleSet ruleSet,
|
104 |
| int id, |
105 |
| String text) throws CompilationException, IOException, ClassNotFoundException |
106 |
| { |
107 |
1
| this.text = text;
|
108 |
1
| this.ruleSet = ruleSet;
|
109 |
| |
110 |
1
| this.className = "Function_" + id;
|
111 |
| |
112 |
1
| compile();
|
113 |
| } |
114 |
| |
115 |
1
| private void compile() throws IOException, CompilationException, ClassNotFoundException
|
116 |
| { |
117 |
1
| RuleBaseContext ruleBaseContext = ruleSet.getRuleBaseContext( );
|
118 |
1
| ClassLoader classLoader = (ClassLoader) ruleBaseContext.get( "java-classLoader" );
|
119 |
| |
120 |
1
| if ( classLoader == null )
|
121 |
| { |
122 |
1
| ClassLoader cl = (ClassLoader) ruleBaseContext.get( "smf-classLoader" );
|
123 |
| |
124 |
1
| if ( cl == null )
|
125 |
| { |
126 |
1
| cl = Thread.currentThread( ).getContextClassLoader( );
|
127 |
1
| ruleBaseContext.put( "smf-classLoader",
|
128 |
| cl ); |
129 |
| } |
130 |
| |
131 |
1
| if ( cl == null )
|
132 |
| { |
133 |
0
| cl = JavaCompiler.class.getClassLoader( );
|
134 |
0
| ruleBaseContext.put( "smf-classLoader",
|
135 |
| cl ); |
136 |
| } |
137 |
| |
138 |
1
| classLoader = new ByteArrayClassLoader( new HashMap( ),
|
139 |
| cl ); |
140 |
| } |
141 |
| |
142 |
| |
143 |
1
| Set imports = new HashSet();
|
144 |
1
| Importer importer = ruleSet.getImporter();
|
145 |
| |
146 |
1
| try
|
147 |
| { |
148 |
1
| ImporterClassBodyEvaluator classBody = new ImporterClassBodyEvaluator(importer, this.className, new Scanner(null, new java.io.StringReader(this.text)), classLoader);
|
149 |
0
| this.functionsClass = classBody.evaluate();
|
150 |
| } |
151 |
| catch ( Scanner.LocatedException e ) |
152 |
| { |
153 |
1
| throw new CompilationException( ruleSet,
|
154 |
| null, |
155 |
| this.text, |
156 |
1
| e.getLocation( ) != null ? e.getLocation( ).getLineNumber( ) : -1,
|
157 |
1
| e.getLocation( ) != null ? e.getLocation( ).getColumnNumber( ) : -1,
|
158 |
| e.getMessage( ) ); |
159 |
| } |
160 |
| |
161 |
0
| ruleBaseContext.put( "java-classLoader",
|
162 |
| this.functionsClass.getClassLoader() ); |
163 |
| } |
164 |
| |
165 |
| |
166 |
| |
167 |
| |
168 |
| |
169 |
| |
170 |
0
| public String getText()
|
171 |
| { |
172 |
0
| return this.text;
|
173 |
| } |
174 |
| |
175 |
0
| public Class getFunctionsClass() throws CompilationException, IOException, ClassNotFoundException
|
176 |
| { |
177 |
0
| if (functionsClass == null)
|
178 |
| { |
179 |
0
| compile();
|
180 |
| } |
181 |
0
| return functionsClass;
|
182 |
| } |
183 |
| |
184 |
| |
185 |
| |
186 |
| |
187 |
| |
188 |
| |
189 |
0
| public String getSemantic()
|
190 |
| { |
191 |
0
| return "java";
|
192 |
| } |
193 |
| |
194 |
| } |