1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.chukwa.validationframework.interceptor;
19
20
21 import java.io.DataOutputStream;
22 import java.io.File;
23 import java.io.FileNotFoundException;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import org.apache.hadoop.chukwa.Chunk;
29
30 public class ChunkDumper {
31 static public String testRepositoryDumpDir = "/tmp/chukwaDump/";
32 static HashMap<String, DataOutputStream> hash = new HashMap<String, DataOutputStream>();
33
34 public static void dump(String component, Chunk chunk) {
35
36 String fileName = chunk.getStreamName();
37
38 if (!hash.containsKey(component + "-" + fileName)) {
39 File directory = new File(testRepositoryDumpDir + "/" + component);
40 if (!directory.exists()) {
41 directory.mkdirs();
42 }
43 String name = fileName;
44 if (fileName.indexOf("/") >= 0) {
45 name = fileName.substring(fileName.lastIndexOf("/"));
46 }
47 name += ".bin";
48
49 synchronized (name.intern()) {
50 System.out.println("FileName [" + name + "]");
51 try {
52 DataOutputStream dos = new DataOutputStream(new FileOutputStream(
53 new File(testRepositoryDumpDir + "/" + component + "/" + name)));
54 System.out.println("Writing to [" + testRepositoryDumpDir + "/"
55 + component + "/" + name + "]");
56 hash.put(component + "-" + fileName, dos);
57 } catch (FileNotFoundException e) {
58 e.printStackTrace();
59 }
60 }
61 }
62 String key = component + "-" + fileName;
63 synchronized (key.intern()) {
64 DataOutputStream dos = hash.get(key);
65 try {
66 chunk.write(dos);
67 dos.flush();
68 } catch (IOException e) {
69 e.printStackTrace();
70 }
71 }
72 }
73
74 static void close() {
75 Iterator<String> it = hash.keySet().iterator();
76 while (it.hasNext()) {
77 String key = it.next();
78 DataOutputStream dos = hash.get(key);
79 try {
80 dos.close();
81 } catch (Exception e) {
82 e.printStackTrace();
83 }
84 }
85 }
86 }