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: package com.sun.jumpimpl.process;
025:
026: import com.sun.jump.common.JUMPProcessProxy;
027: import com.sun.jump.message.JUMPMessagingService;
028: import com.sun.jump.message.JUMPMessageSender;
029: import com.sun.jump.message.JUMPMessage;
030: import com.sun.jump.message.JUMPMessageDispatcher;
031: import com.sun.jump.message.JUMPMessagable;
032: import com.sun.jump.message.JUMPOutgoingMessage;
033: import com.sun.jump.message.JUMPTimedOutException;
034:
035: import com.sun.jump.os.JUMPOSInterface;
036: import com.sun.jumpimpl.os.JUMPMessageQueueInterfaceImpl;
037:
038: import java.util.HashMap;
039: import java.io.IOException;
040:
041: /**
042: * Base class for any proxy that refers to another process
043: */
044: public class JUMPProcessProxyImpl implements JUMPProcessProxy,
045: JUMPMessagingService, JUMPMessageSender, JUMPMessagable {
046: int processId;
047:
048: private static final JUMPMessageQueueInterfaceImpl queue = (JUMPMessageQueueInterfaceImpl) JUMPOSInterface
049: .getInstance().getQueueInterface();
050: private static final HashMap proxyMap = new HashMap();
051:
052: /**
053: * Given a process id, create a proxy that can forward messages
054: */
055: protected JUMPProcessProxyImpl(int processId) {
056: this .processId = processId;
057: // Register this instance
058: Integer pidObj = new Integer(processId);
059: synchronized (JUMPProcessProxyImpl.class) {
060: proxyMap.put(pidObj, this );
061: }
062: }
063:
064: /*
065: * Make sure to track process proxies here.
066: * Keep one process proxy object per pid.
067: */
068: public static synchronized JUMPProcessProxyImpl createProcessProxyImpl(
069: int processId) {
070: if (processId == -1) {
071: throw new Error("Uninitialized process!");
072: }
073: JUMPProcessProxyImpl ppi = getProcessProxyImpl(processId);
074: if (ppi == null) {
075: ppi = new JUMPProcessProxyImpl(processId);
076: }
077: return ppi;
078: }
079:
080: /*
081: * Make sure to track process proxies here.
082: * Keep one process proxy object per pid.
083: */
084: public static synchronized JUMPProcessProxyImpl getProcessProxyImpl(
085: int processId) {
086: Integer pidObj = new Integer(processId);
087: Object ppiObj = proxyMap.get(pidObj);
088:
089: return (JUMPProcessProxyImpl) ppiObj;
090: }
091:
092: /*
093: * Delete process proxy for 'processId'
094: */
095: public static synchronized void deleteProcessProxyImpl(int processId) {
096: Integer pidObj = new Integer(processId);
097: Object ppiObj = proxyMap.get(pidObj);
098: if (ppiObj != null) {
099: proxyMap.remove(pidObj);
100: }
101: }
102:
103: public int getProcessId() {
104: return processId;
105: }
106:
107: /**
108: * Sends a response message
109: *
110: * @throws java.io.IOException is the message cannot be sent due to
111: * I/O error.
112: */
113: public void sendResponseMessage(JUMPOutgoingMessage message)
114: throws IOException {
115: queue.sendMessageResponse(message.serialize(), message
116: .isResponseMessage());
117: }
118:
119: /**
120: * Sends a message to the <code>process</code>.
121: *
122: * @throws java.io.IOException is the message cannot be sent due to
123: * I/O error.
124: */
125: public void sendMessage(JUMPOutgoingMessage message)
126: throws IOException {
127: queue.sendMessageAsync(this .processId, message.serialize(),
128: message.isResponseMessage());
129: }
130:
131: public JUMPMessage sendMessage(JUMPOutgoingMessage message,
132: long timeout) throws JUMPTimedOutException, IOException {
133: byte[] raw = queue.sendMessageSync(this .processId, message
134: .serialize(), message.isResponseMessage(), timeout);
135: return new MessageImpl.Message(raw);
136: }
137:
138: /**
139: * Gets the message dispacther for the process
140: */
141: public JUMPMessageDispatcher getMessageDispatcher() {
142: return JUMPMessageDispatcherImpl.getInstance();
143: }
144:
145: /**
146: * Creates a new, <i>blank</i> <Code>JUMPOutgoingMessage</code> for the
147: * message type. The sender of the message is automatically filled
148: * in by the factory implementation.
149: */
150: public JUMPOutgoingMessage newOutgoingMessage(String mesgType) {
151: JUMPMessagable s = this ;
152: return new MessageImpl.OutgoingMessage(s, mesgType, queue
153: .getReturnType(), -1);
154: }
155:
156: /**
157: * Create a new, <i>blank</i> <code>JUMPOutgoingMessage</code> as a
158: * response to the request message passed. The sender of the
159: * message is automatically filled in by the factory
160: * implementation.
161: * Make sure to pass the return type of the request message.
162: */
163: public JUMPOutgoingMessage newOutgoingMessage(
164: JUMPMessage requestMessage) {
165: JUMPMessagable s = (JUMPMessagable) requestMessage.getSender();
166: return new MessageImpl.OutgoingMessage(s, requestMessage
167: .getType(), requestMessage.getReturnType(),
168: requestMessage.getResponseId());
169: }
170:
171: /**
172: * Create a new, <i>immutable</i> <code>JUMPMessage</code>
173: * out of a raw array of bytes received on the message queue.
174: */
175: public JUMPMessage newMessage(byte[] rawData) {
176: return new MessageImpl.Message(rawData);
177: }
178:
179: }
|