1 /*------------------------------------------------------------------------------
 2 Name:      xmlBlaster/demo/javaclients/email/DelegateDemo.java
 3 Project:   xmlBlaster.org
 4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
 5 ------------------------------------------------------------------------------*/
 6 package javaclients.email;
 7 
 8 import org.xmlBlaster.util.Global;
 9 import org.xmlBlaster.client.qos.ConnectQos;
10 import org.xmlBlaster.client.I_XmlBlasterAccess;
11 import org.xmlBlaster.util.MsgUnit;
12 import org.xmlBlaster.util.qos.address.CallbackAddress;
13 
14 /**
15  * This client connects to xmlBlaster (for example with IOR or XMLRPC or EMAIL)
16  * and subscribes for somebody else to a message to be delivered by email.
17  * <p />
18  * We then publish a message and the configured email address
19  * receives the message. The message is send oneway, so that the receiver does
20  * not need to ACK the mail.
21  * <p />
22  * Invoke:
23  * <pre>
24  *  java javaclients.email.DelegateDemo -dispatch/connection/protocol XMLRPC -emailDestination blue@localhost
25  * </pre>
26  * 
27  * @see <a
28  *      href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/protocol.email.html">The
29  *      protocol.email requirement</a>
30  * @author <a href="mailto:xmlBlaster@marcelruff.info">Marcel Ruff</a>
31  */
32 public class DelegateDemo {
33    private Global glob;
34 
35    public DelegateDemo(Global glob) {
36       this.glob = glob;
37       try {
38          I_XmlBlasterAccess con = this.glob.getXmlBlasterAccess();
39 
40          ConnectQos qos = new ConnectQos(this.glob);
41 
42          String receiver = this.glob.getProperty().get("emailDestination",
43                (String) null);
44          if (receiver == null) {
45             System.out.println("Usage:");
46             System.out
47                   .println("  java javaclients.email.DelegateDemo -protocol email -emailDestination blue@localhost");
48             System.exit(1);
49          }
50 
51          CallbackAddress cbAddr = new CallbackAddress(this.glob, "email");
52          cbAddr.setRawAddress(receiver);
53          cbAddr.setOneway(true);
54          qos.addCallbackAddress(cbAddr);
55 
56          // null: Login to xmlBlaster without callback instantiation!
57          con.connect(qos, null);
58 
59          con.subscribe("<key oid='EmailDemo'/>", "<qos/>");
60          System.out
61                .println("Subscribed topic 'EmailDemo' for to email destination '"
62                      + receiver + "'");
63 
64          while (true) {
65             int ch = Global
66                   .waitOnKeyboardHit("["
67                         + receiver
68                         + "] Hit a key 'p' to publish, 'u' to unSubscribe or 'q' to quit >");
69             if (ch == 'q')
70                break;
71             if (ch == 'p') {
72                con.publish(new MsgUnit("<key oid='EmailDemo'/>", "Hi"
73                      .getBytes(), "<qos/>"));
74             }
75             if (ch == 'u') {
76                con.unSubscribe("<key oid='EmailDemo'/>", "<qos/>");
77                break;
78             }
79          }
80 
81          con.disconnect(null);
82       } catch (Exception e) {
83          System.out.println(e.toString());
84       }
85    }
86 
87    public static void main(String args[]) {
88       Global glob = new Global(); // initializes args, properties etc.
89       if (glob.init(args) < 0) {
90          System.out.println("EmailDemo wrong args, Bye");
91          System.exit(1);
92       }
93 
94       new DelegateDemo(glob);
95    }
96 }


syntax highlighted by Code2HTML, v. 0.9.1