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.services;
028:
029: import com.sun.cldc.isolate.*;
030: import com.sun.midp.links.*;
031: import java.io.*;
032:
033: final class IsolateSystemServiceRequestHandler implements
034: SystemServiceRequestListener {
035:
036: private Isolate serviceIsolate = null;
037: private Isolate clientIsolate = null;
038:
039: private SystemServiceManager serviceManager = null;
040: private SystemService requestedService = null;
041:
042: private SystemServiceConnectionLinks sendReceiveLinks = null;
043: private SystemServiceRequestProtocolAMS serviceRequestProtocol = null;
044:
045: IsolateSystemServiceRequestHandler(
046: SystemServiceManager serviceManager, Isolate clientIsolate) {
047:
048: this .serviceIsolate = Isolate.currentIsolate();
049: this .clientIsolate = clientIsolate;
050: this .serviceManager = serviceManager;
051:
052: Link sendLink = Link.newLink(serviceIsolate, clientIsolate);
053: Link receiveLink = Link.newLink(clientIsolate, serviceIsolate);
054: this .sendReceiveLinks = new SystemServiceConnectionLinks(
055: sendLink, receiveLink);
056:
057: serviceRequestProtocol = new SystemServiceRequestProtocolAMS(
058: this );
059: }
060:
061: SystemServiceConnectionLinks getSendReceiveLinks() {
062: return sendReceiveLinks;
063: }
064:
065: void handleServiceRequest() throws ClosedLinkException,
066: InterruptedIOException, IOException {
067:
068: serviceRequestProtocol.handleServiceRequest(sendReceiveLinks);
069: }
070:
071: public SystemServiceConnectionLinks onServiceRequest(
072: String serviceID) {
073: requestedService = serviceManager.getService(serviceID);
074: if (requestedService == null) {
075: return null;
076: }
077:
078: Link serviceToClient = Link.newLink(serviceIsolate,
079: clientIsolate);
080: Link clientToService = Link.newLink(clientIsolate,
081: serviceIsolate);
082: SystemServiceConnectionLinks connectionLinks = new SystemServiceConnectionLinks(
083: serviceToClient, clientToService);
084:
085: return connectionLinks;
086: }
087:
088: public void onLinksPassedToClient(
089: SystemServiceConnectionLinks connectionLinks) {
090:
091: if (connectionLinks == null || requestedService == null) {
092: throw new IllegalStateException();
093: }
094:
095: SystemServiceConnection serviceConnection = new SystemServiceConnectionImpl(
096: connectionLinks);
097:
098: synchronized (requestedService) {
099: requestedService.acceptConnection(serviceConnection);
100: }
101:
102: requestedService = null;
103: }
104: }
|