View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.constraint;
19  
20  import java.io.IOException;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.hbase.CoprocessorEnvironment;
27  import org.apache.hadoop.hbase.HTableDescriptor;
28  import org.apache.hadoop.hbase.client.Put;
29  import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver;
30  import org.apache.hadoop.hbase.coprocessor.ObserverContext;
31  import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
32  import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
33  
34  /***
35   * Processes multiple {@link Constraint Constraints} on a given table.
36   * <p>
37   * This is an ease of use mechanism - all the functionality here could be
38   * implemented on any given system by a coprocessor.
39   */
40  public class ConstraintProcessor extends BaseRegionObserver {
41  
42    private static final Log LOG = LogFactory.getLog(ConstraintProcessor.class);
43  
44    private final ClassLoader classloader;
45  
46    private List<? extends Constraint> constraints = new ArrayList<Constraint>();
47  
48    /**
49     * Create the constraint processor.
50     * <p>
51     * Stores the current classloader.
52     */
53    public ConstraintProcessor() {
54      classloader = this.getClass().getClassLoader();
55    }
56  
57    @Override
58    public void start(CoprocessorEnvironment environment) {
59      // make sure we are on a region server
60      if (!(environment instanceof RegionCoprocessorEnvironment)) {
61        throw new IllegalArgumentException(
62            "Constraints only act on regions - started in an environment that was not a region");
63      }
64      RegionCoprocessorEnvironment env = (RegionCoprocessorEnvironment) environment;
65      HTableDescriptor desc = env.getRegion().getTableDesc();
66      // load all the constraints from the HTD
67      try {
68        this.constraints = Constraints.getConstraints(desc, classloader);
69      } catch (IOException e) {
70        throw new IllegalArgumentException(e);
71      }
72  
73      if (LOG.isInfoEnabled()) {
74        LOG.info("Finished loading " + constraints.size()
75            + " user Constraints on table: " + new String(desc.getName()));
76      }
77  
78    }
79  
80    @Override
81    public void prePut(ObserverContext<RegionCoprocessorEnvironment> e, Put put,
82        WALEdit edit, boolean writeToWAL) throws IOException {
83      // check the put against the stored constraints
84      for (Constraint c : constraints) {
85        c.check(put);
86      }
87      // if we made it here, then the Put is valid
88    }
89  }