Message from discussion
Loading Scripts in the Env
MIME-Version: 1.0
Received: by 10.100.229.14 with SMTP id b14mr5478anh.17.1224020178179; Tue, 14
Oct 2008 14:36:18 -0700 (PDT)
Date: Tue, 14 Oct 2008 14:36:18 -0700 (PDT)
In-Reply-To: <b99491f30810140725o412a9e88sb3c104df12309c10@mail.gmail.com>
X-IP: 59.167.240.58
References: <4ae4d9f6-ac11-4ff3-b6dd-41b7ab0daab7@26g2000hsk.googlegroups.com>
<9920268e-d37d-4851-9d15-745d8b3c7ca3@q26g2000prq.googlegroups.com>
<b99491f30810140725o412a9e88sb3c104df12309c10@mail.gmail.com>
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.17)
Gecko/20080829 Firefox/2.0.0.17,gzip(gfe),gzip(gfe)
Message-ID: <724240c3-7eb7-4bbd-8ce0-bd6bbdba8564@s20g2000prd.googlegroups.com>
Subject: Re: Loading Scripts in the Env
From: KamBha <kamal.bh...@gmail.com>
To: "Env.js" <envjs@googlegroups.com>
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
That's what I thought. I was going to do something similar, but I was
told not to (my manager thought it was a bit dodgy to reveal the
context within a running script).
John mentioned that he wants env.js to work independent of Rhino and I
think this is a worthwhile goal. Maybe instead of revealing Rhino
functions, we should create our own object that gets included when
env.js is run and anyone who wants to support env.js can build the
functionality in whatever language/Javascript engine they are using.
To me, that sounds like a better approach.
On Oct 15, 1:25 am, "chris thatcher" <thatcher.christop...@gmail.com>
wrote:
> This is how I create my shell, which is in turn used but my servlet
> enviromnent. As you can see I used to inject the xhr implementation but now
> I use the one provided by env. Note I also chose to expose three built in
> rhino methods, load, print, and runCommand(which does nothing right now). I
> also added an implementation of a FileListener so I could start the servlet
> container and change my js without having to restart the container.
>
> Hope this helps. I think I based this off an example I found on the web
> googling arround.
>
> Thatcher
>
> package claypool.continuation;
>
> import java.io.FileInputStream;
> import java.io.FileNotFoundException;
> import java.io.FileReader;
> import java.io.File;
> import java.io.IOException;
> import java.io.InputStreamReader;
> import java.io.BufferedReader;
>
> import org.mozilla.javascript.*;
>
> public class Shell extends ScriptableObject implements FileListener{
>
> private Context cx;
> private String basePath;
> private FileMonitor monitor = new FileMonitor (3000);
>
> public Shell(String basePath) {
> this.basePath = basePath;
>
> cx = Context.enter();
> cx.initStandardObjects(this);
>
> // host objects --------------
>
> // Give easy access to the global object by making a global property
> named "global".
> // This is the same as how "window" is used in browser scripting.
> defineProperty("global", this, ScriptableObject.DONTENUM);
> defineProperty("window", this, ScriptableObject.DONTENUM);
> defineProperty("cwd", basePath, ScriptableObject.DONTENUM);
>
> // global functions
> String[] names = {"load", "print", "runCommand"};
> defineFunctionProperties(names, Shell.class,
> ScriptableObject.PERMANENT);
> //Add the XMLHttpRequest Implementation
> //JsXMLHttpRequest.register(this);
> //JsSimpleDomNode.register(this);
>
> monitor.addListener(this);
> }
>
> public String getClassName() {
> return "global";
> }
>
> public Context getContext() {
> return cx;
> }
>
> public static void print(String msg){
> System.out.println(msg);
> }
> public static void runCommand(String command){
> //TODO
> return;
> }
> /**
> * Load and execute a set of JavaScript source files.
> *
> * This method is defined as a JavaScript function.
> */
> public static void load(Context cx, Scriptable thisObj,
> Object[] args, Function funObj)
> {
> Shell shell = (Shell)getTopLevelScope(thisObj);
> for (int i = 0; i < args.length; i++) {
> shell.processFile(Context.toString(args[i]));
> }
> }
>
> /**
> * Load a JavaScript file.
> *
> * @param filename the JavaScript file to load
> */
> public void loadFile( String filename) {
> System.out.println("Loading absolute file : " + filename + " from
> path : " + basePath);
> Object[] args = {filename};
> Shell.load(cx, this, args, null);
> //this.processAbsoluteFile(filename);
> }
>
> /**
> * A convinence method to call a global JavaScript function.
> *
> * @param methodName the global JavaScript function to be called
> * @param args the arguments for the JavaScript function
> */
> public Object callGlobalFunction(String methodName, Object[] args) {
> return ScriptableObject.callMethod(this, methodName, args);
> }
>
> private void processFile(String relativeFileName){
> String absoluteFileName = this.basePath + relativeFileName;
> this.processAbsoluteFile(absoluteFileName);
> }
> /**
> * Evaluate JavaScript source file.
> *
> * @param filename the name of the file to evaluate
> */
> private void processAbsoluteFile(String absoluteFileName) {
> System.out.println("Loading Script: " + absoluteFileName);
> FileReader in = null;
> try {
> in = new FileReader(absoluteFileName);
> // reloads changed files
> monitor.addFile (new File (absoluteFileName));
> }
> catch (FileNotFoundException ex) {
> Context.reportError("Couldn't open file \"" + absoluteFileName +
> "\".");
> return;
> }
>
> try {
> // Here we evalute the entire contents of the file as a script.
> cx.evaluateReader(this, in, absoluteFileName, 1, null);
> }
> catch (WrappedException we) {
> System.err.println(we.getWrappedException().toString());
> we.printStackTrace();
> }
> catch (EvaluatorException ee) {
> System.err.println("js: " + ee.getMessage());
> }
> catch (JavaScriptException jse) {
> System.err.println("js: " + jse.getMessage());
> }
> catch (IOException ioe) {
> System.err.println(ioe.toString());
> }
> finally {
> try {
> in.close();
> }
> catch (IOException ioe) {
> System.err.println(ioe.toString());
> }
> }
>
> }
>
> public void fileChanged (File file) {
> //Because the Context/Thread relationship the smartest thing I could
> //think to do is modify/touch WEB-INF/web.xml to force a reload.
> File webXml = new File(this.basePath + "/WEB-INF/web.xml");
> webXml.setLastModified(new java.util.Date().getTime());
> }
>
>
>
> }
> On Tue, Oct 14, 2008 at 3:37 AM, KamBha <kamal.bh...@gmail.com> wrote:
>
> > Is there such a thing as a load function in an embedded environment?
> > Last time I played with a load function using embedded Rhino, I got
> > nothing. If you are using load in an embedded environment, can someone
> > give me some pointers as to what I did wrong? I did end up finding a
> > way of creating a load function, but it involved me revealing the
> > Context object to script and it became all rather sinister.
>
> > On Oct 7, 2:06 pm, Thatcher <thatcher.christop...@gmail.com> wrote:
> > > I'd like to suggest that the something along the lines of the
> > > following be added to the windows 'location' setter:
>
> > > xhr.onreadystatechange = function(){
> > > ..
> > > scripts = document.getElementsByTagName('script');
> > > for(i=0;i<scripts.length;i++){
> > > script = scripts[i];
> > > try{
> > > load(script.attributes['src']);
> > > }catch(e){
> > > print(e);
> > > }
> > > }
> > > ..
>
> > > the reason being that I'd like the Env.js to load the javascript so I
> > > can easily run and include the files of my choosing, the same way I
> > > would in a browser. The above example is obviously a little naive
> > > since it doesnt bother to look at the type of script.
>
> > > To be a little more needy I'd actually like a little more control of
> > > the initialization of the Env.js window, so that I could perhaps
> > > specify a callback to override the default script loading strategy
> > > with one of my own so that I can easily implement Jaxer like behavior.
>
> > > I already do this, but have to modify env.js each time it loads up.
>
> > > Any thoughts here?
>
> > > PS I do have some Java I've written to integrate env.js into Tomcat or
> > > Jetty and do Jaxer like stuff on the server. The code is a little
> > > specific to the particular project I've been developing, but it might
> > > make more sense to generalize it by discussion in this group, and
> > > contribute it to the Env project.
>
> > > Thatcher
>
> --