1 package javaclients.jdbc;
  2 
  3 import java.util.logging.Logger;
  4 import java.util.logging.Level;
  5 import org.xmlBlaster.util.Global;
  6 import org.xmlBlaster.client.I_XmlBlasterAccess;
  7 import org.xmlBlaster.client.key.GetKey;
  8 import org.xmlBlaster.client.qos.GetQos;
  9 import org.xmlBlaster.client.XmlDbMessageWrapper;
 10 import org.xmlBlaster.util.MsgUnit;
 11 
 12 
 13 /**
 14  * Example code how to access the xmlBlaster JDBC service
 15  * synchronous with the get() method.
 16  *
 17  * get() requests on key oid="__sys__jdbc" are handled by xmlBlaster (see RequestBroker.java)
 18  * directly and the result set is delivered as the return value of the get() request.
 19  *
 20  * See README for usage
 21  *
 22  * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/engine.service.rdbms.html">Requirement engine.service.rdbms</a>
 23  * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/engine.service.rdbms.jdbcpool.html">Requirement engine.service.rdbms.jdbcpool</a>
 24  */
 25 public class XmlDBClientSync
 26 {
 27    private static String   ME = "XmlDBClientSync";
 28    private final Global glob;
 29    private static Logger log = Logger.getLogger(XmlDBClientSync.class.getName());
 30    private I_XmlBlasterAccess corbaConnection = null;
 31 
 32    /**
 33     * Constructor declaration
 34     */
 35    public XmlDBClientSync(Global glob) {
 36       this.glob = glob;
 37 
 38       initBlaster();
 39       query();
 40       logout();
 41    }
 42 
 43 
 44    /**
 45     * Find xmlBlaster server and login.
 46     */
 47    public void initBlaster() {
 48       try {
 49          corbaConnection = glob.getXmlBlasterAccess();
 50          corbaConnection.connect(null, null);
 51          log.info("Connected to xmlBlaster");
 52       }
 53       catch (Exception e) {
 54          e.printStackTrace();
 55          log.severe("Login to xmlBlaster failed");
 56          System.exit(1);
 57       }
 58    }
 59 
 60 
 61    /**
 62     * Logout from xmlBlaster.
 63     */
 64    public void logout() {
 65       if (corbaConnection == null) return;
 66       log.info("Logout ...");
 67       corbaConnection.disconnect(null);
 68    }
 69 
 70    /**
 71     * Send the SQL message.
 72     */
 73    private void query() {
 74       XmlDbMessageWrapper wrap = new XmlDbMessageWrapper(glob,
 75          glob.getProperty().get("user", "postgres"),
 76          glob.getProperty().get("pass", ""),
 77          glob.getProperty().get("url",  "jdbc:postgresql://24.3.47.214/postgres"));
 78 
 79       boolean confirm = glob.getProperty().get("confirm", true);
 80       String type = glob.getProperty().get("type", "query");
 81       int limit = glob.getProperty().get("limit", 50);
 82       String queryStr = glob.getProperty().get("query", "select * from intrauser");
 83 
 84       wrap.init(type, limit, confirm, queryStr);
 85 
 86       try {
 87          log.info("Sending command string:\n" + wrap.toXml());
 88          GetKey key = new GetKey(glob, "__sys__jdbc");
 89          key.wrap(wrap.toXml());
 90          GetQos qos = new GetQos(glob);
 91          // get() blocks until the query is finished ...
 92          MsgUnit[] msgUnitArr = corbaConnection.get(key.toXml(), qos.toXml());
 93          if (msgUnitArr.length > 0)
 94             System.out.println(new String(msgUnitArr[0].getContent()));
 95          else
 96             log.info("No results for your query");
 97       }
 98       catch (Exception e) { log.severe("Query failed: " + e.toString()); }
 99    }
100 
101    /**
102     * java javaclients.jdbc.XmlDBClientSync \
103     *    -url "jdbc:postgresql://24.3.47.214/postgres" \
104     *    -user postgres \
105     *    -pass secret \
106     *    -query "select * from foo_table" \
107     *    -limit 50
108     * @param args Command line
109     */
110    public static void main(String args[]) {
111       new XmlDBClientSync(new Global(args));
112    }
113 }


syntax highlighted by Code2HTML, v. 0.9.1