FileDocCategorySizeDatePackage
NamedLinkPortal.javaAPI DocphoneME MR2 API (J2ME)5179Wed May 02 18:00:10 BST 2007com.sun.midp.links

NamedLinkPortal

public final class NamedLinkPortal extends Object

Fields Summary
static final String
START_SESSION_STR
static final String
END_SESSION_STR
static Hashtable
links
static boolean
linksSent
static boolean
linksReceived
Constructors Summary
private NamedLinkPortal()

    
Methods Summary
public static LinkgetLink(java.lang.String name)


         
        if (!linksReceived) {
            throw new IllegalStateException();
        }

        Link link = (Link)links.get(name);
        if (link == null) {
            throw new IllegalArgumentException();
        }

        return link;
    
public static voidputLink(java.lang.String linkName, Link link)

        if (linksSent) {
            throw new IllegalStateException();
        }

        if (linkName == null || link == null) {
            throw new IllegalArgumentException();
        }

        links.put(linkName, link);
    
public static voidreceiveLinks(Link receiveLink)


        /**
         * Arguments sanity checks
         */

        if (receiveLink == null) {
            throw new IllegalArgumentException();
        }

        if (linksReceived) {
            throw new IllegalStateException();
        }

        // start session
        LinkMessage startSessionMsg = receiveLink.receive();
        String startSessionStr = startSessionMsg.extractString();
        if (!startSessionStr.equals(START_SESSION_STR)) {
            throw new IllegalStateException();
        }

        Hashtable l = new Hashtable();

        while (true) {
            LinkMessage strMsg = receiveLink.receive();
            String str = strMsg.extractString();
            if (str.equals(END_SESSION_STR)) {
                break;
            }

            String linkName = str;
            LinkMessage linkMsg = receiveLink.receive();
            Link link = linkMsg.extractLink();
            if (!link.isOpen()) {
                throw new IllegalStateException();
            }

            l.put(linkName, link);
            
        }

        links = l;
        linksReceived = true;
    
public static voidsendLinks(Link sendLink)


        /**
         * Arguments sanity checks
         */

        if (sendLink == null) {
            throw new IllegalArgumentException();
        }

        if (linksSent) {
            throw new IllegalStateException();
        }

        Enumeration linksEnum = links.elements();
        while (linksEnum.hasMoreElements()) {
            Link link = null;
            try {
                link = (Link)linksEnum.nextElement();
            } catch (ClassCastException e) {
                throw new IllegalStateException();
            }

            if (!link.isOpen()) {
                throw new IllegalStateException();
            }
        }

        // start session
        LinkMessage startSessionMsg = LinkMessage.newStringMessage(
                START_SESSION_STR);
        sendLink.send(startSessionMsg);

        // send named links
        Enumeration linkNamesEnum = links.keys();
        while (linkNamesEnum.hasMoreElements()) {
            String linkName = (String)linkNamesEnum.nextElement();
            Link link = (Link)links.get(linkName);
            if (link == null) {
                throw new IllegalStateException();
            }

            LinkMessage linkNameMsg = LinkMessage.newStringMessage(linkName); 
            sendLink.send(linkNameMsg);

            LinkMessage linkMsg = LinkMessage.newLinkMessage(link); 
            sendLink.send(linkMsg);
        }

        // end session
        LinkMessage endSessionMsg = LinkMessage.newStringMessage(
                END_SESSION_STR);
        sendLink.send(endSessionMsg);

        linksSent = true;