|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.nbbuild; import java.io.*; import java.net.URL; import java.util.*; import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParser; import javax.xml.parsers.ParserConfigurationException; import org.xml.sax.SAXException; /** * A connection to Issuezilla. Connects to the database and provides * descriptions of issues. Is not thread safe, each thread should use * its own instance of Issuezilla. * * * @author Ivan Bradac, refactored by Jaroslav Tulach */ public final class Issuezilla extends java.lang.Object { /** url base of issuezilla. For netbeans it is * "http://openide.netbeans.org/issues/" */ private java.net.URL urlBase; /** sax parser to use */ private SAXParser saxParser; /** maximum IO failures during connection to IZ */ private int maxIOFailures = 15; private Vector proxyServer = null; private int lastProxy = -1; /** Creates new connection to issuezilla. The urlBase should * point to URL where issuezilla produces its XML results. * In case of NetBeans the URL is * "http://openide.netbeans.org/issues/xml.cgi" * @param urlBase a URI to issuezilla's XML service */ public Issuezilla(java.net.URL urlBase) { this.urlBase = urlBase; try { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating (false); saxParser = factory.newSAXParser(); } catch (Exception ex) { ex.printStackTrace(); throw new IllegalStateException ("Cannot initialize parser"); } } public void setProxyPool( String proxyPool ) { java.util.StringTokenizer tokens = new java.util.StringTokenizer( proxyPool, "," ); proxyServer = new Vector(); while ( tokens.hasMoreTokens() ) { proxyServer.add( tokens.nextToken() ); } rotateProxy(); } private void rotateProxy() { if (proxyServer == null) return; if (proxyServer.size() == 0) return; if (lastProxy + 2 > proxyServer.size()) lastProxy = 0; else lastProxy++; String proxyString = (String) proxyServer.get( lastProxy ); String host = proxyString.substring(0, proxyString.indexOf(':')); String port = proxyString.substring(proxyString.indexOf(':')+1); System.out.println("Rotating http proxy server to " + host + ":" + port); if (!port.equals("")) { System.getProperties ().put ("http.proxyPort", port); } if (!host.equals("")) { System.getProperties ().put ("http.proxyHost", host); } } /** Getter of an issue for given number. * @param number number of the issue * @return the issue * @exception IOException if connection fails * @exception SAXException if parsing fails */ public Issue getBug (int number) throws SAXException, IOException { Issue[] arr = getBugs (new int[] { number }); if (arr.length != 1) { throw new java.io.InvalidObjectException ("Issue not read"); } return arr[0]; } /** Getter of more issues at once. * @param numbers array of integers with numbers of bugs to retrieve * @return the issue array * @exception IOException if connection fails * @exception SAXException if parsing fails */ public Issue[] getBugs (int[] numbers) throws SAXException, IOException { int maxIssuesAtOnce = 10; Issue[] result = new Issue[numbers.length]; GLOBAL: for (int issueToProcess = 0; issueToProcess < numbers.length; ) { int lastIssueRightNow = Math.min (numbers.length, issueToProcess + maxIssuesAtOnce); StringBuffer sb = new StringBuffer (numbers.length * 8); String sep = "xml.cgi?id="; IOException lastEx = null; for (int i = issueToProcess; i < lastIssueRightNow; i++) { sb.append (sep); sb.append (numbers[i]); sep = ","; } sb.append ("&show_attachments=false"); for (int iterate = 0; iterate < maxIOFailures; iterate++) { URL u = null; try { u = new URL(urlBase, sb.toString()); InputStream is = u.openStream(); Issue[] arr; try { arr = getBugs(is); } finally { is.close(); } // copy the results and go on for (int i = 0; i < arr.length; ) { result[issueToProcess++] = arr[i++]; } continue GLOBAL; } catch (IOException ex) { synchronized ( this ) { try { System.out.println( new Date().toString() + ": Can't get URL : " + u.toString() ); hintProxySettings(); rotateProxy(); this.wait( 5000 ); } catch (InterruptedException ex1) {} } lastEx = ex; } } throw lastEx; } // end of GLOBAL return result; } private static void hintProxySettings() { String host = System.getProperty("http.proxyHost", " |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.