Myna Embdedding

31 views
Skip to first unread message

Eric Charles

unread,
May 13, 2011, 11:36:14 PM5/13/11
to MynaJS-General
Hi,

I had [1] and [2] from a conversation with Tony Zakula on the way Myna
could be embedded in a JAVA application to run Javascript.

I see in JsCmd that a Myna Thread can be called and the Myna root file
must be provided.

- Is there a way to load the js files via classpath (the js files
being a jar artifact) ?
- Is there a way to instanciate Myna as plain object without going via
the MynaThread?
- I understand Myna uses Rhino. If I "simply" need to run JavaScript
from JAVA, what would be the Myna added value?

Tks,
- Eric

[1] http://www.mynajs.org/shared/docs/js/libOO/files/install-txt.html
[2] http://code.google.com/p/myna/source/browse/trunk/1.0/src/info/emptybrain/myna/JsCmd.java

Mark Porter

unread,
May 14, 2011, 11:23:07 AM5/14/11
to MynaJS-General
Myna is designed to dynamically compile jsvascript files from the
filesystem in a web environment. That said, it can actually compile
and run raw javascript text, or theoretically use resource URL's since
all file handling id via URL/URI.

MynaThread is as "plain" as it gets. It is the core runtime class that
does all the Rhino interaction. All of the application server related
code is actually in JS files, so that can be heavily modified without
editing MynaThread. For example, all the user management and
permissions can be removed.

What Myna adds over plain Rhino, is it's standard library. Here is a
quick example of a plain Rhino task made easier in Myna:

Converting a Java bean to a JSON object
---- Plain Rhino----
var bean = new org.custom.SomeClass();
var result={}
for (x in bean){
if (/^get./.test(x)){
try{
property=x.replace(/^get_?/,"");
property=property.substr(0,1).toLowerCase() + property.substr(1);
result[property] = bean[x]();
if (result[property] instanceof java.lang.String){
result[property] = result[property];
}

}catch(e){}
}
if (/^is./.test(x)){
try{
property=x.replace(/^is/,"");
property=property.substr(0,1).toLowerCase() + property.substr(1);
result[property] = bean[x]();
if (result[property] instanceof java.lang.String){
result[property] = Boolean(result[property]);
}

}catch(e){}
}
}

try{
//make sure to reverse the slashes for Windows, or make a URI first
var fstream = new java.io.FileWriter("/path/to/app/dir/jsonfiles/
filename.json");
var out = new java.io.BufferedWriter(fstream);
out.write(JSON.stringify(result));
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
} finally{
out.close()
}



---- Myna ----
Myna.applyTo(this);
var bean = new org.custom.SomeClass();
var result = JavaUtils.beanToObject(bean).toJson();
// here, "/" is the rootDir for Myna, and will work in UNIX and
Windows
new File("/jsonfiles/filename.json").writeString(result);


Myna also manages things like caching compiled JS code, and re-using
scopes to speed up repeated tasks


If you have no interest general purpose scripting, you can simplify
things by making your own Rhino runtime, or modifying Myna's JS
runtime. This will require either modifying Tony's code to not use
Myna's libraries, or load just enough of the Myna libs to get the code
to work. If I could see Tony's code I could probably give some
pointers on how big a task it would be convert.
> [2]http://code.google.com/p/myna/source/browse/trunk/1.0/src/info/emptyb...

Tony Zakula

unread,
May 14, 2011, 11:49:13 AM5/14/11
to mynajs-...@googlegroups.com
Yes, I will try to clean up some extraneous details in it and post it
somewhere this week.

Tony Z

> --
> You received this message because you are subscribed to the Google Groups "MynaJS-General" group.
> To post to this group, send email to mynajs-...@googlegroups.com.
> To unsubscribe from this group, send email to mynajs-genera...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mynajs-general?hl=en.
>
>

Tony Zakula

unread,
May 15, 2011, 10:46:05 PM5/15/11
to mynajs-...@googlegroups.com
Hi Eric and all,

I have posted some basic code for parsing emails using Mime4J and JavaScript at

https://bitbucket.org/tzakula/javascript-email-bounce-processor

Thanks.

Tony Z

Reply all
Reply to author
Forward
0 new messages