Making A Javascript Function Available To Java Code
<h3>Problem Description</h3>

<p>A somewhat contrived example to illustrate my question.  Imagine we have some library of javascript functions that is already maintained and updated daily by an army of frontend devs.  To be specific, imagine one such function looks like this:</p>

<pre><code>function employeesForStore(store) {
    var dictionary = {
        "downtown": ["Joe", "Mary", "Steve"],
        "uptown": ["Jules", "Vincent", "Matt"],
        // and so on for hundreds of locations
    };
    return dictionary[store];
}
</code></pre>

<p><em>NOTE:</em> Please ignore the details of this function's implementation.  The actual function may be far more complex than simple JSON dictionary lookups, and assume we don't know any implementation details about the js function.  All we know is it takes a String argument and returns and array of Strings.</p>

<p>Now we would like to take advantage of this function in our Java code.  That is, in our Java code, we'd like to "load" this function, and then be able to call it multiple times, passing it <code>String</code> args and receiving <code>String[]</code> or <code>ArrayList&lt;String&gt;</code> results.</p>

<p>From searching SO and google so far, I understand that this will involve using:</p>

<ul>
<li><code>javax.script.ScriptEngineManager</code></li>
<li><code>javax.script.ScriptEngine</code></li>
<li>and perhaps <code>scriptEngine.getContext()</code> for passing values into the function and receiving results. </li>
</ul>

<p>I am a bit hazy on the details of the above, especially since most examples I've found involve running javascript code a single time, rather than making javascript function available to Java.  </p>

<h3>Example Code I'd Like To See</h3>

<ol>
<li>Assuming the js function is in the file "my_functions.js", load that file into Java so all of its functions will be available for use.</li>
<li>Call <code>employeesForStore("downtown")</code> and store its results in a native java <code>String[]</code> or <code>List&lt;String&gt;</code> in a variable called <code>downtownResults</code>.</li>
<li>Same as 2, but call <code>employeesForStore("uptown")</code> and store in variable <code>uptownResults</code></li>
</ol>
