cs 157b: database management systems ii february 27 class meeting department of computer science san...

15
CS 157B: Database Management Systems II February 27 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak www.cs.sjsu.edu/~mak

Upload: natalie-wheeler

Post on 27-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 157B: Database Management Systems II February 27 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron

CS 157B: Database Management Systems IIFebruary 27 Class Meeting

Department of Computer ScienceSan Jose State University

Spring 2013Instructor: Ron Mak

www.cs.sjsu.edu/~mak

Page 2: CS 157B: Database Management Systems II February 27 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron

Department of Computer ScienceSpring 2013: February 27

CS 157B: Database Management Systems II© R. Mak

2

Oral Presentations

Back by popular demand! Next Monday, March 4.

4 teams from each section that didn’t present last time. First 4 teams to send me email, or I will “volunteer” teams.

Short 15-minute presentations about your Project #2. What is your application? What XML data did you use? How did you do marshalling and unmarshalling? What XQuery queries did you do? PowerPoint slides OK.

Presenting teams can turn in your presentation slides in place of a project report. Presenting teams have until Monday to turn in your projects.

Page 3: CS 157B: Database Management Systems II February 27 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron

Department of Computer ScienceSpring 2013: February 27

CS 157B: Database Management Systems II© R. Mak

3

Review: Web Services

A web service is “a software system designed to support interoperable machine-to-machine interaction over a network”. The service is provided by a server machine

(the web service provider). Client applications make service requests to the server

and get back responses. A popular form of distributed computing.

There are many public web services available. Examples:

http://www.service-repository.com/ http://www.webservicex.net/WS/WSDetails.aspx?CATID=2&WSID=9

You can write a web service client program in Java to connect to a web service and consume its data.

Page 4: CS 157B: Database Management Systems II February 27 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron

Department of Computer ScienceSpring 2013: February 27

CS 157B: Database Management Systems II© R. Mak

4

Review: Web Services

A web service is described by a service contract written in the Web Services Description Language (WSDL). The WSDL document is an XML document. The WSDL document and the request and response messages

are transmitted over HTTP. Messages use the Service Oriented Architecture Protocol

(SOAP), an XML format.

Web services are programming language agnostic. The web service provider code can be written in any language. The web service requester (client) code can be written

in any language. XML is the intermediary

WSDL document SOAP messages

Web services are a major application

of the usefulness of XML.

Page 5: CS 157B: Database Management Systems II February 27 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron

Department of Computer ScienceSpring 2013: February 27

CS 157B: Database Management Systems II© R. Mak

5

Web Service Example: Time Server

A simple web service provider written in Java. Provide the current time as a string. Provide the elapsed Unix time in milliseconds as a long. The required JAXB and JAX-WS packages

are part of standard Java 6 and beyond._

Page 6: CS 157B: Database Management Systems II February 27 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron

Department of Computer ScienceSpring 2013: February 27

CS 157B: Database Management Systems II© R. Mak

6

Time Server Provider

Time service interface:

package wsdemo.timeservice;

import javax.jws.soap.SOAPBinding;import javax.jws.soap.SOAPBinding.Style;import javax.jws.WebMethod;import javax.jws.WebService;

@WebService@SOAPBinding(style = Style.RPC)public interface TimeServer{ @WebMethod String getTimeAsString(); @WebMethod long getTimeAsElapsed();}

Service Endpoint Interface (SEI).

How to construct the WSDL document

Service operations Adopted from Java Web Services:Up and Runningby Martin KalinO’Reilly, 2009

Page 7: CS 157B: Database Management Systems II February 27 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron

Department of Computer ScienceSpring 2013: February 27

CS 157B: Database Management Systems II© R. Mak

7

Time Server Provider

Time service implementation:

package wsdemo.timeservice;

import java.util.Date;import javax.jws.WebService;

@WebService(endpointInterface = "wsdemo.timeservice.TimeServer")public class TimeServerImpl implements TimeServer { public String getTimeAsString() { return String.format("The current server time is %s", new Date().toString()); } public long getTimeAsElapsed() { return new Date().getTime(); }}

Link this Service ImplementationBean (SIB) to the TimeServer SEI.

Current time operation

Elapsed time operation

Adopted from Java Web Services:Up and Runningby Martin KalinO’Reilly, 2009

Page 8: CS 157B: Database Management Systems II February 27 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron

Department of Computer ScienceSpring 2013: February 27

CS 157B: Database Management Systems II© R. Mak

8

Time Server Publisher

Publish the time service:

package wsdemo.timeservice;

import javax.xml.ws.Endpoint;

public class TimeServerPublisher { public static void main(String args[]) { TimeServer ts = new TimeServerImpl(); String url = String.format("http://localhost:%s/ts", args[0]); System.out.printf("Publishing service %s to %s ...\n", ts.getClass().getName(), url); // 1st argument is the publication URL // 2nd argument is an SIB instance Endpoint.publish(url, ts); }}

Pass in the server port numberas a runtime argument.

Run forever until terminated.

Construct the service.

Adopted from Java Web Services:Up and Runningby Martin KalinO’Reilly, 2009

ts is an arbitrary name.

A web service is usuallyhosted by a web serverrunning server software

such as Apache Tomcat.

Page 9: CS 157B: Database Management Systems II February 27 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron

Department of Computer ScienceSpring 2013: February 27

CS 157B: Database Management Systems II© R. Mak

9

Time Service WSDL

Download the WSDL document (the service contract) at http://localhost:9876/ts?wsdl

<definitions ... > ... <portType name="TimeServer"> <operation name="getTimeAsString"> <input ... message="tns:getTimeAsString"></input> <output ... message="tns:getTimeAsStringResponse"></output> </operation> <operation name="getTimeAsElapsed"> <input ... message="tns:getTimeAsElapsed"></input> <output ..." message="tns:getTimeAsElapsedResponse"></output> </operation> </portType> ... <service name="TimeServerImplService"> <port name="TimeServerImplPort" binding="tns:TimeServerImplPortBinding"> <soap:address location="http://localhost:9876/ts"></soap:address> </port> </service></definitions>

The portType is similar to a Java interface:It groups the operations provided by theweb service.

The service endpoint.

Page 10: CS 157B: Database Management Systems II February 27 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron

Department of Computer ScienceSpring 2013: February 27

CS 157B: Database Management Systems II© R. Mak

10

Time Service Client

The client program makes the time service requests:package wsdemo.timeservice;

import java.net.MalformedURLException;import java.net.URL;import javax.xml.namespace.QName;import javax.xml.ws.Service;class TimeClient { public static void main(String args[]) throws MalformedURLException { URL url = new URL(String.format("http://localhost:%s/ts?wsdl", args[0]));

QName qname = new QName("http://timeservice.wsdemo/", "TimeServerImplService");

Service service = Service.create(url, qname); TimeServer eif = service.getPort(TimeServer.class);

System.out.println(eif.getTimeAsString()); System.out.println(eif.getTimeAsElapsed()); }}

Pass in the server port numberas a runtime argument.

Create the qualified service name.

Factory to get theservice port(endpoint interface).

Request services.

Get the WSDL.

Adopted from Java Web Services:Up and Runningby Martin KalinO’Reilly, 2009

Page 11: CS 157B: Database Management Systems II February 27 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron

Department of Computer ScienceSpring 2013: February 27

CS 157B: Database Management Systems II© R. Mak

11

Connecting to an Existing Web Service

More often, we want to connect to an existing web service in order to consume the data it provides. Connect to the web service via its URL. Download its WSDL service contract. Write the client application to consume and process the data.

The client application can be written in any language,such as Java.

You don’t know (nor should you care) what language the web service provider is written in.

If you write the client application in Java,there are tools that will help._

Page 12: CS 157B: Database Management Systems II February 27 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron

Department of Computer ScienceSpring 2013: February 27

CS 157B: Database Management Systems II© R. Mak

12

The wsimport Utility Program

The Java utility program wsimport helps you to write a Java client program to connect to a web service. wsimport reads the WSDL service contract. wsimport generates Java source code

that your client application can use. It can also compile the generated source code

and create a jar file.

Command line:

-keep to keep the generated Java source files -s source-directory where to put the generated source files -d class-directory where to put the compiled files -p package for the generated source files wsdl-url to access the WSDL service contract Also: -verbose and –clientjar jarfile

wsimport -keep -s java-directory -d class-directory -p package wsdl-url

Page 13: CS 157B: Database Management Systems II February 27 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron

Department of Computer ScienceSpring 2013: February 27

CS 157B: Database Management Systems II© R. Mak

13

NetBeans Support for Web Services

Tutorial: http://netbeans.org/kb/docs/websvc/jax-ws.html NetBeans supports writing web service provider code

and web service client code. For example, if you’re developing client code to access an

existing web service, NetBeans calls wsimport for you. _

Demo

Page 14: CS 157B: Database Management Systems II February 27 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron

Department of Computer ScienceSpring 2013: February 27

CS 157B: Database Management Systems II© R. Mak

14

Example Public Web Services

Try googling: free web services open web services public web services

Currency conversion ratehttp://www.webservicex.net/CurrencyConvertor.asmx

Spell checkerhttp://wsf.cdyne.com/SpellChecker/check.asmx

Stock quoteshttp://www.webservicex.net/stockquote.asmx

Weather reportshttp://wsf.cdyne.com/WeatherWS/Weather.asmx Demo

Page 15: CS 157B: Database Management Systems II February 27 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron

Department of Computer ScienceSpring 2013: February 27

CS 157B: Database Management Systems II© R. Mak

15

Project #3

A web services mashup! Create a new web service by combining data

from two or more existing web services. Your new web service provider will itself be a client

to each of the existing web services.

Deploy your web service. Use the sample “publisher” code.

Create a Java client to your mashup web service. It should have some meaningful output.

Watch the class website for the official write-up._