EIDSOutputWedge.java

  1. package gov.usgs.earthquake.eids;

  2. import java.io.File;
  3. import java.util.logging.Logger;
  4. import gov.usgs.earthquake.distribution.DefaultNotificationListener;
  5. import gov.usgs.earthquake.product.Product;
  6. import gov.usgs.util.Config;
  7. import gov.usgs.util.FileUtils;

  8. /**
  9.  * Output received products as files in a folder.
  10.  */
  11. public class EIDSOutputWedge extends DefaultNotificationListener {

  12.     /** Logging object. */
  13.     private static final Logger LOGGER = Logger
  14.             .getLogger(EIDSOutputWedge.class.getName());

  15.     /** String for output type of EQXML */
  16.     public static final String OUTPUT_TYPE_EQXML = "eqxml.xml";
  17.     /** String for output type of quakeml */
  18.     public static final String OUTPUT_TYPE_QUAKEML = "quakeml.xml";
  19.     /** String for output type of cube */
  20.     public static final String OUTPUT_TYPE_CUBE = "cube.txt";

  21.     /** Property for output directory */
  22.     public static final String OUTPUT_DIRECTORY_PROPERTY = "directory";
  23.     /** Property for temp directory */
  24.     public static final String TEMP_DIRECTORY_PROPERTY = "tempDirectory";
  25.     /** Property for file name */
  26.     public static final String FILE_NAME_PROPERTY = "contentFile";
  27.     /** Property for output format */
  28.     public static final String OUTPUT_FORMAT_PROPERTY = "outputFormat";

  29.     /** Default output directory */
  30.     public static final File DEFAULT_DIRECTORY = new File("outputdir");
  31.     /** Default temp directory */
  32.     public static final File DEFAULT_TEMP_DIRECTORY = new File(
  33.             System.getProperty("java.io.tmpdir"));
  34.     /** Sets default output format to cube.txt */
  35.     public static final String DEFAULT_OUTPUT_FORMAT = OUTPUT_TYPE_CUBE;

  36.     // Local Variables
  37.     private File directory = DEFAULT_DIRECTORY;
  38.     private File tempDirectory = DEFAULT_TEMP_DIRECTORY;
  39.     private String outputFormat = DEFAULT_OUTPUT_FORMAT;
  40.     // converter object
  41.     private LegacyConverter converter;

  42.     /**
  43.      * Create a new EIDSOutputWedge.
  44.      *
  45.      * Sets up the includeTypes list to contain "origin". Override this if you
  46.      * want the behavior to extend past origin products.
  47.      */
  48.     public EIDSOutputWedge() {
  49.         this.getIncludeTypes().add("origin");
  50.         converter = LegacyConverter.cubeConverter();
  51.     }

  52.     /**
  53.      * Receive a product from Product Distribution.
  54.      *
  55.      * @param product A product
  56.      */
  57.     @Override
  58.     public void onProduct(final Product product) throws Exception {
  59.         byte[] data = converter.convert(product);

  60.         if (data != null) {
  61.             write(data);
  62.         }
  63.     }

  64.     /**
  65.      * Configuration
  66.      */
  67.     @Override
  68.     public void configure(final Config config) throws Exception {
  69.         super.configure(config);

  70.         setDirectory(new File(config.getProperty(OUTPUT_DIRECTORY_PROPERTY,
  71.                 DEFAULT_DIRECTORY.getName())));

  72.         setTempDirectory(new File(config.getProperty(TEMP_DIRECTORY_PROPERTY,
  73.                 DEFAULT_TEMP_DIRECTORY.getName())));

  74.         setOutputFormat(config.getProperty(OUTPUT_FORMAT_PROPERTY,
  75.                 DEFAULT_OUTPUT_FORMAT));

  76.     }

  77.     /**
  78.      * Writes the content of the file you wish to extract to disk with a unique
  79.      * name and at the directory specified in configuration
  80.      *
  81.      * @param data
  82.      * @throws Exception
  83.      */
  84.     private void write(byte[] data) throws Exception {
  85.         String uniqueFileName = System.currentTimeMillis() + "_" + outputFormat;
  86.         File destFile = new File(directory, uniqueFileName);

  87.         // Handles case where filename is already in use
  88.         // In practice this shouldn't trigger
  89.         while (destFile.exists()) {
  90.             uniqueFileName = System.currentTimeMillis() + "_"
  91.                     + (int) (Math.random() * 10000) + "_" + outputFormat;
  92.             destFile = new File(directory, uniqueFileName);

  93.             LOGGER.info("Eqxml name not unique. Attempting to resolve as "
  94.                     + uniqueFileName);
  95.         }

  96.         File srcFile = new File(tempDirectory, uniqueFileName);
  97.         FileUtils.writeFileThenMove(srcFile, destFile, data);
  98.     }

  99.     /** @return directory */
  100.     public File getDirectory() {
  101.         return directory;
  102.     }

  103.     /** @return tempDirectory */
  104.     public File getTempDirectory() {
  105.         return tempDirectory;
  106.     }

  107.     /** @return outputFormat */
  108.     public String getOutputFormat() {
  109.         return outputFormat;
  110.     }

  111.     /** @return legacy converter */
  112.     public LegacyConverter getConverter() {
  113.         return converter;
  114.     }

  115.     /** @param directory file to set */
  116.     public void setDirectory(File directory) {
  117.         this.directory = directory;
  118.     }

  119.     /** @param tempDirectory file to set */
  120.     public void setTempDirectory(File tempDirectory) {
  121.         this.tempDirectory = tempDirectory;
  122.     }

  123.     /** @param outputFormat string to set */
  124.     public void setOutputFormat(String outputFormat) {
  125.         if (outputFormat.equals(OUTPUT_TYPE_EQXML)) {
  126.             converter = LegacyConverter.eqxmlConverter();
  127.         } else if (outputFormat.equals(OUTPUT_TYPE_QUAKEML)) {
  128.             converter = LegacyConverter.quakemlConverter();
  129.         } else if (outputFormat.equals(OUTPUT_TYPE_CUBE)) {
  130.             converter = LegacyConverter.cubeConverter();
  131.         } else {
  132.             throw new IllegalArgumentException("Unknown outputFormat '"
  133.                     + outputFormat + "'");
  134.         }
  135.         this.outputFormat = outputFormat;
  136.     }

  137. }