assignment 1 “deploying a simple web service” mark holliday department of mathematics and...

29
Assignment 1 “Deploying a Simple Web Service” Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Post on 20-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Assignment 1“Deploying a Simple Web Service”

Mark HollidayDepartment of Mathematics and Computer ScienceWestern Carolina University

Acknowledgement

• This assignment is derived from: “Classroom Exercises for Grid Services” by A. Apon, J. Mache, Y. Yara, and K. Landrus, Proc. 5th Int. Conference on Linux Clusters: The HPC Revolution, May 2004.

About The Assignment

• The main task of this assignment is to build, deploy and test a simple web service.

• The tools involved for this assignment includes:– Java Development Kit 1.5.0– Apache Jakarta Tomcat Java

servlet container– Apache Axis tools

We will:

• Write the Java code to implement the web service.

• Use Axis to generate all needed Java source files.

• Compile the source files just generated.

• Create client source and compile.

• Execute client to access service.

This Process in A Diagram

There are two ways to attain our Web Service

• The Standard Approach – Using two different Apache Axis tools to generate the required documents.

• The Shortest Path – Use one command to trigger both Apache Axis tools.– We will only use the

Standard Approach.

Demonstration: Creating our directory structure.

• As part of the account creation process, the system administrator created a directory in $CATALINA_HOME/webapps/axis that represents your username.

• Check out this directory yourself:% cd \$CATALINA_HOME/webapps/axis/yourusername

Step 1 – Generating the source code for the Service

• Java is used to provide the service. The following code defines our service:

public class MyMath { public int squared( int x ){

return x * x; }

}

• This code is located in ‘/home/yourusername/MyMath.jws’

Demonstration: Deploy The Math Service

• Copy the .jws file to the axis directory:

cp MyMath.jws \$CATALINA_HOME/webapps/axis/yourusername

• Copying is needed so that the axis tools will be able to find the .jws file. (Also the service will be locatable from a web browser.)

Demonstration: View Your Service

• Load up any web browser. (Internet Explorer, Mozilla, Firefox, Netscape)

• Point your web browser to

http://masterhost:8080/axis/yourusername

• Click on the “MyMath.jws” link.– Axis will tell you that there is a service there!

• Click on the WSDL link to view the WSDL code.

What is WSDL?

• WSDL stands for Web Service Definition Language.

• WSDL is an XML document that defines what operations a Web Service offers.

• Each Java method that the service offers has an entry in the WSDL.

We have our service out there, now what?

• Yes, the service is deployed. However, we have no client access to the server.

• We must now create a set of interfaces that can access the server.

• Axis provides tools to create the WSDL that can then be used to create interfaces to the service.

Demonstration: Acquire the WSDL

• In order to create a client that can access the Web Service, we must have access to the WSDL.

• Apache Axis provides a command line tool to create the WSDL file.

Demonstration (Con’t)

• Get a port type prepared:% cp MyMath.jws MyMath.java

• Type in the following command to generate the WSDL file:

% java org.apache.axis.wsdl.Java2WSDL –o \

MyMath.wsdl \

-l"http://localhost:8080/axis/jruff/MyMath“ \

MyMath

What does the Java2WSDL arguments do?

• The ‘org.apache.axis.wsdl.Java2WSDL’ argument gives the package information on where to find the Java2WSDL class.

• The ‘-o MyMath.wsdl’ argument gives the filename of the output WSDL file.

• The ‘-l”http://localhost:8080/axis/yourusername/MyMath”' argument gives the location of the service in question.

What does Java2WSDL do?• Java2WSDL takes a Java

class (or interface) and generates the WSDL document that describes the service.

• Requires either the original service’s source code as a *.java file, or an interface that displays each method.

Demonstration: Creating stubs for the new service

• Stub creation is done using the Axis tool ‘WSDL2Java’.

• WSDL2Java is not the reverse of Java2WSDL. (That is, one does not undo the other.)

• We use the WSDL document that we created in the last step to create the stubs.

Demonstration (Con’t): Creation of Stubs

• The WSDL2Java will create the interfaces:

%java org.apache.axis.wsdl.WSDL2Java \–o . –d Session –s –S true –p \localhost.axis.yourusername.MyMath_jws \MyMath.wsdl

Demonstration (Con’t): Creation of Stubs – What the command means

• The ‘org.apache.axis.wsdl.WSDL2Java’ argument defines which package the WSDL2Java class resides in.

• The ‘-o’ argument specifies that the output files should be placed in the current directory.

• The ‘-s -S true’ argument specifies that the server side bindings and the skeleton should be deployed.

• The ‘-p localhost.axis.yourusername.MyMath_jws’ argument specifies the package the classes and stubs should be placed in. This command outputs several files which will be described later in this section.

The Created Data Structure• The result of this command is

the following files:– MyMathService.java – the source

for the Java interface that includes the getMyMath() method specification.

– MyMathServiceLocator.java – the source for the Java class MyMathServiceLocator.

– MyMathSoapBindingStub – the source for the Java class MyMathSoapBindingStub, which provides bindings to the MathService.

Demonstration: Compilation Process

• Compile the new Java classes using the following command:

%javac -cp $AXISCLASSPATH:$CLASSPATH \domain/server/username/MyMath_jws/*.java

Creation of the Client

• The client for the service uses the previously compiled Java classes.

• The client to the service is provided in

/home/username/MyMathClient.java

Explanation of Client

• For the time being, just assume that the import statements are invisible.

MyMathService service = new MyMathServiceLocator();

Options options = new Options( args ); String endpoint = "http://localhost:" + options.getPort() +

"/axis/yourusername/MyMath.jws";

Call call = (Call) service.createCall();

call.setTargetEndpointAddress( new URL( endpoint ) );

call.setOperationName( "squared" );

call.addParameter( "op1" , XMLType.XSD_INT, ParameterMode.IN);

call.addParameter( "op2" , XMLType.XSD_INT , ParameterMode.OUT );

call.setReturnType( XMLType.XSD_INT ); ret = (Integer) call.invoke( new Object[] { x } );

Each of these commands add a different argument to the interface that the method ‘squared’ uses.

Demonstration: Compiling the Java Client

• The client code is located in “/home/yourusername/assignment1/MyMathClient.java”

• Compilation occurs in one step:

% javac -classpath \$AXISCLASSPATH:$CLASSPATH:. \MyMathClient.java

Demonstration: Running the Client

• Running the client is also done from the command line:

% java -classpath \$AXISCLASSPATH:$CLASSPATH:.\ MyMathClient –p8080 4

• This call will use port 8080 to find the service, and will square the number 4.

What’s Next?

• At this point, we would extend the functionality of the MyMath service by adding two new methods:– boolean isPrime( int number )

– Determines whether a number is prime or not.

– boolean isEven( int number ) – Determines whether a number is even.

Conclusion

• We have:– Written the Java code to

implement a math web service.– Used Axis to generate all needed

Java source files.– Compiled the source files that we

generated.– Created the client source code.– Compiled the client source code.– Executed the client to access the

service.

Questions?

Please Ask!