1 /*------------------------------------------------------------------------------
2 Name: XmlScript.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 ------------------------------------------------------------------------------*/
6 package javaclients.script;
7
8 import java.io.FileOutputStream;
9 import java.io.FileReader;
10 import java.io.IOException;
11 import java.io.InputStreamReader;
12 import java.io.OutputStream;
13 import java.io.Reader;
14
15 import java.util.logging.Logger;
16 import org.xmlBlaster.util.Global;
17 import org.xmlBlaster.util.XmlBlasterException;
18 import org.xmlBlaster.client.script.XmlScriptClient;
19 import org.xmlBlaster.client.script.I_MsgUnitCb;
20 import org.xmlBlaster.util.MsgUnit;
21
22
23 /**
24 * This demo shows how an xml file (or stream) can perform invocations on an XmlBlaster
25 * access. Everything you can do with xmlBlaster can be done invoked in a script.
26 * <p>
27 * Invocation examples:<br />
28 * <pre>
29 * java -cp ../../../lib/xmlBlaster.jar javaclients.script.XmlScript
30 *
31 * java javaclients.script.XmlScript -requestFile inFile.xml -responseFile outFile.xml -updateFile updFile.xml
32 *
33 * java javaclients.script.XmlScript -help
34 *
35 * java javaclients.script.XmlScript -prepareForPublish true -requestFile 2004-10-23_20_44_43_579.xml
36 * </pre>
37 * <p>
38 * The setting <tt>-prepareForPublish true</tt> strips away routing informations if you want to publish
39 * a dumped dead message given by -requestFile.</p>
40 * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/client.script.html">The client.script requirement</a>
41 */
42 public class XmlScript {
43 private final Global glob;
44 private static Logger log = Logger.getLogger(XmlScript.class.getName());
45 private XmlScriptClient interpreter;
46 private Reader reader;
47 private OutputStream outStream;
48 private OutputStream updStream;
49 private boolean prepareForPublish;
50
51 public final class OutputStreamWithDelay extends OutputStream {
52
53 private OutputStream out;
54 private long msgDelay;
55 private long bytesPerSecond;
56 private double kappa;
57
58 public OutputStreamWithDelay(OutputStream out, long msgDelay, long bytesPerSecond) {
59 this.out = out;
60 this.msgDelay = msgDelay;
61 this.bytesPerSecond = bytesPerSecond;
62 if (this.bytesPerSecond > 0L)
63 this.kappa = 1000.0 / this.bytesPerSecond;
64
65 }
66
67 private final void waitForRate(int len) {
68 if (bytesPerSecond < 0L)
69 return;
70 long val = (long)(kappa*len);
71 if (val > 0L) {
72 try {
73 Thread.sleep(val);
74 }
75 catch (InterruptedException ex) {
76 ex.printStackTrace();
77 }
78 }
79 }
80
81 public void write(byte[] b, int off, int len) throws IOException {
82 waitForRate(len);
83 this.out.write(b, off, len);
84 }
85
86 public void write(byte[] b) throws IOException {
87 if (this.msgDelay > 0L) {
88 try {
89 Thread.sleep(this.msgDelay);
90 }
91 catch (InterruptedException ex) {
92 ex.printStackTrace();
93 }
94 }
95 this.write(b, 0, b.length);
96 }
97
98 public void write(int b) throws IOException {
99 this.out.write(b);
100 }
101 }
102
103 public XmlScript(Global glob, String inFile, String outFile, String updFile, long msgDelay, long rateBytesPerSecond) {
104 this.glob = glob;
105 this.prepareForPublish = glob.getProperty().get("prepareForPublish", this.prepareForPublish);
106
107 try {
108 if (inFile == null) this.reader = new InputStreamReader(System.in);
109 else {
110 this.reader = new FileReader(inFile);
111 }
112 if (outFile == null) this.outStream = System.out;
113 else {
114 this.outStream = new FileOutputStream(outFile);
115 }
116 if (updFile == null) this.updStream = this.outStream;
117 else {
118 this.updStream = new FileOutputStream(updFile);
119 }
120 if (msgDelay > 0L || rateBytesPerSecond > 0L) {
121 this.updStream = new OutputStreamWithDelay(this.updStream, msgDelay, rateBytesPerSecond);
122 }
123
124 this.interpreter = new XmlScriptClient(this.glob, this.glob.getXmlBlasterAccess(), this.outStream, this.updStream, null);
125
126 if (this.prepareForPublish) {
127 this.interpreter.registerMsgUnitCb(new I_MsgUnitCb() {
128 public boolean intercept(MsgUnit msgUnit) {
129 msgUnit.getQosData().clearRoutes();
130 return true;
131 }
132 });
133 }
134 this.interpreter.parse(this.reader);
135 }
136 catch (XmlBlasterException e) {
137 log.severe("Scripting failed: " + e.getMessage());
138 }
139 catch (Throwable e) {
140 log.severe("Scripting to xmlBlaster failed: " + e.toString());
141 e.printStackTrace();
142 }
143 }
144
145 public static void main(String args[]) {
146 Global glob = new Global();
147 if (glob.init(args) != 0) {
148 System.out.println(glob.usage());
149 System.out.println("Get help: java javaclients.script.XmlScript -help\n");
150 System.out.println("Example: java javaclients.XmlScript -requestFile inFile.xml -responseFile outFile.xml -updateFile updFile.xml\n");
151 System.out.println(" if you don't specify anything as '-requestFile', then the standard input stream is taken.\n");
152 System.out.println(" if you don't specify anything as '-responseFile', then the standard output stream is taken.\n");
153 System.out.println(" if you don't specify anything as '-updateFile', then the same stream as for the output stream is used.\n");
154 System.out.println(" if you don't specify anything as '-msgDelay', it will not wait after each update, otherwise it will wait so many ms as specified.\n");
155 System.out.println(" if you don't specify anything as '-bytesPerSecond', it will not wait after each read, otherwise it will try to get the rate specified.\n");
156 System.out.println(" -prepareForPublish true If you want to publish a dumped dead message given by -requestFile.\n");
157 System.out.println(" if you don't specify anything as '-numRuns', it will execute the script one time, otherwise it will repeat execution the given times.\n");
158 System.exit(1);
159 }
160
161 String inFile = glob.getProperty().get("requestFile", (String)null);
162 String outFile = glob.getProperty().get("responseFile", (String)null);
163 String updFile = glob.getProperty().get("updateFile", (String)null);
164 int numRuns = glob.getProperty().get("numRuns", 1);
165 long msgDelay = glob.getProperty().get("msgDelay", 0L);
166 long bytesPerSecond = glob.getProperty().get("bytesPerSecond", 0L);
167
168 if (numRuns < 0) numRuns = Integer.MAX_VALUE; // forever
169 for (int i=0; i<numRuns; i++)
170 new XmlScript(glob, inFile, outFile, updFile, msgDelay, bytesPerSecond);
171 }
172 } // XmlScript
syntax highlighted by Code2HTML, v. 0.9.1