PHP on TomEE with Quercus (Caucho)


Caucho wrote for Resin Quercus. This is mainly (from a user point of view) a servlet managing PHP in a java container.

I first thought it was linked to Resin but in fact it is pretty generic and can be used in almost all containers (with sometimes some few limitations).

So i tried with my prefered container: TomEE.

I wanted to start with a maven war project then discovered quercus was not available in maven reporitories (at least last versions – 4.x.y). The expected development process (if i understood it correctly) is to deploy the quercus war then add php files in the exploded war.

As a workaround i used this pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.superbiz</groupId>
  <artifactId>wphp</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <build>
    <plugins>
      <!--
        quercus is available as a war so prepare the overlay manually
        Note: for real project install manually resin.jar
      -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.7</version>
        <executions>
          <execution>
            <id>download-quercus</id>
            <phase>initialize</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <target>
                <!-- download and extract -->
                <get src="${quercus.url}" dest="${quercus.war}" />
                <unzip src="${quercus.war}" dest="${work.dir}" />

                <!-- clean up a bit - API are in the container -->
                <delete>
                  <fileset dir="${work.dir}/WEB-INF/lib" includes="cdi*.jar"/>
                  <fileset dir="${work.dir}/WEB-INF/lib" includes="java*.jar"/>
                </delete>
              </target>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <!-- to test it quickly with mvn tomee:run -->
      <plugin>
        <groupId>org.apache.openejb.maven</groupId>
        <artifactId>tomee-maven-plugin</artifactId>
        <version>1.0.1-SNAPSHOT</version>
      </plugin>
    </plugins>
  </build>

  <properties>
    <quercus.version>4.0.25</quercus.version>
    <quercus.url>http://caucho.com/download/quercus-${quercus.version}.war</quercus.url>
    <quercus.war>${project.build.directory}/quercus.war</quercus.war>
    <work.dir>${project.build.directory}/${project.build.finalName}</work.dir>
  </properties>
</project>

This simply downloads quercus war and explodes it in the maven directory which will be used to build the war (i deleted Java EE APIs too since it was useless in TomEE).
In a real life project you’ll want to install quercus war to do a real overlay (or better: install resin library and configure yourself the quercus servlet). Nothing complicated so i’ll not detail it here.

Once this is done you can add all php files you want in src/main/webapp and then deploy your war. PHP files will be interpreted (and can be compiled if a licence file is added in quercus servlet configuration – see quercus documentation for more detail).

So fine you can execute PHP but you’re in a Java container! Quercus manages it, there are some linked between PHP and Java. I’ll just speak about one (but look quercus doc, it is much better than it): jndi_lookup(string). This method lets you lookup anything in the container JNDI tree.

By default TomEE deploys some EJBs. One of them is the TomEE Deployer. Its JNDI global name is ‘java:global/openejb/openejb/Deployer’ (we often use the shortname openejb/DeployerBusinessRemote ;)). From a quercus webapp you can query this EJB easily:

<?php
    $deployer = jndi_lookup('java:global/openejb/openejb/Deployer');
    echo 'Unique File = '.$deployer->getUniqueFile();
?>

For information you can lookup ‘java:app/BeanManager’ too and get access to CDI world very easily :).

Now the harder will be to keep the better of both Java (EE) and PHP worlds without bringing back the worse of them.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s