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.getLogger(EIDSOutputWedge.class.getName());

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

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

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

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

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

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

  58.     if (data != null) {
  59.       write(data);
  60.     }
  61.   }

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

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

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

  70.     setOutputFormat(config.getProperty(OUTPUT_FORMAT_PROPERTY, DEFAULT_OUTPUT_FORMAT));

  71.   }

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

  82.     // Handles case where filename is already in use
  83.     // In practice this shouldn't trigger
  84.     while (destFile.exists()) {
  85.       uniqueFileName = System.currentTimeMillis() + "_" + (int) (Math.random() * 10000) + "_" + outputFormat;
  86.       destFile = new File(directory, uniqueFileName);

  87.       LOGGER.info("Eqxml name not unique. Attempting to resolve as " + uniqueFileName);
  88.     }

  89.     File srcFile = new File(tempDirectory, uniqueFileName);
  90.     FileUtils.writeFileThenMove(srcFile, destFile, data);
  91.   }

  92.   /** @return directory */
  93.   public File getDirectory() {
  94.     return directory;
  95.   }

  96.   /** @return tempDirectory */
  97.   public File getTempDirectory() {
  98.     return tempDirectory;
  99.   }

  100.   /** @return outputFormat */
  101.   public String getOutputFormat() {
  102.     return outputFormat;
  103.   }

  104.   /** @return legacy converter */
  105.   public LegacyConverter getConverter() {
  106.     return converter;
  107.   }

  108.   /** @param directory file to set */
  109.   public void setDirectory(File directory) {
  110.     this.directory = directory;
  111.   }

  112.   /** @param tempDirectory file to set */
  113.   public void setTempDirectory(File tempDirectory) {
  114.     this.tempDirectory = tempDirectory;
  115.   }

  116.   /** @param outputFormat string to set */
  117.   public void setOutputFormat(String outputFormat) {
  118.     if (outputFormat.equals(OUTPUT_TYPE_EQXML)) {
  119.       converter = LegacyConverter.eqxmlConverter();
  120.     } else if (outputFormat.equals(OUTPUT_TYPE_QUAKEML)) {
  121.       converter = LegacyConverter.quakemlConverter();
  122.     } else if (outputFormat.equals(OUTPUT_TYPE_CUBE)) {
  123.       converter = LegacyConverter.cubeConverter();
  124.     } else {
  125.       throw new IllegalArgumentException("Unknown outputFormat '" + outputFormat + "'");
  126.     }
  127.     this.outputFormat = outputFormat;
  128.   }

  129. }