views:

436

answers:

3

I have three types of get requests that are delivered to a class file on web application from a mobile device. Because the mobile device provides no cookies, the log file hit only has

in.ter.nal.ip   ser.ver.i.p:port 2009-06-05 09:14:44 GET /applicationname/mobiledevicexml reqtype=login&userid=xx### 200 87 - MercuryMobile/1.0 CFNetwork/342.1 Darwin/9.4.1 cookieArrayLength=0;

If I can instantiate javascript in my class file, and generate a javascript function call to urchinTracker() from inside the class file, I can replace that useless cookieArrayLength=0; with some useful data urchin can read from the log file into analytics reports. We have been looking at scripting in Java with Rhino; Safari Bookshelf has:

Scripting in JavaTM: Languages, Frameworks, and Patterns

which helped us immediately demo that we can run javascript in class files --this works out-of-the-box on Java 6.

Anyone know any resources for scripting with Rhino on Java 1.5 or 1.4?

Alternately, any suggestions for running javascript from java 1.5 would be appreciated.

+1  A: 

The Java Scripting API (javax.scripting) package was introduced in Java 6, so that will not be available in Java 1.4 or 5. As the default installation, Java SE 6 comes with a stripped down version of Mozilla Rhino which is interfaced through javax.scripting.

However, Mozilla Rhino itself does not require Java 6. From the requirements page:

Recent versions of Rhino have only been tested with JDK 1.4 and greater. Older versions support JDKs as early as 1.1.

Therefore, to use Rhino, it appears that Java 1.4 is actually sufficient.

As for resources, the documentation for Rhino seems to have a lot of information. In particular, the Embedding Rhino section might be useful to see how the scripting will work.

Of course, the lack of the javax.scripting package means that interfacing to Rhino itself is going to require the use of the Rhino API rather than the Java 6 native scripting API, but I would guess that the functionality is going to be fairly similar. The only downside I can see is, if in the future, Java 6 is going to be supported on the target platform and/or using another language, it may necessitate a rewrite to use the Java Scripting API rather than directly supporting Rhino.

coobird
thanks for the resource links; we are able to use the stripped-down Rhino in Java 6 via javax.scripting to compile test.js (javascript) to class file and call tests() locally but have not been successful in compiling the __utm.js and calling urchinTracker() compiling from source and trying again, but at the moment it looks like /users/58787/unknown-google may be right in comment below.
jonhwilliams
We followed Rhino docs to run JavaScript interpreter from Java 1.4 and 1.5.
jonhwilliams
A: 

See Server-side JavaScript for list of projects that runs JavaScript at the server-side.

For your usage, using Rhino seems like the way to go.

eed3si9n
+1  A: 

[I'm posting in an answer, because I don't have enough points to post a comment on the question itself.]

Are you sure that the urchinTracker() function will operate outside of a web browser? Running the Rhino JavaScript interpreter (which isn't too difficult) won't be enough if the function relies on various browser objects, like the Document Object Model (DOM) or XmlHttpRequest.

I suggest that you at least scan the internals of the urchinTracker() function to see if this is the case.

James Tikalsky
not sure yet, but was unable this morning to pass values via urchinTracker() through javax.scripting (Java 6 test environment) so it is unlikely to work on dev. will know by tomorrow...
jonhwilliams
This is correct. __utm.js has dependancies on various browser objects.Thanks!
jonhwilliams
Just wanted to point out the envjs project: "envjs is a pure JavaScript browser environment that runs in Rhino."Blog Posting: http://ejohn.org/blog/bringing-the-browser-to-the-server/Discussion Group: http://groups.google.com/group/envjsSource Repository: http://github.com/thatcher/env-js/tree/master
James Tikalsky