Running Quercus, A Pure Java PHP Engine, inside Weblogic Server

by Tim Hanson
06/20/2007

Abstract

PHP 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 Quercus

Quercus 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 Running

Download the Quercus WAR file. Extract the WAR file to a temporary directory, such as C:\quercus (This assumes that you are running on Windows. For Unix, substitute all paths appropriately.) Next, deploy the extracted web application to a running Weblogic Server instance. Note that due to a limitation of the Quercus engine, it cannot be deployed as a war file. Quercus can be deployed via Weblogic Studio. You can download a trial version from the Workshop page on Dev2Dev. Alternatively, you can deploy via the console or the command line.

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. javatest.php demonstrating embedded Java access within PHP.

 

<?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 C:\quercus\javatest.php. Then point your web browser to http://localhost:7001/quercus/javatest.php.

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 Java. This also works for Java classes that are not normally instantiated like java.lang.System. Access to fields and methods of the Java object look just like PHP field and method access. Look here for more details.

EJB Usage

I 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:

  1. Put ejbref.php in c:\quercus.
  2. Put TraderClient.jar in c:\quercus\WEB-INF\lib.
  3. Deploy the Trader.ear file to the target WebLogic Server instance.
  4. Point your web browser to http://localhost:7001/quercus/ejbref.php

You can see the source code for ejbref.php in Listing 2. It loads a form, and when the user posts a request back, it instantiates the EJB on WebLogic Server.

Listing 2. ejbref.php

<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 jndi_lookup() used to interact with the JNDI directory, and the simple calls to the EJB with $trader->buy() and $trader->sell().

Deploying MediaWiki

Using Quercus, you can even deploy and run software such as MediaWiki, the wiki implementation behind Wikipedia. To deploy and run MediaWiki, do the following:

  1. Install and start MySQL
  2. Download and extract MediaWiki to c:\. I used version 1.9.3. Rename the directory 'wiki'
  3. Create a subdirectory called WEB-INF.
  4. Use an editor to create web.xml and weblogic.xml inside of WEB-INF from the templates below.
  5. Create a subdirectory of WEB-INF called lib.
  6. copy c:\quercus\WEB-INF\lib\* c:\wiki\WEB-INF\lib\*
  7. Copy the MySQL JDBC Driver jar into c:\quercus\WEB-INF\lib
  8. Deploy the web app.
  9. Point your browser to http://localhost:7001/wiki/

Listing 3. web.xml

<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.xml

<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

  • php-samples.zip - sample code and utilities for this article

Summary

This 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

  • Quercus - Home page for the Java open source PHP Engine
  • MediaWiki - Open source wiki written PHP used by wikipedia.
  • MySQL - Open source database often used in PHP apps.

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.