ObjectProductSource.java

  1. /*
  2.  * ObjectProductSource
  3.  */
  4. package gov.usgs.earthquake.product.io;

  5. import gov.usgs.earthquake.product.Content;
  6. import gov.usgs.earthquake.product.Product;
  7. import gov.usgs.earthquake.product.ProductId;

  8. import java.net.URI;

  9. import java.util.Iterator;
  10. import java.util.Map;
  11. import java.util.List;
  12. import java.util.TreeSet;

  13. /**
  14.  * Convert a java Product object into events for a ProductHandler.
  15.  *
  16.  * ObjectProductSource turns a product object into a stream of events for
  17.  * ProductOutputs.
  18.  *
  19.  * ObjectProductSources are reuseable for a given product.
  20.  */
  21. public class ObjectProductSource implements ProductSource {

  22.     /** The product being sent. */
  23.     private Product product;

  24.     /**
  25.      * Construct a new ObjectProductSource.
  26.      *
  27.      * @param product
  28.      *            the product used for input.
  29.      */
  30.     public ObjectProductSource(final Product product) {
  31.         this.product = product;
  32.     }

  33.     /**
  34.      * Send a product object to a ProductOutput.
  35.      *
  36.      * Calls these methods in the following order:
  37.      * <ol>
  38.      * <li>sendBeginProduct
  39.      * <li>sendProperties
  40.      * <li>sendLinks
  41.      * <li>sendContents
  42.      * <li>sendSignature
  43.      * <li>sendEndProducct
  44.      * </ol>
  45.      *
  46.      * @param out
  47.      *            the ProductOutput that will receive the Product.
  48.      */
  49.     public void streamTo(final ProductHandler out) throws Exception {
  50.         sendBeginProduct(out);
  51.         sendProperties(out);
  52.         sendLinks(out);
  53.         sendContents(out);
  54.         sendSignatureVersion(out);
  55.         sendSignature(out);
  56.         sendEndProduct(out);
  57.     }

  58.     /**
  59.      * Call out's onBeginProduct method for this product.
  60.      *
  61.      * @param out
  62.      *            the receiving ProductOutput.
  63.      * @throws Exception
  64.      *             if out.onBeginProduct throws an Exception.
  65.      */
  66.     public void sendBeginProduct(final ProductHandler out) throws Exception {
  67.         out.onBeginProduct(product.getId(), product.getStatus(), product
  68.                 .getTrackerURL());
  69.     }

  70.     /**
  71.      * Call out's onProperty method for each product property. Calls in
  72.      * alphabetical order by property name.
  73.      *
  74.      * @param out
  75.      *            the receiving ProductOutput.
  76.      * @throws Exception
  77.      *             if out.onProperty throws an Exception.
  78.      */
  79.     public void sendProperties(final ProductHandler out) throws Exception {
  80.         ProductId id = product.getId();
  81.         Map<String, String> props = product.getProperties();
  82.         // in alphabetical order by property name
  83.         Iterator<String> keys = new TreeSet<String>(props.keySet()).iterator();
  84.         while (keys.hasNext()) {
  85.             String key = keys.next().toString();
  86.             String value = props.get(key);
  87.             out.onProperty(id, key, value);
  88.         }
  89.     }

  90.     /**
  91.      * Call out's onLink method for each product link. Calls in alphabetical
  92.      * order by relation name, by URI.
  93.      *
  94.      * @param out
  95.      *            the receiving ProductOutput.
  96.      * @throws Exception
  97.      *             if out.onProperty throws an Exception.
  98.      */
  99.     public void sendLinks(final ProductHandler out) throws Exception {
  100.         ProductId id = product.getId();
  101.         Map<String, List<URI>> links = product.getLinks();
  102.         // in alphabetical order by relation name
  103.         Iterator<String> linkRelations = new TreeSet<String>(links.keySet())
  104.                 .iterator();
  105.         while (linkRelations.hasNext()) {
  106.             String relation = linkRelations.next();
  107.             // in alphabetical order by URI
  108.             Iterator<URI> linkURIs = new TreeSet<URI>(links.get(relation))
  109.                     .iterator();
  110.             while (linkURIs.hasNext()) {
  111.                 URI uri = linkURIs.next();
  112.                 out.onLink(id, relation, uri);
  113.             }
  114.         }
  115.     }

  116.     /**
  117.      * Call out's onContent method for each product content. Calls
  118.      * alphabetically by content path.
  119.      *
  120.      * @param out
  121.      *            the receiving ProductOutput.
  122.      * @throws Exception
  123.      *             if out.onContent throws an Exception.
  124.      */
  125.     public void sendContents(final ProductHandler out) throws Exception {
  126.         ProductId id = product.getId();
  127.         Map<String, Content> contents = product.getContents();
  128.         // in alphabetical order by content path
  129.         Iterator<String> paths = new TreeSet<String>(contents.keySet())
  130.                 .iterator();
  131.         while (paths.hasNext()) {
  132.             String path = paths.next();
  133.             out.onContent(id, path, contents.get(path));
  134.         }
  135.     }


  136.     /**
  137.      * Call out's onSignature method with product signature.
  138.      *
  139.      * @param out
  140.      *            the receiving ProductOutput.
  141.      * @throws Exception
  142.      *             if out.onSignature throws an Exception.
  143.      */
  144.     public void sendSignatureVersion(final ProductHandler out) throws Exception {
  145.         out.onSignatureVersion(product.getId(), product.getSignatureVersion());
  146.     }

  147.     /**
  148.      * Call out's onSignature method with product signature.
  149.      *
  150.      * @param out
  151.      *            the receiving ProductOutput.
  152.      * @throws Exception
  153.      *             if out.onSignature throws an Exception.
  154.      */
  155.     public void sendSignature(final ProductHandler out) throws Exception {
  156.         out.onSignature(product.getId(), product.getSignature());
  157.     }

  158.     /**
  159.      * Call out's onEndProduct method for product.
  160.      *
  161.      * @param out
  162.      *            the receiving ProductOutput.
  163.      * @throws Exception
  164.      *             if out.onEndProduct throws an Exception.
  165.      */
  166.     public void sendEndProduct(final ProductHandler out) throws Exception {
  167.         out.onEndProduct(product.getId());
  168.     }


  169.     /**
  170.      * Free any resources associated with this source.
  171.      */
  172.     @Override
  173.     public void close() {
  174.         this.product = null;
  175.     }

  176. }