001: /*
002: * @(#)StubObject.java 1.5 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.IOException;
030: import java.io.DataOutputStream;
031: import java.io.InputStream;
032: import java.io.OutputStream;
033: import java.io.ObjectInputStream;
034: import java.io.ObjectOutputStream;
035: import java.io.EOFException;
036: import java.net.Socket;
037: import java.net.SocketException;
038: import java.net.UnknownHostException;
039: import java.rmi.Remote;
040: import java.rmi.RemoteException;
041: import java.security.PrivilegedAction;
042: import java.security.AccessController;
043:
044: import javax.microedition.xlet.XletContext;
045:
046: /*
047: * The superclass of an generated stub.
048: * com_sun_xlet_execute(long, Object[]) is the method
049: * which handles the remote method invocation.
050: */
051:
052: public class StubObject {
053:
054: /**
055: * Importing Xlet's XletContext.
056: * (Contains info for the ClassLoader used to define this stub)
057: */
058: XletContext context;
059:
060: /**
061: * RemoteRef used to generate this StubObject.
062: * Package private, to support the library code to
063: * map the stub back into RemoteRef.
064: */
065: final RemoteRef remoteRef;
066:
067: /* Set to true for debugging info */
068: private static boolean debug = false;
069:
070: protected StubObject(Object obj1, Object obj2) {
071: remoteRef = (RemoteRef) obj1;
072: context = (XletContext) obj2;
073: }
074:
075: //
076: // Execute a remote method.
077: //
078: protected final Object com_sun_xlet_execute(long methodHash,
079: Object[] args) throws Exception {
080:
081: Object returnValue = null;
082: Exception exceptionValue = null;
083:
084: InputStream in = null;
085: OutputStream out = null;
086: Socket clientSocket = null;
087:
088: try {
089:
090: Object obj = AccessController
091: .doPrivileged(new PrivilegedAction() {
092: public Object run() {
093: try {
094: Socket s = new Socket("localhost",
095: remoteRef.getPortID());
096: s.setReuseAddress(true);
097: return s;
098: } catch (UnknownHostException uhe) {
099: return uhe;
100: } catch (IOException ioe) {
101: return ioe;
102: }
103: }
104: });
105:
106: if (obj instanceof Exception)
107: throw (Exception) obj;
108:
109: clientSocket = (Socket) obj;
110:
111: // connnected, start communicating!
112:
113: out = clientSocket.getOutputStream();
114: in = clientSocket.getInputStream();
115:
116: if (debug)
117: debugOut("Client writing out: "
118: + remoteRef.getObjectID() + "," + methodHash);
119:
120: DataOutputStream dout = new DataOutputStream(out);
121: dout.writeLong(remoteRef.getObjectID());
122: dout.writeLong(methodHash);
123:
124: IxcOutputStream oout = new IxcOutputStream(out, context,
125: false);
126:
127: for (int i = 0; i < args.length; i++) {
128: oout.writeObject(args[i]);
129: }
130: if (debug)
131: debugOut("Stub done sending data, waiting for reply");
132:
133: IxcInputStream oin = new IxcInputStream(in, context, false);
134:
135: boolean didExceptionHappen = oin.readBoolean();
136: if (!didExceptionHappen) {
137: returnValue = oin.readObject();
138: } else {
139: exceptionValue = (Exception) oin.readObject();
140: }
141:
142: if (debug)
143: debugOut("Done with reading result, closing, returning "
144: + returnValue);
145:
146: if (debug && exceptionValue != null)
147: exceptionValue.printStackTrace();
148:
149: oin.close();
150: oout.flush();
151: oout.close();
152: } catch (RemoteException re) {
153: throw re;
154: } catch (EOFException eofe) {
155: if (debug)
156: debugOut("Problem in communicating with the other xlet: "
157: + eofe);
158: throw new RemoteException(
159: "Error in remote method invocation", eofe);
160: } catch (java.net.ConnectException ce) {
161: if (debug)
162: debugOut("Cannot connect to the other xlet (Xlet died?)"
163: + ce);
164: throw new RemoteException(
165: "Cannot connect to the exported xlet (xlet died?)",
166: ce);
167: } catch (Exception e) {
168: if (debug)
169: debugOut("General Exception in stub_execute(): " + e);
170: throw new RemoteException(
171: "Error in remote method invocation", e);
172: }
173:
174: try {
175: clientSocket.shutdownInput();
176: clientSocket.shutdownOutput();
177: clientSocket.close();
178: } catch (IOException e) {
179: }
180:
181: if (exceptionValue != null) {
182: throw exceptionValue;
183: }
184:
185: return returnValue;
186: }
187:
188: private void debugOut(String s) {
189: System.out.println(s);
190: }
191:
192: public String toString() {
193: return (this .getClass().getName() + "[" + remoteRef + "]");
194: }
195: }
|