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.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   * Represents an arbitrary method invocation against a Coprocessor
38   * instance.  In order for a coprocessor implementation to be remotely callable
39   * by clients, it must define and implement a {@link CoprocessorProtocol}
40   * subclass.  Only methods defined in the {@code CoprocessorProtocol} interface
41   * will be callable by clients.
42   *
43   * <p>
44   * This class is used internally by
45   * {@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)}
46   * to wrap the {@code CoprocessorProtocol} method invocations requested in
47   * RPC calls.  It should not be used directly by HBase clients.
48   * </p>
49   *
50   * @see ExecResult
51   * @see org.apache.hadoop.hbase.client.HTable#coprocessorExec(Class, byte[], byte[], org.apache.hadoop.hbase.client.coprocessor.Batch.Call)
52   * @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)
53   */
54  public class Exec extends Invocation implements Row {
55    private Configuration conf = HBaseConfiguration.create();
56    /** Row key used as a reference for any region lookups */
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      // fields for Invocation
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      // fields for Exec
97      Bytes.writeByteArray(out, referenceRow);
98      out.writeUTF(protocol.getName());
99    }
100 
101   @Override
102   public void readFields(DataInput in) throws IOException {
103     // fields for Invocation
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     // fields for Exec
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 }