Showing posts with label Webservices. Show all posts
Showing posts with label Webservices. Show all posts

Thursday, March 08, 2007

Integrating Java and PHP: the Web Services way

Recently on a project, we were faced with the question:
  1. do we put the business logic in the web tier (which was written in PHP 4.7.3) or
  2. do we write the business logic in Java (better tools for development, reliable libraries for some of the tasks we wanted to perform as a part of our business logic, better OO language features than PHP, easy to debug in an IDE, code hidden in class files).
The first option was what we usually call the model 1 architecture, where business logic is written in the same script which serves the presentation code.

In the second approach, we were getting to the point of introducing the MVC pattern to our PHP based web tier by introducing a Java layer to play the model and let PHP scripts be controller and view. This had the added advantage that we could leverage the model written as Java web services in other clients (and we did have other clients than web interface too for our product which provided an alternate interface for user/program to interact with our product and hence we required that the business logic we write for our web interface could be leveraged in other client types too, written in other languages, possibly, which in our case was C/C++). Hence, we opted for the 2nd choice.

We had two options for integrating Java and PHP:
  1. Do it using PHP/Java bridge
  2. Expose the business logic written in Java as web services.
The bridge option is not well supported as of this writing in the PHP world. The Java extension for PHP language is labeled as experimental. So we decided to go with the other option.

Our business logic did not necessitate the use of EJBs (did not require container managed transactions or persistence) for the present, so going the Java path to write our business logic, we wanted to use Tomcat as web container (which could easily be integrated with the Apache web server, running the existing PHP code, using mod_jk(2)).

We developed our web services in Java using the latest JAX-WS 2.1 RI library, primarily because JAX-WS spec makes writing web services in Java pretty easy as compared to the predecessor JAX-RPC 1.1 release (aided by annotations, no more of XML hell). We also made a design choice to use the document/literal style of messaging for maximum flexibility. Here's how a typical JAX-WS code looks:

MyWebServiceImpl.java:

@WebMethod
public MyClassA myWebMethod(
@WebParam(name = "aParam") int aParam) throws MyAppExceptionA, MyAppExceptionB { //.. implementation code }


We chose to use the nusoap 0.7.2 as the PHP web service stack and here's how a typical WS client was written in PHP using nusoap:
client.php:

<?php
require_once('../lib/nusoap.php');

// create a soapclient from wsdl url
$wsdl = 'http://192.168.1.101:8080/MyWebservices/MyWebServiceImpl?wsdl';
$client = new soapclient($wsdl, true);
$err = $client->getError();
if ($err) {
// Display error
}

// specify the soap body content for doc/literal request
$msg = "<web:mywebmethod xmlns:web="\"http://webservice.mycompany.com/\"">
<aparam>3334</aparam>
</web:mywebmethod>";

$namespace = 'http://webservice.mycompany.com/';
$method = 'myWebMethod'; // web method to invoke

$result = $client->call($method, $msg, $namespace, '', false, null, 'document', 'literal');

// Check for a fault
if ($client->fault) {
// Here is how we can do exception handling.
$myAppExceptionA = $result['detail']['MyAppExceptionA'];
if ($myAppExceptionA) {
print "Exception: ". $myAppExceptionA['message'];
}

$myAppExceptionB = $result['detail']['MyAppExceptionB'];
if ($myAppExceptionB) {
print "Exception: ". $myAppExceptionB['message'];
}
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error - You will not come here in working condition :)
} else {
// Display the result
print_r($result);
// Grab the values in the $result array
}
}
?>

Nusoap stack on PHP end will convert the result in an associative array (as shown in the case of fault handling in the code above). To know the structure of response message to get more clarity on how to extract the data out of result, you can add the following 3 magic debug statements towards the end of the above PHP client code:

echo "<h2>Request</h2><pre>" . htmlspecialchars($client->request, ENT_QUOTES) . "</pre>";
echo "<h2>Response</h2><pre>" . htmlspecialchars($client->response, ENT_QUOTES) . "</pre>";
echo "<h2>Debug</h2><pre>" . htmlspecialchars($client->debug_str, ENT_QUOTES) . "</pre>";

I also used the SoapUI tool (version 1.6) for unit testing my web services and found it to be very useful, especially when we needed to know the exact soap request body content which we needed to embed on the PHP side while invoking the web method. You can do several things with this tools, like generate request templates which you can fill with values to test individual web methods, save such requests and do regression testing later, save the requests to do load testing, view test reports etc.


That's in short about how we can get PHP web tier to invoke methods on a Java web service based business logic tier. I had initially had some trouble in figuring out the way to pass the right arguments to the soapclient() method of nusoap library and after some web searching we could identify how to get it working.

As an aside, this was my first experience of working with PHP language and in my self learning mode i used WAMP distribution (version WAMP5_1.6.5 which includes PHP 5.1.6, Apache 2.0.59, MySQL 5.0.24a and phpmyadmin 2.8.2.4) to quickly setup an environment on my Windows workstation for experiments. I also found the EasyEclipse for PHP (version 1.2.1.1), a very useful IDE for PHP development. And a book i use as reference (only when i need to know how a certain programming thing is done in PHP) is Programming PHP by Kevin Tatroe, Rasmus Lerdorf, and Peter MacIntyre. Its excellent book and has stood by me till date.

The opinion i have formed about PHP till now is that its ideal for a model 1 architecture web application. Its better in such cases than say Servlets/JSP duo because of its simplicity in terms of programming constructs. It simply does not have those advanced features of JSP tag libraries. So for developers who are not fussy about mixing their HTML code with some PHP scripts and have a previous background in Perl or some other such dynamic languages, will be more at ease with PHP than say Java development. Java is object oriented language (a hybrid one though like C++) but PHP is object based (ie PHP has construct for defining classes as Perl but it does not support inheritance, polymorphism etc). So most of the PHP code is more or less procedural, at least thats what is more commonly done (like you are programming in C and creating libraries of functions). Also PHP eases the learning curve as its a dynamic language and does not have standard data types other than arrays (indexed and associative) and scalars. Of course you can define your ADTs but most of the time you will be good working with arrays alone. The arrays in PHP are like a mix of Java's ArrayList and HashMap ie you can use an array as an indexed or associative one. So, in general, i feel if there is a simple web application where all that has to be done is create a catalog out of a database and then allow CRUD using web forms then PHP (and hence model 1 architecture) is suitable, since it gets the work done quicker and resulting code is maintainable. But if you want to support multiple client types (hence model 1 architecture is not what you want) and probably need some advanced middleware services (like persistence, security, transactions) then you are better off with Java EE and other such frameworks.

Share your thoughts by leaving your comments.

Wednesday, August 23, 2006

Learning BPEL

After having passed the SCDJWS certification i was a bit confused as to what next to learn to gain more mileage with Webservices. For sometime i simply kept idlying my time at the javaranch.com webservices forum trying to answer some questions and keeping in touch and testing the depth of my knowledge on webservices. Well that process still continues but in the meanwhile i got to know about BPEL for WS orchestration and it got me interested. I found only one book which addressed only BPEL and not the whole paraphernalia of webservices which i had already learned/read in the course of getting certified. This was the book Business Process Execution Language for Webservices 2nd Edition by Matjaz Juric et al. Also i found this article from Sun quite informative.

I have today got the book and will update this post with a review of how i find it. Java ranch has a review on this book too and my first impression is - it covers exactly as much as i would want to read on BPEL to get my hands dirty. It covers both theory for BPEL 1.1 and practicals of how to develop using a BPEL engine - Oracle Business Process Manager 10.1.2. Though it also addresses the practical aspects of another BPEL engine from Microsoft - the BizTalk Server 2004 in the chapter 7 (the last chapter). Book starts with an overview of SOA and BPEL and an overview of competitors of BPEL. It also introduces the different WS-* standards which BPEL standard builds on. Then it delves into BPEL theory for the next 2 chapters. Then it covers the practicals of developing using Oracle BPM and JDeveloper 10g + Eclipse 3.1 for the next 2 chapters. Lastly it covers Microsoft BizTalk server 2004. There is an appendix of BPEL 1.1 syntax. Its not a big book (only 350 pages).

Tuesday, July 04, 2006

Preparing for SCDJWS

SCDJWS 1.4 is Sun Certified Developer for Java Web Services certification exam. I started preparing for this exam after taking the SCJP 5.0 exam earlier in May 2006. This exam covers alot of specifications (XML 1.0, XML Schema 1.0, WS-I BP 1.0a, J2EE 1.4 for WS 1.1, WSDL 1.1, SOAP 1.1, UDDI 2.0, JAX-RPC 1.1 to name a few). You can see the complete list at Valentin Crettaz's site. There isnt any certification guide available as of this writing for this exam so i followed the advices of people who have already taken this certification exam at JavaRanch SCDJWS Forum and following is my approach to prepare for the exam.

Again, as has become a standard practice for me, i started with a word document where i copy pasted the exam objectives and wrote down the reference material (name of the book and chapters) for each topic on the exam. In summary, following are the resources i am referring:
  • J2EE WebServices by Richard Monson-Haefel (RMH): all chapters + appendices E, F & G. I did not read the F and G from here but referred to MZ's notes for SwA with SAAJ.
  • Sun's Blueprint book - Designing Webservices using J2EE 1.4: whole book
  • Mikalai Zaikin's (MZ) WSD Notes and Quiz You will need to buy the quiz (~95+ questions) from Mikalai. I referred to the Security (chapter 8) and a section on SwA with SAAJ (a section in chapter 2) from MZ's notes. I went through all quiz questions once, a night before the exam.
  • XYZWS.COM - provides a free mock exam (in all 200 questions collection).
  • XSLT and TrAX from Elliot Rusty Harold's (ERH) excellent book Processing XML with Java.
  • Java Webservices Tutorial (WST) for tutorial on JAXB (chapters 1 & 2)

14th Jul 2006 - I am through with all chapters from the RMH book and am mid-way through the Blueprint book. I have to go back to RMH to cover appendices E, F & G which cover SwA, SAAJ with Attachments and JAX-RPC & SwA. I also have to cover the ERH book's XSLT/TrAX, MZ's notes + WS-Security and WST for JAXB/Security. This might take me another week. I plan to complete the syllabus by 21st Jul 2006. After this i will need another week's time to revise my notes and practice more on the code.

I have created some sample programs from RMH's book (on topics like XSD, XML namespaces, JAX-RPC JSE/EJB Endpoints and clients Generated stubs/Dynamic Proxies/DII, JAXR, JAXP DOM/SAX).

I practiced the JAXB samples (first 5 only) as discussed in the WST. The exam does not require one to know

customizing the JAXB bindings so i did not cover those topics. For security, i did not do any programming but just read the concepts from MZ's notes.

---
10th Aug, 2006 - I passed the exam (CX-310-220) with 88%. If you need my study notes (305 pages covering whole syllabus completely) the download it here. You can read my final tips to the ranchers on how to prepare at javaranch.com.

---
15th Mar, 2007 - I noticed that a few people (infact just 2, one wrote to me a thank you email and another mentioned of referring to my notes at Javaranch forum ) have used my SCDJWS 1.4 notes while preparing for the certification exam. If i could have posted my notes on a personal web site like MZ then probably more people will have been able to use it. Presently the single 305 pages pdf file might seem daunting to many. So setting up a web site is on my list of to-dos.

Popular micro services patterns

Here are some popular Microservice design patterns that a programmer should know: Service Registry  pattern provides a  central location  fo...