Monday March 19, 2007 Experiments with the JSR-223 compatible Quercus PHP engine
There's a lot of hype for all sorts of Java scripting engines at the moment. For myself I've made some experiments with groovy/grails. Today I've found the PHP Quercus engine from Caucho.com, which is licensed under GPL and I also tried to use it as a JSR-223 scripting engine. As the goal of the test, I want to generate some text from a PHP file, which prints out some PHP variables ($x, $y) in a PHP double quoted string.
Therefore I opened the downloadable *.war file from the Quercus 3.1 snapshot (alpha) with my ZIP tool and extracted:
in the /lib classpath of a newly created quercus.script.test Eclipse project.
Additionally I added a servlet-api.jar from a tomcat 6.x installation to the project's classpath.
The Quercus standard JSR-223 script engine is implemented in the package com.caucho.quercus.script.
Note: if you're not using JDK 1.6, but only JDK 1.5, you'll also need the javax.scripting.* JSR 223 (see link at the end of the blog entry). I also didn't install necessary additional *.jar libraries for Quercus MailModule and UnicodeModule classes.
The Test.java file below now creates the following output
hello world Hello - World Hasta la vista Baby
Test.java file (updated: it now reflects the comments from baennaeck)
package info.bliki.quercus.script.test;
import java.io.FileReader;
import java.io.StringWriter;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class Test {
public static void main(String[] args) {
ScriptEngineManager scriptManager = new ScriptEngineManager();
Object php2javaResult = null;
ScriptEngine phpEngine = scriptManager.getEngineByExtension("php");
ScriptContext context = phpEngine.getContext();
try {
context.setWriter(new StringWriter());
php2javaResult = phpEngine.eval("<?php echo \"hello world\"; ?>",
context);
StringWriter writer = (StringWriter) context.getWriter();
// show output from PHP script in console:
System.out.println(writer.toString());
} catch (Exception ex) {
ex.printStackTrace();
}
try {
context.setWriter(new StringWriter());
phpEngine.put("x", "Hello");
phpEngine.put("y", "World");
php2javaResult = phpEngine.eval("<?php echo \"$x - $y\"; ?>", context);
StringWriter writer = (StringWriter) context.getWriter();
System.out.println(writer.toString());
} catch (Exception ex) {
ex.printStackTrace();
}
try {// file test
context.setWriter(new StringWriter());
//redefine variable in ENGINE_SCOPE x,y:
phpEngine.put("x", "Hasta la vista");
phpEngine.put("y", "Baby");
// the following file contains the line: <?php echo "$x $y"; ?>
php2javaResult = phpEngine
.eval(new FileReader(
"C:\\temp\\echo_test.php"), context);
StringWriter writer = (StringWriter) context.getWriter();
System.out.println(writer.toString());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
BTW: the Quercus phpinfo() implementation gives the following output:
<h1>Quercus</h1><pre>PHP Version => 5.2.0 System => Windows XP 5.1 x86 Build Date => 20061220T1222 Configure Command => n/a Server API => CGI Virtual Directory Support => disabled Configuration File (php.ini) Path => WEB-INF/php.ini PHP API => 20031224 PHP Extension => 20041030 Debug Build => no Thread Safety => enabled Registered PHP Streams => php, file, http, https </pre>
Related links: