MIDP file & resource sharing Example

Introduction

The Net Caboodle platform makes it quick and easy to write and deploy mobile applications that access business services.

In this simple example we are going to 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.

The POJO service we are going to write will be managed by the Net Caboodle Gateway and will be executing on a gateway host that is accessible OTA (over the air) from the device.

Storing files and objects on the device

MIDP applications can store information as "records" in the RMS on the device, but the amount of available storage is dictated by the device itself and can be very limited, from anything as low a 1KB all the way up to Megabytes of data. This presents challenges when writing mobile applications -- how do we know if all the data a user wants to save can actually fit onto the device?

The usual course of action is to prompt the user to say that the device is low on storage space, and ask the user to delete some of the information the mobile application has saved. Now whilst this approach works well, in practice it can leave the user quite frustrated, for example do I delete the picture of my favorite pet or the picture I took last night at the company party?

An alternative approach is to provide the user with the option to move some of their data to an remote storage system, e.g. Upload the picture from the office party to the Internet (just in case I need it later! ) and keep the picture of my favorite pet available to the application locally.

File sharing

The ability to upload information from a mobile application for personal storage and retrieval is useful in its own capacity, but why stop there?

There are plenty of mobile applications that can benefit from online file sharing, for example, the users of a mobile picture capture and cataloging application, may want to publish their pictures so other people can browse and download them to their phone.

The number of possible mobile applications that could benefit from online file sharing is almost limitless.

Code example

The code example below shows a simple POJO that acts as the remote file sharing service.

By using the Net Caboodle plugins you can simply write and import the any CLDC1.0 compatible POJO/RMI or Jini Service and use the generated stubs in your MIDP application.

MIDP Code example

//get an instance of the FileSevice
FileService service=FileServiceFactory.getService();

//Data is stored in the RMS as byte [] records
//so we can send the byte [] to the remote service instead

service.saveFile("myapp","myfile",bytes);

//the MIDP client can also retrieve remote files
byte [] getFile("myapp","myfile");


Gateway connector code


/**
 * A simple POJO (Plain old Java Object) service connector
 * example for Net Caboodle v3.5.1 or above.
 *
 * Net Caboodle service connectors provide a simple way
 * for your MIDP applications to use remote services, without
 * you having to write any protocol specific code
 * All data transfered from a Net Caboodle enabled MIDP client
 * via the Caboodle gateway is also encrypted.
 *
 * @author Net Caboodle
 */
public class FileService {
    
    
    private static String STORAGE_ROOT="STORAGE_ROOT";
    private static File publicFolders;
    
    /** Required public no-args constructor */
    public FileService() {
        publicFolders=new File(STORAGE_ROOT+"/public");
        publicFolders.mkdirs();
    }
    
    /**
     * Example method to save a file
     */
    public void saveFile(String category, String fileName, byte [] file) throws Exception{
        File cat=new File(publicFolders.getAbsolutePath()+"/"+convertFileName(category));
        cat.mkdirs();
        storeFile(cat,fileName,file);
    }
    /**
     * List all the public categories of files
     */
    public Vector listPublicCategories(int startIndex, int endIndex){
        Vector catList=new Vector();
        File [] dirs=publicFolders.listFiles();
        for(int i=0;dirs!=null && i=startIndex && i<=endIndex){
                if(dirs[i].isDirectory()){
                    catList.add(dirs[i].getName());
                }
            }
        }
        return catList;
    }
    /**
     * Get a serialized file based on a category/file name
     */
    public byte [] getFile(String category,String fileName) throws Exception{
        File fileFile=new File(publicFolders.getAbsolutePath()+
                "/"+convertFileName(category)+"/"+
                convertFileName(fileName));
        
        if(!fileFile.exists()){
            throw new Exception("["+category+"] "+fileName+" does not exist");
        }
        long len=fileFile.length();
        byte [] bytes=new byte[(int)len];
        FileInputStream fis=new FileInputStream(fileFile);
        fis.read(bytes);
        
        return bytes;
    }
   
    /**
     * Save the file
     */
    private void storeFile(File dir,String fileName,byte [] bytes) throws Exception{
        FileOutputStream fos=new FileOutputStream(dir.getAbsolutePath()+"/"+convertFileName(fileName));
        fos.write(bytes);
        fos.close();
    }
    
}

Summary

The above example shows a very basic implementation of a remote file storing/sharing service that can be built and deployed with Net Caboodle in a fraction of the time it would take to write all your own protocol and connection code.

The example shows sending and returning byte [] objects, because this is how you store records on a MIDP device (using the RMS API), however, for MIDP applications that need to always store database to a remote filesystem, simple define your POJO, RMI or Jini interface to send and receive Java objects instead of byte arrays, for example for a MIDP phonebook, you could declare the service interface to take a VCard object for storage and the write your service to serialize that Java object to the disk.

For more information about using Net Caboodle please contact contact us

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.

Database connector Example
In this article (which include a source code download) we explain how to create a mobile (MIDP) messaging client that reads and stores messages in a relational 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.

Downloads

Support and Feedback

If you have any questions relating to this article please contact contact us

 

 

 

Copyright notice

The information within this document remains the sole property of Net Caboodle - www.netcaboodle.com

No part of this document may be copied or reproduced in any form or by any means, and the information
contained within it is not to be communicated to a third party, without the prior written consent of
Net Caboodle. All trademarks remain the property of their respective owners. NetCaboodle, Caboodle server
and Raven are all trademarks of Net Caboodle.