1 | package com.hammurapi.extract.scripting; |
2 | |
3 | import java.util.Arrays; |
4 | import java.util.Map; |
5 | import java.util.Set; |
6 | import java.util.TreeSet; |
7 | |
8 | import javax.script.ScriptEngine; |
9 | import javax.script.ScriptEngineFactory; |
10 | import javax.script.ScriptException; |
11 | |
12 | import com.hammurapi.extract.Extractor; |
13 | import com.hammurapi.extract.ExtractorBase; |
14 | import com.hammurapi.extract.ExtractorException; |
15 | import com.hammurapi.extract.ExtractorFactory; |
16 | |
17 | class ScriptingExtractor<T, V, C> extends ExtractorBase<T, V, C> { |
18 | |
19 | private boolean doArgs = true; |
20 | private Set<Integer> parameterIndices = new TreeSet<Integer>(); |
21 | private String[] parameterNames; |
22 | private ScriptEngineFactory sef; |
23 | private String code; |
24 | |
25 | public ScriptingExtractor(String code, ScriptEngineFactory sef, String[] parameterNames) { |
26 | super(0, null); // TODO cost from the factory? |
27 | for (String pn: parameterNames) { |
28 | if (pn!=null) { |
29 | doArgs = false; |
30 | } |
31 | } |
32 | |
33 | for (int i=0; i<parameterNames.length; ++i) { |
34 | if (doArgs || parameterNames[i]!=null) { |
35 | parameterIndices.add(i); |
36 | } |
37 | } |
38 | this.parameterNames = parameterNames; |
39 | this.sef = sef; |
40 | this.code = code; |
41 | } |
42 | |
43 | public boolean isContextDependent() { |
44 | return true; |
45 | } |
46 | |
47 | public Set<Integer> parameterIndices() { |
48 | return parameterIndices; |
49 | } |
50 | |
51 | ThreadLocal<ScriptEngine> setl = new ThreadLocal<ScriptEngine>() { |
52 | |
53 | @Override |
54 | protected ScriptEngine initialValue() { |
55 | return sef.getScriptEngine(); |
56 | } |
57 | }; |
58 | |
59 | @SuppressWarnings("unchecked") |
60 | @Override |
61 | protected V extractInternal(C context, Map<C, Map<Extractor<T, ? super V, C>, ? super V>> cache, T... obj) { |
62 | ScriptEngine engine = setl.get(); // Engine is requested once per thread. sef.getScriptEngine(); |
63 | if (doArgs) { |
64 | engine.put(ExtractorFactory.ARGS_BINDING, obj); |
65 | } else { |
66 | for (int i=0, l=Math.min(parameterNames.length, obj.length); i<l; ++i) { |
67 | if (parameterNames[i]!=null) { |
68 | engine.put(parameterNames[i], obj[i]); |
69 | } |
70 | } |
71 | } |
72 | engine.put(ExtractorFactory.CONTEXT_BINDING, context); |
73 | try { |
74 | return (V) engine.eval(code); |
75 | } catch (ScriptException e) { |
76 | throw new ExtractorException("Error evaluating '"+code+"'", e); |
77 | } |
78 | } |
79 | |
80 | @Override |
81 | public int hashCode() { |
82 | final int prime = 31; |
83 | int result = 1; |
84 | result = prime * result + ((code == null) ? 0 : code.hashCode()); |
85 | result = prime * result + (doArgs ? 1231 : 1237); |
86 | result = prime |
87 | * result |
88 | + ((parameterIndices == null) ? 0 : parameterIndices.hashCode()); |
89 | result = prime * result + Arrays.hashCode(parameterNames); |
90 | result = prime * result + ((sef == null) ? 0 : sef.hashCode()); |
91 | return result; |
92 | } |
93 | |
94 | @Override |
95 | public boolean equals(Object obj) { |
96 | if (this == obj) |
97 | return true; |
98 | |
99 | if (obj == null) |
100 | return false; |
101 | |
102 | if (getClass() != obj.getClass()) |
103 | return false; |
104 | |
105 | ScriptingExtractor other = (ScriptingExtractor) obj; |
106 | |
107 | if (code == null) { |
108 | if (other.code != null) |
109 | return false; |
110 | } else if (!code.equals(other.code)) |
111 | return false; |
112 | |
113 | if (doArgs != other.doArgs) |
114 | return false; |
115 | |
116 | if (parameterIndices == null) { |
117 | if (other.parameterIndices != null) |
118 | return false; |
119 | } else if (!parameterIndices.equals(other.parameterIndices)) |
120 | return false; |
121 | |
122 | if (!Arrays.equals(parameterNames, other.parameterNames)) |
123 | return false; |
124 | |
125 | if (sef == null) { |
126 | if (other.sef != null) |
127 | return false; |
128 | } else if (!sef.equals(other.sef)) { |
129 | return sef.getClass().equals(other.sef.getClass()) |
130 | && equal(sef.getEngineName(), other.sef.getEngineName()) |
131 | && equal(sef.getEngineVersion(), other.sef.getEngineVersion()); |
132 | } |
133 | return true; |
134 | } |
135 | |
136 | private boolean equal(String s1, String s2) { |
137 | return (s1==null && s2==null) || (s1!=null && s2!=null && s1.equals(s2)); |
138 | } |
139 | |
140 | @Override |
141 | public String toString() { |
142 | return getClass().getName()+"("+sef.getEngineName()+" v. "+sef.getEngineVersion()+", code '"+code+"', parameter indices "+parameterIndices+")"; |
143 | } |
144 | } |