You are on page 1of 17

DEPARTMENT OF INFORMATION SCIENCE AND ENGINEERING M S RAMAIAH INSTITUTE OF TECHNOLOGY

NOV-2011

Title: J2ME Client for Accessing StockQuote WebService Subject: Service Oriented Architecture Submitted by: Abhijeet Vaidya(1MS08IS002) Abhishek Verma(1MS08IS007) Dinesh hedge(1MS08IS030) Keerthan Pai K(1MS08IS038)

Table of Contents

Introduction Creating j2me client for stock quote using .net web service Generating the Stub Class Creating the Client Project Creating midlet file for the user interface Source code: StockMid.java Simulator Output WSDL Code Conclusion

Introduction
What is a web service? A web service is a network accessible interface to application functionality, built using standard Internet technologies. The fundamental concept behind web services is the SOA (service-oriented architecture), where an application is no longer a large monolithic program, but is divided into smaller, loosely coupled programs, and provided services are loosely coupled together with standardized and well-defined interfaces. These loosely coupled programs make the architecture very extensible, as it acquires the ability to add or remove services with limited costs. Therefore, new services can be created by combining and reusing existing services. Because of the abstraction provided by the standards-based interfaces, it does not matter whether the application services are written in Java and the browser written in C++, or the application services deployed on a UNIX box while the browser is deployed on Windows. Web services allow for cross-platform interoperability in a way that makes the platform irrelevant. Interoperability is one of the key benefits gained from implementing web services.

Figure : The web services invocation form j2me client

In the web services architecture, the service provider publishes a description of the service(s) it offers via the service registry. The service consumer searches the service registry to find a service that meets their needs. The service consumer could be a person or a program. In the web services architecture, the service provider publishes a description of the service(s) it offers via the service registry. The

service consumer searches the service registry to find a service that meets their needs. The service consumer could be a person or a program. The service provider offers business processes in the form of services. The services offered by the provider are called by the consumer to achieve certain sets of business goals. The process of services being provided and consumed is achieved by using directory services that lie between the provider and the consumer, in the form of broker. The service to be made available to the consumer is published to the directory services in the broker. The consumer wanting to achieve the set of business goal(s) will discover the service from the broker. If the service is found, it will bind to the service and execute the processing logic. What is J2ME? The Java 2 Micro Edition (J2ME) is a Java platform that is designed for small devices. It contains specially designed, lightweight virtual machines; a bare minimum of core-class libraries; and lightweight substitutes for standard Java libraries. J2ME is the ideal mobile client platform for wireless PDAs and enhanced mobile phones. It supports the standard Java programming language and contains a subset of APIs from the Java 2 Platform, Standard Edition (J2SE) with the addition of device-specific APIs. The Mobile Information Device Profile (MIDP) is a flavor of J2ME that runs on mobile phones. All major mobile device vendors, including Nokia, Motorola, Siemens, Samsung, Fujitsu, Inventec, LG Electronics, Mitsubishi, NEC, Panasonic, Psion, RIM, Sharp, and Sony, have adopted Java as part of their core strategy for future smart devices. Major wireless carriers such as SprintPCS, and AT&T have committed to support Java devices and applications on their networks. In this tutorial, you focus on MIDP application development. For more information about J2ME and MIDP, see the J2ME 101 series of tutorials published by developerWorks (see Resources).

The Sun J2ME Wireless Toolkit The J2ME Wireless Toolkit is a comprehensive set of tools for building MIDP applications. The toolkit can be used standalone, or incorporated into many popular integrated development environments (IDEs). Version 5 of Sun One Studio, Mobile Edition comes with the J2ME Wireless Toolkit 3.0 bundled in. It provides the byte code pre-verification tool, implementation of API class libraries, and a device emulator. The emulator does not correspond to any real physical device. Instead, it supports cutting-edge MIDP optional package APIs that are not yet implemented on physical devices. Key Features of SUN J2ME Wireless Toolkit 3.0 Integration with 3rd party emulators and Windows Mobile devices. On-device deployment and on-device debugging. CLDC/MIDP, CDC/FP/PBP/AGUI and BD-J integrated into one SDK. New CLDC Hot-Spot Virtual Machine. Optimized MSA 1.1 Stack with Extensions. Profiling support. BD-J support (for Windows Only). JavaFX Mobile Emulator.

Creating j2me client for stock quote using .net web service
This section shows how to consume a web service already hosted in the Internet (not in the Local host) from your J2ME Client. For this example, we are going to access the Stock Quote web service build on.NET platform: Click on to this http://www.webservicex.net/stockquote.asmx If you navigate there, you will see a description of the service, and it's one method: getQuote(String name). We are going to invoke that method remotely from a JavaME Midlet. Figure: StockQuote hosted web service

If we click on to the Service Description link in the right corner of the web page, it will display the WSDL of the Stock-Quote service. As the service is already deployed and active we just need to create a client and invoke it through the stubs.

Generating the Stub Class

we will need the Web Service Description Language (WSDL) description of the service to generate the "stub" Java classes The Java ME SDK contains a tool called "wscompile", that reads the WSDL data and generates a "stub" Java class. This class acts as a local proxy for the remote service. We call a method in the stub class, and it calls the remote method for us.

1. You need to create the 'config.xml' file as shown below (for StockQuote service):(The wsdl location must match the URL for the service (with "?WSDL"tacked on the end). The packageName is the package for the generated files.)

For Example config.xml would look like


<?xml version="1.0"?> <configuration xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config"> <!-- This is shown for the Stock Quote service,just replace wsdl location and package name for other service --> <wsdl location="http://www.webservicex.net/stockquote.asmx?WSDL" packageName="StockDemo" /> </configuration>

2. Next generate the stubs by executing the command as show below:

Four files will be created using stock quote web service.

Creating the Client Project 1. Now start the Java(TM) ME SDK Platform and Create a Project as shown in the figure below.

2. Now open project explorer and add all stub generated files

Creating midlet file for the user interface Steps for creating stockQuote midlet Create a form. Add a text field in the form. Create an object of the stub. Use the 'object._setProperty()' function to specify the EndpointInterface. Invoke the service method by 'object.method(parameters)'. Process and display the returns

Source code: StockMid.java


package stockdemo; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class StockMid extends MIDlet implements Runnable, CommandListener { Display display; private Form f; private TextField tf; private Command sendCommand = new Command("Send", Command.ITEM, 1); private Command exitCommand = new Command("Exit", Command.EXIT, 1); String name = ""; public void startApp() { display = Display.getDisplay(this); f = new Form("Stock Client"); tf = new TextField("Stock Quote:", "", 30, TextField.ANY); f.append(tf); f.addCommand(sendCommand); f.addCommand(exitCommand); f.setCommandListener(this); display.setCurrent(f); } public void pauseApp() {} public void destroyApp(boolean unconditional) {} public void commandAction(Command c, Displayable d) { if (c == sendCommand) { name = tf.getString(); /* Start a new thread so that the remote invocation won't block the process.*/ new Thread(this).start(); } if (c == exitCommand) { notifyDestroyed(); destroyApp(true); } } public void run() { try { /* Create Stub and set URL for the service*/ StockQuoteSoap_Stub service = new StockQuoteSoap_Stub(); service._setProperty(javax.xml.rpc.Stub.ENDPOINT_ADDRESS_PROPERTY, "http://www.webservicex.net/stockquote.asmx"); println("Stock Quote for:"+tf.getString()); //println("Results are given below "); String msg = service.getQuote(name); println("Name : "+returnTagName(msg, "<Name>", "</Name>", 6));

println("Symbol : "+returnTagName(msg, "<Symbol>", "</Symbol>", 8)); println("Change : "+returnTagName(msg, "<Change>", "</Change>", 8)); println("Earnings : "+returnTagName(msg, "<PercentageChange>", "</PercentageChange>", 18)); println("Stock Value : "+returnTagName(msg, "<Last>", "</Last>", 6)); println("Date : "+returnTagName(msg, "<Date>", "</Date>", 6)); println("Open : "+returnTagName(msg, "<Open>", "</Open>", 6)); println("High : "+returnTagName(msg, "<High>", "</High>", 6)); println("Low : "+returnTagName(msg, "<Low>", "</Low>", 5)); println("Market Cap : "+returnTagName(msg, "<MktCap>", "</MktCap>", 8)); println("Earnings : "+returnTagName(msg, "<Earns>", "</Earns>", 7)); } catch (Exception e) { println(e.toString()); } } private void println(String s) { f.append(s + "\n"); }

public String returnTagName(String original, String openTag,String closeTag,int inc) { int start,end; start = original.indexOf(openTag); end = original.indexOf(closeTag); return original.substring(start+inc, end); } }

Simulator output:

Snapshot -1

Snapshot-2

Snapshot-3

WSDL Code:

<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="ht tp://microsoft.com/wsdl/mime/textMatching/"xmlns:soapenc="http://schemas.xmlsoap. org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"xmlns:tns=" http://www.webserviceX.NET/" xmlns:s="http://www.w3.org/2001/XMLSchema"xmlns:soap 12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.o rg/wsdl/http/"xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http ://www.webserviceX.NET/"> <wsdl:types> <s:schema elementFormDefault="qualified" targetNamespace="http://www.webserviceX. NET/"> <s:element name="GetQuote"> <s:complexType> <s:sequence> <s:element minOccurs="0" maxOccurs="1" name="symbol" type="s:string"/> </s:sequence> </s:complexType> </s:element> <s:element name="GetQuoteResponse"> <s:complexType> <s:sequence> <s:element minOccurs="0" maxOccurs="1" name="GetQuoteResult" type="s:string"/> </s:sequence> </s:complexType> </s:element> <s:element name="string" nillable="true" type="s:string"/> </s:schema> </wsdl:types> <wsdl:message name="GetQuoteSoapIn"> <wsdl:part name="parameters" element="tns:GetQuote"/> </wsdl:message> <wsdl:message name="GetQuoteSoapOut"> <wsdl:part name="parameters" element="tns:GetQuoteResponse"/> </wsdl:message> <wsdl:message name="GetQuoteHttpGetIn"> <wsdl:part name="symbol" type="s:string"/> </wsdl:message> <wsdl:message name="GetQuoteHttpGetOut"> <wsdl:part name="Body" element="tns:string"/> </wsdl:message> <wsdl:message name="GetQuoteHttpPostIn"> <wsdl:part name="symbol" type="s:string"/> </wsdl:message>

<wsdl:message name="GetQuoteHttpPostOut"> <wsdl:part name="Body" element="tns:string"/> </wsdl:message> <wsdl:portType name="StockQuoteSoap"> <wsdl:operation name="GetQuote"> <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">Get Stock quote for a company Symbol</wsdl:documentation> <wsdl:input message="tns:GetQuoteSoapIn"/> <wsdl:output message="tns:GetQuoteSoapOut"/> </wsdl:operation> </wsdl:portType> <wsdl:portType name="StockQuoteHttpGet"> <wsdl:operation name="GetQuote"> <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">Get Stock quote for a company Symbol</wsdl:documentation> <wsdl:input message="tns:GetQuoteHttpGetIn"/> <wsdl:output message="tns:GetQuoteHttpGetOut"/> </wsdl:operation> </wsdl:portType> <wsdl:portType name="StockQuoteHttpPost"> <wsdl:operation name="GetQuote"> <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">Get Stock quote for a company Symbol</wsdl:documentation> <wsdl:input message="tns:GetQuoteHttpPostIn"/> <wsdl:output message="tns:GetQuoteHttpPostOut"/> </wsdl:operation> </wsdl:portType> <wsdl:binding name="StockQuoteSoap" type="tns:StockQuoteSoap"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="GetQuote"> <soap:operation soapAction="http://www.webserviceX.NET/GetQuote" style="document" /> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:binding name="StockQuoteSoap12" type="tns:StockQuoteSoap"> <soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="GetQuote"> <soap12:operation soapAction="http://www.webserviceX.NET/GetQuote" style="documen t"/> <wsdl:input> <soap12:body use="literal"/> </wsdl:input> <wsdl:output> <soap12:body use="literal"/>

</wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:binding name="StockQuoteHttpGet" type="tns:StockQuoteHttpGet"> <http:binding verb="GET"/> <wsdl:operation name="GetQuote"> <http:operation location="/GetQuote"/> <wsdl:input> <http:urlEncoded/> </wsdl:input> <wsdl:output> <mime:mimeXml part="Body"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:binding name="StockQuoteHttpPost" type="tns:StockQuoteHttpPost"> <http:binding verb="POST"/> <wsdl:operation name="GetQuote"> <http:operation location="/GetQuote"/> <wsdl:input> <mime:content type="application/x-www-form-urlencoded"/> </wsdl:input> <wsdl:output> <mime:mimeXml part="Body"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="StockQuote"> <wsdl:port name="StockQuoteSoap" binding="tns:StockQuoteSoap"> <soap:address location="http://www.webservicex.net/stockquote.asmx"/> </wsdl:port> <wsdl:port name="StockQuoteSoap12" binding="tns:StockQuoteSoap12"> <soap12:address location="http://www.webservicex.net/stockquote.asmx"/> </wsdl:port> <wsdl:port name="StockQuoteHttpGet" binding="tns:StockQuoteHttpGet"> <http:address location="http://www.webservicex.net/stockquote.asmx"/> </wsdl:port> <wsdl:port name="StockQuoteHttpPost" binding="tns:StockQuoteHttpPost"> <http:address location="http://www.webservicex.net/stockquote.asmx"/> </wsdl:port> </wsdl:service> </wsdl:definitions>

Conclusion
The above StockQuote example gives you an overview how J2ME can be used with today's modern technologies in a loosely coupled manner following the methodology of SOA

You might also like