|
From: <php...@li...> - 2009-08-12 11:25:36
|
Hi Jost & all, I have to build a standalone, multiplatform PHP application that needs to use a number of java classes in both the "business" logic (e.g. the cryptography package from Bouncy Castle) and the graphical user interface (3rd party UI components). While being proficient in PHP the same cannot be said for Java, so I've being researching a good "hybrid" solution to get the job done and am currently focusing on a Java Swing front-end calling a PHP-equipped Jetty (http://jetty.mortbay.org/jetty/) on localhost. Specifically, I'd like to visually create the GUI with the NetBeans IDE, register the appropriate Event Listeners and implement all non-trivial (or forcibly Java) event-handling code in PHP, and I am struggling to understand strengths and weaknesses of the available options ... following is the list of alternatives I've come up with until now: 1) Just make an HTTP request from the event method to a PHP script on the Jetty back-end, where PHP can interact via PJB with any needed Java classes or simply make use of all its built-in functionality to generate a response. This solution implies I have to manage all response data and interface navigation in Java; also, there won't be any continuation passing between the Swing front-end and the PHP back-end, resulting in a somewhat less efficient interaction; 2) Write Java code using JSR 223 and PJB to call PHP classes/methods on the Jetty back-end. Still need to write Java code, nonetheless it looks like a better solution performance-wise: problem is, to date I didn't manage to call PHP user functions/classes from such an application - I can run the phpversion() sample provided, but that's it. Probably my fault, anyway I'd welcome a more complex example; 3) Expand the example in http://php-java-bridge.sourceforge.net/pjb/standalone.php so the Swing application makes HTTP calls to Jetty's PHP scripts. Instead of returning a response like in 1), the PHP code interacts with appropriately exposed application methods. I believe one could, to a point, even control the interface navigation from PHP in this way. Are there other alternatives to consider (I may be missing even the simplest, most elegant solution)? Regards, Massimo |
|
From: <php...@li...> - 2009-08-12 17:41:52
|
Hi Massimo, > I have to build a standalone, multiplatform PHP application that needs > to use a number of java classes Is it a standalone web- or desktop application? Since you mention swing, I assume you want to write a desktop app. I would start by writing the Java GUI skeleton and then add PHP functionality to it, please see http://php-java-bridge.sourceforge.net/pjb/standalone.php for an example. *I* would write the entire application in java. PHP makes sense for short-lived scripts, but I wouldn't use it to drive the entire desktop application. -- I think PHP 5.3's new reference counter can reclaim certain cycles, but I wouldn't trust it. If you want to use PHP as a plugin only, use the JSR 223 API. > PHP-equipped Jetty (http://jetty.mortbay.org/jetty/) on localhost Why do you need jetty for a desktop application? Regards, Jost Boekemeier |
|
From: <php...@li...> - 2009-08-17 14:43:58
|
Hi Jost, thank you for the reply. > Is it a standalone web- or desktop application? It is a standalone desktop application, but several tasks will also interface a remote Web server, if/when a network connection is available. > Why do you need jetty for a desktop application? I found it helps a lot to give structure and order to the application(s). It takes care of potential CLASSPATH issues, it's easy to setup and start/stop, and has a small footprint. Last but not least, it keeps a door open for more advanced functionality I may need in the future - this is just the first of (hopefully) several applications. > If you want to use PHP as a plugin only, use the JSR 223 API. I am currently trying to connect the Swing application with Jetty-hosted PHP scripts via JSON-RPC, since the (RAW POST based) protocol is quite efficient and easy to implement, both in PHP and in Java. Sadly though, while I can readily access the RAW POST data via php://input and can also write the JSON string received from Java to a file, I see the fastcgi is unable to output the response ... looks like the communication channel is corrupted somehow as soon as I fopen it for reading. On the grounds that I may be violating the standard way of interacting with a servlet, I've been struggling the past few days to solve the problem by using java_context() methods and properties, but without success so far ... I'd appreciate some advice. Massimo |
|
From: <php...@li...> - 2009-08-17 16:45:29
|
Hi Massimo,
The servlet can handle POST, but I haven't tried to open the input stream.
However, if I understand you correctly, you want to invoke PHP application
methods from a java desktop application. Sure, you could use json or even
invent your own text-based communication protocol, but why not use the php
java bridge to handle the communication?
Here's a simple php script which acts as a receiver. It waits for java
requests and invokes methods from the closed-over environment:
<?php require_once("java/Java.inc");
class MyClass {
function hello() { return "hello"; }
function world() { return "world"; }
}
if (java_getHeader("X_JAVABRIDGE_INCLUDE", $_SERVER))
java_context()->call(java_closure(new MyClass()));
?>
And this is the Java part, which uses the jsr223 API to call the remote
methods:
import java.io.*;
import java.net.*;
import javax.script.*;
public class JsrTest {
public static void main(String s[]) throws Exception {
ScriptEngine e = new ScriptEngineManager().getEngineByName("php-invocable");
e.eval (new php.java.script.URLReader(new URL("http://localhost/jsrtest.php
")));
System.out.println(((Invocable)e).invokeFunction("hello", new Object[]{}));
System.out.println(((Invocable)e).invokeFunction("world", new Object[]{}));
((Closeable)e).close();
}
}
I have tested the above code using Java.inc from
http://php-java-bridge.sourceforge.net/pjb/java/Java.inc and JavaBridge.jar
from http://php-java-bridge.sourceforge.net/pjb/java/JavaBridge.jar.
Regards,
Jost Boekemeier
17. Aug 2009 4:44 nachm. schrieb am <
php...@li...>:
Hi Jost,
thank you for the reply.
> Is it a standalone web- or desktop application?
It is a standalone desktop application, but several tasks will also
interface a remote Web server, if/when a network connection is
available.
> Why do you need jetty for a desktop application?
I found it helps a lot to give structure and order to the
application(s). It takes care of potential CLASSPATH issues, it's easy
to setup and start/stop, and has a small footprint. Last but not least,
it keeps a door open for more advanced functionality I may need in the
future - this is just the first of (hopefully) several applications.
> If you want to use PHP as a plugin only, use the JSR 223 API.
I am currently trying to connect the Swing application with Jetty-hosted
PHP scripts via JSON-RPC, since the (RAW POST based) protocol is quite
efficient and easy to implement, both in PHP and in Java.
Sadly though, while I can readily access the RAW POST data via
php://input and can also write the JSON string received from Java to a
file, I see the fastcgi is unable to output the response ... looks like
the communication channel is corrupted somehow as soon as I fopen it for
reading.
On the grounds that I may be violating the standard way of interacting
with a servlet, I've been struggling the past few days to solve the
problem by using java_context() methods and properties, but without
success so far ... I'd appreciate some advice.
Massimo
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus
on
what you do best, core application coding. Discover what's new with
Crystal Reports now. http://p.sf.net/sfu/bobj-july
_______________________________________________
php-java-bridge-users mailing list
php...@li...
https://lists.sourceforge.net/lists/listinfo/php-java-bridge-users
|
|
From: <php...@li...> - 2009-08-18 09:47:55
|
Hi Jost, > Sure, you could use json or even invent your own text-based > communication protocol, but why not use the php java bridge > to handle the communication? > > Here's a simple php script which acts as a receiver. Thank for the advice, I will try your code. It may be me, but I would never have tried what you suggest; I can't find documentation about the several X_JAVABRIDGE_* constants and their intended usage, nor was I aware of the java_getHeader() function. Moreover, I still don't quite grasp how to use java_closure() and lastly, I believe several methods of the ScriptEngine API are still to be implemented ... is this correct? Massimo |
|
From: <php...@li...> - 2009-08-18 10:23:40
|
Hi Massimo, I will add a jsr223 tutorial to our web site. The bindings are complete. The reason for the new script-api.jar was a degression in version 5.4.4.2; I have cleaned up a little bit too much, leaving the standalone script engine w/o a urlreader path, even though it has been documented in the api doc. The path was re-introduced in 5.5.1. Please see the jsr223 api documentation for details. The X_ headers and the java_getHeader() are internal and shouldn't be used in general. The line I have given is an exception. You can wrap it in a function and use 'if called_from_java() java_context()-call(java_closure())' if you want. > I still don't quite grasp how to use java_closure( That's the core idea of the bridge. These calls never return but call others ad infinitum. Regards, Jost Boekemeier 18. Aug 2009 11:48 vorm. schrieb am < php...@li...>: Hi Jost, > Sure, you could use json or even invent your own text-based > communication protocol, but why not use the php java bridge > to handle the communication? > > Here's a simple php script which acts as a receiver. Thank for the advice, I will try your code. It may be me, but I would never have tried what you suggest; I can't find documentation about the several X_JAVABRIDGE_* constants and their intended usage, nor was I aware of the java_getHeader() function. Moreover, I still don't quite grasp how to use java_closure() and lastly, I believe several methods of the ScriptEngine API are still to be implemented ... is this correct? Massimo ------------------------------------------------------------------------------ Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day trial. Simplify your report design, integration and deployment - and focus on what you do best, core application coding. Discover what's new with Crystal Reports now. http://p.sf.net/sfu/bobj-july _______________________________________________ php-java-bridge-users mailing list php...@li... https://lists.sourceforge.net/lists/listinfo/php-java-bridge-users |
|
From: <php...@li...> - 2009-08-18 10:49:48
|
Hi again, > I will add a jsr223 tutorial to our web site. Great! > The bindings are complete. The reason for the new > script-api.jar was a degression in version 5.4.4.2; I have > cleaned up a little bit too much, leaving the standalone > script engine w/o a urlreader path, even though it has been > documented in the api doc. The path was re-introduced in 5.5.1. I will upgrade to the new release ASAP. Regards, Massimo |
|
From: <php...@li...> - 2009-08-17 17:00:48
|
You'll also need the php-script.jar from http://php-java-bridge.sourceforge.net/pjb/java/php-script.jar 17. Aug 2009 4:44 nachm. schrieb am < php...@li...>: > > > Hi Jost, > > thank you for the reply. > > > Is it a standalone web- or desktop application? ... |