001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.midp.links;
028:
029: import java.util.Hashtable;
030: import java.util.Enumeration;
031: import java.io.IOException;
032: import java.io.InterruptedIOException;
033:
034: public final class NamedLinkPortal {
035: final static String START_SESSION_STR = "Starting sending named links";
036: final static String END_SESSION_STR = "Finished sending named links";
037:
038: static Hashtable links = new Hashtable();
039: static boolean linksSent = false;
040: static boolean linksReceived = false;
041:
042: public static Link getLink(String name) {
043: if (!linksReceived) {
044: throw new IllegalStateException();
045: }
046:
047: Link link = (Link) links.get(name);
048: if (link == null) {
049: throw new IllegalArgumentException();
050: }
051:
052: return link;
053: }
054:
055: public static void putLink(String linkName, Link link) {
056: if (linksSent) {
057: throw new IllegalStateException();
058: }
059:
060: if (linkName == null || link == null) {
061: throw new IllegalArgumentException();
062: }
063:
064: links.put(linkName, link);
065: }
066:
067: public static void sendLinks(Link sendLink)
068: throws ClosedLinkException, InterruptedIOException,
069: IOException {
070:
071: /**
072: * Arguments sanity checks
073: */
074:
075: if (sendLink == null) {
076: throw new IllegalArgumentException();
077: }
078:
079: if (linksSent) {
080: throw new IllegalStateException();
081: }
082:
083: Enumeration linksEnum = links.elements();
084: while (linksEnum.hasMoreElements()) {
085: Link link = null;
086: try {
087: link = (Link) linksEnum.nextElement();
088: } catch (ClassCastException e) {
089: throw new IllegalStateException();
090: }
091:
092: if (!link.isOpen()) {
093: throw new IllegalStateException();
094: }
095: }
096:
097: // start session
098: LinkMessage startSessionMsg = LinkMessage
099: .newStringMessage(START_SESSION_STR);
100: sendLink.send(startSessionMsg);
101:
102: // send named links
103: Enumeration linkNamesEnum = links.keys();
104: while (linkNamesEnum.hasMoreElements()) {
105: String linkName = (String) linkNamesEnum.nextElement();
106: Link link = (Link) links.get(linkName);
107: if (link == null) {
108: throw new IllegalStateException();
109: }
110:
111: LinkMessage linkNameMsg = LinkMessage
112: .newStringMessage(linkName);
113: sendLink.send(linkNameMsg);
114:
115: LinkMessage linkMsg = LinkMessage.newLinkMessage(link);
116: sendLink.send(linkMsg);
117: }
118:
119: // end session
120: LinkMessage endSessionMsg = LinkMessage
121: .newStringMessage(END_SESSION_STR);
122: sendLink.send(endSessionMsg);
123:
124: linksSent = true;
125: }
126:
127: public static void receiveLinks(Link receiveLink)
128: throws ClosedLinkException, InterruptedIOException,
129: IOException {
130:
131: /**
132: * Arguments sanity checks
133: */
134:
135: if (receiveLink == null) {
136: throw new IllegalArgumentException();
137: }
138:
139: if (linksReceived) {
140: throw new IllegalStateException();
141: }
142:
143: // start session
144: LinkMessage startSessionMsg = receiveLink.receive();
145: String startSessionStr = startSessionMsg.extractString();
146: if (!startSessionStr.equals(START_SESSION_STR)) {
147: throw new IllegalStateException();
148: }
149:
150: Hashtable l = new Hashtable();
151:
152: while (true) {
153: LinkMessage strMsg = receiveLink.receive();
154: String str = strMsg.extractString();
155: if (str.equals(END_SESSION_STR)) {
156: break;
157: }
158:
159: String linkName = str;
160: LinkMessage linkMsg = receiveLink.receive();
161: Link link = linkMsg.extractLink();
162: if (!link.isOpen()) {
163: throw new IllegalStateException();
164: }
165:
166: l.put(linkName, link);
167:
168: }
169:
170: links = l;
171: linksReceived = true;
172: }
173:
174: private NamedLinkPortal() {
175: }
176: }
|