001: /*
002: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License version
007: * 2 only, as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful, but
010: * WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * General Public License version 2 for more details (a copy is
013: * included at /legal/license.txt).
014: *
015: * You should have received a copy of the GNU General Public License
016: * version 2 along with this work; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
018: * 02110-1301 USA
019: *
020: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
021: * Clara, CA 95054 or visit www.sun.com if you need additional
022: * information or have any questions.
023: */
024:
025: package com.sun.jumpimpl.process;
026:
027: import com.sun.jump.message.JUMPMessage;
028: import com.sun.jump.message.JUMPMessageResponseSender;
029: import com.sun.jump.message.JUMPOutgoingMessage;
030: import com.sun.jump.message.JUMPMessageSender;
031: import com.sun.jump.message.JUMPMessagingService;
032: import com.sun.jump.message.JUMPTimedOutException;
033: import java.io.IOException;
034: import com.sun.jump.command.JUMPCommand;
035: import com.sun.jump.command.JUMPRequest;
036: import com.sun.jump.command.JUMPResponse;
037: import com.sun.jump.command.JUMPResponseInteger;
038:
039: /**
040: * Helper class to handle request/response exchange.
041: */
042: public class RequestSenderHelper {
043: // FIXME: Timeout values should be centralized somewhere
044: private static final long DEFAULT_TIMEOUT = 5000L;
045:
046: private JUMPMessagingService host; // either isolate or executive
047:
048: public RequestSenderHelper(JUMPMessagingService host) {
049: this .host = host;
050: }
051:
052: public boolean handleBooleanResponse(JUMPResponse response) {
053: String id = null;
054: if (response != null) {
055: id = response.getCommandId();
056: if (id.equals(JUMPResponse.ID_SUCCESS)) {
057: return true;
058: } else if (id.equals(JUMPResponse.ID_FAILURE)) {
059: return false;
060: }
061: }
062:
063: throw new IllegalArgumentException(); // FIXME: throw exception or System.exit(1)?
064: }
065:
066: private int handleIntegerResponse(JUMPResponseInteger rint) {
067: String id = rint.getCommandId();
068: if (id.equals(JUMPResponseInteger.ID_SUCCESS)) {
069: // Return the data in the message
070: return rint.getInt();
071: } else if (id.equals(JUMPResponse.ID_FAILURE)) {
072: return -1;
073: }
074:
075: throw new IllegalArgumentException(); // FIXME: throw exception or System.exit(1)?
076: }
077:
078: /**
079: * Send outgoing request and get a response message
080: */
081: private JUMPMessage sendRequestWork(JUMPMessageSender target,
082: JUMPRequest request) {
083: try {
084: JUMPOutgoingMessage m = request.toMessage(host);
085: JUMPMessage r = target.sendMessage(m, DEFAULT_TIMEOUT);
086:
087: return r;
088: } catch (JUMPTimedOutException e) {
089: e.printStackTrace();
090: } catch (IOException e) {
091: e.printStackTrace();
092: }
093: return null;
094: }
095:
096: /**
097: * Send outgoing request and get a response
098: */
099: public JUMPCommand sendRequest(JUMPMessageSender target,
100: JUMPRequest request, Class responseClass) {
101:
102: JUMPMessage r = sendRequestWork(target, request);
103: if (r == null) {
104: return null;
105: }
106: return JUMPCommand.fromMessage(r, responseClass);
107: }
108:
109: /**
110: * Send outgoing request and get a response
111: */
112: public JUMPResponse sendRequest(JUMPMessageSender target,
113: JUMPRequest request) {
114: return (JUMPResponse) sendRequest(target, request,
115: JUMPResponse.class);
116: }
117:
118: /**
119: * Send outgoing request and get a response
120: */
121: public int sendRequestWithIntegerResponse(JUMPMessageSender target,
122: JUMPRequest request) {
123: JUMPMessage r = sendRequestWork(target, request);
124: if (r == null) {
125: return -1;
126: }
127: JUMPResponseInteger rint = (JUMPResponseInteger) JUMPResponseInteger
128: .fromMessage(r);
129: return handleIntegerResponse(rint);
130: }
131:
132: /**
133: * Send async request
134: */
135: public void sendRequestAsync(JUMPMessageSender target,
136: JUMPRequest request) {
137: try {
138: JUMPOutgoingMessage m = request.toMessage(host);
139: target.sendMessage(m);
140: } catch (IOException e) {
141: e.printStackTrace();
142: }
143: }
144:
145: /**
146: * Send boolean response to incoming request
147: */
148: public void sendBooleanResponse(JUMPMessage incoming, boolean value) {
149: try {
150: JUMPOutgoingMessage m = host.newOutgoingMessage(incoming);
151: JUMPMessageResponseSender mrs = incoming.getSender();
152:
153: m.addUTF(value ? JUMPResponse.ID_SUCCESS
154: : JUMPResponse.ID_FAILURE);
155: mrs.sendResponseMessage(m);
156: } catch (IOException e) {
157: e.printStackTrace();
158: }
159: }
160:
161: /**
162: * Send response to incoming request
163: */
164: public void sendResponse(JUMPMessage incoming, JUMPResponse value) {
165: try {
166: JUMPOutgoingMessage m = value.toMessageInResponseTo(
167: incoming, host);
168: JUMPMessageResponseSender mrs = incoming.getSender();
169:
170: mrs.sendResponseMessage(m);
171: } catch (IOException e) {
172: e.printStackTrace();
173: }
174: }
175: }
|