1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import java.util.Iterator;
25 import java.util.ServiceLoader;
26
27
28
29
30 public class CompatibilityFactory {
31
32 private static final Log LOG = LogFactory.getLog(CompatibilitySingletonFactory.class);
33 public static final String EXCEPTION_START = "Could not create ";
34 public static final String EXCEPTION_END = " Is the hadoop compatibility jar on the classpath?";
35
36
37
38
39 protected CompatibilityFactory() {}
40
41 public static synchronized <T> T getInstance(Class<T> klass) {
42 T instance = null;
43 try {
44 ServiceLoader<T> loader = ServiceLoader.load(klass);
45 Iterator<T> it = loader.iterator();
46 instance = it.next();
47 if (it.hasNext()) {
48 StringBuilder msg = new StringBuilder();
49 msg.append("ServiceLoader provided more than one implementation for class: ")
50 .append(klass)
51 .append(", using implementation: ").append(instance.getClass())
52 .append(", other implementations: {");
53 while (it.hasNext()) {
54 msg.append(it.next()).append(" ");
55 }
56 msg.append("}");
57 LOG.warn(msg);
58 }
59 } catch (Exception e) {
60 throw new RuntimeException(createExceptionString(klass), e);
61 } catch (Error e) {
62 throw new RuntimeException(createExceptionString(klass), e);
63 }
64
65
66 if (instance == null) {
67 throw new RuntimeException(createExceptionString(klass));
68 }
69 return instance;
70 }
71
72 protected static String createExceptionString(Class klass) {
73 return EXCEPTION_START + klass.toString() + EXCEPTION_END;
74 }
75 }