View Javadoc

1   /*
2    * The contents of this file are subject to the terms 
3    * of the Common Development and Distribution License 
4    * (the "License").  You may not use this file except 
5    * in compliance with the License.
6    * 
7    * You can obtain a copy of the license at 
8    * http://www.sun.com/cddl/cddl.html. 
9    * See the License for the specific language governing 
10   * permissions and limitations under the License.
11   * 
12   * When distributing Covered Code, include this CDDL 
13   * HEADER in each file and include the License file at 
14   * license.txt.  If applicable, add the following below 
15   * this CDDL HEADER, with the fields enclosed by brackets 
16   * "[]" replaced with your own identifying information: 
17   * Portions Copyright [yyyy] [name of copyright owner]
18   * 
19   * Portions Copyright 2004 eBay, Inc.
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&lt;String, String&gt; &lt;path on classpath, relative output path&gt;
61       */
62      public void setClasspathPaths(Map classpathPaths) {
63          mClasspathPaths = classpathPaths;
64      }
65      /***
66       * @param filesystemPaths Map&lt;String, String&gt; &lt;path on file system, relative output path&gt;
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      	// TODO : this copyFile method should be extracted to a general utility
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         // Transfer bytes from in to out
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