Google Answers Logo
View Question
 
Q: Making clones of JavaScript-created Java objects in Java ( No Answer,   4 Comments )
Question  
Subject: Making clones of JavaScript-created Java objects in Java
Category: Computers > Programming
Asked by: jadon-ga
List Price: $50.00
Posted: 02 May 2006 20:27 PDT
Expires: 01 Jun 2006 20:27 PDT
Question ID: 724927
I originally tried to ask this question to a Rhino Usenet group, but
didn't get any responses.  See,

http://groups.google.com/group/netscape.public.mozilla.jseng/browse_frm/thread/f69fd9ee0c157fea

My question is how do I create a Java object from JavaScript that
allows you to getClass() to get the class, then newInstance() to
create a new object.  Yes, that is not exactly the same as my subject
line, but that is because I think it is about the same thing.

Ultimately, I'm trying to create an HttpServlet in JavaScript.  I can
already invoke Jetty from JavaScript by using Rhino's js.jar along
with the required jar files for Jetty.  I can configure an HttpServlet
written in Java from JavaScript and it works properly.  When I try to
extend the HttpServlet class using JavaScript, the object construction
seems to work, but I can't get Jetty to properly make an instance of
that object and respond to the HTTP queries.

To configure Jetty, you actually pass the class name you want to use
as a handler, not the object itself.  Jetty will then create an
instance of that class at initialization time.  I think this is
failing because there isn't an appropriate constructor for the class.

I've generated a simple example that does not use the Jetty jar files
to illustrate the issue.  The example simply requires that you invoke
Rhino's JavaScript shell.  Here is the code:

This works...

java_o1 = new java.lang.Thread;
java_c = java_o1.getClass();
java_o2 = java_c.newInstance();

This does not...

js_o = new Object;
js_o = { run: function() { print("got here"); } };
js_o.run();
java_o1 = new JavaAdapter(java.lang.Runnable, js_o);
// or java_o1 = new java.lang.Runnable(js_o);
java_o1.run();
java_c = java_o1.getClass();
java_o2 = java_c.newInstance();

I think this shows why the second example doesn't work:

js> for (i in java_c.constructors) { print("java_c.constructors[" + i +
"] = " + java_c.constructors[i]); }
java_c.constructors[0] = public
adapter1(org.mozilla.javascript.ContextFactory,org.mozilla.javascript.Scriptable)
java_c.constructors[1] = public
adapter1(org.mozilla.javascript.ContextFactory,org.mozilla.javascript.Scriptable,org.mozilla.javascript.Scriptable)

So, for the "reward", I want a good description or working example of
a clean solution to modify/construct a Java class to allow the
newInstance() method to work.  Ideally the solution would be entirely
in JavaScript and make only use of Java classes available from Sun's
J2EE and Mozilla's Rhino.  A Java source solution that can be invoked
from JavaScript would be okay, as long as you could provide enough of
the solution to show it was viable and not overly fragile to new
versions of Rhino.  Web links to FAQs, etc. that get me to the answer
are perfectly valid solutions.

Clarification of Question by jadon-ga on 03 May 2006 17:02 PDT
If this question is too hard, let me know how I might scale it back. 
I'd rather give someone the money for helping me continue down the
right path, but not quite answer the original question, than to have
the question go unresponded.  Please let me know if I can improve the
question.
Answer  
There is no answer at this time.

Comments  
Subject: Re: Making clones of JavaScript-created Java objects in Java
From: pik0-ga on 30 May 2006 16:34 PDT
 
I think that the problem lies in that JavaScript's access to a
Java-like object layer is limited. You're asking it do do things it
was never designed to do.

Better question? What are you trying to accomplish? This approach
seems like a long row to hoe. Perhaps it would be easier if you took
an Ajax approach? Have you heard about GWT:

   http://code.google.com/webtoolkit

GWT allows you to write plain Java code using the Google Web Toolkit
API to build a web-based (Ajax style) user interface. The supplied
cross-compiler then converts that Java code to JavaScript that is
modularly loadable from a web page.

It's amazing the kind of complex web pages you can develop in such a
short time using GWT.

Piko
Subject: Re: Making clones of JavaScript-created Java objects in Java
From: jadon-ga on 04 Jun 2006 06:36 PDT
 
I'm trying to create a programming environment where I can write
servlets in JavaScript.  I want a single-language solution for client
and server side development.  My preference for JavaScript is based on
the native support in web browsers.  The environment would be a
wiki-web, but it would allow you to add script on both the host and
target sides.

I'll check-out GWT and see if the compiler is reflective enough to be
used in place of a scripting language.

I've seen some tools that provide JavaScript on the host-side, but
they all seem pretty limited and the interface to the server didn't
seem as clean as javax.servlet.http.HttpServlet.
Subject: Re: Making clones of JavaScript-created Java objects in Java
From: jadon-ga on 26 Aug 2006 06:28 PDT
 
Sun released Phobos (https://phobos.dev.java.net/) which allows me to
create servlets in JavaScript, working-around my problem.
Subject: Re: Making clones of JavaScript-created Java objects in Java
From: jadon-ga on 04 Sep 2006 19:52 PDT
 
A more direct solution:
http://www.bluishcoder.co.nz/2006/05/server-side-javascript.html. 
Apparently, Jetty 6 includes a holder class that helps, along with
java.lang.reflect.Array.newInstance to create Java objects for calling
some native Java methods (though I'm not sure it is strictly
required).

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy