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.client.coprocessor;
21
22 import org.apache.hadoop.conf.Configuration;
23 import org.apache.hadoop.hbase.HBaseConfiguration;
24 import org.apache.hadoop.hbase.client.Row;
25 import org.apache.hadoop.hbase.io.HbaseObjectWritable;
26 import org.apache.hadoop.hbase.ipc.CoprocessorProtocol;
27 import org.apache.hadoop.hbase.ipc.Invocation;
28 import org.apache.hadoop.hbase.util.Bytes;
29 import org.apache.hadoop.hbase.util.Classes;
30
31 import java.io.DataInput;
32 import java.io.DataOutput;
33 import java.io.IOException;
34 import java.lang.reflect.Method;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public class Exec extends Invocation implements Row {
55 private Configuration conf = HBaseConfiguration.create();
56
57 private byte[] referenceRow;
58 private Class<? extends CoprocessorProtocol> protocol;
59
60 public Exec() {
61 }
62
63 public Exec(Configuration configuration,
64 byte[] row,
65 Class<? extends CoprocessorProtocol> protocol,
66 Method method, Object[] parameters) {
67 super(method, parameters);
68 this.conf = configuration;
69 this.referenceRow = row;
70 this.protocol = protocol;
71 }
72
73 public Class<? extends CoprocessorProtocol> getProtocol() {
74 return protocol;
75 }
76
77 public byte[] getRow() {
78 return referenceRow;
79 }
80
81 public int compareTo(Row row) {
82 return Bytes.compareTo(referenceRow, row.getRow());
83 }
84
85 @Override
86 public void write(DataOutput out) throws IOException {
87
88 out.writeUTF(this.methodName);
89 out.writeInt(parameterClasses.length);
90 for (int i = 0; i < parameterClasses.length; i++) {
91 HbaseObjectWritable.writeObject(out, parameters[i],
92 parameters[i] != null ? parameters[i].getClass() : parameterClasses[i],
93 conf);
94 out.writeUTF(parameterClasses[i].getName());
95 }
96
97 Bytes.writeByteArray(out, referenceRow);
98 out.writeUTF(protocol.getName());
99 }
100
101 @Override
102 public void readFields(DataInput in) throws IOException {
103
104 methodName = in.readUTF();
105 parameters = new Object[in.readInt()];
106 parameterClasses = new Class[parameters.length];
107 HbaseObjectWritable objectWritable = new HbaseObjectWritable();
108 for (int i = 0; i < parameters.length; i++) {
109 parameters[i] = HbaseObjectWritable.readObject(in, objectWritable,
110 this.conf);
111 String parameterClassName = in.readUTF();
112 try {
113 parameterClasses[i] = Classes.extendedForName(parameterClassName);
114 } catch (ClassNotFoundException e) {
115 throw new IOException("Couldn't find class: " + parameterClassName);
116 }
117 }
118
119 referenceRow = Bytes.readByteArray(in);
120 String protocolName = in.readUTF();
121 try {
122 protocol = (Class<CoprocessorProtocol>)conf.getClassByName(protocolName);
123 }
124 catch (ClassNotFoundException cnfe) {
125 throw new IOException("Protocol class "+protocolName+" not found", cnfe);
126 }
127 }
128 }