Transcript J2ME

Java 2 Platform, Micro Edition
Implementation of J2ME RMI Application
Farooq Sheikh
J2ME RMI
1
Contents

What’s J2ME RMI?
What’s MeRMI?
 MeRMI core concepts
 How to use MeRMI



HelloWorld Example
Demo (PhoneBook Application)
J2ME RMI
2
J2ME RMI
The Java 2 Micro Edition(J2ME), Remote Method Invocation
(RMI) Optional Package is a J2ME Connected Device
Configuration (CDC) Optional Package
 The J2ME CDC requires a complete implementation of the
Java virtual machine (JVM)* specification and core Java APIs
including java.lang, java.io, java.net, and java.util.
 Implementations of the RMI Optional Package can only
function on devices that include support for the J2ME CDC and
the Foundation Profile.
The RMI Optional Package requires implementations of the
Foundation Profile and the J2ME CDC. The RMI Optional
Package for J2ME/CDC is not supported by the J2ME
Connected Limited Device Configuration (CLDC).

J2ME RMI
3
MeRMI

MeRMI is Simplified RMI for Java2 Micro Edition
(MIDP 1.0)
 It’s a Zonski project available
https://mermi.dev.java.net/
 MeRMI brings the concepts of RMI, such as
interface driven RPC, to J2ME. It provides an API
and tool-set for the development of applications
that use RMI-like remote procedure calls from a
J2ME client to a J2SE server.
 Current Release: Jan 2004
 Mermi1.0.2 Beta API’s
J2ME RMI
4
MeRMI Concept



Like RMI, MeRMI hides the communication protocol from the
developer.
 The default implementation uses serialized data over
HTTP to ensure compatibility with all MIDP 1.0
implementations.
MeRMI allows developers to write J2SE server side objects
using a very similar technique to that used when developing
remote objects in RMI.
The MeRMI compiler is then used to generate appropriate
server side skeletons for the remote interface.
 When a client connects to the server and attempts to use
the object, this skeleton is used to translate the network
request, invoking the appropriate method on the server
side implementation.
J2ME RMI
5
MeRMI Concept

The MeRMI compiler also generates a
client side stub, which the client can
use to remotely invoke methods on
the remote server.

Unlike standard RMI, this stub does not
implement the remote interface of the
server object. Instead the stub is
generated with methods that are similar
to the remote interface but are more
suitable to J2ME.
J2ME RMI
6
MeRMI Concept
For instance if the remote interface was defined as follows
public interface MyRemote extends Remote {
public int doSomething(int param1, String param2) throws
RemoteException;
}
Then the following stub would be generated:
public class MyRemoteMicro extends HttpRemoteProxy {
public int doSomething(int param1, String param2) throws IOException
{ // generated code to make remote invocation }
}
J2ME RMI
7
How to Use MeRMI (Server)

In MeRMI many of the concepts and
techniques are same as in standard RMI
programming.


In fact server side MeRMI components use the
classes from the java.rmi package.
As with standard RMI it is first necessary to
create a remote interface to our server side
object.


This interface must be declared to extend the
java.rmi.Remote interface.
Each method can optionally be declared to
throw java.rmi.RemoteException, however this is
not required by MeRMI.
J2ME RMI
8
How to Use MeRMI (Server)

Once the Remote interface has been defined, we must then
provide an implementation for it.

This implementation has no restrictions, it may start
threads, call out to remote servers, or perform any other
normal Java operation.
package example.hello;
public class HelloWorldImpl implements HelloWorld {
public String sayHello(String name) {
return "Hello " + name; }
}
J2ME RMI
9
How to Use MeRMI (Server)

Once we have server side classes we then need to generate
the server side skeletons and the client side stubs
 These classes handle the network communications
between the client and the server. MeRMI provides a tool,
called mermic, to generate these classes

The following commmands will compile the server classes
and generate the MeRMI stubs and skeletons.
javac -d ./classes -classpath mermi-server.jar ./example/hello/*.java
mermic -d ./generated -cp ./classes example.hello.HelloWorld
J2ME RMI
10
How to Use MeRMI (Server)

Next Step to compile them and include them into a server side jar
file. The following commands do this.
javac -d ./classes -classpath mermi-server.jar;./classes
./generated/j2se/example/hello/*.java
jar -cvf hello.jar ./classes
The server then needs to be deployed into a MeRMI server. The MeRMI
server should already be installed as a servlet. (Tomcat)
Two Steps need to Deploy the Server
Step 1: hello.jar into the mermi/WEB-INF/lib directory of your MeRMI
server.
copy hello.jar /mermi/WEB-INF/lib
Step 2: register the Hello implementation with the server
Edit dregistrations.txt and add following line
HelloWorld=example.hello.HelloWorldImpl
J2ME RMI
11
How to Use MeRMI (Client)

It is a point that You will see MeRMI begins
to differ to the approach used in standard
RMI
 In standard RMI, the remote interface used
by the server is also used by the client.


Unfortunately J2ME devices find this approach
too heavy.
MeRMI generates new classes for the client
to use

These classes have the same methods as the
remote interface with slight changes, making
them more J2ME friendly
J2ME RMI
12
How to Use MeRMI (Client)

The following client class is generated by MeRMI for our
HelloWorld interface.
package example.hello;
import com.zonski.mermi.http.HttpRemoteProxy;
public class HelloWorldMicro extends HttpRemoteProxy {
public String sayHello(String name) throws IOException {
return (String)super.invoke(0, new Object[] {name});
}}
o The generated class extends HttpRemoteProxy, a base class
provided by MeRMI, which performs the remote invocations on the
server
o
Each method on the remote interface is reproduced on the
generated client class.
o
o
Our sayHello method is here, taking the same parameters and returning
the same value.
The method however, does not throw a RemoteException. Instead it
throws an IOException, which is part of the MIDP 1.0 API.
J2ME RMI
13
How to Use MeRMI (Client)

You can see that our sayHello method has been
implemented to use the base class method invoke.
 All generated methods use this same approach.
This method translates the invocation request
into a remote call and then reads the response
from the server.
 The details of how this works are hidden and it
is not nescessary to deal with this
implementation.
 In order to use this generated class we must
create a MIDlet that locates a reference to the
remote object.
J2ME RMI
14
How to Use MeRMI (Client)

MeRMI provides a Directory for performing this
operation.



Currently only the HttpDirectory exists, however in the
future new Directory implementations may be created to
support other protocols
The HttpDirectory is created with the url of the
running MeRMI server
The name used to register the hello
implementation is then used to lookup a reference
to the object.
J2ME RMI
15
How to Use MeRMI (Client)
import example.hello.HelloWorldMicro;
public class HelloMIDlet extends MIDlet {
protected void startApp() {
try {
HttpDirectory
directory = new HttpDirectory("http://localhost:8080/mermi/mermi");
HelloWorldMicro hello =
(HelloWorldMicro)directory.locate("HelloWorld");
String message = hello.sayHello(“Farooq"); //
} catch (IOException e) { // handle exception }
} // other MIDlet code here
}
J2ME RMI
16
How to Use MeRMI (Client)
import example.hello.HelloWorldMicro;
public class HelloMIDlet extends MIDlet {
HttpDirectory is
created with the url
protected void startApp() {
of the running
try {
MeRMI server
HttpDirectory
HttpDirectory
directory = new
directory = new
HttpDirectory("http://localhost:8080/mermi/mermi");
HelloWorldMicro
hello =
HttpDirectory("http://localhost:8080/mermi/mermi");
(HelloWorldMicro)directory.locate("HelloWorld");
String message = hello.sayHello(“Farooq"); //
} catch (IOException e) { // handle exception }
} // other MIDlet code here
}
J2ME RMI
17
How to Use MeRMI (Client)
import example.hello.HelloWorldMicro;
public class HelloMIDlet extends MIDlet {
lookup a
protected void startApp() {
reference to
try {
the object
HttpDirectory
HelloWorldMicro
directory = new
hello
=
HttpDirectory("http://localhost:8080/mermi/mermi");
(HelloWorldMicro)directory.locate("HelloWorld");
HelloWorldMicro
hello =
(HelloWorldMicro)directory.locate("HelloWorld");
String message = hello.sayHello(“Farooq"); //
} catch (IOException e) { // handle exception }
} // other MIDlet code here
}
J2ME RMI
18
How to Use MeRMI (Client)
import example.hello.HelloWorldMicro;
public class HelloMIDlet extends MIDlet {
protected void startApp() {
MeRMI performs
the remote
try {
invocations on
HttpDirectory
the server
directory = new
HttpDirectory("http://localhost:8080/mermi/mermi");
HelloWorldMicro hello =
String
message =
(HelloWorldMicro)directory.locate("HelloWorld");
hello.sayHello(“Farooq");
// Remote
Method
String
message = hello.sayHello(“Farooq");
// Remote
Method
Invocation
Invocation
} catch (IOException e) { // handle exception }
} // other MIDlet code here
}
J2ME RMI
19
Demo Phone Book
My SQL Client 
J2ME RMI
20
The END!

You can download all the demos
JARs from:
http://students.cs.wichita.edu/~fasheik
h/cs843/

Special Thanks to

Dr. Li Jia

Bioinformatics Dept WSU using facilities
J2ME RMI
21