JBoss.orgCommunity Documentation

Chapter 2. Resource Adaptor Type

2.1. Activities
2.2. Events
2.3. Activity Context Interface Factory
2.4. Resource Adaptor Interface
2.5. Restrictions
2.6. Sbb Code Examples
2.6.1. WRITE Request Event Handling
2.6.2. READ Request Event Handling with event suspending

The Resource Adaptor Type is the interface which defines the contract between the RA implementations, the SLEE container, and the Applications running in it.

The name of the RA Type is TftpServerResourceAdaptorType, its vendor is org.mobicents and its version is 1.0.

The Resource Adaptor Type defines one activity object, net.java.slee.resource.tftp.TransferActivity.

The TransferActivity represents a TFTP transfer. It is created on reception of a TFTP requests, and ends on end-of-file, closure of the datastream, or by an SBB invoking sendError() on the TransferActivity Object. The activity object interface is defined as follows:



        
package net.java.slee.resource.tftp;
public interface TransferActivity {
    public String getTransferId();
    public InputStream getInputStream() throws IOException;
    public OutputStream getOutputStream() throws IOException;
    public void receiveFile(String filename) throws FileNotFoundException, IOException;
    public void receiveFile(File file) throws FileNotFoundException, IOException;
    public void sendFile(String filename) throws FileNotFoundException, IOException;
    public void sendFile(File file) throws FileNotFoundException, IOException;
    public void sendError(int errorCode, String reason);
}
     

The Events fired by TFTP Server Resource Adaptor represent an incoming TFTP Request. The table below lists the Resource Adaptor Type event types.


Important

Spaces where introduced in Name and Event Class column values, to correctly render the table. Please remove them when using copy/paste.

All event types use the same type net.java.slee.resource.tftp.events.RequestEvent. It's interface is as follows:



        
package net.java.slee.resource.tftp.events;
import org.apache.commons.net.tftp.TFTPPacket;
public interface RequestEvent {
    public TFTPPacket getRequest(); 
    public String getTypeDescr();
    public String getId();
}
     
The getRequest() method:

Retrieves the TFTP request packet which is associated with the event.

The getTypeDescr() method:

Retrieves the type description associated with the request.

The getId() method:

Retrieves unique event ID.

The Resource Adaptor's Activity Context Interface Factory is of type net.java.slee.resource.tftp.TftpServerActivityContextInterfaceFactory, it allows the SBB to retrieve the ActivityContextInterface related with an existing Resource Adaptor activity object. The interface is defined as follows:



        
package net.java.slee.resource.tftp;
import javax.slee.ActivityContextInterface;
import javax.slee.FactoryException;
import javax.slee.UnrecognizedActivityException;
public interface TftpServerActivityContextInterfaceFactory {
    public ActivityContextInterface getActivityContextInterface(
            TransferActivity activity) throws NullPointerException,
            UnrecognizedActivityException, FactoryException;
}
     

The TFTP Server Resource Adaptor interface is currently not defined.

There are currently no known restrictions

The following code shows how to use the Resource Adaptor Type for common functionalities

The following code handles a TFTP WRITE request:



            
    import net.java.slee.resource.tftp.TransferActivity;
    import net.java.slee.resource.tftp.events.RequestEvent;
    import org.apache.commons.net.tftp.TFTPWriteRequestPacket;
    import org.apache.commons.net.tftp.TFTPRequestPacket;
    public void onWrite(RequestEvent event, ActivityContextInterface aci) {
        TFTPWriteRequestPacket req = (TFTPWriteRequestPacket) event.getRequest();
        if (isSpecialRequest(req) {
            doSpecialThings(req);
        } else {
            // just receive data and create the requested file
            TransferActivity activity = (TransferActivity) aci.getActivity();
            try {
                activity.receiveFile(req.getFilename());
            } catch (FileNotFoundException e) {
                activity.sendError(TFTPErrorPacket.FILE_NOT_FOUND, e.getMessage());
                return;
            } catch (Exception e) {
                activity.sendError(TFTPErrorPacket.UNDEFINED, e.getMessage());
                return;
            }
        }
    }
    private boolean isSpecialRequest(TFTPRequestPacket packet) {
        // inspect packet and decide whether it needs special treatment
        return false;
    }
    private void doSpecialThings(TFTPRequestPacket packet) {
        // actually do the special things that need to be done.
        ...;
    }
        

The following code handles a TFTP READ request:



            
    import net.java.slee.resource.tftp.TransferActivity;
    import net.java.slee.resource.tftp.events.RequestEvent;
    import org.apache.commons.net.tftp.TFTPReadRequestPacket;
    import org.apache.commons.net.tftp.TFTPRequestPacket;
    // CMP fields
    public abstract void setSuspendedEventCmp(EventContext ctxt);
    public abstract EventContext getSuspendedEventCmp();
    public void onRead(RequestEvent event, ActivityContextInterface aci,
                        EventContext ctxt) {
        TFTPReadRequestPacket req = (TFTPReadRequestPacket) event.getRequest();
        if (isSpecialRequest(req) {
            doSpecialThings(req);
        } else {
            // just send the requested file
            TransferActivity activity = (TransferActivity) aci.getActivity();
            try {
                activity.sendFile(req.getFilename());
            } catch (FileNotFoundException e) {
                activity.sendError(TFTPErrorPacket.FILE_NOT_FOUND, e.getMessage());
                return;
            } catch (Exception e) {
                activity.sendError(TFTPErrorPacket.UNDEFINED, e.getMessage());
                return;
            }
        }
    }
    private boolean isSpecialRequest(TFTPRequestPacket packet) {
        // inspect packet and decide whether it needs special treatment
        return false;
    }
    private void doSpecialThings(TFTPRequestPacket packet, EventContext ctxt) {
        // actually do the special thing that needs to be done, starting some
        // other activity or whatever.
        ...;
    }
    // the other -triggering- event that'll continue the read-transfer.
    public void onSomeOtherRelatedEvent(RelatedEvent event, ActivityContextInterface aci) {
        try {
            OutputStream os = getRequestActivity().getOutputStream();
            os.write("whatever data needs to be written".getBytes());
            os.flush(); os.close();
        } catch (Exception e) {
            
        }
    }
    private TransferActivity getRequestActivity() {
        for (ActivityContextInterface aci : sbbContext.getActivities())
            if (aci.getActivity() instanceof TransferActivity)
                return (TransferActivity) aci.getActivity();
        return null;
    }