1 // xmlBlaster/demo/javaclients/HelloWorldErase.java
2 package javaclients;
3
4 import java.util.logging.Logger;
5 import org.xmlBlaster.util.Global;
6 import org.xmlBlaster.util.XmlBlasterException;
7 import org.xmlBlaster.util.def.Constants;
8 import org.xmlBlaster.util.qos.HistoryQos;
9 import org.xmlBlaster.client.qos.ConnectQos;
10 import org.xmlBlaster.client.qos.ConnectReturnQos;
11 import org.xmlBlaster.client.qos.DisconnectQos;
12 import org.xmlBlaster.client.key.EraseKey;
13 import org.xmlBlaster.client.qos.EraseQos;
14 import org.xmlBlaster.client.qos.EraseReturnQos;
15 import org.xmlBlaster.client.I_XmlBlasterAccess;
16
17 /**
18 * This client connects to xmlBlaster and erases a configurable amount of messages.
19 * <p>
20 * Try using 'java javaclients.HelloWorldPublish' in another window to create some messages.
21 * </p>
22 *
23 * Invoke (after starting the xmlBlaster server):
24 * <pre>
25 *Erase all topics matching the given XPATH query:
26 * java javaclients.HelloWorldErase -xpath /xmlBlaster/key/myToaster
27 *
28 *Erase topic "Sport"
29 * java javaclients.HelloWorldErase -oid Sport
30 * </pre>
31 * @see javaclients.HelloWorldPublish
32 * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/interface.html" target="others">xmlBlaster interface</a>
33 */
34 public class HelloWorldErase
35 {
36 private final String ME = "HelloWorldErase";
37 private final Global glob;
38 private static Logger log = Logger.getLogger(HelloWorldErase.class.getName());
39
40 public HelloWorldErase(Global glob) {
41 this.glob = glob;
42
43 try {
44 boolean interactive = glob.getProperty().get("interactive", true);
45 String oid = glob.getProperty().get("oid", "Hello");
46 String xpath = glob.getProperty().get("xpath", (String)null);
47 boolean forceDestroy = glob.getProperty().get("forceDestroy", false);
48 boolean persistent = glob.getProperty().get("persistent", false);
49 int historyNumErase = glob.getProperty().get("historyNumErase", -99);
50
51 log.info("Used settings are:");
52 log.info(" -interactive " + interactive);
53 log.info(" -oid " + oid);
54 log.info(" -xpath " + xpath);
55 log.info(" -forceDestroy " + forceDestroy);
56 log.info(" -persistent " + persistent);
57
58 I_XmlBlasterAccess con = glob.getXmlBlasterAccess();
59 log.info("============= CreatingConnectQos");
60 ConnectQos qos = new ConnectQos(glob);
61 log.info("ConnectQos is " + qos.toXml());
62 ConnectReturnQos crq = con.connect(qos, null); // Login to xmlBlaster, register for updates
63 log.info("Connect success as " + crq.toXml());
64
65 if (interactive) {
66 log.info("Hit a key to erase");
67 try { System.in.read(); } catch(java.io.IOException e) {}
68 }
69
70 EraseKey ek;
71 if (xpath != null)
72 ek = new EraseKey(glob, xpath, Constants.XPATH);
73 else
74 ek = new EraseKey(glob, oid);
75 EraseQos eq = new EraseQos(glob);
76 eq.setForceDestroy(forceDestroy);
77 eq.setPersistent(persistent);
78 if (historyNumErase != -99) {
79 eq.getData().setHistoryQos(new HistoryQos(glob, historyNumErase));
80 }
81 EraseReturnQos[] eraseArr = con.erase(ek, eq);
82 for (int i=0; i < eraseArr.length; i++) {
83 log.info(eraseArr[i].toXml());
84 }
85 log.info("Erased " + eraseArr.length + " topics");
86
87 log.info("Hit a key to exit");
88 try { System.in.read(); } catch(java.io.IOException e) {}
89
90 DisconnectQos dq = new DisconnectQos(glob);
91 con.disconnect(dq);
92 }
93 catch (XmlBlasterException e) {
94 log.severe(e.getMessage());
95 }
96 catch (Exception e) {
97 e.printStackTrace();
98 log.severe(e.toString());
99 }
100 }
101
102 /**
103 * Try
104 * <pre>
105 * java javaclients.HelloWorldErase -help
106 * </pre>
107 * for usage help
108 */
109 public static void main(String args[]) {
110 Global glob = new Global();
111
112 if (glob.init(args) != 0) { // Get help with -help
113 System.out.println(glob.usage());
114 System.err.println("\nExample:");
115 System.err.println(" java javaclients.HelloWorldErase -oid hello\n");
116 System.exit(1);
117 }
118
119 new HelloWorldErase(glob);
120 }
121 }
syntax highlighted by Code2HTML, v. 0.9.1