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.procedure;
19  
20  import java.io.IOException;
21  import java.util.ArrayList;
22  import java.util.HashSet;
23  import java.util.Set;
24  import java.util.List;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.conf.Configuration;
29  import org.apache.hadoop.hbase.classification.InterfaceAudience;
30  import org.apache.hadoop.hbase.classification.InterfaceStability;
31  
32  /**
33   * Provides the common setup framework and runtime services for globally
34   * barriered procedure invocation from HBase services.
35   * @param <E> the specific procedure management extension that a concrete
36   * implementation provides
37   */
38  @InterfaceAudience.Private
39  @InterfaceStability.Evolving
40  public abstract class ProcedureManagerHost<E extends ProcedureManager> {
41  
42    public static final String REGIONSERVER_PROCEDURE_CONF_KEY =
43        "hbase.procedure.regionserver.classes";
44    public static final String MASTER_PROCEUDRE_CONF_KEY =
45        "hbase.procedure.master.classes";
46  
47    private static final Log LOG = LogFactory.getLog(ProcedureManagerHost.class);
48  
49    protected Set<E> procedures = new HashSet<E>();
50  
51    /**
52     * Load system procedures. Read the class names from configuration.
53     * Called by constructor.
54     */
55    protected void loadUserProcedures(Configuration conf, String confKey) {
56      Class<?> implClass = null;
57  
58      // load default procedures from configure file
59      String[] defaultProcClasses = conf.getStrings(confKey);
60      if (defaultProcClasses == null || defaultProcClasses.length == 0)
61        return;
62  
63      List<E> configured = new ArrayList<E>();
64      for (String className : defaultProcClasses) {
65        className = className.trim();
66        ClassLoader cl = this.getClass().getClassLoader();
67        Thread.currentThread().setContextClassLoader(cl);
68        try {
69          implClass = cl.loadClass(className);
70          configured.add(loadInstance(implClass));
71          LOG.info("User procedure " + className + " was loaded successfully.");
72        } catch (ClassNotFoundException e) {
73          LOG.warn("Class " + className + " cannot be found. " +
74              e.getMessage());
75        } catch (IOException e) {
76          LOG.warn("Load procedure " + className + " failed. " +
77              e.getMessage());
78        }
79      }
80  
81      // add entire set to the collection
82      procedures.addAll(configured);
83    }
84  
85    @SuppressWarnings("unchecked")
86    public E loadInstance(Class<?> implClass) throws IOException {
87      // create the instance
88      E impl;
89      Object o = null;
90      try {
91        o = implClass.newInstance();
92        impl = (E)o;
93      } catch (InstantiationException e) {
94        throw new IOException(e);
95      } catch (IllegalAccessException e) {
96        throw new IOException(e);
97      }
98  
99      return impl;
100   }
101 
102   // Register a procedure manager object
103   public void register(E obj) {
104     procedures.add(obj);
105   }
106 
107   public Set<E> getProcedureManagers() {
108     Set<E> returnValue = new HashSet<E>();
109     for (E e: procedures) {
110       returnValue.add(e);
111     }
112     return returnValue;
113   }
114 
115   public abstract void loadProcedures(Configuration conf);
116 }