1 19 20 package org.netbeans.modules.tasklist.bugs.bugzilla; 21 22 import java.io.*; 23 import java.net.URL ; 24 import java.util.*; 25 import java.text.MessageFormat ; 26 27 import javax.xml.parsers.SAXParserFactory ; 28 import javax.xml.parsers.SAXParser ; 29 import javax.xml.parsers.ParserConfigurationException ; 30 import org.xml.sax.SAXException ; 31 import org.xml.sax.XMLReader ; 32 import org.xml.sax.InputSource ; 33 34 import org.openide.awt.StatusDisplayer; 35 import org.openide.util.NbBundle; 36 37 63 public final class Bugzilla extends java.lang.Object { 64 67 private java.net.URL urlBase; 68 69 private SAXParser saxParser; 70 71 72 private int maxIOFailures = 15; 73 74 private Vector proxyServer = null; 75 76 private int lastProxy = -1; 77 78 79 85 public Bugzilla(java.net.URL urlBase) { 86 this.urlBase = urlBase; 87 88 try { 89 SAXParserFactory factory = SAXParserFactory.newInstance(); 90 factory.setValidating (false); 91 saxParser = factory.newSAXParser(); 92 } catch (Exception ex) { 93 ex.printStackTrace(); 94 throw new IllegalStateException ("Cannot initialize parser"); 95 } 96 } 97 98 public void setProxyPool( String proxyPool ) { 99 java.util.StringTokenizer tokens = new java.util.StringTokenizer ( proxyPool, "," ); 100 101 proxyServer = new Vector(); 102 103 while ( tokens.hasMoreTokens() ) { 104 proxyServer.add( tokens.nextToken() ); 105 } 106 rotateProxy(); 107 } 108 109 private void rotateProxy() { 110 if (proxyServer == null) return; 111 112 if (proxyServer.size() == 0) return; 113 114 if (lastProxy + 2 > proxyServer.size()) lastProxy = 0; 115 else lastProxy++; 116 117 String proxyString = (String ) proxyServer.get( lastProxy ); 118 String host = proxyString.substring(0, proxyString.indexOf(':')); 119 String port = proxyString.substring(proxyString.indexOf(':')+1); 120 121 System.out.println("Rotating http proxy server to " + host + ":" + port); 122 123 if (!port.equals("")) { 124 System.getProperties ().put ("http.proxyPort", port); 125 } 126 if (!host.equals("")) { 127 System.getProperties ().put ("http.proxyHost", host); 128 } 129 } 130 131 137 public Issue getBug (int number) throws SAXException , IOException { 138 Issue[] arr = getBugs (new int[] { number }); 139 if (arr.length != 1) { 140 throw new java.io.InvalidObjectException ("Issue not read"); 141 } 142 143 return arr[0]; 144 } 145 146 152 public Issue[] getBugs (int[] numbers) throws SAXException , IOException { 153 int maxIssuesAtOnce = 10; 154 155 Issue[] result = new Issue[numbers.length]; 156 157 GLOBAL: for (int issueToProcess = 0; issueToProcess < numbers.length; ) { 158 int lastIssueRightNow = Math.min (numbers.length, issueToProcess + maxIssuesAtOnce); 159 160 StringBuffer sb = new StringBuffer (numbers.length * 8); 161 String sep = "xml.cgi?id="; 162 IOException lastEx = null; 163 for (int i = issueToProcess; i < lastIssueRightNow; i++) { 164 sb.append (sep); 165 sb.append (numbers[i]); 166 sep = ","; 167 } 168 sb.append ("&show_attachments=false"); 169 for (int iterate = 0; iterate < maxIOFailures; iterate++) { 170 URL u = null; 171 try { 172 u = new URL (urlBase, sb.toString()); 173 InputStream is = u.openStream(); 174 175 Issue[] arr; 176 try { 177 arr = getBugs(is, urlBase); 178 } finally { 179 is.close(); 180 } 181 182 for (int i = 0; i < arr.length; ) { 184 result[issueToProcess++] = arr[i++]; 185 } 186 187 188 continue GLOBAL; 189 } 190 catch (IOException ex) { 191 synchronized ( this ) { 192 try { 193 StatusDisplayer.getDefault().setStatusText( 194 MessageFormat.format( 195 NbBundle.getMessage(Bugzilla.class, 196 "CantConnect"), new String [] { 198 new Date().toString(), 199 urlBase.getHost() 200 })); 201 rotateProxy(); 202 this.wait( 5000 ); 203 } 204 catch (InterruptedException ex1) {} 205 } 206 lastEx = ex; 207 } 208 } 209 210 throw lastEx; 211 } 213 return result; 214 } 215 216 220 public int[] query (String query) throws SAXException , IOException { 221 URL u = new URL (urlBase, "buglist.cgi?" + query); 222 IOException lastEx = null; 223 BufferedReader reader = null; 224 225 for (int iterate = 0; iterate < maxIOFailures; iterate++) { 226 try { 227 reader = new BufferedReader ( 228 new InputStreamReader (u.openStream (), "UTF-8") 229 ); 230 } 231 catch (IOException ex) { 232 synchronized ( this ) { 233 try { 234 StatusDisplayer.getDefault().setStatusText( 235 MessageFormat.format( 236 NbBundle.getMessage(Bugzilla.class, 237 "CantConnect"), new String [] { 239 new Date().toString(), 240 urlBase.getHost() 241 })); 242 rotateProxy(); 243 this.wait( 5000 ); 244 } 245 catch (InterruptedException ex1) {} 246 } 247 lastEx = ex; 248 } 249 } 250 if (reader == null) { 251 if (lastEx != null) throw lastEx; 252 else throw new IOException("Can't get connection to " + u.toString() + " for " + maxIOFailures + "times."); 253 } 254 255 ArrayList result = new ArrayList (); 256 257 String magic = "show_bug.cgi?id="; 258 for (;;) { 259 String line = reader.readLine(); 260 if (line == null) { 261 break; 262 } 263 264 int index = line.indexOf (magic); 265 if (index == -1) { 266 continue; 267 } 268 269 index += magic.length (); 270 271 int end = line.indexOf ('"', index); 272 if (end == -1) { 273 throw new IOException ("No ending \" from index " + index + " in " + line); 274 } 275 276 String number = line.substring (index, end); 277 StatusDisplayer.getDefault().setStatusText( 278 MessageFormat.format( 279 NbBundle.getMessage(Bugzilla.class, 280 "QueryBug"), new String [] { 282 number 283 })); 284 285 286 result.add (Integer.valueOf (number)); 287 } 288 289 int[] arr = new int[result.size ()]; 290 291 Iterator it = result.iterator (); 292 for (int i = 0; i < arr.length; i++) { 293 arr[i] = ((Integer )it.next ()).intValue(); 294 } 295 296 return arr; 297 } 298 299 300 301 307 private Issue[] getBugs(InputStream in, URL source) 308 throws SAXException , IOException { 309 BugzillaXMLHandler handler = new BugzillaXMLHandler(); 310 InputSource input = new InputSource (in); 311 input.setSystemId(source.toExternalForm()); 312 saxParser.parse(input, handler); 313 return getBugsFromHandler(handler); 314 } 315 316 320 private Issue[] getBugsFromHandler(BugzillaXMLHandler handler) { 321 List bugList = handler.getBugList(); 322 if (bugList == null) { 323 return null; 324 } 325 Issue[] bugs = new Issue[bugList.size()]; 326 for (int i = 0; i < bugList.size(); i++) { 327 Issue bug = new Issue(); 328 Map atts = (Map) bugList.get(i); 329 Iterator it = atts.entrySet().iterator(); 330 while (it.hasNext()) { 331 Map.Entry entry = (Map.Entry) it.next(); 332 bug.setAttribute((String ) entry.getKey(), entry.getValue()); 333 } 334 bugs[i] = bug; 335 } 336 return bugs; 337 } 338 351 352 369 370 } 371 | Popular Tags |