001: /*
002: * @(#)JumpExecIxcRegistryWrapper.java 1.3 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:
028: package com.sun.jumpimpl.ixc.executive;
029:
030: import java.rmi.*;
031: import java.util.Arrays;
032: import java.util.ArrayList;
033: import java.util.List;
034:
035: import javax.microedition.xlet.XletContext;
036: import javax.microedition.xlet.ixc.IxcPermission;
037: import javax.microedition.xlet.ixc.IxcRegistry;
038: import javax.microedition.xlet.ixc.StubException;
039:
040: import com.sun.jumpimpl.ixc.ExportedObject;
041: import com.sun.jumpimpl.ixc.ImportedObject;
042: import com.sun.jumpimpl.ixc.RemoteRef;
043:
044: /*
045: * Special purpose IxcRegistry for the Executive VM only.
046: * Talks with JUMPExecIxcRegistry directly without serialization
047: * or Socket connection.
048: */
049:
050: public class JUMPExecIxcRegistryWrapper extends IxcRegistry {
051:
052: private static JUMPExecIxcRegistryWrapper serviceRegistry;
053: private JUMPExecIxcRegistry mainRegistry;
054: private XletContext context;
055:
056: // The 'name' Strings binded by this IxcRegistry
057: private ArrayList exportedNames;
058:
059: protected JUMPExecIxcRegistryWrapper(XletContext context) {
060: this .context = context;
061: exportedNames = new ArrayList();
062: mainRegistry = JUMPExecIxcRegistry.getJUMPExecIxcRegistry();
063: }
064:
065: public static IxcRegistry getRegistry(XletContext context) {
066: if (serviceRegistry == null)
067: serviceRegistry = new JUMPExecIxcRegistryWrapper(context);
068:
069: return serviceRegistry;
070: }
071:
072: public Remote lookup(String name) throws StubException,
073: NotBoundException {
074:
075: SecurityManager sm = System.getSecurityManager();
076: if (sm != null)
077: sm.checkPermission(new IxcPermission(name, "lookup"));
078:
079: RemoteRef remoteRef = (RemoteRef) mainRegistry.lookup(name);
080:
081: Remote nextObject;
082:
083: ExportedObject eo = ExportedObject.findExportedObject(remoteRef
084: .getObjectID());
085:
086: if (eo != null && eo.getRemoteRef().equals(remoteRef)) {
087: nextObject = eo.getRemoteObject(); // get the original
088: } else {
089: nextObject = ImportedObject.registerImportedObject(
090: remoteRef, context); // Implicit import
091: }
092:
093: return nextObject;
094: }
095:
096: public void bind(String name, Remote obj) throws StubException,
097: AlreadyBoundException {
098:
099: SecurityManager sm = System.getSecurityManager();
100: if (sm != null)
101: sm.checkPermission(new IxcPermission(name, "bind"));
102:
103: try {
104: ExportedObject eo = ExportedObject.registerExportedObject(
105: (Remote) obj, context);
106:
107: RemoteRef remoteRef = eo.getRemoteRef();
108:
109: mainRegistry.bind(name, remoteRef);
110:
111: } catch (RemoteException e) {
112: if (e instanceof StubException)
113: throw (StubException) e;
114: throw new RuntimeException(e.getCause());
115: }
116:
117: // The object is successfully registered.
118: // Record the name to the local list.
119: synchronized (exportedNames) {
120: exportedNames.add(name);
121: }
122:
123: }
124:
125: public void unbind(String name) throws NotBoundException,
126: AccessException {
127:
128: if (!exportedNames.contains(name)) {
129: // First, a security check.
130: SecurityManager sm = System.getSecurityManager();
131: if (sm != null) {
132: sm.checkPermission(new IxcPermission(name, "bind"));
133: }
134:
135: // This is inefficient, but we need to check for
136: // NotBoundException first.
137: List list = Arrays.asList(mainRegistry.list());
138: if (!list.contains(name))
139: throw new NotBoundException("Name " + name
140: + " not bound");
141:
142: // At this point, just throw AccessException
143: throw new AccessException(
144: "Cannot unbind objects bound by other xlets");
145: }
146:
147: mainRegistry.unbind(name);
148:
149: // Successfully unbinded, remove the name
150: // from the local list as well.
151: synchronized (exportedNames) {
152: exportedNames.remove(name);
153: }
154:
155: }
156:
157: public void rebind(String name, Remote obj) throws StubException,
158: AccessException {
159:
160: SecurityManager sm = System.getSecurityManager();
161: if (sm != null)
162: sm.checkPermission(new IxcPermission(name, "bind"));
163:
164: if (name == null || obj == null)
165: throw new NullPointerException("name and obj can't be null");
166:
167: try {
168: mainRegistry.unbind(name);
169: bind(name, obj);
170: } catch (NotBoundException nbe) { // just ignore
171: } catch (AlreadyBoundException abe) { // can't happen
172: }
173: }
174:
175: public String[] list() {
176: String[] names = mainRegistry.list();
177:
178: SecurityManager sm = System.getSecurityManager();
179: if (sm != null) {
180: ArrayList list = new ArrayList();
181: for (int i = 0; i < names.length; i++) {
182: try {
183: sm.checkPermission(new IxcPermission(names[i],
184: "lookup"));
185: list.add(names[i]);
186: } catch (SecurityException e) {
187: }
188: }
189: names = (String[]) list.toArray(new String[] {});
190: }
191:
192: return names;
193:
194: }
195:
196: public void unbindAll() {
197:
198: String[] names = (String[]) exportedNames
199: .toArray(new String[] {});
200:
201: for (int i = 0; i < names.length; i++) {
202: try {
203: mainRegistry.unbind(names[i]);
204: } catch (NotBoundException nbe) {
205: // Just ignore it.
206: } catch (RemoteException re) {
207: System.err
208: .println("Unexpected exception during unbindAll()");
209: throw new RuntimeException(re.getCause());
210: }
211: }
212:
213: // Successfully cleared, remove all names from the local list.
214: synchronized (exportedNames) {
215: exportedNames.clear();
216: }
217:
218: }
219: }
|