Introduction
WSA is designed to work with J2ME profiles, and J2ME configuration files are either based on Connected Device Configuration (CDC), or based on Connected Limited Device Configuration (CLDC 1.0 or CLDC 1.1). Remote call API A complete subset of J2SE-based Java API for XML-based RPC (JAX-RPC 1.1) contains some remote method calls (RMI) classes to meet JAX-RPC dependence. XML parsing API is based on Simple API for XML, V2 (SAX2).
WSA's goal is to integrate basic support for web service calls and XML parsing to the runtime environment of the device, so that developers do not have to embed such features in each application - like mobile phones and personal digital assistants This is a special waste of resources.
Core specification
Web Services Interoperability Organization (WS-i) promotes the core specifications and application layer protocols defined by Web services, and they are guided by World Wide Web Consortium (W3C) and Organization for The Advancement Of Structured Information Standards (OASIS). 4 key criteria specify how to create, deploy, discovery, and how to use Web services:
Web Service Standard Description Simple Object Access Protocol (SOAP) 1.1 Define Transfer and Data Coding Web Services Definition Language (WSDL) 1.1 Define how to describe remote services Universal Description, Discovery, & Integration (UDDI) 2.0 Define how to discover remote service EXTENSIBLE Markup Language (XML) 1.0 and XML Schema define Extensible Markup Language, XML and XML Mode (Schema)
These primary standards are often very broad, and Web service developers have discovered difficult to achieve complete interoperability. In order to resolve the difference in standard interpretation, WS-I has defined a set of consistency rules called WS-I Basic Profile, Version 1.0. The JSR 172 is in line with the basic profile (Basic Profile).
Web services on the J2ME platform
JSR 172 specifies standardized client technology, allowing J2ME applications to use remote services on a typical web service schema, as shown in Figure 1:
Figure 1 J2ME web service on a typical web service architecture
At the high level, the web service architecture has three elements:
A network-aware application that resides on the WSA wireless device. The application includes JSR 172 stub that communicates with the network using JSR 172 runtime library. The roles of the stub and the running library will be described below. Wireless networks and Internet and corresponding communication and data coding protocols, including binary protocols, HTTP, and SOAP / XML. Web servers, play a role of service providers, usually behind one or more firewalls and proxy gateways. The web server typically provides access to backend applications and servers on private networks.
The first version of the WSA only solves the use of web services, does not support the creation and development of the service endpoint; the J2ME device can be a service user, but it is not a service provider. JSR 172 has not specified the API discovered using UDDI service. Learn about JSR 172
First examine how typical JSR 172-based applications start understanding how J2ME Web Services works:
The application itself is a smart client based on mobile information device profile (MIDP) or Personal Basis Profile, PBP), has a business-specific logic, user interface, persistence. Logic and lifecycle and application status management. The application can use the JAXP subset API to process XML documents. You can also use the JAX-RPC subset API to use the web service to use JSR 172 stub and run.
In devices like a hand phone, the application and JSR 172 stub typically reside in the device memory, and all JSR 172 elements are embedded together with the base profile and configuration.
JSR 172 Running and Service Provider Interface
The center of JSR 172 is the run library, with a service provider interface, allowing the stub to perform all tasks related to the calling RPC service endpoint:
Set the properties specific to RPC calls. Describe the RPC call input and return values. Encoded input value. Call the RPC service endpoint. Decoding the application and returns all the values returned by the service endpoint.
Figure 3 depicts the relationship between client applications, stub, and three runs:
Figure 3 JSR 172 Running and SPI
Although the JSR 172 runtime hides complexity like connection management and data encoding, SPI has separated the stub from the runtime implementation details, allowing the replacement of the replacement between the supplier.
The client application is not interacting with the running library and SPI; but use the stub. Runners and SPIs are primarily developed to develop third party vendors of JSR 172 runtime and automation tools, such as developers to generate WSDL to Java mapping tools for stub.
Since the application developer does not use the SPI directly, this article will not introduce the run library and SPI more detailed. If you want to know more about them, you can check the JSR 172 specification.
JSR 172 JAX-RPC Subset API
As mentioned earlier, JSR 172 Web Services Remote Call API A complete subset of JDSE-based JAX-RPC 1.1 API. The following list describes the subset at the high level. For more information, see JSR 172 Specification:
Compliance with WS-I Basic Profile. Supports SOAP 1.1. Supports all transmission, such as HTTP 1.1, which can transmit SOAP messages and have a defined protocol for bundled SOAP 1.1. Support full set of data types: logical, character, short integer, integer, long integer, floating point, double precision type, string type (String), basic type array, and complex type (including basic or complex types) structure).
Note that real floating point support appears only in a CDC or CLDC 1.1. On CLDC 1.0 software stack, floating point and double-precision mapping is String. Also note that the supported data type constrains the supported WSDL data type. The following sections will discuss WSDL to Java data type mapping.
Support for Literal representation of the SOAP message indicating the RPC call or response (using the WSDL operation of the Document / Literal message transmission mode); no encoded is supported. SOAP messages with attachments are not supported. SOAP message handler is not supported. Service endpoints are not supported; that is, the device is not allowed to be a web service provider. No discovery support (UDDI). To reduce the needs of network bandwidth - and to save users' time and overhead of each byte, please do not force the device itself to use XML encoding. Note: Implementation may use more efficient encodings, such as the binary protocol between the equipment and the wireless gateway, as long as it is transparent to consumers and producers.
The following table describes the software packages in JSR 172 JAX-RPC subset:
Java / package
description
Javax.microedition.xml.rpc
Defines the Type used by the stub, it is a legal simple type enumeration: logical, byte, short integer, long integer, floating point, double precision String
Define the use of stub
ComplexType, it is WSDL
XSD: special type defined by ComplexType
Define the use of stub
ELEMENT, it represents WSDL
XSD: Element Definition
Define the use of stub
Operation, it represents the WSDL of the service endpoint
WSDL: Operation
Defined
FaultDetailHandler, this is an interface implemented by the stub of the custom fault
Define the use of stub
FaultDetailexception, this is an Exception class that means that the runtime is thrown as a
The exception of the jaxrpcexception results; returns the QNAME class (qualified name) class used by the SMVice-SpecificDefines, and will be related
QNAME returns to
STUB instance
Javax.xml.namespace
Define the use of the stub
QNAME (limited name) class
Javax.xml.rpc
Defines the Stub interface, which is the basic interface of all JAX-RPC stubs; it is also defined
NamespaceConstants and with
JAXRPCEXCEPTION
Java.rmi
standard
Java.RMI software package subset, contains it to meet JAX-RPC-dependent; only the Remote handler and three exception classes are defined:
Marshalexception, RemoteException and ServeRexception
In addition to exceptions such as RemoteException, the stub also uses most of the above APIs; the application itself uses the stub.
Use JAX-RPC subset in the application
The general steps of creating JSR 172 JAX-RPC clients are as follows:
Generate the JSR 172 JAX-RPC Store Class from the WSDL XML document describing the remote web service. Create an instance of generating a stub in your own code. After instantiation, the method of generating a stub is called. These methods correspond to the WSDL of the service endpoint in the WSDL XML document.
Generate JSR 172 JAX-RPC stub
The client application interacts with the remote service through the JSR 172 runtime running library of the root and equipment. The runtime hides complexity related to connection, SOAP, and data coding management.
To interact with the run library, the client usually uses the stub of the stub generated or WSDL to Java mapping tool, which is used to enter the remote service interface definition in the WSDL document, and generate the JAX-RPC subset API and run Subcubes of the library SPI. Although these stubs can also be manually developed, the effect of using this generator will be better. The following figure summarizes the generation of stub: the general step of generating JSR 172 stub.
The final set of stubs achieves the remote interface of Stub Interface and services and uses the support class.
The step of generating JSR 172 JAX-RPC stub is similar to the step of creating RMI-based applications. In standard RMI, the RMI compiler (RMIC) is defined from a remote interface to generate a stub. As shown in Figure 4, after the WSA application is created, WSDL to Java mapping tool generates a stub from the WSDL XML file. The mapping tool generates a remote interface Java file and support class that is used by the generated stub itself. The JSR 172 stub is then compiled into a Java class and packaged with the application.
Sun's J2ME Wireless Toolkit v2.1 contains JSR 172 JAX-RPC Store Builder - This article will tell how to use it. Third-party suppliers provide their own stub generators.
Use JSR 172 JAX-RPC stub
Once the renovation is generated, the application can use it. The following code snippet uses a service called PubService to retrieve wireless technical articles:
Package J2Medeveloper.pubwebservice
// MIDP
Import javax.microedition.midlet.mIdlet;
Import javax.microedition.lcdui.display;
Import javax.microedition.lcdui.form;
...
// jax-rpc
Import java.rmi.remoteexception;
// jax-rpc
String serviceURL = "www.j2medeveloper.com/pubwebservice";
String articleid = "introjsr172";
...
// instantiate the service stub.
PubService_stub service = new pubservice_stub ();
// set up the stub.
Service._setproperty (stub.endpoint_address_property, servicearch);
Service._setproperty (stub.session_maintain_property, new bolean (true));
...
Try {
// invoke the pubservice method getArticlebyid () to Retrieve
// a specific article by id. a WirelessArticle represents an
// Article and Has Methods to get the article's author, date,
// Title, Summary, And Body. Pubservice Also Has a Method To
// Get The PubService's RSS feed.
WirelessArticle Article = Service.GetArticlebyid (ArticleID);
// Create a form to display the article.
Javax.microedition.lcdui.form form = new form (article.getshsttitle ());
Form.Append (Wrap (Article.getsummary ()));
...
...
Display.SetCurrent (Form);
} catch (remoteException E) {
// Handle RMI Exception.
} catch (exception e) {
// Handle Exception.
}
...
Using stub will make remote service calls very easy. The code uses an example in which PubService_stub is initialized, and the instance is initialized, and one of its methods getArticleByid () is called so that the article itself is retrieved. Then, the application uses the access method of the article to obtain the title, summary, etc. of the article.
Note that because the PubService_stub.getArticleById () is called a block call, in the actual application, you want to assign the call with separate threads. In WSA, method calls follow common client / server interaction synchronous requests - Response Model: The client issues a request to the server, then wait for the server response:
Figure 5 Synchronous Request / Response Model
How to encode, serialize, and sending methods and their parameters, and how to receive, decoding, and reverse selecinscence, which is completely transparent to applications. JSR 172 stub and runtime processes all of these monotonous lengthy details.
Map the WSDL data type to Java
The following is a brief look at the data type mapping of WSDL to Java in JSR 172's JAX-RPC subset API:
WSDL data type
Java data type
XSD: String
String
XSD: INT
Int or Integer
XSD: Long
Long or long
XSD: short
Short or short
XSD: Boolean
Boolean or Boolean
XSD: Byte
BYTE or BYTE
XSD: Float
String, Float or Float
XSD: DOUBLE
String, Double or Double
XSD: Qname
javax.xml.namespace.qname
XSD: Base64Binary
Byte []
XSD: HEXBINARY
Byte []
Arrays
Depending on the mode of the XML array
XSD: ComplexType
Basic and class types of sequences
Don't forget, because CLDC 1.0 does not support floating point types, so you must map floating-point and double-precision types to String. Developers should use this default mapping when the target contains both CLDC 1.0 and the CLDC 1.1 platform.
To learn more about WSDL, see JSR 172 Specification and W3C'S WSDL Specification.
Use JAXP subsets in the app
The attention will now be transferred to the JAXP subset - other JSR 172 API. Remember that JAXP and JAX-RPC are optional packages; they exist independently of each other.
JAXP XML parses API based on a complete subset of SIMPLE API for XML V2 (SAX2). Although most classes in the standard SAX2 API are not included in the subset, they have retained the necessary core functions.
The following list describes the JAXP subset in the high level. For more information, see JSR 172 Specification:
A complete subset of SAX 2.0
SAX 1.0 is not supported because SAX 2.0 has replaced it.
Support XML namespace supports both UTF-8 and support UTF-16 character encoding. Either the document object model (Document Object Model (DOM)) is not supported, or does not support the DOM's Level 2.0 because it is too cumbersome. Extensible Stylesheet Language Transformations, XSLT is not supported. Support Document Type Definition, DTD. The parser can verify these documents according to the specified internal or external definition of the XML document type. JSR 172 specifies that document verification is optional because the operation overhead is extremely large, and a document is not suitable for a higher-end CLDC-based environment, but may be successful on higher-end CDC-based platforms. If you skip DTD processing, the defaulthandler.skippedentity () method notifies the application. This logic meets the XML 1.0 specification.
The following table describes the software packages in JSR 172 JAXP subset:
Java package
description
Javax.xml.parsers
Defined
SAXPARSER, this is an API that represents a simple SAX-based parser.
Defined
SaxParserFactory, this is a factory that gets and configures SAX-based parsers.
Defined
ParserConfigurationException, indicating the configuration error when thrown.
Defined
FactoryConfigurationError, indicating the problem of the parser factory when thrown.
ORG.XML.SAX
Defined
Attribute, this is an interface to access the attribute list in the following form:
Attribute index limits the name limit (prefix) name of the namespace
Defined
Locator, this is an interface for associating the SAX event with the document location. By using LoCator (this Locator by calling the defaulthandler object
SetDocumentLocator () method and specified) instance, the application can obtain resolution event location information, such as event line number, column number, and public and system identifiers.
Locator provides a method of obtaining a line number, a list number of the current document event, and a method of obtaining a public and system identifier for the current document event.
Defined
InputSource, it packages the input source. It provides methods for acquiring or setting word streams, character sets, codes, and public and system identifiers.
Universal
SAX exception class,
SAXEXCEPTION and three subclasses:
SAXNOTRECognizedException, SAXNOTSUPPORTEDEXCEPTION and SAXPARSEEXCEPTION
Org.xml.sax.helpers
Defined
DefaultHandler, this is the base class of the SAX2 event handler. It provides the default implementation of event handling behavior to extension. Application developers only need to overwrite the relevant methods, you can design parsing behavior.
When reading the XML flow, when the parser conflicts with the XML element or attribute, it notifies the application's application by calling one of the DEFAULTHANDLER methods; for example::
StartDocument () and endDocument (), at the beginning and end of the XML document StarTelement () and endElement (), at the beginning of the XML element, characters (), when you find the character data inside the element, SkipPedentity (), when you skip one When the entity is jsr 172 spec, describes the rest of the DefaultHandler method.
The following method defines how to use JAXP to resolve the RSS summary from PubService:
/ **
* Invoke The Service and Parse The RSS feed.
* /
Private void Parserssfeed () {
Try {
// Create a RSS Parser Handler
Parser = new rssparserhandler ();
Try {
SAXPARSERFAACTORY FACTORY = SAXPARSERFACTORY.NEWINSTANCE ();
SAXPARSER = Factory.newsaxparser ();
} catch (exception e) {
// Handle Exception ...
Return;
}
// Invoke the Remote Service Method getrssfeed to
// Retrieve The Rss Feed for the Pub Service
// Web site.
String Rssfeed = service.getrssseed ();
// Parse the RSS feed.
Byte [] rSFeedByteArray = rssfeed.getbytes ("UTF-8");
ByteaRrayInputStream RssfeedbyTearRayinputStream =
New ByteArrayInputStream (RssfeedbyteArray);
SAXPARSER.PARSE (RssfeedbyTearRayinputStream, Parser);
} catch (remoteException E) {
// Handle RMI Exception.
} catch (exception e) {
// Handle Exception.
}
}
The ParsersSfeed () method uses the pubService_stub.getrsSfeed () remote service to retrieve the PubService's RSS Content XML document and passed it to the JAXP RSS-feed XML parser. The following type definitions show how to implement a handler responsible for resolving the RSS's request:
/ **
* RSS Parser Handler Class to Parse The Rss Feed from The
* Server.
* /
Class RssparserHandler Extends defaulthandler {
// Stack to Keep Track of Document Parsing.
Stack stack;
// Current Document Element.
Object Current;
/ ** Start of Document Processing. * /
Public void startdocument () {
rssparsingcomplete = false;
Title = link = description = NULL;
Stack = new stack ();
}
/ ** Process the new element. * /
Public Void StartElement (String Uri, String LocalName, String Qname,
Attributes attributes) throws saxexception {
IF ("Item" .Equals (QNAME)) {
Title = link = description = NULL;
}
Stack.push (QNAME);
}
/ ** Process the character data for current tag. * /
Public void character
CHAR [] CH, INT Start, INT Length) {
Object current = stack.peek ();
IF ("Title" .equals (QNAME)) {
Title = New String (ch, start, length));
} else if ("link" .Equals (qname)) {
Link = New String (CH, Start, Length));
} Else IF ("description" .Equals (qname)) {
Description = New String (ch, start, length);
}
}
/ ** process the end element tag. * /
Public void endelement
String Uri, String Localname, String Qname) {
Stack.pop ();
IF ("RSS". Equals (QNAME)) {
rssparsingcomplete = true;
} else {
// Do Something with Title, Link, Description, Such
// as displaming it to the user.
}
}
}
Note that StartDocument (), StarTelement (), StopeElement (), and the definition of the CHARACTERS () method provides the minimum functionality required for SAX2 XML resolution.
J2ME Wireless Toolkit Support for Web Services
J2ME Wireless Toolkit V2.1 includes the library required to develop MIDlets that take advantage of J2ME Web Service. The kit also includes a JAX-RPC stub generator that can run it from the command line or the KTOOLBAR menu, as shown below:
Figure 6 J2ME Wireless Toolkit 2.1 Utility and Store Generator
Select Stub Generator on the Utilities screen. In the dialog that appears, enter the URL of the input WSDL document to be used to use the service, generate the output path of the file and the software package name used to generate the stub file. The CLDC 1.0 or 1.1 is selected accordingly.
The J2ME Wireless Kit V2.1 also contains an example application JSR172Demo.
Conclude
J2ME Web Services Specifications (J2ME Web Services Specification) standardize the Web services and XML parsing interfaces on the J2ME platform. With the emergence of JAX-RPC subset API, developers can use Java programming languages and familiar JAX-RPC APIs to create applications that use XML-based remote services. It does not have to deal with the underlying details of HTTP, SOAP, and data conversion. Moreover, as the introduction of the JAXP subset API, XML analysis is currently an integral part of the J2ME platform itself. The J2ME Web Services API eliminates the need to include the needs of remote calls and XML parsing codes in each application.
These powerful APIs allow mobile applications more easily access to web-based services, but developers don't forget the restricted application environment provided by J2ME devices. The existing XML-based desktop or enterprise application is not transplanted is not an appropriate way to develop a Web service client for the J2ME platform. Appropriate considerations for device processing capabilities, battery life, network bandwidth, and security are also indispensable. More information
JSR 172: J2ME Web Services Specification Web Services Interoperability Organization (WS-I) World Wide Web Consortium (W3C) Organization for the Advancement of Structured Information Standards (OASIS) The Complexity of Developing Mobile Networked Data Services, J2ME Wireless Connection Wizard For Sun Java System Studio W3C WSDL Website
Thank you
I am very grateful to JSR 172 Specification of Jon Ellis and Mark Young to feedback to this article. Http://gceclub.sun.com.cn/staticContent/html/2004-05-20/j2me.html