KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > bugs > scarab > Scarab


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.bugs.scarab;
21
22 import java.io.*;
23 import java.net.URL JavaDoc;
24 import java.util.*;
25 import java.text.MessageFormat JavaDoc;
26
27 import javax.xml.parsers.SAXParserFactory JavaDoc;
28 import javax.xml.parsers.SAXParser JavaDoc;
29 import javax.xml.parsers.ParserConfigurationException JavaDoc;
30 import org.xml.sax.SAXException JavaDoc;
31 import org.xml.sax.XMLReader JavaDoc;
32 import org.xml.sax.InputSource JavaDoc;
33
34 import org.openide.awt.StatusDisplayer;
35 import org.openide.util.NbBundle;
36
37 /**
38  * A connection to Scarab. Connects to the database and provides
39  * descriptions of bugs. Is not thread safe, each thread should use
40  * its own instance of Scarab.
41  *
42  * tor@netbeans.org:
43  * This class is virtually identical to
44  * nbbuild/antsrc/org/netbeans/nbbuild/Issuezilla.java
45  * At first, I inclouded its class file directly as part of
46  * the build. However, treating Issuezilla as a black box
47  * didn't work well because when connections fail (and are
48  * retried), or even during a query, there is no feedback - and
49  * since issuezilla is so slow, it's hard to know in the GUI
50  * that things are working. Therefore, I've modified the java
51  * file to give us a little bit more feedback.
52  * In CVS I stored the original file as the first revision,
53  * so you can easily diff to see what has changed - and generate
54  * a patch which you can then apply to an updated version
55  * of nbbuild/antsrc/ to keep the two in sync.
56  *
57  * serff@netbeans.org:
58  * This class is almost exactally the same as Issuezilla, but modified to
59  * work with bugzilla.
60  *
61  * @author Ivan Bradac, refactored by Jaroslav Tulach, modified by serff
62  */

63 public final class Scarab{
64     /** url base of issuezilla. For netbeans it is
65      * "http://openide.netbeans.org/issues/"
66      */

67     private java.net.URL JavaDoc urlBase;
68     /** sax parser to use */
69     private SAXParser JavaDoc saxParser;
70
71     /** maximum IO failures during connection to IZ */
72     private int maxIOFailures = 15;
73     
74     private Vector proxyServer = null;
75     
76     private int lastProxy = -1;
77    
78    
79     /** Creates new connection to issuezilla. The urlBase should
80      * point to URL where issuezilla produces its XML results.
81      * In case of NetBeans the URL is
82      * <B>"http://openide.netbeans.org/issues/xml.cgi"</B>
83      * @param urlBase a URI to issuezilla's XML service
84      */

85     public Scarab(final java.net.URL JavaDoc urlBase) {
86         this.urlBase = urlBase;
87         
88         try {
89             final SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
90             factory.setValidating (false);
91             saxParser = factory.newSAXParser();
92         } catch (Exception JavaDoc ex) {
93             ex.printStackTrace();
94             throw new IllegalStateException JavaDoc ("Cannot initialize parser"); //NOI18N
95
}
96     }
97     
98     public void setProxyPool( final String JavaDoc proxyPool ) {
99         java.util.StringTokenizer JavaDoc tokens = new java.util.StringTokenizer JavaDoc( proxyPool, "," ); //NOI18N
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         final String JavaDoc proxyString = (String JavaDoc) proxyServer.get( lastProxy );
118         final String JavaDoc host = proxyString.substring(0, proxyString.indexOf(':'));
119         final String JavaDoc port = proxyString.substring(proxyString.indexOf(':')+1);
120   
121         System.out.println("Rotating http proxy server to " + host + ":" + port); //NOI18N
122

123         if (!port.equals("")) {
124             System.getProperties ().put ("http.proxyPort", port); //NOI18N
125
}
126         if (!host.equals("")) {
127             System.getProperties ().put ("http.proxyHost", host); //NOI18N
128
}
129     }
130     
131     /** Executes a query and returns array of issue numbers that fullfils the query.
132      * @param query the query string that should be appended to the URL after question mark part
133      * @return array of integers
134      */

135     public List query (final String JavaDoc query) throws SAXException JavaDoc, IOException {
136     
137         final String JavaDoc search = "curmodule/0/tqk/0/template/admin%2CViewXMLExportIssues.vm?"; //NOI18N
138
final URL JavaDoc u = new URL JavaDoc (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"); //NOI18N
146
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"), // NOI18N
155
new String JavaDoc[] {
156                                        new Date().toString(),
157                                        urlBase.getHost()
158                                    }));
159                         rotateProxy();
160                         this.wait( 5000 );
161                     }
162                     catch (InterruptedException JavaDoc 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."); //NOI18N
172
}
173         
174         final List list = getBugs(reader,u);
175         reader.close();
176         return list;
177     }
178         
179         
180     
181     /**
182      * Gets the Issuezilla bugs from the InputStream.
183      *
184      * @return Issue[] objects from the InputStream containing
185      * their XML representation.
186      */

187     private List getBugs(final Reader JavaDoc in, final URL JavaDoc source)
188             throws SAXException JavaDoc, IOException {
189         
190         final ScarabXMLHandler handler = new ScarabXMLHandler();
191         final InputSource JavaDoc input = new InputSource JavaDoc(in);
192         input.setSystemId(source.toExternalForm());
193         saxParser.parse(input, handler);
194         return handler.getIssueList();
195     }
196
197 }
198
Popular Tags