EIDSNotificationSender.java

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

  5. import gov.usgs.earthquake.eidsutil.CorbaSender;
  6. import gov.usgs.util.Config;
  7. import gov.usgs.util.FileUtils;

  8. import java.io.File;
  9. import java.util.Date;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;

  12. public class EIDSNotificationSender extends DefaultNotificationSender {

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

  16.     /** Property referencing directory where notifications are "sent". */
  17.     public static final String EIDS_POLLDIR_PROPERTY = "serverPolldir";

  18.     /** Default directory where notifications are sent. */
  19.     public static final String EIDS_DEFAULT_POLLDIR = "polldir";

  20.     /** Directory for Polldir send, in case CORBA send fails. */
  21.     private File serverPolldir = null;
  22.     /** CORBA sending object. */
  23.     private CorbaSender corbaSender = null;

  24.     @Override
  25.     protected void sendNotification(final Notification notification) throws Exception {
  26.         boolean sent = false;
  27.         String message = URLNotificationXMLConverter.toXML((URLNotification) notification);

  28.         if (serverHost != null && serverPort != null) {
  29.             try {
  30.                 if (corbaSender == null) {
  31.                     // try to establish a connection
  32.                     corbaSender = new CorbaSender(serverHost, serverPort);
  33.                 }
  34.                 sent = corbaSender.sendMessage(message);
  35.                 if (sent) {
  36.                     LOGGER.fine("[" + getName()
  37.                             + "] sent notification to EIDS via CORBA");
  38.                 }
  39.             } catch (Exception e) {
  40.                 LOGGER.log(Level.WARNING, "[" + getName()
  41.                         + "] unable to send notification using CORBA", e);
  42.             }
  43.         }

  44.         if (!sent) {
  45.             // when unable to send directly to corba service
  46.             // send notification via EIDS polldir

  47.             // create a uniqueish filename
  48.             String filename = "out_" + new Date().getTime();
  49.             File outFile = new File(serverPolldir, filename + ".xml");
  50.             while (outFile.exists()) {
  51.                 filename = filename += "_1";
  52.                 outFile = new File(serverPolldir, filename + ".xml");
  53.             }

  54.             // is this atomic enough?, write then move may be better
  55.             FileUtils.writeFile(outFile, message.getBytes());
  56.             LOGGER.log(Level.INFO, "[" + getName()
  57.                     + "] sent notification to EIDS via " + outFile.getPath());
  58.         }
  59.     }

  60.     public void configure(Config config) throws Exception {
  61.         // let default notification sender configure itself
  62.         super.configure(config);

  63.         serverPolldir = new File(config.getProperty(EIDS_POLLDIR_PROPERTY,
  64.                 EIDS_DEFAULT_POLLDIR));
  65.         LOGGER.config("[" + getName() + "] EIDS server polldir '"
  66.                 + serverPolldir + "'");

  67.     }

  68.     @Override
  69.     public void shutdown() throws Exception {
  70.         super.shutdown();

  71.         if (corbaSender != null) {
  72.             try {
  73.                 corbaSender.destroy();
  74.             } catch (Exception e) {
  75.                 // ignore
  76.             }
  77.             corbaSender = null;
  78.         }
  79.     }

  80.     @Override
  81.     public void startup() throws Exception {
  82.         if (serverHost != null && serverPort != null) {
  83.             try {
  84.                 corbaSender = new CorbaSender(serverHost, serverPort);
  85.             } catch (org.omg.CORBA.COMM_FAILURE e) {
  86.                 LOGGER.warning("[" + getName()
  87.                         + "] unable to connect to EIDS using CORBA");
  88.                 corbaSender = null;
  89.             }
  90.         }

  91.         super.startup();
  92.     }

  93.     /** @return serverPolldir */
  94.     public File getServerPolldir() {
  95.         return serverPolldir;
  96.     }

  97.     /** @param serverPolldir file to set */
  98.     public void setServerPolldir(File serverPolldir) {
  99.         this.serverPolldir = serverPolldir;
  100.     }

  101. }