ExtentIndexerListener.java

  1. /**
  2.  * Extent Indexer Listener
  3.  */
  4. package gov.usgs.earthquake.indexer;

  5. import gov.usgs.util.Config;
  6. import gov.usgs.earthquake.distribution.ConfigurationException;

  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;

  9. /**
  10.  * ExtentIndexerListener is an extension of the ReliableIndexerListener. It
  11.  * populates the ExtentSummary table with viable products.
  12.  *
  13.  * This listener takes an ExtentIndex for querying and table updates:
  14.  * <dl>
  15.  * <dt>index</dt>
  16.  * <dd>(Required) the ExtentIndex used for querying and updates</dd>
  17.  * </dl>
  18.  */
  19. public class ExtentIndexerListener extends ReliableIndexerListener {

  20.   private static final Logger LOGGER = Logger.getLogger(ExtentIndexerListener.class.getName());

  21.   /**
  22.    * Configures listener, checking for correct type
  23.    *
  24.    * @param config configuration
  25.    *
  26.    * @throws ConfigurationException if incorrect type provided
  27.    */
  28.   @Override
  29.   public void configure(Config config) throws Exception {
  30.     super.configure(config);
  31.     if (!(this.getProductIndex() instanceof ExtentIndex)) {
  32.       throw new ConfigurationException(
  33.           "[" + getName() + "] index must be of type ExtentIndex. Given type "
  34.           + productIndex.getClass().getName());
  35.     }
  36.   }

  37.   /**
  38.    * Loads the last index id in the extent table before the listener has to use it
  39.    *
  40.    * @throws Exception if ExtentIndex can't do database transaction
  41.    */
  42.   @Override
  43.   protected void onBeforeProcessThreadStart() throws Exception {
  44.     long lastIndexId = ((ExtentIndex) productIndex).getLastExtentIndexId();
  45.     LOGGER.fine("[" + getName() + "] last index id " + lastIndexId);
  46.     setLastIndexId(lastIndexId);
  47.   }

  48.   /**
  49.    * Hands product to index to be added to table
  50.    *
  51.    * @param product the product to be added
  52.    *
  53.    * @throws Exception if ExtentIndex can't do database transaction
  54.    */
  55.   @Override
  56.   public void processProduct(ProductSummary product) throws Exception {
  57.     super.processProduct(product);
  58.     ExtentSummary extent = new ExtentSummary(product);

  59.     if (extent.isValid()) {
  60.       LOGGER.log(Level.FINEST, "[" + getName() + "] adding product "
  61.           + product.getId().toString()
  62.           + " to extent table");
  63.       ((ExtentIndex) productIndex).addExtentSummary(extent);
  64.     } else {
  65.       LOGGER.log(Level.FINEST, "[" + getName() + "] product "
  66.           + product.getId().toString()
  67.           + " has no extent information; won't add to extent table");
  68.     }

  69.     setLastIndexId(product.getIndexId());
  70.   }

  71. }