KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > util > SqlToolSprayer


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.util;
33
34 import java.io.File JavaDoc;
35 import java.io.FileInputStream JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.Date JavaDoc;
38 import java.util.Properties JavaDoc;
39
40 /* $Id: SqlToolSprayer.java,v 1.15 2005/10/23 19:25:14 fredt Exp $ */
41
42 /**
43  * Sql Tool Sprayer.
44  * Invokes SqlTool.main() multiple times with the same SQL.
45  * Invokes for multiple urlids and/or retries.
46  *
47  * See JavaDocs for the main method for syntax of how to run.
48  *
49  * System properties used if set:
50  * <UL>
51  * <LI>sqltoolsprayer.period (in ms.)</LI>
52  * <LI>sqltoolsprayer.maxtime (in ms.)</LI>
53  * <LI>sqltoolsprayer.rcfile (filepath)</LI>
54  * </UL>
55  *
56  * @see @main()
57  * @version $Revision: 1.15 $
58  * @author Blaine Simpson unsaved@users
59  */

60 public class SqlToolSprayer {
61
62     private static final String JavaDoc SYNTAX_MSG =
63         "SYNTAX: java [-D...] SqlToolSprayer 'SQL;' [urlid1 urlid2...]\n"
64         + "System properties you may use [default values]:\n"
65         + " sqltoolsprayer.period (in ms.) [500]\n"
66         + " sqltoolsprayer.maxtime (in ms.) [0]\n"
67         + " sqltoolsprayer.monfile (filepath) [none]\n"
68         + " sqltoolsprayer.rcfile (filepath) [none. SqlTool default used.]\n"
69         + " sqltoolsprayer.propfile (filepath) [none]";
70
71     public static void main(String JavaDoc[] sa) {
72
73         if (sa.length < 1) {
74             System.err.println(SYNTAX_MSG);
75             System.exit(4);
76         }
77
78         System.setProperty("sqltool.noexit", "true");
79
80         long period = ((System.getProperty("sqltoolsprayer.period") == null)
81                        ? 500
82                        : Integer.parseInt(
83                            System.getProperty("sqltoolsprayer.period")));
84         long maxtime =
85             ((System.getProperty("sqltoolsprayer.maxtime") == null) ? 0
86                                                                     : Integer.parseInt(
87                                                                         System.getProperty(
88                                                                             "sqltoolsprayer.maxtime")));
89         String JavaDoc rcFile = System.getProperty("sqltoolsprayer.rcfile");
90         String JavaDoc propfile = System.getProperty("sqltoolsprayer.propfile");
91         File JavaDoc monitorFile =
92             (System.getProperty("sqltoolsprayer.monfile") == null) ? null
93                                                                    : new File JavaDoc(
94                                                                        System.getProperty(
95                                                                            "sqltoolsprayer.monfile"));
96         ArrayList JavaDoc urlids = new ArrayList JavaDoc();
97
98         if (propfile != null) {
99             try {
100                 getUrlsFromPropFile(propfile, urlids);
101             } catch (Exception JavaDoc e) {
102                 System.err.println("Failed to load property file '"
103                                    + propfile + "': " + e);
104                 System.exit(3);
105             }
106         }
107
108         for (int i = 1; i < sa.length; i++) {
109             urlids.add(sa[i]);
110         }
111
112         boolean[] status = new boolean[urlids.size()];
113
114         for (int i = 0; i < status.length; i++) {
115             status[i] = false;
116         }
117
118         String JavaDoc[] withRcArgs = {
119             "--sql", sa[0], "--rcfile", rcFile, null
120         };
121         String JavaDoc[] withoutRcArgs = {
122             "--sql", sa[0], null
123         };
124         String JavaDoc[] sqlToolArgs = (rcFile == null) ? withoutRcArgs
125                                                   : withRcArgs;
126         boolean onefailed = false;
127         long startTime = (new Date JavaDoc()).getTime();
128
129         while (true) {
130             if (monitorFile != null &&!monitorFile.exists()) {
131                 System.err.println("Required file is gone: " + monitorFile);
132                 System.exit(2);
133             }
134
135             onefailed = false;
136
137             for (int i = 0; i < status.length; i++) {
138                 if (status[i]) {
139                     continue;
140                 }
141
142                 sqlToolArgs[sqlToolArgs.length - 1] = (String JavaDoc) urlids.get(i);
143
144                 try {
145                     SqlTool.main(sqlToolArgs);
146
147                     status[i] = true;
148
149                     System.err.println("Success for instance '"
150                                        + urlids.get(i) + "'");
151                 } catch (Exception JavaDoc e) {
152                     onefailed = true;
153                 }
154             }
155
156             if (!onefailed) {
157                 break;
158             }
159
160             if (maxtime == 0
161                     || (new Date JavaDoc()).getTime() > startTime + maxtime) {
162                 break;
163             }
164
165             try {
166                 Thread.sleep(period);
167             } catch (InterruptedException JavaDoc ie) {}
168         }
169
170         ArrayList JavaDoc failedUrlids = new ArrayList JavaDoc();
171
172         // If all statuses true, then System.exit(0);
173
for (int i = 0; i < status.length; i++) {
174             if (status[i] != true) {
175                 failedUrlids.add((String JavaDoc) urlids.get(i));
176             }
177         }
178
179         if (failedUrlids.size() > 0) {
180             System.err.println("Failed instances: " + failedUrlids);
181             System.exit(1);
182         }
183
184         System.exit(0);
185     }
186
187     private static void getUrlsFromPropFile(String JavaDoc fileName,
188             ArrayList JavaDoc al) throws Exception JavaDoc {
189
190         Properties JavaDoc p = new Properties JavaDoc();
191
192         p.load(new FileInputStream JavaDoc(fileName));
193
194         int i = -1;
195         String JavaDoc val;
196
197         while (true) {
198             i++;
199
200             val = p.getProperty("server.urlid." + i);
201
202             if (val == null) {
203                 return;
204             }
205
206             al.add(val);
207         }
208     }
209 }
210
Popular Tags