|
Overview
The Net Caboodle mobile gateway allows you to connect
your mobile clients to your existing business systems. In this article we look
at how to write a simple mobile client that shares data with other mobile and
fixed line clients via a relational database.
To implement the example messaging system we are going
to
- Create a "connector" bean that uses
JDBC to access the database
- Use the Net Caboodle stub generator to create
the mobile stubs
- Write a MIDP client that can send and receive
messages via the connector bean.
Setting up the database
The first thing we need to do is to create a simple database.
To keep this example simple we have created two tables in the database as described
below.
- Users - contains login information for the users
of the system
<ID>,<name>,<password>
- Messages - stores the messages sent by the users
<messageID>,<userID>,<sender>,<sentAt>,<message>
If you are using Windows and have Microsoft Access
installed on your computer you can use the database file supplied with the example
code.
Writing a connector bean
Having set-up the database and added a few users
to the Users table the next step is to write a class that contains the methods
that we want our mobile (MIDP) client to use -- these are
- login
- getUsers
- getMessage
- sendMessage
Having determined the methods we need to implement let's
take a look at some code -- below is the code for the beans constructor, which
is used to initialize the JDBC driver we are using (in this case the ODBC bridge)
/**
* Create a MessageServiceBean
* JDBC initialization can occur here or in a static initializer
*/
public MessageServiceBean(){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
dbCon=DriverManager.getConnection("jdbc:odbc:message-db");
dbCon.setAutoCommit(true);
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
Next let's take a look at the login method, which actually
uses the database.
/**
* The login method simply queries the database
* @return either the user's ID or -1 if login fails
*/
public int login(String userName,String password) throws Exception{
ResultSet rs=null;
Statement stmt=null;
String query="select * from users where name='"+userName+
"' and password='"+password+"'";
try{
stmt=dbCon.createStatement();
rs=stmt.executeQuery(query);
if(rs.next()){
return rs.getInt(1);
}
}finally{
if(rs!=null){rs.close();}
if(stmt!=null){stmt.close();}
}
return -1;
}
The remaining methods pretty much follow the same pattern.
Please refer to the example source code to see how the complete class has been
implemented.
Testing the connector code
Although we refer to the connector code as a bean, the
code is actually just a class, which means we can write another class with a
main() to test the connector code. Testing your connector code this way means
that by the time you use the Net Caboodle stub generator your connector code
should be robust -- let's take a look at an example test class.
public static void main(String[] args) {
try{
MessageServiceBean service=new MessageServiceBean();
int userId=service.login("spock",args[0]);
System.out.println("Spock logged in OK id="+userId);
//print out the registered users
Vector users=service.getUsers();
Iterator iter=users.iterator();
while(iter.hasNext()){
System.out.println(iter.next());
}
//send a message to Kirk
service.sendMessage(2,"spock","Illogical captain");
}catch(Exception ex){
ex.printStackTrace();
}
}
Having written and tested the connector code you need
to create a JAR file for the code, which will be used later on to generate the
mobile stubs required for the mobile client to be able to "talk to"
the bean.
Create a new MIDP project
The next step is to create a new MIDP project for the
mobile client application -- please refer to the Eclipse
or NetBeans plugin documentation
for information about creating new projects
Generating the mobile stubs
Before the Net Caboodle plugins can generate the mobile
stubs you must first start the Net Caboodle gateway.
- Eclipse plugin
Right-click on your MIDP project and select "Start Gateway" from
the "Caboodle" Menu.
- NetBeans plugin
Use the "Tools->Net Caboodle Gateway->Start Gateway" option.
Once the gateway is up and running we can generate the
mobile stubs and then get on to the fun stuff -- writing the mobile client application.
- Eclipse plugin
Right-click on your MIDP project and select "New bean connector"
from the "Caboodle" Menu.
- NetBeans plugin
First click on the "File" tab and select your MIDP project - this
is to activate the required menus.
Then, use the "Tools->Net Caboodle Business 2 Mobile->New Connector
" option and then select the "Bean Connector" tab.
Next use the select directory button on the dialog and
choose the JAR file that contains the connector code we wrote earlier. If you
just want to use the example code out of the box, then select "service-bean,jar"
that is supplied with the example code.
You will now be presented with a list of the classes contained
in the JAR file as shown below
| Eclipse plugin |
NetBeans & Intelli J plugin |
 |
 |
Select the "MessageServiceBean" and the click
the OK button. The Net Caboodle plugin will now generate the mobile stubs.
- Eclipse plugin
Net Caboodle will have added the mobile stubs JAR to your project.
- NetBeans plugin
Net Caboodle will have added the mobile stubs JAR to your project.
The stub generator will have created classes that map
directly to those used by the connector bean, plus an additional factory class
that allows the mobile client to obtain a 'shadow' proxy to the connector bean.
In the supplied example code these are
- com.nc.example.MessageServiceBean
- com.nc.example.MessageServiceBeanFactory
- com.nc.example.Message
- com.nc.example.User
We should now be ready to implement the mobile client
code.
Writing the mobile messenger client
Before delving into the code in depth, here's the basic
code to log into the MessageServiceBean...
//get the shadow proxy
MessageServiceBean service = MessageServiceBeanFactory.getService();
//login to service
int userID=service.login( user, pwd );
As you can see writing the mobile client looks virtually
identical to the test code we wrote earlier. The only difference is that you
obtain a reference to the MessageServiceBean via a factory, rather than instantiating
the bean directory. To learn about what is going on under the hood please refer
to the article Taking
Service-Oriented Architectures Mobile, Part 1
Below is a snippet of the supplied example MIDlet, where
we have assigned the "service" variable to a field in the MIDlet.
private void listUsers() {
try {
users = service.getUsers();
int nUsers = users.size();
String[] userList = new String[nUsers];
for (int i = 0; i < nUsers; i++) {
User user = (User) users.elementAt(i);
userList[i] = user.getName();
}
List mainList = new List("Registered Users", Choice.IMPLICIT, userList, null);
mainList.addCommand(CMD_BACK);
mainList.addCommand(CMD_WRITE);
mainList.setCommandListener(this);
display.setCurrent(mainList);
} catch (Exception ex) {
showException(ex);
}
}
Configuring the gateway to load additional libraries
If you are using this example with a database that doesn't
use the ODBC bridge, then you need to use the Net Caboodle plugin's Gateway
Preferences option to add your JDBC driver's JAR file.
The end result
Having implement the MIDlet, we now have a very basic
mobile client that can retrieve and store messages in a relational database,
below are a few screenshots of how the MIDlet (in the example code) looks and
works.
As you can see it's not as pretty as the full-blown demo
version of the system we have build, but hopefully it illustrates how easy it
is to write mobile clients that access business services, plus gives you a starting
point to develop similar systems for yourself.
Summary
In this article we have shown how to create a connector
bean to provide a bridge between the Net Caboodle gateway and a relational database.
Writing connector beans provides an easy way to integrate
existing systems that do not have already have a remote service interface.
The mobile stubs generated by the Net Caboodle plugin
remove the need to write any of your own code for a mobile business application
to communicate with existing business services.
All communication between the mobile stubs and the Net
Caboodle Gateway are encrypted.
By using the Net Caboodle gateway
authentication instead of the simple database authentication shown in this
example, it is possible to stop any mobile client accessing business service
with having first been authenticated by the gateway.
Downloads
Related documents
Message
Board Example
This example includes the source code for a simple POJO (Plain Old Java
Object) service and for a mobile MIDP client that can send and receive messages
to/from other mobile users without storing the messages in a database.
Site Engineer Example
This example explains how to create a mobile application that allows service
engineers in the field to access and order the equipment and parts they need
to repair applicances.
MIDP File Sharing Example
In this example we take a look at how to write a simple POJO (Plan Old Java
Object) service that allows MIDP clients to store data remotely for either personal
or public consumption -- you can think of this example like an Internet file
sharing service for Mobile devices.
Support and Feedback
If you have any questions relating to this article please
contact us
|