1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package com.ebay.carad.os.vitalsigns.listeners;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.OutputStream;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.Map;
32 import java.util.Map.Entry;
33
34 import org.apache.log4j.Logger;
35
36 import com.ebay.carad.os.vitalsigns.IDashboardAgent;
37 import com.ebay.carad.os.vitalsigns.ReportingException;
38
39 /***
40 * <p>Used to copy arbitrary files when reporting is started.
41 * This is useful if you have files associated with the output of the reporting
42 * that are not directly generated by the reporting.</p>
43 *
44 * <p>Files can currently be copied from either the classpath or a file system path.</p>
45 *
46 * @author Jeremy Thomerson
47 * @version $Id$
48 */
49 public class FileCopyingListener extends AbstractReportingListener {
50
51 private static final Logger LOGGER = Logger.getLogger(FileCopyingListener.class);
52 private Map mClasspathPaths = new HashMap();
53 private Map mFilesystemPaths = new HashMap();
54
55 public FileCopyingListener() {
56 super();
57 }
58
59 /***
60 * @param classpathPaths Map<String, String> <path on classpath, relative output path>
61 */
62 public void setClasspathPaths(Map classpathPaths) {
63 mClasspathPaths = classpathPaths;
64 }
65 /***
66 * @param filesystemPaths Map<String, String> <path on file system, relative output path>
67 */
68 public void setFilesystemPaths(Map filesystemPaths) {
69 mFilesystemPaths = filesystemPaths;
70 }
71 public void addClasspathPath(String path, String relativeOutputPath) {
72 mClasspathPaths.put(path, relativeOutputPath);
73 }
74 public void addFilesystemPath(String path, String relativeOutputPath) {
75 mFilesystemPaths.put(path, relativeOutputPath);
76 }
77
78 public void reportingPreStart(IDashboardAgent agent) {
79 super.reportingPreStart(agent);
80 try {
81 for (Iterator it = mClasspathPaths.entrySet().iterator(); it.hasNext();) {
82 Entry entry = (Entry) it.next();
83 InputStream st = getClass().getResourceAsStream((String) entry.getKey());
84 LOGGER.info("copying [classpath] file: " + entry.getKey() + " to: " + agent.getDestinationPath() + entry.getValue());
85 copyFile(agent, st, (String) entry.getValue());
86 }
87 for (Iterator it = mFilesystemPaths.entrySet().iterator(); it.hasNext();) {
88 Entry entry = (Entry) it.next();
89 InputStream st = new FileInputStream(new File((String) entry.getKey()));
90 LOGGER.info("copying file: " + entry.getKey() + " to: " + agent.getDestinationPath() + entry.getValue());
91 copyFile(agent, st, (String) entry.getValue());
92 }
93 } catch (IOException ioe) {
94 throw new ReportingException("Exception while copying files: " + ioe.getMessage(), ioe);
95 }
96 }
97
98 private void copyFile(IDashboardAgent agent, InputStream st, String relativeOutputPath) throws IOException {
99
100 File base = new File(agent.getDestinationPath());
101 base.mkdirs();
102
103 File outputFile = new File(base, relativeOutputPath);
104 outputFile.getParentFile().mkdirs();
105 OutputStream out = new FileOutputStream(outputFile);
106
107
108 byte[] buf = new byte[1024];
109 int len;
110 while ((len = st.read(buf)) > 0) {
111 out.write(buf, 0, len);
112 }
113 st.close();
114 out.flush();
115 out.close();
116 }
117 }
118