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.mapper;
20
21
22 import java.util.HashMap;
23 import org.apache.log4j.Logger;
24
25 public class ProcessorFactory {
26 static Logger log = Logger.getLogger(ProcessorFactory.class);
27
28
29
30
31
32
33
34
35
36 private static HashMap<String, ChunkProcessor> processors = new HashMap<String, ChunkProcessor>();
37
38 private ProcessorFactory() {
39 }
40
41 public static ChunkProcessor getProcessor(String recordType)
42 throws UnknownRecordTypeException {
43 String path = "org.apache.hadoop.chukwa.extraction.demux.processor.mapper"
44 + recordType;
45 if (processors.containsKey(recordType)) {
46 return processors.get(recordType);
47 } else {
48 ChunkProcessor processor = null;
49 try {
50 processor = (ChunkProcessor) Class.forName(path).getConstructor()
51 .newInstance();
52 } catch (ClassNotFoundException e) {
53 throw new UnknownRecordTypeException(
54 "Unknown recordType:" + recordType, e);
55 } catch (Exception e) {
56 throw new UnknownRecordTypeException("error constructing processor", e);
57 }
58
59
60
61 register(recordType, processor);
62 return processor;
63 }
64 }
65
66
67
68
69 public static synchronized void register(String recordType,
70 ChunkProcessor processor) {
71 log.info("register " + processor.getClass().getName()
72 + " for this recordType :" + recordType);
73 if (processors.containsKey(recordType)) {
74 throw new DuplicateProcessorException(
75 "Duplicate processor for recordType:" + recordType);
76 }
77 ProcessorFactory.processors.put(recordType, processor);
78 }
79
80 }