|
From: <php...@li...> - 2006-11-20 15:36:44
|
Am Freitag, 17. November 2006 19:40 schrieb
php...@li...:
> However, you'll get a warning that the instance of
> MyClass is not a Java object. Use
>
> java_closure(new MyClass())
>
> instead.
OK, this is working now without the warning.
>
> > Invocable inv = (Invocable) engine;
> > inv.invokeMethod( engine.get("obj"), "saySomething",
> > new Object() );
>
> You will get a null pointer exception because obj is
> bound to null, see the warning above. Furthermore you
> have captured the top-level environment, but you
> probably want to invoke methods from MyClass().
Why did I capture the top-level environment? I thought with engine.get("obj")
I get the PHP object returned and so the method saySomething is invoked on
that object?
What is the correct syntax to invoke a method on MyClass()?
I tried this:
PhpScriptEngine engine = new PhpScriptEngine();
String s = "<?php \n";
s += "require_once 'MyClass.php'; \n";
s += "java_context()->setAttribute( 'obj', java_closure(new MyClass()),
100 ); \n";
s += "?>";
engine.eval( new StringReader(s) );
Invocable inv = (Invocable) engine;
inv.invokeMethod( engine.get("obj"), "saySomething", new Object() );
and it results in this error message:
Nov 20 10:55:20 JavaBridge ERROR: An exception occured: java.io.IOException:
Bad file descriptor
java.io.IOException: Bad file descriptor
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(FileOutputStream.java:260)
at java.io.ByteArrayOutputStream.writeTo(ByteArrayOutputStream.java:109)
at php.java.bridge.Response$Writer.flush(Response.java:125)
at php.java.bridge.Response.flush(Response.java:671)
at php.java.bridge.ClassicResponse.copyResponse(ClassicResponse.java:82)
at php.java.bridge.Request.handleSubRequests(Request.java:607)
at php.java.bridge.PhpProcedure.invoke(PhpProcedure.java:77)
at php.java.bridge.PhpProcedure.invoke(PhpProcedure.java:95)
at php.java.script.PhpScriptEngine.invokeMethod(PhpScriptEngine.java:204)
at Bootstrap.main(Bootstrap.java:39)
Bootstrap.java, line 39 is the last one showed above.
> [...]
>
> return
> ((Invocable)((PhpFacesContext)FacesContext.getCurrentInstance()).getScriptE
>ngine(this, new URL(script))).invoke(name, args);
>
> I think this is exactly what you want. The only
> difference is that the above method calls out to a URL
> instead of a local script file.
Yes, it sounds like, but actually I'm not sure how to do it. This faces things
are about the web framework, aren't they? So how do I transform this to a
J2SE app?
In the code example I can't see how to call a method on a previously
instanciated PHP object. It only uses the invoke Method with 2 parameters
(invokeFunction).
> > String script = "var obj = new Object();
> > obj.hello = function(name)
> > { print('Hello, ' + name); }";
> >
> > Any chance todo something similar with PHP?
>
> It is certainly possible to automatically call
> java_closure(), when a php object is passed to a Java
> procedure. But for several reasons we've decided to
> require java_closure(php_object). Furthermore, if I
> understand the above code correctly, the "new
> Object()" above instanciates a Java object, not an
> object from the js script interpreter.
No, it instanciates a JavaSript object which has one method called hello. But
if the only difference to this snippet and the possibiblities of the php-java
bridge is the automatic call to java_close(), it is similar enough for my
project.
Regards,
Verena
|