Arun Gupta, Miles to go ...

Arun Gupta is a technology enthusiast, a passionate runner, and a community guy who works for Sun Microsystems.
« Previous day (Aug 22, 2007) | Main | Next day (Aug 24, 2007) »

http://blogs.sun.com/arungupta/date/20070823 Thursday August 23, 2007

PHP in GlassFish using Caucho Quercus

Quercus is Caucho Technology's 100% Java implementation of PHP 5. Ludo described the steps to deploy PHP web applications on GlassFish. Caucho has released a new version of Quercus since then. This blog entry is an update to the steps described earlier.

  1. First, PHP-enable GlassFish.
    1. Unjar quercus-3.1.1.war and copy the JAR files in "WEB-INF/lib" directory to "GLASSFISH_HOME/domains/domain/lib" directory. That's it! Although the original entry requires to copy the JARs in "GLASSFISH_HOME/lib/addons" directory but that didn't work.
  2. Create a PHP web application
    1. Create a new Web application project, lets say "hellophp", using NetBeans IDE and choose GlassFish as the server.
    2. Replace the contents of "web.xml" with the following fragment:

      <?xml version="1.0" encoding="UTF-8"?>
      <web-app xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        version="2.5">
        <description>Caucho Technology's PHP Implementation, Running on GlassFish Java EE 5</description>
        <servlet>
          <servlet-name>Quercus Servlet</servlet-name>
          <servlet-class>com.caucho.quercus.servlet.QuercusServlet</servlet-class>
        </servlet>
        <servlet-mapping>
          <servlet-name>Quercus Servlet</servlet-name>
          <url-pattern>*.php</url-pattern>
        </servlet-mapping>
        <welcome-file-list>
          <welcome-file>index.php</welcome-file>
        </welcome-file-list>
      </web-app>


      This will declare PHP engine as the servlet.
    3. Add a new page "index.php" in "Web pages" folder. The contents of the page are:

      <?php
      echo "Hello World!";
      phpinfo();
      ?>

      This page prints "Hello World!" on the browser and some configuration settings of PHP. The directory structure of the created project looks like:

      META-INF/
      META-INF/MANIFEST.MF
      WEB-INF/
      WEB-INF/classes/
      WEB-INF/sun-web.xml
      WEB-INF/web.xml
      index.jsp
      index.php

      Notice, "index.jsp" is only a template file to get started with JSPs and "sun-web.xml" is GlassFish-specific deployment descriptor. These files are not required for this PHP application although it does not hurt to leave them in the webapp as well.
  3. Deploy the application by right-clicking on the project and selecting "Deploy Project". Your first PHP application in GlassFish is now deployed at "http://localhost:8080/hellophp/index.php".

Now that you have verified that your GlassFish is ready to host PHP applications, try the different applications that are described in Ludo's blog.

Technorati: php glassfish caucho quercus

del.icio.us | furl | simpy | slashdot | technorati | digg |
|

TOTD #4: How to convert a Session EJB to a Web service ?

This TOTD describes how to convert a stateless session EJB to a Web service and uses information from this thread.

  1. Add @javax.jws.WebService annotation at the top of your EJB class. The modified code looks like:

    @javax.ejb.Stateless
    @javax.jws.WebService
    public class HelloSessionBean implements server.HelloSessionLocal {
        public String sayHello(String name) {
            return "Hello " + name + " from session bean";
        }
    }

    The new annotation is shown in this color.
  2. Re-build your project and redeploy it.

That's it!

There is no need to specify any additional deployment descriptor or parameters.The WSDL exposed by the EJB Web service endpoint is available at "http://localhost:8080/HelloSessionBeanService/HelloSessionBean?wsdl". The generated WSDL looks like:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.2-hudson-182-RC1. -->
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.2-hudson-182-RC1. -->
<definitions
  xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
  xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"
  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
  xmlns:tns="http://server/"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns="http://schemas.xmlsoap.org/wsdl/"
  targetNamespace="http://server/"
  name="HelloSessionBeanService">
  <wsp:UsingPolicy></wsp:UsingPolicy>
  <wsp:Policy wsu:Id="HelloSessionBeanPortBinding_sayHello_WSAT_Policy">
    <wsp:ExactlyOne>
      <wsp:All>
        <ns1:ATAlwaysCapability xmlns:ns1="http://schemas.xmlsoap.org/ws/2004/10/wsat" wsp:Optional="false"></ns1:ATAlwaysCapability>
        <ns2:ATAssertion xmlns:ns3="http://schemas.xmlsoap.org/ws/2002/12/policy"
                         xmlns:ns2="http://schemas.xmlsoap.org/ws/2004/10/wsat" ns3:Optional="true"
                         wsp:Optional="true"></ns2:ATAssertion>
      </wsp:All>
    </wsp:ExactlyOne>
  </wsp:Policy>
  <types>
    <xsd:schema>
      <xsd:import namespace="http://server/" schemaLocation="http://localhost:8080/HelloSessionBeanService/HelloSessionBean?xsd=1"></xsd:import>
    </xsd:schema>
  </types>
  <message name="sayHello">
    <part name="parameters" element="tns:sayHello"></part>
  </message>
  <message name="sayHelloResponse">
    <part name="parameters" element="tns:sayHelloResponse"></part>
  </message>
  <portType name="HelloSessionBean">
    <operation name="sayHello">
      <input message="tns:sayHello"></input>
      <output message="tns:sayHelloResponse"></output>
    </operation>
  </portType>
  <binding name="HelloSessionBeanPortBinding" type="tns:HelloSessionBean">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"></soap:binding>
    <operation name="sayHello">
      <wsp:PolicyReference URI="#HelloSessionBeanPortBinding_sayHello_WSAT_Policy"></wsp:PolicyReference>
      <soap:operation soapAction=""></soap:operation>
      <input>
        <soap:body use="literal"></soap:body>
      </input>
      <output>
        <soap:body use="literal"></soap:body>
      </output>
    </operation>
  </binding>
  <service name="HelloSessionBeanService">
    <port name="HelloSessionBeanPort" binding="tns:HelloSessionBeanPortBinding">
      <soap:address location="http://localhost:8080/HelloSessionBeanService/HelloSessionBean"></soap:address>
    </port>
  </service>
</definitions>

Few points to notice:

  1. A reasonable set of defaults are chosen for portType/@name, binding/@name, service/@name and even the soap:address/@location. Most of these values can be changed by specifying a different value in the @WebService annotation.

  2. Accordingly to EJB 3.0 specification, if @TransactionAttribute is not specified on the method then a default value of REQUIRED is applied. This default value is automatically converted to ATAlwaysCapability and ATAssertion policy assertions.

  3. Accordingly to Web Services for Java EE, Version 1.2, webservices.xml is optional so there is no need to write any other deployment descriptor.

  4. Be careful not to deploy a WAR file with the context root generated (HelloSessionBeanService) for the Web service endpoint. The EJB Web service endpoint will be inaccessible after that.

Please leave suggestions on other TOTD that you'd like to see. A complete archive is available here.

Technorati: totd webservices ejb glassfish

del.icio.us | furl | simpy | slashdot | technorati | digg |
|
« Previous day (Aug 22, 2007) | Main | Next day (Aug 24, 2007) »

Valid HTML! Valid CSS!

This is a personal weblog, I do not speak for my employer.