1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.ipc;
21
22 import org.apache.hadoop.conf.Configurable;
23 import org.apache.hadoop.conf.Configuration;
24 import org.apache.hadoop.hbase.io.HbaseObjectWritable;
25 import org.apache.hadoop.io.VersionMismatchException;
26 import org.apache.hadoop.io.VersionedWritable;
27
28 import java.io.DataInput;
29 import java.io.DataOutput;
30 import java.io.IOException;
31 import java.lang.reflect.Field;
32 import java.lang.reflect.Method;
33
34
35 public class Invocation extends VersionedWritable implements Configurable {
36 protected String methodName;
37 @SuppressWarnings("rawtypes")
38 protected Class[] parameterClasses;
39 protected Object[] parameters;
40 protected Configuration conf;
41 private long clientVersion;
42 private int clientMethodsHash;
43
44 private static byte RPC_VERSION = 1;
45
46 public Invocation() {}
47
48 public Invocation(Method method,
49 Class<? extends VersionedProtocol> declaringClass, Object[] parameters) {
50 this.methodName = method.getName();
51 this.parameterClasses = method.getParameterTypes();
52 this.parameters = parameters;
53 if (declaringClass.equals(VersionedProtocol.class)) {
54
55 clientVersion = 0;
56 clientMethodsHash = 0;
57 } else {
58 try {
59 Field versionField = declaringClass.getField("VERSION");
60 versionField.setAccessible(true);
61 this.clientVersion = versionField.getLong(declaringClass);
62 } catch (NoSuchFieldException ex) {
63 throw new RuntimeException("The " + declaringClass, ex);
64 } catch (IllegalAccessException ex) {
65 throw new RuntimeException(ex);
66 }
67 this.clientMethodsHash = ProtocolSignature.getFingerprint(
68 declaringClass.getMethods());
69 }
70 }
71
72
73 public String getMethodName() { return methodName; }
74
75
76 @SuppressWarnings({ "rawtypes" })
77 public Class[] getParameterClasses() { return parameterClasses; }
78
79
80 public Object[] getParameters() { return parameters; }
81
82 long getProtocolVersion() {
83 return clientVersion;
84 }
85
86 protected int getClientMethodsHash() {
87 return clientMethodsHash;
88 }
89
90
91
92
93
94 public long getRpcVersion() {
95 return RPC_VERSION;
96 }
97
98 public void readFields(DataInput in) throws IOException {
99 try {
100 super.readFields(in);
101 methodName = in.readUTF();
102 clientVersion = in.readLong();
103 clientMethodsHash = in.readInt();
104 } catch (VersionMismatchException e) {
105
106
107 if (e.toString().endsWith("found v0")) {
108
109
110
111
112
113
114
115
116 final short len = (short) (in.readByte() & 0xFF);
117 final byte[] buf = new byte[len];
118 in.readFully(buf, 0, len);
119 methodName = new String(buf);
120 }
121 }
122 parameters = new Object[in.readInt()];
123 parameterClasses = new Class[parameters.length];
124 HbaseObjectWritable objectWritable = new HbaseObjectWritable();
125 for (int i = 0; i < parameters.length; i++) {
126 parameters[i] = HbaseObjectWritable.readObject(in, objectWritable,
127 this.conf);
128 parameterClasses[i] = objectWritable.getDeclaredClass();
129 }
130 }
131
132 public void write(DataOutput out) throws IOException {
133 super.write(out);
134 out.writeUTF(this.methodName);
135 out.writeLong(clientVersion);
136 out.writeInt(clientMethodsHash);
137 out.writeInt(parameterClasses.length);
138 for (int i = 0; i < parameterClasses.length; i++) {
139 HbaseObjectWritable.writeObject(out, parameters[i], parameterClasses[i],
140 conf);
141 }
142 }
143
144 @Override
145 public String toString() {
146 StringBuilder buffer = new StringBuilder(256);
147 buffer.append(methodName);
148 buffer.append("(");
149 for (int i = 0; i < parameters.length; i++) {
150 if (i != 0)
151 buffer.append(", ");
152 buffer.append(parameters[i]);
153 }
154 buffer.append(")");
155 buffer.append(", rpc version="+RPC_VERSION);
156 buffer.append(", client version="+clientVersion);
157 buffer.append(", methodsFingerPrint="+clientMethodsHash);
158 return buffer.toString();
159 }
160
161 public void setConf(Configuration conf) {
162 this.conf = conf;
163 }
164
165 public Configuration getConf() {
166 return this.conf;
167 }
168
169 @Override
170 public byte getVersion() {
171 return RPC_VERSION;
172 }
173 }