001: /*
002: * @(#)ExportedObject.java 1.7 06/09/06
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.rmi.Remote;
030: import java.rmi.RemoteException;
031: import java.util.HashMap;
032: import java.util.HashSet;
033: import java.util.Iterator;
034: import java.util.Arrays;
035: import java.util.List;
036: import java.lang.reflect.Method;
037:
038: import javax.microedition.xlet.XletContext;
039:
040: /*
041: * A record of Remote objects exported by this VM.
042: * ExportObject is also responsible for creating a corresponding
043: * RemoteRef.
044: *
045: * When ConnectionReceiver receives a request for a remote method
046: * invocation, it queries for the ExportedObject corresponding to
047: * the incoming RemoteRef, and find out the original Remote object
048: * which the method can be invoked on.
049: */
050:
051: public class ExportedObject {
052:
053: static HashMap objectsByID = new HashMap(); // <object ID, ExportedObject>
054: static HashMap typeByName = new HashMap(); // <RemoteObject Class, RemoteObjectType>
055:
056: long objectID;
057: Remote remoteObject; // The original "Remote" object exported
058: XletContext context; // The xlet that owns the original Remote object
059: RemoteObjectType type; // This remote object's Object type.
060: RemoteRef remoteRef; // This ExportedObject's RemoteRef.
061:
062: static final long startingObjectID = 1000L; // the first ID,
063:
064: /*
065: * Note: this initial value is assigned for Executive VM's
066: * IxcRegistry object as well as the first object exported by
067: * any child xlet.
068: */
069: private static long nextObjectID = startingObjectID;
070:
071: private ExportedObject(long objectID, Remote remote,
072: XletContext context) throws RemoteException {
073:
074: this .context = context;
075: this .objectID = objectID;
076: this .remoteObject = remote;
077:
078: Class remoteObjectClass = remote.getClass();
079:
080: synchronized (typeByName) {
081: this .type = (RemoteObjectType) typeByName
082: .get(remoteObjectClass);
083: if (this .type == null) {
084: this .type = new RemoteObjectType(remoteObjectClass);
085: typeByName.put(remoteObjectClass, this .type);
086: }
087: }
088: }
089:
090: static long newObjectID() {
091: return nextObjectID++;
092: }
093:
094: /* Returns the "original" remote object this ExportedObject encapselates. */
095: public Remote getRemoteObject() {
096: return remoteObject;
097: }
098:
099: public static ExportedObject registerExportedObject(Remote r,
100: XletContext context) throws RemoteException {
101: long id = newObjectID();
102: ExportedObject obj = new ExportedObject(id, r, context);
103: objectsByID.put(new Long(id), obj);
104: return obj;
105: }
106:
107: public static ExportedObject findExportedObject(long ObjID) {
108: return (ExportedObject) objectsByID.get(new Long(ObjID));
109: }
110:
111: public Method findExportedMethod(long methodID) {
112: return (Method) type.methodsByID.get(new Long(methodID));
113: }
114:
115: public RemoteRef getRemoteRef() {
116:
117: if (remoteRef == null) {
118: remoteRef = new RemoteRef(objectID, ConnectionReceiver
119: .getLocalServicePort(), Utils.getMtaskClientID(),
120: type.getRemoteInterfaceNames(), type
121: .getMethodIDsAslongs());
122: }
123: return remoteRef;
124: }
125: }
|