View Javadoc

1   /*
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
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.HConstants;
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   */
55  public class Exec extends Invocation implements Row {
56    /** Row key used as a reference for any region lookups */
57    private byte[] referenceRow;
58    private Class<? extends CoprocessorProtocol> protocol;
59    private String protocolName;
60  
61    public Exec() {
62    }
63  
64    public Exec(Configuration configuration,
65        Class<? extends CoprocessorProtocol> protocol,
66        Method method, Object[] parameters) {
67      this(configuration, HConstants.EMPTY_BYTE_ARRAY,
68          protocol, method, parameters);
69    }
70  
71    public Exec(Configuration configuration,
72        byte[] row,
73        Class<? extends CoprocessorProtocol> protocol,
74        Method method, Object[] parameters) {
75      super(method, protocol, parameters);
76      this.conf = configuration;
77      this.referenceRow = row;
78      this.protocol = protocol;
79      this.protocolName = protocol.getName();
80    }
81  
82    public String getProtocolName() {
83      return protocolName;
84    }
85  
86    public Class<? extends CoprocessorProtocol> getProtocol() {
87      return protocol;
88    }
89  
90    public byte[] getRow() {
91      return referenceRow;
92    }
93  
94    public int compareTo(Row row) {
95      return Bytes.compareTo(referenceRow, row.getRow());
96    }
97  
98    @Override
99    public void write(DataOutput out) throws IOException {
100     // fields for Invocation
101     out.writeUTF(this.methodName);
102     out.writeInt(parameterClasses.length);
103     for (int i = 0; i < parameterClasses.length; i++) {
104       HbaseObjectWritable.writeObject(out, parameters[i],
105           parameters[i] != null ? parameters[i].getClass() : parameterClasses[i],
106           conf);
107       out.writeUTF(parameterClasses[i].getName());
108     }
109     // fields for Exec
110     Bytes.writeByteArray(out, referenceRow);
111     out.writeUTF(protocol.getName());
112   }
113 
114   @Override
115   public void readFields(DataInput in) throws IOException {
116     // fields for Invocation
117     methodName = in.readUTF();
118     parameters = new Object[in.readInt()];
119     parameterClasses = new Class[parameters.length];
120     HbaseObjectWritable objectWritable = new HbaseObjectWritable();
121     for (int i = 0; i < parameters.length; i++) {
122       parameters[i] = HbaseObjectWritable.readObject(in, objectWritable,
123         this.conf);
124       String parameterClassName = in.readUTF();
125       try {
126         parameterClasses[i] = Classes.extendedForName(parameterClassName);
127       } catch (ClassNotFoundException e) {
128         throw new IOException("Couldn't find class: " + parameterClassName);
129       }
130     }
131     // fields for Exec
132     referenceRow = Bytes.readByteArray(in);
133     protocolName = in.readUTF();
134   }
135 }