1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.chukwa.extraction.demux.processor.reducer;
20
21
22 import java.util.HashMap;
23
24 import org.apache.log4j.Logger;
25
26 public class ReduceProcessorFactory {
27 static Logger log = Logger.getLogger(ReduceProcessorFactory.class);
28
29
30
31
32
33
34
35
36
37
38
39
40 private static HashMap<String, ReduceProcessor> processors = new HashMap<String, ReduceProcessor>();
41
42 private ReduceProcessorFactory() {
43 }
44
45
46
47
48
49
50 public static synchronized void register(String reduceType,
51 ReduceProcessor processor) {
52 log.info("register " + processor.getClass().getName()
53 + " for this recordType :" + reduceType);
54 if (processors.containsKey(reduceType)) {
55 throw new DuplicateReduceProcessorException(
56 "Duplicate processor for recordType:" + reduceType);
57 }
58 ReduceProcessorFactory.processors.put(reduceType, processor);
59 }
60
61 public static ReduceProcessor getProcessor(String processorClass) throws UnknownReduceTypeException {
62 if (processors.containsKey(processorClass)) {
63 return processors.get(processorClass);
64 } else {
65 ReduceProcessor processor = null;
66 try {
67 processor = (ReduceProcessor) Class.forName(processorClass).newInstance();
68 } catch (ClassNotFoundException e) {
69 throw new UnknownReduceTypeException("Unknown reducer class for:" + processorClass, e);
70 } catch (Exception e) {
71 throw new UnknownReduceTypeException("error constructing processor", e);
72 }
73
74
75 register(processorClass, processor);
76 return processor;
77 }
78
79 }
80
81
82 }