001: /*
002: * $RCSfile: JAIRMIUtil.java,v $
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Use is subject to license terms.
007: *
008: * $Revision: 1.1 $
009: * $Date: 2005/02/11 04:56:51 $
010: * $State: Exp $
011: */package com.sun.media.jai.rmi;
012:
013: import java.awt.RenderingHints;
014: import java.awt.image.RenderedImage;
015: import java.awt.image.renderable.ParameterBlock;
016: import java.io.Serializable;
017: import java.util.Vector;
018: import java.util.Hashtable;
019: import javax.media.jai.RenderedOp;
020: import javax.media.jai.PlanarImage;
021: import javax.media.jai.remote.RemoteRenderedOp;
022: import javax.media.jai.remote.SerializableRenderedImage;
023:
024: /**
025: * A class containing utility methods used for the implementation of
026: * the "jairmi" protocol.
027: */
028: public final class JAIRMIUtil {
029:
030: /**
031: * Replaces any element in the source Vector which is an ID
032: * with the server-side node that is associated with the
033: * given ID.
034: */
035: public static Vector replaceIdWithSources(Vector srcs,
036: Hashtable nodes, String opName, RenderingHints hints) {
037:
038: Vector replacedSrcs = new Vector();
039: Object obj;
040: for (int i = 0; i < srcs.size(); i++) {
041: obj = srcs.elementAt(i);
042: if (obj instanceof String) {
043: String serverNodeDesc = (String) obj;
044: int index = serverNodeDesc.indexOf("::");
045: boolean diffServer = index != -1;
046: if (diffServer) {
047: // If the source is on a different server, create
048: // a RMIServerProxy to access it.
049: replacedSrcs.add(new RMIServerProxy(serverNodeDesc,
050: opName, hints));
051: } else {
052: // If the source is on the same server, get it
053: // from the nodes Hashtable and set it as a source
054: // in the sources Vector.
055: replacedSrcs.add(nodes.get(Long
056: .valueOf(serverNodeDesc)));
057: }
058: } else {
059: PlanarImage pi = PlanarImage
060: .wrapRenderedImage((RenderedImage) obj);
061: replacedSrcs.add(pi);
062: }
063: }
064:
065: return replacedSrcs;
066: }
067:
068: /**
069: * Replaces any source that is an <code>RMIServerProxy</code> with
070: * the id of the server-side node that is represented by the
071: * <code>RMIServerProxy</code>.
072: */
073: public static Vector replaceSourcesWithId(Vector srcs,
074: String serverName) {
075:
076: Vector replacedSrcs = new Vector();
077: Object obj;
078: for (int i = 0; i < srcs.size(); i++) {
079: obj = srcs.elementAt(i);
080: if (obj instanceof RMIServerProxy) {
081: RMIServerProxy rmisp = (RMIServerProxy) obj;
082: if (rmisp.getServerName().equalsIgnoreCase(serverName)) {
083: replacedSrcs.add(rmisp.getRMIID().toString());
084: } else {
085: String str = new String(rmisp.getServerName()
086: + "::" + rmisp.getRMIID());
087: replacedSrcs.add(str);
088: }
089: } else if (obj instanceof RemoteRenderedOp) {
090: RemoteRenderedOp rrop = (RemoteRenderedOp) obj;
091: Object ai = rrop.getRendering();
092: if (ai instanceof RMIServerProxy) {
093: RMIServerProxy rmisp = (RMIServerProxy) ai;
094: if (rmisp.getServerName().equalsIgnoreCase(
095: serverName)) {
096: replacedSrcs.add(rmisp.getRMIID().toString());
097: } else {
098: String str = new String(rmisp.getServerName()
099: + "::" + rmisp.getRMIID());
100: replacedSrcs.add(str);
101: }
102: } else {
103: RenderedImage ri = (RenderedImage) ai;
104: replacedSrcs.add(new SerializableRenderedImage(ri));
105: }
106: } else if (obj instanceof RenderedOp) {
107: RenderedOp rop = (RenderedOp) obj;
108: replacedSrcs.add(new SerializableRenderedImage(
109: (RenderedImage) rop.getRendering()));
110: } else if (obj instanceof Serializable) {
111: replacedSrcs.add(obj);
112: } else if (obj instanceof RenderedImage) {
113: RenderedImage ri = (RenderedImage) obj;
114: replacedSrcs.add(new SerializableRenderedImage(ri));
115: }
116: }
117:
118: return replacedSrcs;
119: }
120:
121: /**
122: * Replace an image with an equivalent representation that can be accessed
123: * on the server.
124: */
125: public static Object replaceImage(RenderedImage obj,
126: String this ServerName) {
127:
128: if (obj instanceof RMIServerProxy) {
129:
130: RMIServerProxy rmisp = (RMIServerProxy) obj;
131: if (rmisp.getServerName().equalsIgnoreCase(this ServerName))
132: return "::" + rmisp.getRMIID();
133: else
134: return rmisp.getServerName() + "::" + rmisp.getRMIID()
135: + ";;" + rmisp.getOperationName();
136:
137: } else if (obj instanceof RenderedOp) {
138:
139: RenderedImage rendering = ((RenderedOp) obj).getRendering();
140: return replaceImage(rendering, this ServerName);
141:
142: } else if (obj instanceof RenderedImage) {
143:
144: if (obj instanceof Serializable)
145: return obj;
146: else
147: return new SerializableRenderedImage(obj);
148: }
149:
150: return obj;
151: }
152:
153: public static void checkClientParameters(ParameterBlock pb,
154: String this ServerName) {
155:
156: // XXX 07/18/01, aastha TODO
157: // This code should check every parameter to test Serializability
158: // If parameter is Serializable, then keep it as is
159: // If not serializable then use SerializerFactory to serialize it
160: // If that doesn't work, throw an Exception
161: // Note that parameters that are images may still need to be treated
162: // differently as in the code below.
163:
164: if (pb == null)
165: return;
166:
167: int numParams = pb.getNumParameters();
168: Vector params = pb.getParameters();
169:
170: Object obj;
171: for (int i = 0; i < numParams; i++) {
172: obj = params.elementAt(i);
173:
174: if (obj == null) {
175:
176: continue;
177:
178: } else if (obj instanceof RenderedImage) {
179:
180: pb.set(
181: replaceImage((RenderedImage) obj,
182: this ServerName), i);
183:
184: }
185: }
186: }
187:
188: public static void checkClientParameters(Vector parameters,
189: String this ServerName) {
190:
191: // XXX 07/18/01, aastha TODO
192: // This code should check every parameter to test Serializability
193: // If parameter is Serializable, then keep it as is
194: // If not serializable then use SerializerFactory to serialize it
195: // If that doesn't work, throw an Exception
196: // Note that parameters that are images may still need to be treated
197: // differently as in the code below.
198:
199: if (parameters == null)
200: return;
201:
202: Object obj;
203: for (int i = 0; i < parameters.size(); i++) {
204: obj = parameters.elementAt(i);
205:
206: if (obj == null) {
207:
208: continue;
209:
210: } else if (obj instanceof RenderedImage) {
211:
212: parameters.set(i, replaceImage((RenderedImage) obj,
213: this ServerName));
214:
215: }
216: }
217: }
218:
219: /**
220: * Method to convert a String representation of an image into the
221: * image itself. The String representation is supplied by the client
222: * generally as a parameter in the ParameterBlock.
223: */
224: public static Object replaceStringWithImage(String s,
225: Hashtable nodes) {
226: String paramServerName, opName;
227: int index1 = s.indexOf("::");
228: int index2 = s.indexOf(";;");
229: Long id;
230:
231: if (index1 == -1) {
232: return s;
233: } else if (index2 == -1) {
234: id = Long.valueOf(s.substring(index1 + 2));
235: return nodes.get(id);
236:
237: } else {
238: // Extract the RMI ID from the servername string and
239: // replace the original serverName string with one of
240: // the usual type.
241: id = Long.valueOf(s.substring(index1 + 2, index2));
242: paramServerName = s.substring(0, index1);
243: opName = s.substring(index2 + 2);
244:
245: // Create an RMIServerProxy to access the image on
246: // the other server.
247: return new RMIServerProxy((paramServerName + "::" + id),
248: opName, null);
249: }
250: }
251:
252: /**
253: * Method to check whether any of the parameters in the ParameterBlock
254: * are replacement representations of images.
255: */
256: public static void checkServerParameters(ParameterBlock pb,
257: Hashtable nodes) {
258:
259: if (pb == null)
260: return;
261:
262: int numParams = pb.getNumParameters();
263: Vector params = pb.getParameters();
264:
265: Object obj;
266: for (int i = 0; i < numParams; i++) {
267: obj = params.elementAt(i);
268:
269: if (obj == null) {
270: continue;
271: } else if (obj instanceof String) {
272: pb.set(replaceStringWithImage((String) obj, nodes), i);
273: }
274: }
275: }
276:
277: /**
278: * Method to check whether any of the parameters in the ParameterBlock
279: * are replacement representations of images.
280: */
281: public static void checkServerParameters(Vector parameters,
282: Hashtable nodes) {
283:
284: if (parameters == null)
285: return;
286:
287: Object obj;
288: for (int i = 0; i < parameters.size(); i++) {
289: obj = parameters.elementAt(i);
290:
291: if (obj == null) {
292: continue;
293: } else if (obj instanceof String) {
294: parameters.set(i, replaceStringWithImage((String) obj,
295: nodes));
296: }
297: }
298: }
299:
300: }
|