comparison c1x4hotspotsrc/HotSpotVM/src/com/sun/hotspot/c1x/InvocationSocket.java @ 1423:760213a60e8b

* rewrite of the code installation * partial support for safepoints * macro-based CiTargetMethod interface * code stub support
author Lukas Stadler <lukas.stadler@oracle.com>
date Mon, 16 Aug 2010 18:59:36 -0700
parents
children 9e5e83ca2259
comparison
equal deleted inserted replaced
1422:3483ec571caf 1423:760213a60e8b
1 /*
2 * Copyright (c) 2010 Sun Microsystems, Inc. All rights reserved.
3 *
4 * Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product that is
5 * described in this document. In particular, and without limitation, these intellectual property rights may include one
6 * or more of the U.S. patents listed at http://www.sun.com/patents and one or more additional patents or pending patent
7 * applications in the U.S. and in other countries.
8 *
9 * U.S. Government Rights - Commercial software. Government users are subject to the Sun Microsystems, Inc. standard
10 * license agreement and applicable provisions of the FAR and its supplements.
11 *
12 * Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and Solaris are trademarks or registered
13 * trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks are used under license and
14 * are trademarks or registered trademarks of SPARC International, Inc. in the U.S. and other countries.
15 *
16 * UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open Company, Ltd.
17 */
18 package com.sun.hotspot.c1x;
19
20 import java.io.*;
21 import java.lang.reflect.*;
22
23 import com.sun.hotspot.c1x.logging.*;
24
25 /**
26 * A java.lang.reflect proxy that communicates over a socket connection.
27 *
28 * Calling a method sends the method name and the parameters through the socket. Afterwards this class waits for a result.
29 * While waiting for a result three types of objects can arrive through the socket: a method invocation, a method result or an exception.
30 * Method invocation can thus be recursive.
31 *
32 * @author Lukas Stadler
33 */
34 public class InvocationSocket implements InvocationHandler {
35
36 private final ObjectOutputStream output;
37 private final ObjectInputStream input;
38 private Object delegate;
39
40 public InvocationSocket(ObjectOutputStream output, ObjectInputStream input) {
41 this.output = output;
42 this.input = input;
43 }
44
45 public void setDelegate(Object delegate) {
46 this.delegate = delegate;
47 }
48
49 private static class Invocation implements Serializable {
50
51 public String methodName;
52 public Object[] args;
53
54 public Invocation(String methodName, Object[] args) {
55 this.methodName = methodName;
56 this.args = args;
57 }
58 }
59
60 private static class Result implements Serializable {
61
62 public Object result;
63
64 public Result(Object result) {
65 this.result = result;
66 }
67 }
68
69 @Override
70 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
71 try {
72 Logger.startScope("invoking remote " + method.getName());
73 output.writeObject(new Invocation(method.getName(), args));
74 output.flush();
75 return waitForResult();
76 } catch (Throwable t) {
77 t.printStackTrace();
78 throw t;
79 } finally {
80 Logger.endScope("");
81 }
82 }
83
84 public Object waitForResult() throws IOException, ClassNotFoundException {
85 while (true) {
86 Object in = input.readObject();
87 if (in instanceof Result)
88 return ((Result) in).result;
89 if (in instanceof RuntimeException)
90 throw (RuntimeException) in;
91 if (in instanceof Throwable)
92 throw new RuntimeException((Throwable) in);
93
94 Invocation invoke = (Invocation) in;
95 Method method = null;
96 for (Method m : delegate.getClass().getDeclaredMethods()) {
97 if (invoke.methodName.equals(m.getName())) {
98 method = m;
99 break;
100 }
101 }
102 if (method == null) {
103 Exception e = new UnsupportedOperationException("unknown method " + invoke.methodName);
104 e.printStackTrace();
105 output.writeObject(e);
106 output.flush();
107 } else {
108 Object result;
109 try {
110 Logger.startScope("invoking local " + invoke.methodName);
111 if (invoke.args == null) {
112 result = method.invoke(delegate);
113 } else {
114 result = method.invoke(delegate, invoke.args);
115 }
116 result = new Result(result);
117 } catch (IllegalArgumentException e) {
118 System.out.println("error while invoking " + invoke.methodName);
119 e.getCause().printStackTrace();
120 result = e.getCause();
121 } catch (InvocationTargetException e) {
122 System.out.println("error while invoking " + invoke.methodName);
123 e.getCause().printStackTrace();
124 result = e.getCause();
125 } catch (IllegalAccessException e) {
126 System.out.println("error while invoking " + invoke.methodName);
127 e.getCause().printStackTrace();
128 result = e.getCause();
129 } finally {
130 Logger.endScope("");
131 }
132 output.writeObject(result);
133 output.flush();
134 }
135 }
136 }
137
138 }