CorbaSender.java

  1. package gov.usgs.earthquake.eidsutil;

  2. import com.isti.quakewatch.server.qw_feeder.QWFeeder;
  3. import com.isti.quakewatch.server.qw_feeder.QWFeederHelper;

  4. import org.omg.CORBA.ORB;
  5. //import org.omg.CORBA.Object;
  6. import org.omg.CORBA.ORBPackage.InvalidName;
  7. import org.omg.PortableServer.POA;
  8. import org.omg.PortableServer.POAHelper;
  9. import org.omg.PortableServer.POAManager;
  10. import org.omg.PortableServer.POAManagerPackage.AdapterInactive;

  11. /**
  12.  * A <code>CorbaSender</code> is essentially a client-side wrapper for the
  13.  * <code>QWFeeder</code> IDL file specified by ISTI. This class is designed to
  14.  * provide simplified CORBA interaction with a QWServer (EIDS) machine. All the
  15.  * varied methods of sending messages are provided, however they are wrapped
  16.  * into a single method, namely, <code>sendMessage</code>. See the method
  17.  * documentation for details.
  18.  *
  19.  * @since 0.0.1
  20.  * @author <a href="mailto:emartinez@usgs.gov">Eric Martinez</a>
  21.  */
  22. public class CorbaSender {

  23.     //---------------------------------------------------------------------------
  24.     // Member Variables
  25.     //---------------------------------------------------------------------------

  26.     /** This is the URL used to obtain the underlying QWFeeder object. */
  27.     private String url = null;

  28.     /**
  29.      * This is the underlying <code>QWFeeder</code> used to send the messages to
  30.      * the server. The interface specification can be found in the
  31.      * <code>QWFeeder.idl</code> file from ISTI.
  32.      */
  33.     private QWFeeder feeder = null;

  34.     private ORB orb = null;
  35.     private POA poa = null;
  36.     private POAManager poaManager = null;
  37.     private org.omg.CORBA.Object object = null;

  38.     //---------------------------------------------------------------------------
  39.     // Constructors
  40.     //---------------------------------------------------------------------------

  41.     /**
  42.      * Initializes the <code>CorbaSender</code> such that it is ready to send a
  43.      * message to the specified <code>host</code> over the specified
  44.      * <code>port</code>. This uses the <code>QWFeeder</code> idl specified by
  45.      * ISTI as the underlying feeder. After instantiating a
  46.      * <code>CorbaSender</code> through this constructor, the instance is 100%
  47.      * ready to use. One downside is one cannot reuse that instance to send
  48.      * messages to another host; for such a feature one must instantiate a new
  49.      * object.
  50.      *
  51.      * @param host The host machine (ip or cname) to which you want to send the
  52.      * messages using this <code>CorbaSender</code>.
  53.      * @param port The port number to send messages to on the host.
  54.      *
  55.      * @throws InvalidName If the RootPOA is not aware of the type of object we
  56.      * request from it.
  57.      * @throws AdapterInactive If the poaManager is not active.
  58.      */
  59.     public CorbaSender(String host, String port)
  60.             throws InvalidName, AdapterInactive {

  61.         // Create the corbaloc url
  62.         url = "corbaloc:iiop:1.2@" + host + ":" + port + "/QWFeeder";

  63.         // Initialize the ORB
  64.         orb = ORB.init((new String[0]), null);

  65.         // Get the Root POA
  66.         poa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));

  67.         // Activate the POAManager
  68.         poaManager = poa.the_POAManager();
  69.         poaManager.activate();

  70.         // Get the generic object reference from the orb
  71.         object = orb.string_to_object(url);

  72.         // Narrow the generic object down to our a QWFeeder
  73.         feeder = QWFeederHelper.narrow(object);

  74.     } // END: constructor CorbaSender

  75.     /** Cleanup */
  76.     public void destroy() {
  77.         try { feeder._release(); } catch (Exception ex) { /* ignore */ }
  78.         try { object._release(); } catch (Exception ex) { /* ignore */ }
  79.         try { orb.shutdown(true); } catch (Exception ex) { /* ignore */ }
  80.         try { orb.destroy(); } catch (Exception ex) { /* ignore */ }

  81.         feeder = null;
  82.         object = null;
  83.         poaManager = null;
  84.         poa = null;
  85.         orb = null;
  86.     }

  87.     /**
  88.      * Retrieve a QWFeeder object associated with this CorbaSender.
  89.      *
  90.      * First checks if the object is "non_existent", and if so re-narrows the object.
  91.      * @return QWFeeder object, or null if unable to narrow.
  92.      */
  93.     protected QWFeeder getFeeder() {
  94.         if (feeder != null) {
  95.             try {
  96.                 if (feeder._non_existent()) {
  97.                     feeder = null;
  98.                     object = null;
  99.                 }
  100.             } catch (org.omg.CORBA.OBJECT_NOT_EXIST e) {
  101.                 // feeder._non_existent throws the OBJECT_NOT_EXIST exception
  102.                 // instead of returning true...
  103.                 feeder = null;
  104.                 object = null;
  105.             }
  106.         }

  107.         if (feeder == null) {
  108.             //recreate the object, must string_to_object AND narrow
  109.             object = orb.string_to_object(url);
  110.             feeder = QWFeederHelper.narrow(object);
  111.         }

  112.         return feeder;
  113.     }

  114.     //---------------------------------------------------------------------------
  115.     // Static Methods
  116.     //---------------------------------------------------------------------------

  117.     /* None at this time. */

  118.     //---------------------------------------------------------------------------
  119.     // Public Methods
  120.     //---------------------------------------------------------------------------

  121.     /**
  122.      * Sends a data event message. If the event data does not begin with a
  123.      * &ldquo;DataMessage&rdquo; XML element then the data will be surrounded
  124.      * with one. The &ldquo;sendSourced...&rdquo; methods are preferred because
  125.      * the feeder-source host name and message number are used for improved
  126.      * message tracking.
  127.      *
  128.      * @param message The data event message string.
  129.      * @return <code>true</code> after the message has been successfully stored
  130.      * and processed; <code>false</code> if an error occurred.
  131.      */
  132.     public boolean sendMessage(String message) {
  133.         return getFeeder().sendMessage(message);
  134.     }

  135.     /**
  136.      * Sends a data event message. If the event data does not begin with a
  137.      * &ldquo;DataMessage&rdquo; XML element then the data will be surrounded
  138.      * with one. The &ldquo;sendSourced...&rdquo; methods are preferred because
  139.      * the feeder-source host name and message number are used for improved
  140.      * message tracking.
  141.      *
  142.      * @param domain The domain name to use.
  143.      * @param type The type name to use.
  144.      * @param message The data event message string.
  145.      *
  146.      * @return <code>true</code> after the message has been successfully stored
  147.      * and processed; <code>false</code> if an error occurred.
  148.      */
  149.     public boolean sendMessage(String domain, String type, String message) {
  150.         return getFeeder().sendDomainTypeMessage(domain, type, message);
  151.     }


  152.     /**
  153.      * Sends a data event message. If the event data does not begin with a
  154.      * &ldquo;DataMessage&rdquo; XML element then the data will be surrounded
  155.      * with one. The &ldquo;sendSourced...&rdquo; methods are preferred because
  156.      * the feeder-source host name and message number are used for improved
  157.      * message tracking.
  158.      *
  159.      * @param domain The domain name to use.
  160.      * @param type The type name to use.
  161.      * @param name The event name to use.
  162.      * @param message The data event message string.
  163.      *
  164.      * @return <code>true</code> after the message has been successfully stored
  165.      * and processed; <code>false</code> if an error occurred.
  166.      */
  167.     public boolean sendMessage(String domain, String type, String name,
  168.             String message) {
  169.         return getFeeder().sendDomainTypeNameMessage(domain, type, name, message);
  170.     }

  171.     /**
  172.      * Sends a data event message. If the event data does not begin with a
  173.      * &ldquo;DataMessage&rdquo; XML element then the data will be surrounded
  174.      * with one. The &ldquo;sendSourced...&rdquo; methods are preferred because
  175.      * the feeder-source host name and message number are used for improved
  176.      * message tracking.
  177.      *
  178.      * @param message The data event message string.
  179.      * @param feederSourceHost The data-source host string for the message.
  180.      * @param feederSrcHostMsgId the message-ID number from the data source
  181.      * (positive value incremented after each message).
  182.      *
  183.      * @return <code>true</code> after the message has been successfully stored
  184.      * and processed; <code>false</code> if an error occurred.
  185.      */
  186.     public boolean sendMessage(String message, String feederSourceHost,
  187.             long feederSrcHostMsgId) {
  188.         return getFeeder().sendSourcedMsg(message, feederSourceHost,
  189.                 feederSrcHostMsgId);
  190.     }

  191.     /**
  192.      * Sends a data event message. If the event data does not begin with a
  193.      * &ldquo;DataMessage&rdquo; XML element then the data will be surrounded
  194.      * with one. The &ldquo;sendSourced...&rdquo; methods are preferred because
  195.      * the feeder-source host name and message number are used for improved
  196.      * message tracking.
  197.      *
  198.      * @param domain The domain name to use.
  199.      * @param type The type name to use.
  200.      * @param message The data event message string.
  201.      * @param feederSourceHost The data-source host string for the message.
  202.      * @param feederSrcHostMsgId The message-ID number from the data source
  203.      * (positive value incremented after each message).
  204.      *
  205.      * @return <code>true</code> after the message has been successfully stored
  206.      * and processed; <code>false</code> if an error occurred.
  207.      */
  208.     public boolean sendMessage(String domain, String type, String message,
  209.             String feederSourceHost, long feederSrcHostMsgId) {
  210.         return getFeeder().sendSourcedDomainTypeMsg(domain, type, message,
  211.                 feederSourceHost, feederSrcHostMsgId);
  212.     }

  213.     /**
  214.      * Sends a data event message. If the event data does not begin with a
  215.      * &ldquo;DataMessage&rdquo; XML element then the data will be surrounded
  216.      * with one. The &ldquo;sendSourced...&rdquo; methods are preferred because
  217.      * the feeder-source host name and message number are used for improved
  218.      * message tracking.
  219.      *
  220.      * @param domain The domain name to use.
  221.      * @param type The type name to use.
  222.      * @param name The event name to use.
  223.      * @param message The data event message string.
  224.      * @param feederSourceHost The data-source host string for the message.
  225.      * @param feederSrcHostMsgId The message-ID number from the data source
  226.      * (positive value incremented after each message).
  227.      *
  228.      * @return <code>true</code> after the message has been successfully stored
  229.      * and processed; <code>false</code> if an error occurred.
  230.      */
  231.     public boolean sendMessage(String domain, String type, String name,
  232.             String message, String feederSourceHost, long feederSrcHostMsgId) {
  233.         return getFeeder().sendSourcedDomainTypeNameMsg(domain, type, name, message,
  234.                 feederSourceHost, feederSrcHostMsgId);
  235.     }

  236.     //---------------------------------------------------------------------------
  237.     // Private Methods
  238.     //---------------------------------------------------------------------------

  239.     /* None at this time. */

  240. } // END: class CorbaSender