EIDSNotificationReceiver.java

  1. /*
  2.  * EIDSNotificationReceiver
  3.  */
  4. package gov.usgs.earthquake.distribution;

  5. import java.io.InputStream;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;

  8. import gov.usgs.earthquake.eidsutil.EIDSClient;
  9. import gov.usgs.earthquake.eidsutil.EIDSListener;
  10. import gov.usgs.earthquake.eidsutil.EIDSMessageEvent;
  11. import gov.usgs.util.Config;
  12. import gov.usgs.util.StreamUtils;

  13. /**
  14.  * Receive XML notifications using EIDS.
  15.  *
  16.  *
  17.  * This class implements the Configurable interface, and has the following
  18.  * options:
  19.  * <dl>
  20.  * <dt>serverHost</dt>
  21.  * <dd>The EIDS Server hostname or IP address.</dd>
  22.  * <dt>serverPort</dt>
  23.  * <dd>The EIDS Server listen port, usually 39977.</dd>
  24.  * <dt>alternateServers</dt>
  25.  * <dd>A comma separated list of HOST:PORT pairs. The EIDSClient will attempt to
  26.  * connect to these servers when unable to connect to the primary
  27.  * serverHost:serverPort.</dd>
  28.  * <dt>trackingFile</dt>
  29.  * <dd>A file name used for tracking connection state. This is used to receive
  30.  * missed messages when (re)connecting.</dd>
  31.  * </dl>
  32.  */
  33. public class EIDSNotificationReceiver extends DefaultNotificationReceiver
  34.         implements EIDSListener {

  35.     /** Logging object. */
  36.     private static final Logger LOGGER = Logger
  37.             .getLogger(EIDSNotificationReceiver.class.getName());

  38.     /** Property name for eids server host address. */
  39.     public static final String EIDS_SERVER_HOST_PROPERTY = "serverHost";

  40.     /** Property name for eids server port. */
  41.     public static final String EIDS_SERVER_PORT = "serverPort";

  42.     /** Property name for eids server alternate servers list. */
  43.     public static final String EIDS_ALTERNATE_SERVERS = "alternateServers";

  44.     /** Property name for eids client tracking file. */
  45.     public static final String EIDS_TRACKING_FILE = "trackingfile";

  46.     /** Property name for eids client log level. */
  47.     public static final String EIDS_DEBUG = "eidsDebug";

  48.     /** Property name for eids max server event age. */
  49.     public static final String EIDS_MAX_EVENT_AGE = "maxServerEventAgeDays";

  50.     /** EIDSClient that receives notifications. */
  51.     private EIDSClient client;

  52.     //TODO: Change condition using URLNotificationParser
  53.     /**
  54.      * Implement the EIDSListener interface to process messages from EIDS.
  55.      *
  56.      * Checks to make sure message has Correct namespace and root element before
  57.      * parsing.
  58.      */
  59.     public void onEIDSMessage(EIDSMessageEvent event) {
  60.         if (URLNotificationParser.PRODUCT_XML_NAMESPACE.equals(event
  61.                 .getRootNamespace())
  62.                 && URLNotificationParser.NOTIFICATION_ELEMENT.equals(event
  63.                         .getRootElement())) {
  64.             InputStream in = null;
  65.             try {
  66.                 in = StreamUtils.getInputStream(event.getMessage());
  67.                 // this is a notification message
  68.                 URLNotification notification = URLNotificationXMLConverter.parseXML(in);
  69.                 // process the notification
  70.                 receiveNotification(notification);
  71.             } catch (Exception e) {
  72.                 LOGGER.log(Level.WARNING, "[" + getName()
  73.                         + "] exception while parsing URLNotification", e);
  74.             } finally {
  75.                 StreamUtils.closeStream(in);
  76.             }
  77.         } else {
  78.             LOGGER.info("[" + getName() + "] ignoring message type "
  79.                     + event.getRootNamespace() + ":" + event.getRootElement());
  80.             LOGGER.info("[" + getName() + "] message content: "
  81.                     + event.getMessage());
  82.         }
  83.     }

  84.     public void configure(Config config) throws Exception {
  85.         super.configure(config);
  86.         // configure eids client
  87.         client = new EIDSClient();
  88.         client.addListener(this);

  89.         String serverHost = config.getProperty(EIDS_SERVER_HOST_PROPERTY,
  90.                 EIDSClient.DEFAULT_SERVER_HOST);
  91.         LOGGER.config("[" + getName() + "] EIDS server host is '" + serverHost
  92.                 + "'");
  93.         client.setServerHost(serverHost);

  94.         Integer serverPort = Integer.valueOf(config.getProperty(
  95.                 EIDS_SERVER_PORT,
  96.                 Integer.toString(EIDSClient.DEFAULT_SERVER_PORT)));
  97.         LOGGER.config("[" + getName() + "] EIDS server port is '" + serverPort
  98.                 + "'");
  99.         client.setServerPort(serverPort);

  100.         String alternateServers = config
  101.                 .getProperty(EIDS_ALTERNATE_SERVERS, "");
  102.         LOGGER.config("[" + getName() + "] EIDS alternate servers '"
  103.                 + alternateServers + "'");
  104.         client.setAlternateServersList(alternateServers);

  105.         String trackingFile = config.getProperty(EIDS_TRACKING_FILE);
  106.         if (trackingFile != null) {
  107.             LOGGER.config("[" + getName() + "] EIDS tracking file is '"
  108.                     + trackingFile + "'");
  109.             client.setTrackingFileName(trackingFile);
  110.         }

  111.         String debug = config.getProperty(EIDS_DEBUG);
  112.         if (debug != null) {
  113.             LOGGER.config("[" + getName() + "] EIDS debug mode = '" + debug
  114.                     + "'");
  115.             client.setDebug(Boolean.parseBoolean(debug));
  116.         }

  117.         String maxEventAgeString = config.getProperty(EIDS_MAX_EVENT_AGE);
  118.         if (maxEventAgeString != null) {
  119.             LOGGER.config("[" + getName() + "] EIDS max event age is " + maxEventAgeString + " days");
  120.             client.setMaxServerEventAgeDays(Double.parseDouble(maxEventAgeString));
  121.         }
  122.     }

  123.     public void shutdown() throws Exception {
  124.         client.shutdown();
  125.         super.shutdown();
  126.     }

  127.     public void startup() throws Exception {
  128.         super.startup();
  129.         if (client == null) {
  130.             throw new ConfigurationException("[" + getName()
  131.                     + "] EIDS client is not properly configured");
  132.         }
  133.         client.startup();
  134.     }

  135.     /** @return EIDSClient */
  136.     public EIDSClient getClient() {
  137.         return client;
  138.     }

  139.     /** @param client set EIDSClient */
  140.     public void setClient(EIDSClient client) {
  141.         this.client = client;
  142.     }

  143. }