001: /*
002: * @(#)IxcInputStream.java 1.6 06/08/10
003: *
004: * Copyright 1990-2006 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.jumpimpl.ixc;
028:
029: import java.io.ObjectInputStream;
030: import java.io.InputStream;
031: import java.io.IOException;
032: import java.io.ObjectStreamClass;
033: import java.rmi.Remote;
034: import java.security.AccessController;
035: import java.security.PrivilegedAction;
036:
037: import java.lang.reflect.Array;
038:
039: import javax.microedition.xlet.XletContext;
040: import javax.microedition.xlet.ixc.IxcRegistry;
041: import javax.microedition.xlet.ixc.StubException;
042:
043: /*
044: *
045: */
046:
047: public class IxcInputStream extends ObjectInputStream {
048:
049: XletContext context;
050:
051: IxcInputStream(InputStream in, XletContext context,
052: boolean isExecutiveVM) throws IOException {
053: super (in);
054: this .context = context;
055:
056: /***
057: * isExecutiveVM value indicates that this IxcInputStream is used
058: * for the central JUMPExecIxcRegistry's input stream.
059: * In thie JUMPExecIxcRegistry, we don't want to be converting
060: * Remote object to a stub or vice versa, but just record incoming
061: * RemoteRef objects.
062: **/
063: if (!isExecutiveVM) {
064: AccessController.doPrivileged(new PrivilegedAction() {
065: public Object run() {
066: enableResolveObject(true);
067: return null;
068: }
069: });
070: }
071: }
072:
073: protected Class resolveClass(ObjectStreamClass desc)
074: throws IOException, StubException {
075: String name = desc.getName();
076: try {
077: // First, try to find the class in the xlet's classloader
078: return Class.forName(name, false, context.getClassLoader());
079: } catch (ClassNotFoundException ex) {
080: try {
081: // If not found, fall back into the default classloader implementation
082: // in the superclass
083: return super .resolveClass(desc);
084: } catch (ClassNotFoundException cnfe) {
085: throw new StubException("Class " + name + " not found",
086: cnfe);
087: }
088: }
089: }
090:
091: // If the incoming object is a RemoteRef, inspect it's origin.
092: // If this VM is the exporter, return the original remote object,
093: // else record it as an import and return a generated stub instead.
094: protected Object resolveObject(Object obj) throws IOException {
095:
096: Object nextObject = obj;
097:
098: if (nextObject instanceof NullObject) {
099: nextObject = null;
100: }
101:
102: if (nextObject instanceof Remote) {
103: /* Remote object over the wire should be a RemoteRef instance */
104: RemoteRef remoteRef = (RemoteRef) nextObject;
105:
106: ExportedObject eo = ExportedObject
107: .findExportedObject(remoteRef.getObjectID());
108:
109: if (eo != null && eo.getRemoteRef().equals(remoteRef)) {
110: nextObject = eo.remoteObject; // get the original
111: } else {
112: nextObject = ImportedObject.registerImportedObject(
113: remoteRef, context); // Implicit import
114: reportImportToJumpExecIxcRegistry(
115: (StubObject) nextObject, context);
116: }
117: }
118:
119: return nextObject;
120: }
121:
122: void reportImportToJumpExecIxcRegistry(StubObject r,
123: XletContext importer) {
124:
125: // Report to the JUMPExecIxcRegistry about this new import if
126: // the connection is made not with
127: // JumpExecIxcRegistry itself but with some other client VM.
128:
129: IxcRegistry registry = IxcRegistry.getRegistry(importer);
130:
131: if (registry instanceof JUMPIxcRegistryImpl) {
132: ((JUMPIxcRegistryImpl) registry).amHandler
133: .notifyObjectImport(Utils.getMtaskClientID(),
134: (Remote) r);
135: }
136: }
137:
138: }
|