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.
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:
> 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.
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.
// 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:
> > 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.
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.
> // 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);
> /**
> * 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;
> }
> 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:
> > > 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.
On Tue, Oct 14, 2008 at 5:36 PM, KamBha <kamal.bh...@gmail.com> wrote:
> 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.
>> // 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);
>> /** >> * 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; >> }
>> 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:
>> > > 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.
> I could absolutely foresee a set of replaceable functions like:
> var Env = {
> readFile: function(file){},
> writeFile: function(file, str){},
> loadJS: function(file){},
> // etc.
> };
> and we could just swap them out based upon the platform.
> --John
> On Tue, Oct 14, 2008 at 5:36 PM, KamBha <kamal.bh...@gmail.com> wrote:
> > 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.
> >> // 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);
> >> /**
> >> * 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);
> >> }
> >> 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:
> >> > > 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.
If someone wants to use it for a server-side library than that's fine - but I don't see it progressing in that manner, explicitly. The functions that I defined in the Env object are *only* functions that would be used internally in env.js. I'd rather not include anything that isn't explicitly needed (much in the same way with jQuery).
On Tue, Oct 14, 2008 at 6:31 PM, Alex Robinson <solidgold...@gmail.com> wrote:
> John,
> Is env.js meant to be an all-encompassing server-side library or just > a tool to use on the server side?
> If the former, have you thought about talking to Peter Michaux about > where he's going with xjs and xpkg?
> If the latter, just how much utility functionality do you envisage > bolting on?
> On Oct 14, 10:53 pm, "John Resig" <jere...@gmail.com> wrote: >> I could absolutely foresee a set of replaceable functions like:
>> var Env = { >> readFile: function(file){}, >> writeFile: function(file, str){}, >> loadJS: function(file){}, >> // etc.
>> };
>> and we could just swap them out based upon the platform.
>> --John
>> On Tue, Oct 14, 2008 at 5:36 PM, KamBha <kamal.bh...@gmail.com> wrote:
>> > 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.
>> >> // 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);
>> >> /** >> >> * 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); >> >> }
>> >> 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:
>> >> > > 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.
Alex, env.js already includes code that writes an ajax post request to a file:// protocol to a local file. What we were talking about is isolating any code that cannot be written using the native javascript and encapsulate it into a simple API so that we can easily swap out platforms, eg rhino, spidermonkey, v8.
Thatcher
On Wed, Oct 15, 2008 at 1:08 PM, Alex Robinson <solidgold...@gmail.com>wrote:
> > I'd rather not include anything > > that isn't explicitly needed (much in the same way with jQuery).
> That sounds good to me - all this talk of adding writeFile etc had me > worried. Fight the bloat!
> Alex, env.js already includes code that writes an ajax post request to a
> file:// protocol to a local file. What we were talking about is isolating
> any code that cannot be written using the native javascript and encapsulate
> it into a simple API so that we can easily swap out platforms, eg rhino,
> spidermonkey, v8.
> Thatcher
> On Wed, Oct 15, 2008 at 1:08 PM, Alex Robinson <solidgold...@gmail.com>wrote:
> > > I'd rather not include anything
> > > that isn't explicitly needed (much in the same way with jQuery).
> > That sounds good to me - all this talk of adding writeFile etc had me
> > worried. Fight the bloat!
> On Oct 16, 10:13 am, "chris thatcher" <thatcher.christop...@gmail.com> > wrote: >> Alex, env.js already includes code that writes an ajax post request to a >> file:// protocol to a local file. What we were talking about is isolating >> any code that cannot be written using the native javascript and encapsulate >> it into a simple API so that we can easily swap out platforms, eg rhino, >> spidermonkey, v8.
>> Thatcher
>> On Wed, Oct 15, 2008 at 1:08 PM, Alex Robinson <solidgold...@gmail.com>wrote:
>> > > I'd rather not include anything >> > > that isn't explicitly needed (much in the same way with jQuery).
>> > That sounds good to me - all this talk of adding writeFile etc had me >> > worried. Fight the bloat!