Oracle Application Server Wireless Java API Reference
B14043-01


oracle.panama.messaging.transport
Interface Driver

All Superinterfaces:
DriverBase

public interface Driver
extends DriverBase

The driver interface. Driver is used by Messenger to access devices. More devices can be supported by the transport system via plug in more drivers. To develop a driver, basically, you need to implement the driver interface. Your driver needs to be plugged into the messaging system to run.

When the messaging server starts up, it loads your driver and calls the init() method of your driver to initialize it. Once it is properly initialized, it can be used to send/receive messages. The destroy() method is called when the messaging server shuts down.

A sample driver looks like:


// Copyright (c) 2001 Oracle Corporation. All rights reserved.
package oracle.panama.messaging.transport.driver.sample;

import Java.util.Properties;
import Java.net.ServerSocket;
import Java.net.Socket;
import Java.io.BufferedReader;
import Java.io.PrintStream;
import Java.io.InputStreamReader;
import Java.io.IOException;
import Java.io.PrintWriter;
import Java.io.FileOutputStream;
import Java.text.SimpleDateFormat;
import oracle.panama.messaging.transport.*;
import oracle.panama.messaging.common.*;
import oracle.panama.model.DeliveryType;
import oracle.panama.util.MessageCatalog;
import oracle.panama.core.admin.L;

public class SimpleDriver implements Driver {

  private String mCompanyName;
  private String mDeliveryType;
  private String mVersion;
  private PrintWriter log = null;

  public int init(Properties properties) {
    // get the locator instance and various listeners
    TransportLocator locator = TransportLocator.getInstance();
    DriverController manager = locator.getDriverController();
    mMessageListener = manager.getMessageListener();
    mStatusListener = manager.getStatusListener();
    // read pr operties
    mCompanyName = properties.getProperty("company-name");
    // delivery type is needed. Use SMS
    mDeliveryType = DeliveryType.SMS.getName();
    mVersion = "1.0";
    int status = Driver.FAILED;
    try {
      String logName = properties.getProperty("logfile");
      if (logName == null) logName = new String("SimpleDriver.log");
      log = new PrintWriter(new FileOutputStream(logName, true ) );
    } catch(Exception e) {
      e.printStackTrace();
      return status;
    }
    log ("initialized: " + new Java.util.Date());
    mPort = Integer.parseInt(properties.getProperty("port"));
    mDelay = 20000; // 20 seconds
    mMessage = new Message();
    mSemaphore = new Object();
    status = Driver.SEND_RECEIVE; // TODO - verify the return code
    mStatus = new Status();
    log ("init complete");
    return status;
  }

  public void destroy() {
    try {
      log ("destroy");
      mServerSocket.close();
      mReader.close();
      mWriter.close();
      log ("destroy comp lete");
    }
    catch (Exception e) {
    }
  }

  public Parameter[] getParameters() {
    Parameter[] parameters = new Parameter[3];
    parameters[0] = new Parameter("company-name", "a company name", true, null);
    parameters[1] = new Parameter("logfile", "the log file name", false,
    "SimpleDriver.log");
    parameters[2] = new Parameter("port", "the listening port of the driver", true,
    null);
    return parameters;
  }

  public String getVersion() {
    return mVersion;
  }

  public String getInfo() {
    return "Simple Driver";
  }

  public String send(String dtype, String address, int mode, String encoding,
    int tracking, int expiration, int reliability, int priority, String fromAddr,
    String replyToAddr, String mid, Message message) {
    log ("send: " + address + " => " + message.getContent());
    String id = null;
    try {
      id = mid + ':' + address;
      mWriter.println(id);
      mWriter.println(message.getContent());
      mWriter.flush();
    }
    catch (Exception e) {
      // not synchronized, it works for this toy.
      mWriter = null;
      mReader = null;
      id = null;
    }
    log ("sent id: " + id );
    return id;
  }

  public String[] send(String dtype, String[] addresses, int[] modes,
    String encoding, int tracking, int expiration, int reliability,
    int priority, String fromAddr, String replyToAddr,
    String mid, Message message) {
    String[] ids = null;
    log ("send: mult iple => " + message.getContent());
    try {
      int count = addresses.length;
      ids = new String[count];
      String id = mid + ':' + addresses[0];
      ids[0] = id;
      mWriter.print(id);
      for (int i=1; i<count; i++) {
        id = mid + ':' + addresses[i];
        ids[i] = id;
        mWriter.print(',' + id);
      }
      mWriter.println();
      mWriter.println(message.getContent());
      mWriter.flush();
    }
    catch (Exception e) {
      // no t synchronized, it works for this toy.
      mWriter = null;
      mReader = null;
      ids = null;
    }
    log ("sent multiple");
    return ids;
  }

  public String[] send(String[] dtypes, String[] addresses, int[] modes,
    String encoding, int tracking, int expiration, int reliability,
    int priority, String fromAddr,String replyToAddr, String mid,
    Message message) throws DriverException {
    String[] ids = null;
    int count = addresses.length;
    log (" send: " + count + " recipients : " + message.getContent());
    ids = new String[count];
    for (int i=0; i<count; i++) {
      ids[i] = send(dtypes[i], addresses[i], modes[i], encoding, tracking,
      expiration, reliability, priority, fromAddr,
      replyToAddr, mid, message);
    }
    return ids;
  }

  public void getStatus(String mid) {
  }

  public void getStatus(String[] mids) {
  }

  public boolean queryNotifying() {
    return false;
  }

  public boolean queryTracking() {
    return false;
  }

  public void receive() {
    log ("receive started");
    synchronized (mSemaphore) {
      try {
        if (mServerSocket == null) {
          try {
            mServerSocket = new ServerSocket(mPort);
            mServerSocket.setSoTimeout(mDelay);
          }
          catch (IOException ioe) {
            mServerSocket = null;
            mSocket = null;
            throw ioe;
          }
        }
        if (mSocket == null) {
          try {
            mSocket = mServerSocket.accept();
            mSocket.setSoTimeout(mDelay);
          }
          catch (IOException ioe) {
            mSocket = null;
            throw ioe;
          }
        }
        if (mReader == null) {
          mReader = new BufferedReader(
          new InputStreamReader(mSocket.getInputStream()));
          mWriter = new PrintStream(mSocket.getOutputStream());
        }
        String buf = mReader.readLine();
        log ("receive read: " + buf);
        if (buf.charAt(0) == '*') {
          String address = buf.substring(1);
          mMessage.setContent(mReader.readLine());
          DeviceInfo info = new DeviceInfo();
          info.setDeliveryType(mDeliveryType);
          info.setEncoding("7b");
          String from = "FROM-ME-TODO";
          mMessageListener.onMessage(from, info, address, mMessage);
          log ("message sent to message listener");
        }
        else {
          mStatus.setContent("received");
          mStatusListener.onStatus(buf.substring(1), mStatus);
          log ("status sent to status listener");
        }
      }
      catch (IOException ioe) {
        mReader = null;
        mWriter = null;
      }
    }
  }

  void log(String message) {
    if( log != null ) {
      synchronized( log ) {
        String currentTime = new SimpleDateFormat(
          "yyyy-MM-dd HH:mm:ss").format( new Java.util.Date() );
        log.println(currentTime + " " + message);
        log.flush();
      }
    }
  }

  private Socket mSocket;
  private Object mSemaphore;
  private ServerSocket mServerSocket;
  private MessageListener mMessageListener;
  private StatusListener mStatusListener;
  private BufferedReader mReader;
  private PrintStream mWriter;
  private Message mMessage;
  private Status mStatus;
  private int mDelay;
  private int mPort;
}

  
See Also:
XDriver

Field Summary

Fields inherited from interface oracle.panama.messaging.transport.DriverBase
FAILED, MESSAGE_ID_MAX_LENGTH, RECEIVE, SEND, SEND_RECEIVE

Method Summary
java.lang.String[] send(java.lang.String[] dtypes, java.lang.String[] addresses, int[] modes, java.lang.String encoding, int tracking, int expiration, int reliability, int priority, java.lang.String fromAddr, java.lang.String replyToAddr, java.lang.String mid, Message message)
Send a message to a list of addresses with this driver.
java.lang.String[] send(java.lang.String dtype, java.lang.String[] addresses, int[] modes, java.lang.String encoding, int tracking, int expiration, int reliability, int priority, java.lang.String fromAddr, java.lang.String replyToAddr, java.lang.String mid, Message message)
Send a message to a list of addresses with this driver.
java.lang.String send(java.lang.String dtype, java.lang.String address, int mode, java.lang.String encoding, int tracking, int expiration, int reliability, int priority, java.lang.String fromAddr, java.lang.String replyToAddr, java.lang.String mid, Message message)
Send a message to a single address with this driver.

Methods inherited from interface oracle.panama.messaging.transport.DriverBase
destroy, getInfo, getParameters, getStatus, getStatus, getVersion, init, queryNotifying, queryTracking, receive

Method Detail

send

public java.lang.String[] send(java.lang.String[] dtypes,
                               java.lang.String[] addresses,
                               int[] modes,
                               java.lang.String encoding,
                               int tracking,
                               int expiration,
                               int reliability,
                               int priority,
                               java.lang.String fromAddr,
                               java.lang.String replyToAddr,
                               java.lang.String mid,
                               Message message)
                        throws DriverException
Send a message to a list of addresses with this driver.
Parameters:
dtypes - the delivery types of correspondent destinations.
addresses - the addresses to send to.
modes - the delivery modes.
encoding - the encoding of the device.
tracking - the tracking level.
expiration - the expiration time.
reliability - the reliability level.
priority - the priority level.
fromAddr - the from-address.
replyToAddr - the reply-to-address.
mid - the unique id of the message.
message - the message to send.
Returns:
a list of unique message ids, null if failed.
Throws:
DriverException.
DriverException

send

public java.lang.String[] send(java.lang.String dtype,
                               java.lang.String[] addresses,
                               int[] modes,
                               java.lang.String encoding,
                               int tracking,
                               int expiration,
                               int reliability,
                               int priority,
                               java.lang.String fromAddr,
                               java.lang.String replyToAddr,
                               java.lang.String mid,
                               Message message)
                        throws DriverException
Send a message to a list of addresses with this driver.
Parameters:
dtype - the delivery type of all destinations.
addresses - the addresses to send to.
modes - the delivery modes.
encoding - the encoding of the device.
tracking - the tracking level.
expiration - the expiration time.
reliability - the reliability level.
priority - the priority level.
fromAddr - the from-address.
replyToAddr - the reply-to-address.
mid - the unique id of the message.
message - the message to send.
Returns:
a list of unique message ids, null if failed.
Throws:
DriverException.
DriverException

send

public java.lang.String send(java.lang.String dtype,
                             java.lang.String address,
                             int mode,
                             java.lang.String encoding,
                             int tracking,
                             int expiration,
                             int reliability,
                             int priority,
                             java.lang.String fromAddr,
                             java.lang.String replyToAddr,
                             java.lang.String mid,
                             Message message)
                      throws DriverException
Send a message to a single address with this driver. Drivers implement this method to perform whatever is appropriate for their particular protocols to send out messages. The content to delivery is stored in the Message object passed onto the send() method, while the address parameter specifies one or more recipients to deliver the message to. Further, the driver is expected to return a unique id for each recipient, or ids one for each of the recipients. These ids will be used by the transport to query status of the delivery when necessary. The unique message id passed to the driver can be used as the base to generate the messaged id(s) for the recipient(s). The driver must return a null message ID to make the transport retry. Exceptions thrown out of the send method are ignored, except for exceptions of type DriverException. If the send () method throws an exception of the type DriverException, the transport will not retry. If the code of the exception is marked fatal, the sending capability of this driver instance is revoked. If the exception is not marked fatal, the driver will still be used to send other messages. If a valid message id is returned, its length should not have more than MESSAGE_ID_MAX_LENGTH characters.
Parameters:
dtype - the delivery type of the destination.
address - the address to send to.
mode - the delivery mode.
encoding - the encoding of the device.
tracking - the tracking level.
expiration - the expiration time.
reliability - the reliability level.
priority - the priority level.
fromAddr - the from-address.
replyToAddr - the reply-to-address.
mid - the unique id of the message.
message - the message to send.
Returns:
a unique message id, null if failed.
Throws:
DriverException.
DriverException

Oracle Application Server Wireless Java API Reference
B14043-01


Copyright © 2004 Oracle Corporation. All Rights Reserved.