Skip to content

Commit 17d074a

Browse files
b3nn0faiteanu
authored andcommitted
switch to rhino instead of nashorn for Java 17 compatibility
1 parent 80840e8 commit 17d074a

6 files changed

Lines changed: 162 additions & 58 deletions

File tree

.classpath

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@
2525
<classpathentry kind="lib" path="lib/org.htmlunit/htmlunit-core-js-3.2.0.jar"/>
2626
<classpathentry kind="lib" path="lib/org.htmlunit/htmlunit-cssparser-3.2.0.jar"/>
2727
<classpathentry kind="lib" path="lib/org.htmlunit/neko-htmlunit-3.2.0.jar"/>
28+
<classpathentry kind="lib" path="lib/rhino/rhino-1.7.14.jar"/>
2829
<classpathentry kind="output" path="bin"/>
2930
</classpath>

js/ariva.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,10 @@
44
// Hotfix 21.03.2023 Karl Heesch
55
// Hotfix 07.03.2024 @gnampf1
66

7-
try {
8-
load("nashorn:mozilla_compat.js");
9-
var prejava8 = false;
10-
var ArrayList = Java.type('java.util.ArrayList');
11-
var Logger = Java.type('de.willuhn.logging.Logger');
12-
} catch(e) {
13-
// Rhino
14-
var prejava8 = true;
15-
var ArrayList = java.util.ArrayList;
16-
};
7+
8+
var Logger = Packages.de.willuhn.logging.Logger;
9+
var ArrayList = java.util.ArrayList;
10+
1711
var fetcher;
1812
var webClient;
1913
var url;

js/finanzennet.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
try {
2-
load("nashorn:mozilla_compat.js");
3-
var prejava8 = false;
4-
var ArrayList = Java.type('java.util.ArrayList');
5-
6-
} catch(e) {
7-
// Rhino
8-
var prejava8 = true;
9-
var ArrayList = java.util.ArrayList;
10-
};
11-
importPackage(Packages.jsq.config);
1+
2+
var ArrayList = java.util.ArrayList;
3+
124
var fetcher;
135
var wc;
146

js/portfolioreport.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
var ArrayList = java.util.ArrayList;
2+
var Logger = Packages.de.willuhn.logging.Logger;
3+
4+
var fetcher;
5+
var webClient;
6+
7+
8+
var s,y1,m1,d1,y2,m2,d2;
9+
10+
function getAPIVersion() {
11+
return "1";
12+
};
13+
14+
function getVersion() {
15+
return "2024-02-25";
16+
};
17+
18+
function getName() {
19+
return "PortfolioReport";
20+
};
21+
22+
function getURL() {
23+
return "https://www.portfolio-report.net/";
24+
};
25+
26+
27+
28+
function prepare(fetch, search, startyear, startmon, startday, stopyear, stopmon, stopday) {
29+
Logger.info("prepare...");
30+
fetcher = fetch;
31+
s = search;
32+
y1 = startyear; m1 = startmon; d1 = startday;
33+
y2 = stopyear; m2 = stopmon; d2 = stopday;
34+
return new ArrayList();
35+
}
36+
37+
function process(config) {
38+
Logger.info("process...");
39+
var webClient = fetcher.getWebClient(false);
40+
41+
var page = webClient.getPage("https://api.portfolio-report.net/securities/search/" + s);
42+
var json = JSON.parse(page.getWebResponse().getContentAsString());
43+
var uuid = json[0]["uuid"];
44+
45+
var startDate = new Date(y1, m1, d1);
46+
47+
var start = startDate.toISOString().substring(0, 10);
48+
page = webClient.getPage("https://api.portfolio-report.net/securities/uuid/" + uuid + "/markets/XETR?from=" + start);
49+
var jsondata = page.getWebResponse().getContentAsString();
50+
51+
var data = JSON.parse(jsondata);
52+
53+
var currency = data["currencyCode"];
54+
55+
var res = new ArrayList();
56+
for (var i = 0; i < data["prices"].length; i++) {
57+
var price = data["prices"][i];
58+
var dc = new Packages.jsq.datastructes.Datacontainer();
59+
dc.put("currency", currency);
60+
dc.put("date", Packages.jsq.tools.VarTools.parseDate(price["date"], "yyyy-MM-dd"));
61+
dc.put("last", Packages.jsq.tools.VarTools.stringToBigDecimal(price["close"]));
62+
63+
//dc.put("first", Packages.jsq.tools.VarTools.stringToBigDecimal(record.get("Open")));
64+
//dc.put("last", Packages.jsq.tools.VarTools.stringToBigDecimal(price["close"]));
65+
//dc.put("low", Packages.jsq.tools.VarTools.stringToBigDecimal(record.get("Low")));
66+
//dc.put("high", Packages.jsq.tools.VarTools.stringToBigDecimal(record.get("High")));
67+
//dc.put("currency", defaultcur);
68+
res.add(dc);
69+
}
70+
fetcher.setHistQuotes(res);
71+
}
72+

lib/rhino/rhino-1.7.14.jar

1.32 MB
Binary file not shown.
Lines changed: 82 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
package jsq.fetcher.history;
22

33
import java.io.File;
4-
import java.io.FileInputStream;
5-
import java.io.InputStreamReader;
4+
import java.io.FileReader;
65
import java.util.Calendar;
76
import java.util.Date;
87
import java.util.List;
98

10-
import javax.script.Invocable;
11-
import javax.script.ScriptEngine;
12-
import javax.script.ScriptEngineManager;
13-
import javax.script.ScriptException;
14-
15-
import jsq.config.Config;
16-
import jsq.fetch.factory.Factory;
17-
189
import org.htmlunit.SilentCssErrorHandler;
1910
import org.htmlunit.ThreadedRefreshHandler;
2011
import org.htmlunit.WebClient;
12+
import org.mozilla.javascript.Context;
13+
import org.mozilla.javascript.Function;
14+
import org.mozilla.javascript.NativeJavaObject;
15+
import org.mozilla.javascript.Scriptable;
16+
import org.mozilla.javascript.ScriptableObject;
2117

22-
public class GenericJSFetcher extends BaseFetcher {
18+
import jsq.config.Config;
19+
import jsq.fetch.factory.Factory;
2320

24-
private Invocable inv;
25-
26-
21+
public class GenericJSFetcher extends BaseFetcher {
2722
private Calendar start;
28-
private Calendar stop;
23+
private Calendar stop;
24+
25+
private Scriptable scope;
26+
27+
private File scriptFile;
28+
private long modifiedTs;
2929

3030
/**
3131
* Create a new generic fetcher from the JavaScript file passed in {@code filename}.
@@ -34,48 +34,80 @@ public class GenericJSFetcher extends BaseFetcher {
3434
*/
3535
public GenericJSFetcher(String filename) throws Exception {
3636
try {
37-
File f = new File(filename);
38-
ScriptEngineManager manager = new ScriptEngineManager(null);
39-
ScriptEngine engine = manager.getEngineByName("nashorn");
40-
engine.put("fetcher", this);
41-
engine.eval( new InputStreamReader(new FileInputStream(f),"utf-8"));
42-
43-
inv = (Invocable) engine;
44-
37+
this.scriptFile = new File(filename);
38+
reloadScriptIfNeeded();
4539
} catch (Exception e) {
4640
e.printStackTrace();
4741
throw e;
4842
}
4943
}
44+
45+
private Context enterContext() {
46+
Context context = Context.enter();
47+
context.setLanguageVersion(Context.VERSION_ES6);
48+
context.setOptimizationLevel(-1);
49+
context.getWrapFactory().setJavaPrimitiveWrap(true);
50+
return context;
51+
}
52+
53+
private void reloadScriptIfNeeded() throws Exception {
54+
if (scriptFile.lastModified() == modifiedTs)
55+
return;
56+
modifiedTs = scriptFile.lastModified();
57+
58+
Context context = enterContext();
59+
60+
scope = context.initStandardObjects();
61+
Object jsFetcher = Context.javaToJS(this, scope);
62+
ScriptableObject.putProperty(scope, "fetcher", jsFetcher);
63+
64+
65+
// print function is not part of default rhino
66+
context.evaluateString(scope, "function print() { "
67+
+ "Packages.jsq.fetcher.history.GenericJSFetcher.print(Array.from(arguments).map((x) => x.toString()).join(' '))"
68+
+ "}", "", 1, null);
69+
70+
FileReader reader = new FileReader(scriptFile);
71+
context.evaluateReader(scope, reader, scriptFile.getName(), 1, null);
72+
73+
context.exit();
74+
}
75+
76+
public static void print(Object o) {
77+
System.out.println(o.toString());
78+
}
79+
5080
@Override
5181
public String getName() {
52-
return (String) callFunc("getName");
82+
return (String) callFunc("getName", null);
5383
}
5484

5585
@Override
5686
public String getURL() {
57-
return (String) callFunc("getURL");
87+
return (String) callFunc("getURL", null);
5888
}
5989

6090
public String getAPIVersion() {
61-
return (String) callFunc("getAPIVersion");
91+
return (String) callFunc("getAPIVersion", null);
6292
}
6393
public String getVersion() {
64-
return (String) callFunc("getVersion");
94+
return (String) callFunc("getVersion", null);
6595
}
6696

6797
@Override
6898
public void prepare(String search, int beginYear, int beginMon,
6999
int beginDay, int stopYear, int stopMon, int stopDay) throws Exception {
100+
reloadScriptIfNeeded();
101+
70102
super.prepare(search, beginYear, beginMon, beginDay, stopYear, stopMon, stopDay);
71103
start = Calendar.getInstance();
72104
start.setTime(getStartdate());
73105
stop = Calendar.getInstance();
74106
stop.setTime(getStopdate());
75107
try {
76-
Object x = inv.invokeFunction("prepare", this, search, beginYear, beginMon, beginDay, stopYear, stopMon, stopDay);
108+
Object x = callFunc("prepare", new Object[] { this, search, beginYear, beginMon, beginDay, stopYear, stopMon, stopDay });
77109
setConfig((List<Config>) x);
78-
} catch (NoSuchMethodException | ScriptException e) {
110+
} catch (Exception e) {
79111
e.printStackTrace();
80112
throw e;
81113
}
@@ -86,8 +118,8 @@ public void prepare(String search, int beginYear, int beginMon,
86118
public void process(List<Config> options) {
87119
super.process(options);
88120
try {
89-
inv.invokeFunction("process", options);
90-
} catch (NoSuchMethodException | ScriptException e) {
121+
callFunc("process", new Object[] { options} );
122+
} catch (Exception e) {
91123
// TODO Auto-generated catch block
92124
e.printStackTrace();
93125
}
@@ -111,20 +143,33 @@ public boolean within(Date d) {
111143
(d.getTime() <= getStopdate().getTime());
112144
}
113145

114-
public Object callFunc(String funcname) {
146+
public Object callFunc(String funcname, Object[] args) {
115147
try {
116-
return inv.invokeFunction(funcname);
117-
} catch (NoSuchMethodException | ScriptException e) {
148+
Context context = enterContext();
149+
if (args == null)
150+
args = new Object[0];
151+
Function f = (Function) scope.get(funcname, scope);
152+
Object result = f.call(context, scope, f, args);
153+
if (result instanceof NativeJavaObject)
154+
result = ((NativeJavaObject) result).unwrap();
155+
return result;
156+
} catch (Exception e) {
118157
e.printStackTrace();
119-
}
158+
} finally {
159+
Context.exit();
160+
}
120161
return null;
121162
}
122163
public void search(String string) {
123164
try {
124-
inv.invokeFunction("search", this, string);
125-
} catch (NoSuchMethodException | ScriptException e) {
165+
Context context = enterContext();
166+
Function f = (Function) scope.get("search", scope);
167+
f.call(context, scope, f, new Object[] { string });
168+
} catch (Exception e) {
126169
// TODO Auto-generated catch block
127170
e.printStackTrace();
171+
} finally {
172+
Context.exit();
128173
}
129174
}
130175
}

0 commit comments

Comments
 (0)