1 30 31 32 package org.hsqldb.util; 33 34 import java.io.File ; 35 import java.io.FileInputStream ; 36 import java.util.ArrayList ; 37 import java.util.Date ; 38 import java.util.Properties ; 39 40 41 42 60 public class SqlToolSprayer { 61 62 private static final String 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 [] 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 rcFile = System.getProperty("sqltoolsprayer.rcfile"); 90 String propfile = System.getProperty("sqltoolsprayer.propfile"); 91 File monitorFile = 92 (System.getProperty("sqltoolsprayer.monfile") == null) ? null 93 : new File ( 94 System.getProperty( 95 "sqltoolsprayer.monfile")); 96 ArrayList urlids = new ArrayList (); 97 98 if (propfile != null) { 99 try { 100 getUrlsFromPropFile(propfile, urlids); 101 } catch (Exception 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 [] withRcArgs = { 119 "--sql", sa[0], "--rcfile", rcFile, null 120 }; 121 String [] withoutRcArgs = { 122 "--sql", sa[0], null 123 }; 124 String [] sqlToolArgs = (rcFile == null) ? withoutRcArgs 125 : withRcArgs; 126 boolean onefailed = false; 127 long startTime = (new Date ()).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 ) 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 e) { 152 onefailed = true; 153 } 154 } 155 156 if (!onefailed) { 157 break; 158 } 159 160 if (maxtime == 0 161 || (new Date ()).getTime() > startTime + maxtime) { 162 break; 163 } 164 165 try { 166 Thread.sleep(period); 167 } catch (InterruptedException ie) {} 168 } 169 170 ArrayList failedUrlids = new ArrayList (); 171 172 for (int i = 0; i < status.length; i++) { 174 if (status[i] != true) { 175 failedUrlids.add((String ) 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 fileName, 188 ArrayList al) throws Exception { 189 190 Properties p = new Properties (); 191 192 p.load(new FileInputStream (fileName)); 193 194 int i = -1; 195 String 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 |