1 19 20 package org.netbeans.modules.tasklist.bugs.issuezilla; 21 22 import java.io.*; 23 import java.net.URL ; 24 import java.net.MalformedURLException ; 25 import java.net.URLConnection ; 26 import java.util.*; 27 import java.text.MessageFormat ; 28 29 import javax.xml.parsers.SAXParserFactory ; 30 import javax.xml.parsers.SAXParser ; 31 import javax.xml.parsers.ParserConfigurationException ; 32 import org.openide.awt.StatusDisplayer; 33 import org.xml.sax.SAXException ; 34 35 import org.openide.util.NbBundle; 36 37 59 public final class Issuezilla extends java.lang.Object { 60 63 private java.net.URL urlBase; 64 65 private SAXParser saxParser; 66 67 68 private int maxIOFailures = 15; 69 70 private Vector proxyServer = null; 71 72 private int lastProxy = -1; 73 74 75 81 public Issuezilla(java.net.URL urlBase) { 82 this.urlBase = urlBase; 83 84 try { 85 SAXParserFactory factory = SAXParserFactory.newInstance(); 86 factory.setValidating (false); 87 saxParser = factory.newSAXParser(); 88 } catch (Exception ex) { 89 ex.printStackTrace(); 90 throw new IllegalStateException ("Cannot initialize parser"); 91 } 92 } 93 94 public void setProxyPool( String proxyPool ) { 95 java.util.StringTokenizer tokens = new java.util.StringTokenizer ( proxyPool, "," ); 96 97 proxyServer = new Vector(); 98 99 while ( tokens.hasMoreTokens() ) { 100 proxyServer.add( tokens.nextToken() ); 101 } 102 rotateProxy(); 103 } 104 105 private void rotateProxy() { 106 if (proxyServer == null) return; 107 108 if (proxyServer.size() == 0) return; 109 110 if (lastProxy + 2 > proxyServer.size()) lastProxy = 0; 111 else lastProxy++; 112 113 String proxyString = (String ) proxyServer.get( lastProxy ); 114 String host = proxyString.substring(0, proxyString.indexOf(':')); 115 String port = proxyString.substring(proxyString.indexOf(':')+1); 116 117 System.out.println("Rotating http proxy server to " + host + ":" + port); 118 119 if (!port.equals("")) { 120 System.getProperties ().put ("http.proxyPort", port); 121 } 122 if (!host.equals("")) { 123 System.getProperties ().put ("http.proxyHost", host); 124 } 125 } 126 127 133 public Issue getBug (int number) throws SAXException , IOException { 134 Issue[] arr = getBugs (new int[] { number }); 135 if (arr.length != 1) { 136 throw new java.io.InvalidObjectException ("Issue not read"); 137 } 138 139 return arr[0]; 140 } 141 142 148 public Issue[] getBugs (int[] numbers) throws SAXException , IOException { 149 int maxIssuesAtOnce = 10; 150 151 Issue[] result = new Issue[numbers.length]; 152 153 GLOBAL: for (int issueToProcess = 0; issueToProcess < numbers.length; ) { 154 int lastIssueRightNow = Math.min (numbers.length, issueToProcess + maxIssuesAtOnce); 155 156 StringBuffer sb = new StringBuffer (numbers.length * 8); 157 String sep = "xml.cgi?id="; 158 IOException lastEx = null; 159 for (int i = issueToProcess; i < lastIssueRightNow; i++) { 160 sb.append (sep); 161 sb.append (numbers[i]); 162 sep = ","; 163 } 164 sb.append ("&show_attachments=false"); 165 166 for (int iterate = 0; iterate < maxIOFailures; iterate++) { 167 URL u = null; 168 try { 169 u = new URL (urlBase, sb.toString()); 170 171 InputStream is = u.openStream(); 172 173 Issue[] arr; 174 try { 175 arr = getBugs(is); 176 } finally { 177 is.close(); 178 } 179 180 for (int i = 0; i < arr.length; ) { 182 result[issueToProcess++] = arr[i++]; 183 } 184 185 186 continue GLOBAL; 187 } 188 catch (IOException ex) { 189 synchronized ( this ) { 190 try { 191 StatusDisplayer.getDefault().setStatusText( 192 MessageFormat.format( 193 NbBundle.getMessage(Issuezilla.class, 194 "CantConnect"), new String [] { 196 new Date().toString(), 197 urlBase.getHost() 198 })); 199 rotateProxy(); 200 this.wait( 5000 ); 201 } 202 catch (InterruptedException ex1) {} 203 } 204 lastEx = ex; 205 } 206 } 207 208 throw lastEx; 209 } 211 return result; 212 } 213 214 218 public int[] query (String query) throws SAXException , IOException { 219 URL u = new URL (urlBase, "buglist.cgi?" + query); 220 IOException lastEx = null; 221 BufferedReader reader = null; 222 223 for (int iterate = 0; iterate < maxIOFailures; iterate++) { 224 try { 225 reader = new BufferedReader ( 226 new InputStreamReader (u.openStream (), "UTF-8") 227 ); 228 } 229 catch (IOException ex) { 230 synchronized ( this ) { 231 try { 232 StatusDisplayer.getDefault().setStatusText( 233 NbBundle.getMessage( 234 Issuezilla.class, "CantConnect", new Date().toString(), 236 urlBase.getHost() 237 ) 238 ); 239 rotateProxy(); 240 this.wait( 5000 ); 241 } 242 catch (InterruptedException ex1) {} 243 } 244 lastEx = ex; 245 } 246 } 247 if (reader == null) { 248 if (lastEx != null) throw lastEx; 249 else throw new IOException("Can't get connection to " + u.toString() + " for " + maxIOFailures + "times."); 250 } 251 252 ArrayList result = new ArrayList (); 253 254 String magic = "show_bug.cgi?id="; 255 for (;;) { 256 String line = reader.readLine(); 257 if (line == null) { 258 break; 259 } 260 261 int index = line.indexOf (magic); 262 if (index == -1) { 263 continue; 264 } 265 266 index += magic.length (); 267 268 int end = line.indexOf ('"', index); 269 if (end == -1) { 270 throw new IOException ("No ending \" from index " + index + " in " + line); 271 } 272 273 String number = line.substring (index, end); 274 StatusDisplayer.getDefault().setStatusText( 275 MessageFormat.format( 276 NbBundle.getMessage(Issuezilla.class, 277 "QueryBug"), new String [] { 279 number 280 })); 281 282 283 result.add (Integer.valueOf (number)); 284 } 285 286 int[] arr = new int[result.size ()]; 287 288 Iterator it = result.iterator (); 289 for (int i = 0; i < arr.length; i++) { 290 arr[i] = ((Integer )it.next ()).intValue(); 291 } 292 293 return arr; 294 } 295 296 297 298 304 private Issue[] getBugs(InputStream in) 305 throws SAXException , IOException { 306 IssuezillaXMLHandler handler = new IssuezillaXMLHandler(); 307 saxParser.parse(in, handler); 308 return getBugsFromHandler(handler); 309 } 310 311 315 private Issue[] getBugsFromHandler(IssuezillaXMLHandler handler) { 316 List bugList = handler.getBugList(); 317 if (bugList == null) { 318 return null; 319 } 320 Issue[] bugs = new Issue[bugList.size()]; 321 for (int i = 0; i < bugList.size(); i++) { 322 Issue bug = new Issue(); 323 Map atts = (Map) bugList.get(i); 324 Iterator it = atts.entrySet().iterator(); 325 while (it.hasNext()) { 326 Map.Entry entry = (Map.Entry) it.next(); 327 bug.setAttribute((String ) entry.getKey(), entry.getValue()); 328 } 329 bugs[i] = bug; 330 } 331 return bugs; 332 } 333 346 347 364 365 369 public static String [] getComponents(URL base) { 370 List components = new ArrayList(23); 371 try { 372 URL list = new URL (base, "enter_bug.cgi"); 373 URLConnection io = list.openConnection(); 374 io.connect(); 375 InputStream in = new BufferedInputStream(io.getInputStream()); 376 int next = in.read(); 377 StringBuffer sb = new StringBuffer (); 378 while (next != -1) { 379 sb.append((char) next); 380 next = in.read(); 381 } 382 383 385 String sample = sb.toString(); 386 String MAGIC = "enter_bug.cgi?component="; 388 int entry = 0; 389 int end = -1; 390 while (true) { 391 entry = sample.indexOf(MAGIC, entry); 392 if (entry == -1) break; 393 end = sample.indexOf("\"", entry); 394 if (entry == -1) break; 395 String component = sample.substring(entry + MAGIC.length(), end); 396 entry = end; 397 components.add(component); 398 } 399 return (String []) components.toArray(new String [components.size()]); 400 401 } catch (MalformedURLException e) { 402 return new String [0]; 403 } catch (IOException e) { 404 return new String [0]; 405 } 406 } 407 408 } 409 | Popular Tags |