Running Quercus, A Pure Java PHP Engine, inside Weblogic Serverby Tim Hanson AbstractPHP is a very popular scripting language. BEA Weblogic Server is an application server built for Java. Wouldn't it be nice to have access to all of your Java resources from your PHP scripts? One way to do this is to use a PHP-Java bridge, where by Java and PHP run out of process from each other and communicate via XML. An interesting alternative is Quercus, an implementation of PHP that runs with Java code in the same process. This makes any Java resource available to PHP scripts, including EJBs, JMS Queues, database connection pools, and POJOs. About QuercusQuercus is a 100% pure Java implementation of PHP 5. The PHP engine behaves similarly to JSP. There is a PHP Servlet that accepts all requests for .php files. It is responsible for parsing and interpreting the page. Quercus supports most of the the standard PHP modules and many extensions, including PDF, PDO, MySQL, and JSON. Visit the Quercus wiki for a full list of supported extensions. Quercus also certifies an impressive list of open-source PHP applications that run on it. This includes MediaWiki, Drupal, and Gallery 2. A full list of certified applications and supported versions is also available. Installing and RunningDownload the Quercus WAR file. Extract the WAR file to a temporary directory, such as To deploy from the command line, first set the command line environment. Then, invoke the weblogic deployer. C:\MyDomain\bin> setDomainEnv C:\MyDomain> java weblogic.Deployer -user weblogic -password weblogic -url t3://localhost:7001/ -deploy -source c:\quercus If everything worked, it should now be running. To test, point your web browser to: http://localhost:7001/quercus/. Using Java Objects (POJOs)As you can see, running with a pure Java solution is very easy. It is also very easy to interact with any kind of Java object. Listing 1 provides an example. Listing 1.
<?php
$l = new Java("java.util.ArrayList");
$l->add($buf=new Java("java.lang.StringBuffer"));
$buf->append("100");
echo ($l->get(0)->toString()) + 2;
echo '<br />';
// get instance of Java class java.lang.System in PHP
$system = new Java('java.lang.System');
// demonstrate property access
echo 'Java version=' . $system->getProperty('java.version') . '<br />';
// java.util.Date example
$formatter = new Java('java.text.SimpleDateFormat',
"EEEE, MMMM dd, yyyy 'at' h:mm:ss a zzzz");
echo $formatter->format(new Java('java.util.Date'));
?>
Save this sample as As you can see from the listing, Java code is embedded right in with PHP. You should see something like this: 102 Java version=1.5.0_06 Thursday, June 04, 2007 at 12:33:56 PM Pacific Daylight Time Java classes are created with calls to the speical PHP class EJB UsageI have provided a sample EJB and a PHP client program. The download contains a PHP script, the EAR containing the Trader EJB, and the client jar. (The EAR file also contains source code for all the Java types.) To run the example:
You can see the source code for Listing 2. <html><head><title>Buy or Sell shares</title></head>
<body>
<?php
if (isSet($_POST['ticker'])) {
$trader = getBean();
if (isSet($_POST['buy'])) {
$result = $trader->buy($_POST['ticker'], ($_POST['shares'] + 0) );
$action = 'bought';
}
else {
$result = $trader->sell($_POST['ticker'], $_POST['shares'] + 0);
$action = 'sold';
}
$trader->remove();
print <<<__HTML__
<i>{$result->getNumberTraded()} shares of {$result->getStockSymbol()} $action.</i>
__HTML__;
}
else {
print <<<__HTML__
<form action="http://localhost:7001/JavaBridge/ejbref.php" method="post">
Ticker Symbol: <input type="text" size="4" name="ticker"/><p>
Number of Shares: <input type="text" size="6" name="shares"/><p>
<input type="submit" name="buy" value="buy"/> <input type="submit" name="sell" value="sell"/>
__HTML__;
}
function getBean() {
$home = jndi_lookup('TraderHome');
return $home->create();
}
?>
</body>
</html>
Notice the Deploying MediaWikiUsing Quercus, you can even deploy and run software such as MediaWiki, the wiki implementation behind Wikipedia. To deploy and run MediaWiki, do the following:
Listing 3. <web-app version="2.4">
<servlet>
<servlet-name>PHPServlet</servlet-name>
<servlet-class>com.caucho.quercus.servlet.QuercusServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PHPServlet</servlet-name>
<url-pattern>*.php</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.php</welcome-file>
</welcome-file-list>
</web-app>
As Listing 3 shows, the setup is as simple as registering the Quercus servlet to handle PHP files. Listing 4. <weblogic-web-app> <url-match-map>weblogic.servlet.utils.SimpleApacheURLMatchMap</url-match-map> </weblogic-web-app> That should take you to the wiki setup page. As long as you have MySQL configured and running it should all work. Download
SummaryThis article provided an overview of Quercus, a 100% pure Java implementation of PHP 5 and how to get it running on Weblogic Server. Hopefully, you have a sense of what it can do, and how it interacts with the server. References
Tim Hanson is the Javelin compiler architect at BEA Systems. Tim developed much of BEA's Java compiler - one of the earliest 1.5-compliant implementations. He has written numerous other compilers, including a CORBA/IDL compiler while at IBM, and an XQuery compiler. Return to Dev2Dev. |
Article Tools Related Products Check out the products mentioned in this article:Related Technologies Related Articles Bookmark Article
|