KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > core > Util


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.web.core;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.net.Socket JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.net.InetAddress JavaDoc;
28 import java.net.UnknownHostException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Set JavaDoc;
36 import org.netbeans.api.java.project.JavaProjectConstants;
37 import org.netbeans.api.java.queries.UnitTestForSourceQuery;
38 import org.netbeans.api.project.Project;
39 import org.netbeans.api.project.ProjectUtils;
40 import org.netbeans.api.project.SourceGroup;
41 import org.openide.ErrorManager;
42 import org.openide.filesystems.FileObject;
43 import org.openide.filesystems.URLMapper;
44 //import java.io.*;
45
import org.xml.sax.*;
46 //import java.util.*;
47

48 /** Utility class
49 * @author Petr Jiricka
50 * @version 1.00, Jun 03, 1999
51 */

52 public class Util {
53
54     /** Waits for startup of a server, waits until the connection has
55      * been established. */

56
57     public static boolean waitForURLConnection(URL JavaDoc url, int timeout, int retryTime) {
58         Connect connect = new Connect(url, retryTime);
59         Thread JavaDoc t = new Thread JavaDoc(connect);
60         t.start();
61         try {
62             t.join(timeout);
63         } catch(InterruptedException JavaDoc ie) {
64         }
65         if (t.isAlive()) {
66             connect.finishLoop();
67             t.interrupt();//for thread deadlock
68
}
69         return connect.getStatus();
70     }
71
72     public static String JavaDoc issueGetRequest(URL JavaDoc url) {
73         BufferedReader JavaDoc in = null;
74         StringBuffer JavaDoc input = new StringBuffer JavaDoc();
75         try {
76             in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(
77                                         url.openStream()));
78             String JavaDoc inputLine;
79             while ((inputLine = in.readLine()) != null) {
80                 input.append(inputLine);
81                 input.append("\n"); // NOI18N
82
}
83             return input.toString();
84         }
85         catch (Exception JavaDoc e) {
86         //e.printStackTrace();
87
return null;
88         }
89         finally {
90             if (in != null)
91                 try {
92                     in.close();
93                 }
94                 catch(IOException JavaDoc e) {
95                     //e.printStackTrace();
96
}
97         }
98     }
99
100     private static class Connect implements Runnable JavaDoc {
101
102         URL JavaDoc url = null;
103         int retryTime;
104         boolean status = false;
105         boolean loop = true;
106
107         public Connect(URL JavaDoc url, int retryTime) {
108             this.url = url;
109             this.retryTime = retryTime;
110         }
111
112         public void finishLoop() {
113             loop = false;
114         }
115
116         public void run() {
117             try {
118                 InetAddress.getByName(url.getHost());
119             } catch (UnknownHostException JavaDoc e) {
120                 return;
121             }
122             while (loop) {
123                 try {
124                     Socket JavaDoc socket = new Socket JavaDoc(url.getHost(), url.getPort());
125                     socket.close();
126                     status = true;
127                     break;
128                 } catch (UnknownHostException JavaDoc e) {//nothing to do
129
} catch (IOException JavaDoc e) {//nothing to do
130
}
131                 try {
132                     Thread.currentThread().sleep(retryTime);
133                 } catch(InterruptedException JavaDoc ie) {
134                 }
135             }
136         }
137
138         boolean getStatus() {
139             return status;
140         }
141     }
142
143     // following block is copy/pasted code from
144
// org.netbeans.modules.j2ee.common.Util
145

146     /**
147      * Returns Java source groups for all source packages in given project.<br>
148      * Doesn't include test packages.
149      *
150      * @param project Project to search
151      * @return Array of SourceGroup. It is empty if any probelm occurs.
152      */

153     public static SourceGroup[] getJavaSourceGroups(Project project) {
154         SourceGroup[] sourceGroups = ProjectUtils.getSources(project).getSourceGroups(
155                                     JavaProjectConstants.SOURCES_TYPE_JAVA);
156         Set JavaDoc testGroups = getTestSourceGroups(project, sourceGroups);
157         List JavaDoc result = new ArrayList JavaDoc();
158         for (int i = 0; i < sourceGroups.length; i++) {
159             if (!testGroups.contains(sourceGroups[i])) {
160                 result.add(sourceGroups[i]);
161             }
162         }
163         return (SourceGroup[]) result.toArray(new SourceGroup[result.size()]);
164     }
165
166     private static Set JavaDoc/*<SourceGroup>*/ getTestSourceGroups(Project project, SourceGroup[] sourceGroups) {
167         Map JavaDoc foldersToSourceGroupsMap = createFoldersToSourceGroupsMap(sourceGroups);
168         Set JavaDoc testGroups = new HashSet JavaDoc();
169         for (int i = 0; i < sourceGroups.length; i++) {
170             testGroups.addAll(getTestTargets(sourceGroups[i], foldersToSourceGroupsMap));
171         }
172         return testGroups;
173     }
174     
175     private static Map JavaDoc createFoldersToSourceGroupsMap(final SourceGroup[] sourceGroups) {
176         Map JavaDoc result;
177         if (sourceGroups.length == 0) {
178             result = Collections.EMPTY_MAP;
179         } else {
180             result = new HashMap JavaDoc(2 * sourceGroups.length, .5f);
181             for (int i = 0; i < sourceGroups.length; i++) {
182                 SourceGroup sourceGroup = sourceGroups[i];
183                 result.put(sourceGroup.getRootFolder(), sourceGroup);
184             }
185         }
186         return result;
187     }
188
189     private static List JavaDoc/*<FileObject>*/ getFileObjects(URL JavaDoc[] urls) {
190         List JavaDoc result = new ArrayList JavaDoc();
191         for (int i = 0; i < urls.length; i++) {
192             FileObject sourceRoot = URLMapper.findFileObject(urls[i]);
193             if (sourceRoot != null) {
194                 result.add(sourceRoot);
195             } else {
196                 int severity = ErrorManager.INFORMATIONAL;
197                 if (ErrorManager.getDefault().isNotifiable(severity)) {
198                     ErrorManager.getDefault().notify(severity, new IllegalStateException JavaDoc(
199                        "No FileObject found for the following URL: " + urls[i])); //NOI18N
200
}
201             }
202         }
203         return result;
204     }
205     
206     private static List JavaDoc/*<SourceGroup>*/ getTestTargets(SourceGroup sourceGroup, Map JavaDoc foldersToSourceGroupsMap) {
207         final URL JavaDoc[] rootURLs = UnitTestForSourceQuery.findUnitTests(sourceGroup.getRootFolder());
208         if (rootURLs.length == 0) {
209             return new ArrayList JavaDoc();
210         }
211         List JavaDoc result = new ArrayList JavaDoc();
212         List JavaDoc sourceRoots = getFileObjects(rootURLs);
213         for (int i = 0; i < sourceRoots.size(); i++) {
214             FileObject sourceRoot = (FileObject) sourceRoots.get(i);
215             SourceGroup srcGroup = (SourceGroup) foldersToSourceGroupsMap.get(sourceRoot);
216             if (srcGroup != null) {
217                 result.add(srcGroup);
218             }
219         }
220         return result;
221     }
222     
223     /** Parsing to get Set of Strings that correpond to tagName valeus inside elName, e.g.:
224      * to get all <servlet-name> values inside the <servlet> elements (in web.xml)
225     */

226     public static Set JavaDoc getTagValues (java.io.InputStream JavaDoc is, String JavaDoc elName, String JavaDoc tagName) throws java.io.IOException JavaDoc, SAXException {
227         return getTagValues(is,new String JavaDoc[]{elName},tagName);
228     }
229     /** Parsing to get Set of Strings that correpond to tagName valeus inside elNames, e.g.:
230      * to get all <name> values inside the <tag> and <tag-file> elements (in TLD)
231     */

232     public static Set JavaDoc getTagValues (java.io.InputStream JavaDoc is, String JavaDoc[] elNames, String JavaDoc tagName) throws java.io.IOException JavaDoc, SAXException {
233         javax.xml.parsers.SAXParserFactory JavaDoc fact = javax.xml.parsers.SAXParserFactory.newInstance();
234         fact.setValidating(false);
235         try {
236             javax.xml.parsers.SAXParser JavaDoc parser = fact.newSAXParser();
237             XMLReader reader = parser.getXMLReader();
238             TLDVersionHandler handler = new TLDVersionHandler(elNames,tagName);
239             reader.setContentHandler(handler);
240             try {
241                 reader.parse(new InputSource(is));
242             } catch (SAXException ex) {
243                 String JavaDoc message = ex.getMessage();
244             }
245             return handler.getValues();
246         } catch(javax.xml.parsers.ParserConfigurationException JavaDoc ex) {
247             return new java.util.HashSet JavaDoc();
248         }
249     }
250     
251     private static class TLDVersionHandler extends org.xml.sax.helpers.DefaultHandler JavaDoc {
252         private String JavaDoc tagName;
253         private Set JavaDoc elNames;
254         private Set JavaDoc values;
255         private boolean insideEl, insideTag;
256
257         TLDVersionHandler(String JavaDoc[] elNames, String JavaDoc tagName) {
258             this.elNames=new java.util.HashSet JavaDoc();
259             for (int i=0;i<elNames.length;i++) {
260                 this.elNames.add(elNames[i]);
261             }
262             this.tagName=tagName;
263             values = new HashSet JavaDoc();
264         }
265         public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc rawName, Attributes atts) throws SAXException {
266             if (elNames.contains(rawName)) insideEl=true;
267             else if (tagName.equals(rawName) && insideEl) { //NOI18N
268
insideTag=true;
269             }
270         }
271         public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc rawName) throws SAXException {
272             if (elNames.contains(rawName)) insideEl=false;
273             else if (tagName.equals(rawName) && insideEl) { //NOI18N
274
insideTag=false;
275             }
276         }
277         
278         public void characters(char[] ch,int start,int length) throws SAXException {
279             if (insideTag) {
280                 values.add(String.valueOf(ch,start,length).trim());
281             }
282         }
283         public Set JavaDoc getValues() {
284             return values;
285         }
286     }
287     
288 }
289
Popular Tags