1 19 20 package org.netbeans.modules.tasklist.bugs.scarab; 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 Scarab{ 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 Scarab(final java.net.URL urlBase) { 86 this.urlBase = urlBase; 87 88 try { 89 final 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"); } 96 } 97 98 public void setProxyPool( final String proxyPool ) { 99 java.util.StringTokenizer tokens = new java.util.StringTokenizer ( proxyPool, "," ); 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 final String proxyString = (String ) proxyServer.get( lastProxy ); 118 final String host = proxyString.substring(0, proxyString.indexOf(':')); 119 final String port = proxyString.substring(proxyString.indexOf(':')+1); 120 121 System.out.println("Rotating http proxy server to " + host + ":" + port); 123 if (!port.equals("")) { 124 System.getProperties ().put ("http.proxyPort", port); } 126 if (!host.equals("")) { 127 System.getProperties ().put ("http.proxyHost", host); } 129 } 130 131 135 public List query (final String query) throws SAXException , IOException { 136 137 final String search = "curmodule/0/tqk/0/template/admin%2CViewXMLExportIssues.vm?"; final URL u = new URL (urlBase,search+query); 139 IOException lastEx = null; 140 InputStreamReader isr = null; 141 BufferedReader reader = null; 142 143 for (int iterate = 0; iterate < maxIOFailures; iterate++) { 144 try { 145 isr = new InputStreamReader (u.openStream (), "UTF-8"); reader = new BufferedReader (isr); 147 if( reader != null ){ break; } 148 }catch (IOException ex) { 149 synchronized ( this ) { 150 try { 151 StatusDisplayer.getDefault().setStatusText( 152 MessageFormat.format( 153 NbBundle.getMessage(Scarab.class, 154 "CantConnect"), new String [] { 156 new Date().toString(), 157 urlBase.getHost() 158 })); 159 rotateProxy(); 160 this.wait( 5000 ); 161 } 162 catch (InterruptedException ex1) {} 163 } 164 lastEx = ex; 165 if( reader != null ){ reader.close(); } 166 if( isr != null ){ isr.close(); } 167 } 168 } 169 if (reader == null) { 170 if (lastEx != null) throw lastEx; 171 else throw new IOException("Can't get connection to " + u.toString() + " for " + maxIOFailures + "times."); } 173 174 final List list = getBugs(reader,u); 175 reader.close(); 176 return list; 177 } 178 179 180 181 187 private List getBugs(final Reader in, final URL source) 188 throws SAXException , IOException { 189 190 final ScarabXMLHandler handler = new ScarabXMLHandler(); 191 final InputSource input = new InputSource (in); 192 input.setSystemId(source.toExternalForm()); 193 saxParser.parse(input, handler); 194 return handler.getIssueList(); 195 } 196 197 } 198 | Popular Tags |