View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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  }