|
From: <php...@li...> - 2009-01-18 10:34:47
|
Hi Jost,
> "But I don't understand why you insist using your own test case..."
Sorry, that I didn't explain yet:
The actual reason is that my whole setup is different (which maybe of interest for as well):
I don't use a web environment at all.
I have my PHP script in a file and call PHP from the command line.
Java-Bridge is running standalone.
So, the initiator for all communication is PHP (and not Java using the script engine like you do).
It didn't work at the beginning, but after modyfing Java.inc (e.g. changing ports 8080 to 9267, creating SimpleHandler instead of HttpHandler) it worked.
At the beginning I thought that my code should be understandable/reproducable for you.
But after I realized that in order to do so, you have to modify Java.inc, it seems clear to me, that no one may have done it like this before.
Anyway, I understood something from your latest reply:
With "use the same procedure as above" you meant use it in PHP instead of Java, like this:
> String classA = "class A{function toString(){return '::A';} function invokeA($b){java('java.lang.reflect.Proxy')->getInvocationHandler($b)->invoke($b, "invokeB", array());}}\n";
(this wasn't the case in your first example)
I also think these two lines are not necessary anymore:
> interface IA { public void invokeA(IB ccb); };
> interface IB { public void invokeB(); };
My test case now works (also without the two lines above) - thanks so far.
But still my general question is not answered:
Why shouldn't it be possible to resolve the actual parameter $b to the actual PHP-object directly on the PHP-side without going back to Java?
I thought that if you call java_closure($b) and later a method gets invoked on $b then the bridge first resolves $b and the method gets invoked directly on the PHP-object $b.
Is there any way to avoid this round-trip?
Regards Jürgen
-----Ursprüngliche Nachricht-----
Von: php...@li... [mailto:php...@li...]
Gesendet: Samstag, 17. Januar 2009 19:45
An: php...@li...
Betreff: Re: [Php-java-bridge-users] How to wrap two PHP objects asjavaclosures,and in Java call a method on the first closure with the secondclosureas parameter?
Hi Juergen,
I really don't understand the problem. I have posted a test case which does what you want.
I do understand that you don't have the information to resolve the type, so that you must call the methods through the reflection API.
But I don't understand why you insist using your own test case, which cannot work, because you confuse the proxy with the invocation handler.
-------------------------------------
package test;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.OutputStreamWriter;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class TestGetInterface {
/**
* @param args
*/
public static void main(String[] args) throws Throwable {
//System.setProperty("php.java.bridge.default_log_file", "");
//System.setProperty("php.java.bridge.default_log_level", "3");
new TestGetInterface().doWork();
}
private ScriptEngine scriptEngine;
String classA = "class A{function toString(){return '::A';} function invokeA($b){java('java.lang.reflect.Proxy')->getInvocationHandler($b)->invoke($b, "invokeB", array());}}\n";
String classB = "class B{function toString(){return '::B';} function invokeB(){echo '::B';}}\n";
String test = "<?php "+classA+classB+" $thiz=java_context()->getAttribute('thiz');\n$thiz->call(java_closure(new A()), java_closure(new B())); ?>";
private void doWork() throws Throwable {
scriptEngine = new ScriptEngineManager().getEngineByName("php-invocable");
scriptEngine.put("thiz", this);
ByteArrayOutputStream out;
OutputStreamWriter writer;
scriptEngine.getContext().setWriter(writer = new OutputStreamWriter(out = new ByteArrayOutputStream()));
scriptEngine.eval(test);
((Closeable)scriptEngine).close();
writer.close();
if(!"::B".equals(out.toString())) {
System.err.println("test failed");
System.exit(1);
}
System.out.println("test okay");
System.exit(0);
}
interface IA { public void invokeA(IB ccb); };
interface IB { public void invokeB(); };
public void call(Object $cca, Object $ccb) throws Throwable {
((php.java.bridge.PhpProcedure)java.lang.reflect.Proxy.getInvocationHandler($cca)).invoke($cca, "invokeA", new Object[]{$ccb});
}
}
--------------
> So, what did you mean with "use the same procedure as above"?
Use the Java reflection API. The above example shows how to do this in PHP and in Java.
Regards,
Jost Boekemeier
|