1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
34
35
36
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
53
54
55 protected void loadUserProcedures(Configuration conf, String confKey) {
56 Class<?> implClass = null;
57
58
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
82 procedures.addAll(configured);
83 }
84
85 @SuppressWarnings("unchecked")
86 public E loadInstance(Class<?> implClass) throws IOException {
87
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
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 }