MIT NextLab Cloud Maestro Services Development Guide

materialisticrampantInternet and Web Development

Nov 10, 2013 (3 years and 11 months ago)

108 views


Author: Cloud maestro team
Version: 02




MIT NextLab

Cloud Maestro

Services Development Guide


Author: Cloud Maestro team
Version: 02 1


Contents
Service Development Environment ...................................................................................................................... 3
WSF/Spring Setup .............................................................................................................................................. 3
WSF/Spring hello world tutorial ........................................................................................................................ 3
WSF/Spring Development Environment ......................................................................................................... 7
Suggested IDE .................................................................................................................................................... 7
Eclipse .................................................................................................................................................................. 7
netBeans .............................................................................................................................................................. 7
WSO2/PHP Server Setup .................................................................................................................................. 8
WSO2/PHP Development Environment .......................................................................................................... 8
Common Programming Guide .............................................................................................................................. 9
Naming Web services ........................................................................................................................................ 9
Java coding conventions ................................................................................................................................. 10
PHP Coding conventions ................................................................................................................................. 12
Service Deployment Guide .................................................................................................................................. 14
Java Web Service Deployment ...................................................................................................................... 14
PHP Web Service Deployment ....................................................................................................................... 14
Apendix: Web services interoperability sample ........................................................................................... 15








Author: Cloud Maestro team
Version: 02 2




Author: Cloud Maestro team
Version: 02 3
Service Development Environment

WSF/Spring Setup
Server Requirements:
• Java SE Development Kit (JDK): 1.5.0_22
• Servlet container: Apache Tomcat 5.5.28

Client Requirements:
• JDK 5.0 Update 22 (If you have JDK 6, please either downgrade to JDK 5.0 Update 22
or install JDK 5.0 Update 22 and update the JAVA_HOME and PATH.)
• Spring Framework v 2.0.7 or higher [Get it from Cloud Maestro]

W
SF/Spring v1.0
or higher
[Get it from Cloud Maestro]








WSF/Spring hello world tutorial
In this small tutorial, we go through six steps describing how to setup a greeting service using
Wso2 web services framework within Java’s popular MVC framework, spring. To speed up
development we will be using
Eclipse Java EE IDE. This
tutorial is based on Tharindu Mathew‘s
“Hello World with WSF/Spring” tutorial, it is adapted to cloud maestro environment.



Author: Cloud Maestro team
Version: 02 4


1. Create project

Create a new dynamic web project, give it a name, select Dynamic web module version 2.4 and
choose minimal configuration.
Go to your java build path, make sure that
you’re using jdk 1.5, if you have a different
version use add Library > Add JRE
System library > browse jdk’s
installation path.








2. Add source libraries and configuration files
Copy WSF/Spring’s WEB-INF/lib jars to the project WEB-INF/lib directory.
Copy Spring jars (spring.jar + spring-tomcat-weaver.jar) into project WEB-INF/lib.
Copy axis2Config.xml to WEB-INF.









Author: Cloud Maestro team
Version: 02 5

3. Write the greeting service functions

4. Configure web.xml

We will register the application context using ContextLoaderListener and setup the
servlet class, name and mappings.
package

mit.nextlab.example;


public class Greeting {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String sayHello(String name) {
return name + " says hello to you!!!";
}
}

<
display
-
name
>
wsfSpringExample
</
display
-
name
>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<servlet>
<servlet-name>axis2</servlet-name>
<servlet-class>
org.wso2.spring.ws.servlet.SpringAxis2Servlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>axis2</servlet-name>
<url-pattern>/services/*</url-pattern>

</
servlet
-
mapping
>



Author: Cloud Maestro team
Version: 02 6



5. Configure applicationContext.xml
Let’s create Spring configuration file, add into project WebContent an XML file and
name it applicationContext.xml
We will first import Axis2 configuration file, define our greeting service bean and finally
configure bean properties.


6. Export application.
Right click your project > Export > Export WAR file
Access WSDL1.1 by typing a pattern such as:
http://{host}: {port}/ {webapp}/services/ {serviceName} ? wsdl
e.g. http://localhost:8080/wsfSpringHelloWorld/services/greetingService?wsdl
Access wsdl 2.0 typing “wsdl2” instead of “wsdl”.

<?
xml

version
=
"1.0"

encoding
=
"UTF
-
8"
?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<import resource="axis2Config.xml"/>

<bean id="greeting" class="mit.nextlab.example.Greeting"/>

<bean id="services" class="org.wso2.spring.ws.WebServices">
<property name="services">
<list>
<bean id="greetingService"
class="org.wso2.spring.ws.SpringWebService">
<property name="serviceBean" ref="greeting"/>
<property name="serviceName" value="greetingService"/>
</bean>
</list>
</property>
</bean>

</
beans
>


Author: Cloud Maestro team
Version: 02 7
WSF/Spring Development Environment
Suggested IDE
Eclipse
netBeans
For debugging and testing purpose a TCP monitor can be very useful
Tcpmon is an open tool that can help you analyze SOAP transmission.
Useful tools
WSDL2JAVA and JAVA2WSDL scripts can are easy tools for creating clients starting from a
wsdl file. These scripts are bundled with axis2 and can be found under

/opt/wso2wsas-
3.1.3/bin.









Author: Cloud Maestro team
Version: 02 8
WSO2/PHP Server Setup

Server Requirements: (use the same or lower versions in order to keep full compatibility)
• PHP: 5.2.12
• Apache: 2.2.14
• OpenSSL: 0.9.8
• libxml2: 2.6.26
If installing on Windows you will need iconv and Bindlib libraries in order to build from source.
Both Linux and Windows full installation specifications are at the
WSF/PHP manual
. WSF/PHP
API can also be found on this manual.
Client re
WSO2/PHP Development Environment
Suggested PHP IDE/Editor
Eclipse PHP Development Tools (PDT)
NotePad ++
VIM
For debugging and testing purpose a TCP monitor can be very useful
Tcpmon is an open tool that can help you analyze SOAP transmission.
Useful tools
WSDL2PHP and PHP2WSDL scripts are easy tools for creating clients starting from a wsdl
file. These scripts are bundled with WSF_C and can be found under /wsf_c/scripts.







Author: Cloud Maestro team
Version: 02 9


Common Programming Guide

The following conventions will be used for developing Cloud maestro web services, they are
quite standard so probably you will not change a lot of your programming style.

Naming Web services
Either you use PHP or Java follow this convention for naming your web services:
• Use upper case for your team name
• Use mixed case starting with lower case for your service name
e.g. CDrequestProductInfo would be a correct Common Denominator team web service.




Author: Cloud Maestro team
Version: 02 10
Java coding conventions
Indent your code, we will use either
K&R indentation style

(commonly used in C) or
Eclipse/NetBeans automatic indentation.

1. Classes
Names representing a class or type must be nouns and written in mixed case starting
with upper case.

Line AudioSystem
2. Methods and class variables
Variable names and methods must be in mixed case starting with lower case.

fixedCost checkShipperCredit reqHistoricalCost
3. Constants
Names representing constants (final variables) must be all uppercase using underscore
to separate words, e.g.
MEXICAN_STATES COLOR_RED
MAX_ITERATIONS

4. Packages
packages should be in all lower case

edu.mit.nextlab.mbilling mypackage
class Rectangle extends Shape

implements Cloneable, Serializable

{
public void someMethod() throws SomeException
{
if (condition) {
statements;
}

}
}

Author: Cloud Maestro team
Version: 02 11

Cloud Maestro’s Java developing guidelines are based on Java Programming Style
Guidelines.







Author: Cloud Maestro team
Version: 02 12
PHP Coding conventions
Always use <?php ?> to delimit PHP code, not the <? ?> shorthand.
Indent your code, we will use
K&R indentation style
, commonly used in C, e.g.

Classes
Classes should be given descriptive names. Avoid using abbreviations where possible. Class
names should always begin with an uppercase letter. Class hierarchy is also reflected in the
class name, each level of the hierarchy separated with a single underscore, e.g.

Log Net_Finger HTML_Upload_Error

Class variables and methods

Class variables (properties) and methods should be named using the "studly caps" style (also
referred to as "bumpy case" or "camel caps"). Some examples (these would be "public"
members):

$counter connect() getData() buildSomeStuff()
Variable names may only contain alphanumeric characters. Underscores are not permitted.
Numbers are permitted in variable names but are discouraged in most cases.


<?php


class Foo_Bar
{
function fooFunction($arg1, $arg2 = '')
{
if (condition) {
statement;
}
return $val;
}
}
?>



Author: Cloud Maestro team
Version: 02 13

Private class members are preceded by a single underscore, e.g.

$_status _sort() _initTree()


Constants should always be all-uppercase, with underscores to separate words. Prefix
constant names with the uppercased name of the class/package they are used in. Some
examples:

Cloud Maestro’s PHP developing guidelines are based on PEAR coding standards.




Author: Cloud Maestro team
Version: 02 14
Service Deployment Guide
Deployong web services is just a matter of moving your service file into the server using either
scp or sftp, you can simplify this process by using a drag and drop tool.

Filezilla is a multiplatform, opensource ftp/sftp client, with an easy GUI it facilitates the
deployment task.

Java Web Service Deployment
Deploying Java web services implies copying a WAR file into nextlab tomcat directory
$CATALINA_HOME/webapps using either scp or sftp.
$CATALINA_HOME = /usr/local/apache-tomcat/


PHP Web Service Deployment
Copy PHP service file to nextlab server’s directory $APACHE_HOME/htdocs using either scp
or sftp.
$APACHE_HOME = /usr/local/apache2.2









Author: Cloud Maestro team
Version: 02 15
Apendix: Web services interoperability sample

In order to demonstrate easy web services interoperability I created a PHP client that
consumes an axis2 based web service. WSDL2PHP is a simple tool for generate PHP code
against a WSDL file, with minimal configuration I was able to call a WSF/Spring greeting
service. Here is how.
Run the tool, by default it creates the client side, use –s to create the service side skeleton and
indicate the wsdl file name.
e.g.

I added the following lines:
The following line set up the name “Cloud Maestro” to input, which will be a request parameter
for sayHello call.


Gets the return property (String) from $response which is a sayHelloResponse object.








shell> php wsdl2php.php [
-
s] <wsdl file or url>

shell>
php wsdl2php.php
host:port/wsf-spring1.5-demo/services/greetingService?wsdl2

// set speaker name.


$input
-
>
name

=
"
Cloud Maestro
"
;


echo
"
<h1>
"
.
$response
-
>
return
.
"
</h1>
"
;


Author: Cloud Maestro team
Version: 02 16
Here is the resulting code:

Author: Cloud Maestro team
Version: 02 17

<?php


class getNameResponse
{
public $return;
}
class setName
{
public $name;
}
class sayHello
{
public $name;
}
class sayHelloResponse
{
public $return;

}
// define the class map
$class_map = array(
"getNameResponse" => "getNameResponse",
"setName" => "setName",
"sayHello" => "sayHello",
"sayHelloResponse" => "sayHelloResponse");

try {

// create client in WSDL mode
$client = new WSClient(array ("wsdl" =>"host:port/wsf-spring1.5-
demo/services/greetingService?wsdl2",
"classmap" => $class_map));

// get proxy object reference form client
$proxy = $client->getProxy();
//create the request
$input = new setName();
// set speaker name.
$input->name = "Cloud Maestro";
//$input = new sayHello();
// call the operation
$response = $proxy->sayHello($input);
// Access to return’s string property
echo "<h1>".$response->return."</h1>";

} catch (Exception $e) {
// in case of an error, process the fault
if ($e instanceof WSFault) {
printf("Soap Fault: %s\n", $e->Reason);
} else {
printf("Message = %s\n", $e->getMessage());
}
}
?>