View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.client.coprocessor;
20  
21  import org.apache.hadoop.classification.InterfaceAudience;
22  import org.apache.hadoop.classification.InterfaceStability;
23  import org.apache.hadoop.conf.Configuration;
24  import org.apache.hadoop.hbase.HBaseConfiguration;
25  import org.apache.hadoop.hbase.client.Row;
26  import org.apache.hadoop.hbase.io.HbaseObjectWritable;
27  import org.apache.hadoop.hbase.ipc.CoprocessorProtocol;
28  import org.apache.hadoop.hbase.ipc.Invocation;
29  import org.apache.hadoop.hbase.util.Bytes;
30  import org.apache.hadoop.hbase.util.Classes;
31  
32  import java.io.DataInput;
33  import java.io.DataOutput;
34  import java.io.IOException;
35  import java.lang.reflect.Method;
36  
37  /**
38   * Represents an arbitrary method invocation against a Coprocessor
39   * instance.  In order for a coprocessor implementation to be remotely callable
40   * by clients, it must define and implement a {@link CoprocessorProtocol}
41   * subclass.  Only methods defined in the {@code CoprocessorProtocol} interface
42   * will be callable by clients.
43   *
44   * <p>
45   * This class is used internally by
46   * {@link org.apache.hadoop.hbase.client.HTable#coprocessorExec(Class, byte[], byte[], org.apache.hadoop.hbase.client.coprocessor.Batch.Call, org.apache.hadoop.hbase.client.coprocessor.Batch.Callback)}
47   * to wrap the {@code CoprocessorProtocol} method invocations requested in
48   * RPC calls.  It should not be used directly by HBase clients.
49   * </p>
50   *
51   * @see ExecResult
52   * @see org.apache.hadoop.hbase.client.HTable#coprocessorExec(Class, byte[], byte[], org.apache.hadoop.hbase.client.coprocessor.Batch.Call)
53   * @see org.apache.hadoop.hbase.client.HTable#coprocessorExec(Class, byte[], byte[], org.apache.hadoop.hbase.client.coprocessor.Batch.Call, org.apache.hadoop.hbase.client.coprocessor.Batch.Callback)
54   * @deprecated since 0.96.0.  See {@link org.apache.hadoop.hbase.client.HTable#coprocessorService(byte[])}
55   * or related methods instead.
56   */
57  @Deprecated
58  @InterfaceAudience.Public
59  @InterfaceStability.Evolving
60  public class Exec extends Invocation implements Row {
61    /** Row key used as a reference for any region lookups */
62    private byte[] referenceRow;
63    private Class<? extends CoprocessorProtocol> protocol;
64    private String protocolName;
65  
66    public Exec() {
67    }
68  
69    public Exec(Configuration configuration,
70        byte[] row,
71        Class<? extends CoprocessorProtocol> protocol,
72        Method method, Object[] parameters) {
73      super(method, parameters);
74      this.conf = configuration;
75      this.referenceRow = row;
76      this.protocol = protocol;
77      this.protocolName = protocol.getName();
78    }
79  
80    public String getProtocolName() {
81      return protocolName;
82    }
83  
84    public Class<? extends CoprocessorProtocol> getProtocol() {
85      return protocol;
86    }
87  
88    public byte[] getRow() {
89      return referenceRow;
90    }
91  
92    public int compareTo(Row row) {
93      return Bytes.compareTo(referenceRow, row.getRow());
94    }
95  
96    @Override
97    public void write(DataOutput out) throws IOException {
98      // fields for Invocation
99      out.writeUTF(this.methodName);
100     out.writeInt(parameterClasses.length);
101     for (int i = 0; i < parameterClasses.length; i++) {
102       HbaseObjectWritable.writeObject(out, parameters[i],
103           parameters[i] != null ? parameters[i].getClass() : parameterClasses[i],
104           conf);
105       out.writeUTF(parameterClasses[i].getName());
106     }
107     // fields for Exec
108     Bytes.writeByteArray(out, referenceRow);
109     out.writeUTF(protocol.getName());
110   }
111 
112   @Override
113   public void readFields(DataInput in) throws IOException {
114     // fields for Invocation
115     methodName = in.readUTF();
116     parameters = new Object[in.readInt()];
117     parameterClasses = new Class[parameters.length];
118     HbaseObjectWritable objectWritable = new HbaseObjectWritable();
119     for (int i = 0; i < parameters.length; i++) {
120       parameters[i] = HbaseObjectWritable.readObject(in, objectWritable,
121         this.conf);
122       String parameterClassName = in.readUTF();
123       try {
124         parameterClasses[i] = Classes.extendedForName(parameterClassName);
125       } catch (ClassNotFoundException e) {
126         throw new IOException("Couldn't find class: " + parameterClassName);
127       }
128     }
129     // fields for Exec
130     referenceRow = Bytes.readByteArray(in);
131     protocolName = in.readUTF();
132   }
133 }