Calling SSJS from Java? - java

I have created an xPages application which uses a lot of server side javascript code functions located in a server side javascript library.
Now I have some java code located in the java design element which I would like to use to call the javascript functions.
I do understand that it is not logical to call javascript from java, but I guess that all server side javascript is compiled to java, so I was thinking that it might be possible to get a handle to the compiled java class that was generated.
any ideas?

You can create a value binding,
ValueBinding vb = FacesContext.getCurrentInstance().getApplication().createValueBinding("#{javascript:getData()}");
System.out.println(vb.getValue(FacesContext.getCurrentInstance()).toString());
This would call the getData() method from your SSJS library.

Related

Run and Save output of JavaScript from Java

I'm trying to figure out how to run some JavaScript from my Java class. I know that javax.script can be used to do this, but here's the kicker:
The JavaScript I want to run returns and displays a PDF in the browser. I'd like to store the PDF that's generated in a byte array or something like that so my class can do something with it later such as save it to disk.
So I guess the best approach would be to take a String that I have that contains the HTML and JavaScript. Then emulate a browser page, run it, then store the source? I'm not sure what library I would use to do something like this. How might I achieve this?
Focusing on your problem with javascript and java you can you this lib called Direct Web Remoting:
DWR is a RPC library which makes it easy to call Java functions from
JavaScript and to call JavaScript functions from Java (a.k.a Reverse
Ajax).
http://directwebremoting.org/

Not able to execute browser supported JS code in Java using Rhino

I am trying to execute the JS code in Java using ScriptEngine (Rhino).
I get the js code of 4 different js files at run time and store it in String variable and will invoke a method in js file using the below java code,
String result = (String)invocableEngine.invokeFunction("Execute");
I am able to see the Execute method is invoking correctly, but facing below two issues
Scriptengine is not able to recognize the double slash (//) value used as command line in the js file, so engine is not able to invoke the code after //
Also scriptengine is not able to recognize the browser parameters like window , navigator, etc,,
Is there any option to solve my issues?
The window, navigator, document objects and so on, are not part of the JavaScript language, but part of the DOM, which is a separate part of the web browser. The Rhino engine runs the pure JavaScript language but doesn't simulate a web browser.
To run the browser-dependent JavaScript as-is, you need additional code to mimic the browser APIs that are needed. If it needs only a few simple functions, you can easily write them yourself (in JavaScript). If it needs a more complete simulation of the browser and DOM, see Envjs, which does exactly this, and is designed for Rhino. (Note: I've never tried it myself, though.)
HtmlUnit or Selenium might also be useful to you, or even Greasemonkey. (It might be possible to give a more specific answer if you explain more about where this JavaScript code comes from and your goal in running it in this way.)
I'm not sure what you mean by "double slash (//) value used as command line". A double-slash introduces a comment, not a command. Any code between // and the end of the line isn't supposed to run.

read data from a java applet (preferably with js)

Here's my problem. I have an application that shows some KPIs... the problem is that it displays those KPIs through a Java applet (and I don't have access to it's source). I need only a few data from that applet. Is there a way to parse data from a Java applet? I would like to parse it with Javascript. Would that be possible?
It's possible to call Java applet methods from JavaScript (read), but it would have to be set up like this already. If you don't have access to the source, perhaps inspect Java console output and/or use 'jar' and 'javap' commands to inspect the classes to see if there is something you can call.

Converting JavaScript server to Java server

Well I don't know anything about JavaScript, so I ran into a small problem:
WorldServer = require("./worldserver"),
the previous code is JavaScript and I don't know what I should put for that in Java? Is it somekind of import or something?
what's that require("");
this sounds like node.js. in node.js, require() loads external scripts called "modules". In Java, you can use other Classes and packages by using import.

Execute javascript function from java

I want to know if it's possible to execute a javascript function from java. Basically I have a javascript function to which I want to pass a string and then get the output from that function.
If it's possible, how do I accomplish it?
EDIT -
I couldn't find a solution to this, what I did was re-write all the javascript code in Java. But I am guessing, Rhino ported on Android should do the trick. If someone has tried it out, please post a solution.
You probably want to take a look at the ScriptEngine. There are plenty of samples out there on how to use it. Works on anything but Mac where they for some reason selected to include AppleScript instead of JavaScript by default.
Edit: Take a look at this page, there seems to be a Rhino port for Android out there.
Javascript is not natively supported in java. If you need it, you may implement the Rhino javascript engine to do this.
"Rhino is an open-source implementation of JavaScript written entirely in Java. It is typically embedded into Java applications to provide scripting to end users."

Categories

Resources