1 /*------------------------------------------------------------------------------
2 Name: XmlDbMessageWrapper.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 ------------------------------------------------------------------------------*/
6 package org.xmlBlaster.client;
7
8 import org.xmlBlaster.util.Global;
9 import org.xmlBlaster.util.SessionName;
10 import org.xmlBlaster.util.XmlBlasterException;
11 import org.xmlBlaster.util.MsgUnit;
12 import org.xmlBlaster.util.qos.address.Destination;
13 import org.xmlBlaster.client.key.PublishKey;
14 import org.xmlBlaster.client.qos.PublishQos;
15
16
17 /**
18 * Wrapping a SQL request with XML, to be used in the 'content' of a message.
19 * <p />
20 * This helps you to send a SQL request to the xmlBlaster JDBC service from James.
21 * @see org.xmlBlaster.protocol.jdbc.ConnectionDescriptor
22 */
23 public class XmlDbMessageWrapper
24 {
25 private final Global glob;
26 private static String ME = "XmlDbMessageWrapper";
27 private String content;
28 private String user = null;
29 private String passwd = null;
30 private String url = null;
31
32
33 /**
34 * Constructor creates XML request.
35 * <p />
36 * Initialize current query with init() and access it with the toXml() or the toMessage() method.
37 * @param user The login name to database, "postgres"
38 * @param passwd The DB password
39 * @param url Any valid JDBC url, e.g. "jdbc:postgresql://24.3.47.214/postgres");
40 */
41 public XmlDbMessageWrapper(Global glob, String user, String passwd, String url)
42 {
43 this.glob = glob;
44 this.user = user;
45 this.passwd = passwd;
46 this.url = url;
47 }
48
49 /**
50 * Set the query properties.
51 * <p />
52 * @param limit Maximum results to deliver, 50, used to limit the number of result rows
53 * @param confirm true/false, when set to true, you get an answer
54 * @param queryStr Any valid SQL syntax, "select * from intrauser"
55 */
56 public void initQuery(int limit, boolean confirm, String queryStr)
57 {
58 init("query", limit, confirm, queryStr);
59 }
60
61 /**
62 * Set the update/insert/delete properties.
63 * <p />
64 * @param confirm true/false, when set to true, you get an answer
65 * @param updateStr Any valid SQL syntax, e.g. "INSERT INTO person VALUES(name='Peter')"
66 */
67 public void initUpdate(boolean confirm, String updateStr)
68 {
69 init("update", 1, confirm, updateStr);
70 }
71
72 /**
73 * Set the query properties.
74 * <p />
75 * Access the message to send with the toMessage() method.
76 * You can reuse this object for different init() calls.
77 *
78 * @param type "query" or "update", "insert", "delete"
79 * @param limit Maximum results to deliver, 50, used to limit the number of result rows
80 * @param confirm true/false, when set to true, you get an answer
81 * @param queryStr Any valid SQL syntax, "select * from intrauser"
82 */
83 public void init(String type, int limit, boolean confirm, String queryStr)
84 {
85 if (type.equalsIgnoreCase("insert") || type.equalsIgnoreCase("delete"))
86 type = "update";
87
88 StringBuffer tmp = new StringBuffer();
89 tmp.append("<database:adapter xmlns:database='http://www.xmlBlaster.org/jdbc'>");
90 tmp.append(" <database:url>").append(url).append("</database:url>");
91 tmp.append(" <database:username>").append(user).append("</database:username>");
92 tmp.append(" <database:password>").append(passwd).append("</database:password>");
93 tmp.append(" <database:interaction type='").append(type).append("'/>");
94 tmp.append(" <database:command><![CDATA[").append(queryStr).append("]]></database:command>");
95 tmp.append(" <database:connectionlifespan ttl='1'/>");
96 tmp.append(" <database:rowlimit max='").append(limit).append("'/>");
97 tmp.append(" <database:confirmation confirm='").append(confirm).append("'/>");
98 tmp.append("</database:adapter>");
99 content = tmp.toString();
100 }
101
102 /**
103 * Returns the 'message content' which is the SQL request coded in XML.
104 */
105 public String toXml() throws XmlBlasterException
106 {
107 if (content == null) throw new XmlBlasterException(ME, "Please use init() method before calling toXml().");
108 return content;
109 }
110
111 /**
112 * Creates the complete message for you, which you can publish to xmlBlaster.
113 * <p />
114 * You will receive the result set wrapped in XML with a asynchronous update().
115 */
116 public MsgUnit toMessage() throws XmlBlasterException
117 {
118 PublishQos qos = new PublishQos(glob, new Destination(new SessionName(Global.instance(), "__sys__jdbc")));
119 PublishKey key = new PublishKey(glob, "", "text/xml", "SQL_QUERY");
120 return new MsgUnit(key, toXml().getBytes(), qos);
121 }
122 }
syntax highlighted by Code2HTML, v. 0.9.1