Java and JavaScript, Living Together!
“Dogs and Cats, living together… mass hysteria!”
One of the very interesting features of a JavaScript stage in Fusion (and of Java 8/Nashorn, in general) is the ability to intermingle the two languages. By way of example, I offer the following JavaScript stage. In this case, we’re pulling from an geolocation process, and creating a query from the resulting attributes. You’ll notice at the top that we declare several Java classes, and then when we go to JSON, presto!, we switch over to JavaScript and run through our list, feeding the values into a java.util.ArrayList, and then we switch back to Java and iterate over the list and build our query. We could have used a regular old JavaScript Array, but in this case it makes a textbook illustration how the two languages can interact as one in the context of the Java ScriptEngine.
function (request, response) { // java imports var e = java.lang.Exception; var URL = java.net.URL; var BufferedReader = java.io.BufferedReader; var InputStreamReader = java.io.InputStreamReader; var String = java.lang.String; // local declarations var result = new String(""); var ArrayList = java.util.ArrayList; try { var url = "https://mapservices.gis.saccounty.net/arcgistest/rest/services/ADDRESS_wSubAddress/GeocodeServer/findAddressCandidates?outFields=*&outSR=4326&f=pjson&Single+Line+Input=799+G+St"; var targetUrl = new URL(url); var ins = new BufferedReader(new InputStreamReader(targetUrl.openStream())); var inputLine = ""; var list = new ArrayList(); while ((inputLine = ins.readLine()) !== null) { //logger.info(inputLine); // if you want to see what is on each line result += inputLine; } ins.close(); logger.info("Return RESULT: " + result); // full result // convert to JSON using JavaScript var json = JSON.parse(result); logger.info(" ** JSON CREATED OKAY ** " + JSON.stringify(json)); // get candidates var candidates = json.candidates; logger.info("Candidates: " + candidates.length); for (var i = 0; i < candidates.length; i++) { var candidate = candidates[i]; var refId = candidate.attributes.Ref_ID; list.add(refId); logger.info("ADD REF: " + refId); } var query = ""; logger.info("*** BUILD QUERY ** "); // run through the list and build your query. if (list.size() > 0) { var count = 0; for each (var ref in list) { if (count > 0) { query += " OR "; } // @TODO: CHANGE THE FIELD TO QUERY TO AN EXISTING FIELD IN YOUR SCHEMA query += "FIELD_TO_QUERY:" + ref; count++; } // print out the query in the logs logger.info("**** QUERY: *** " + query); // add to response response.initialEntity.appendStringList("solr_query", Java.to(query, Java.type('java.util.List'))); } } catch (e) { logger.error(e); } } |
Why is this so odd? Well, here’s the thing: Java and JavaScript, despite sharing the similarity of the word “java” are really two entirely different beasts.
Java is a strictly-typed, procedural language (albeit with a few functional traits). JavaScript, on the other hand, is a loosely-typed functional language, with more in common with languages like LISP and Scala. So to see these two playing nice in the same sandbox is certainly a treat, and a powerful tool, indeed.