001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.web.core;
043:
044: import java.io.BufferedReader;
045: import java.io.InputStreamReader;
046: import java.io.IOException;
047: import java.net.Socket;
048: import java.net.URL;
049: import java.net.InetAddress;
050: import java.net.UnknownHostException;
051: import java.util.ArrayList;
052: import java.util.Collections;
053: import java.util.HashMap;
054: import java.util.HashSet;
055: import java.util.List;
056: import java.util.Map;
057: import java.util.Set;
058: import java.util.logging.Level;
059: import java.util.logging.Logger;
060: import org.netbeans.api.java.project.JavaProjectConstants;
061: import org.netbeans.api.java.queries.UnitTestForSourceQuery;
062: import org.netbeans.api.project.Project;
063: import org.netbeans.api.project.ProjectUtils;
064: import org.netbeans.api.project.SourceGroup;
065: import org.openide.filesystems.FileObject;
066: import org.openide.filesystems.URLMapper; //import java.io.*;
067: import org.xml.sax.*;
068:
069: //import java.util.*;
070:
071: /** Utility class
072: * @author Petr Jiricka
073: * @version 1.00, Jun 03, 1999
074: */
075: public class Util {
076:
077: /** Waits for startup of a server, waits until the connection has
078: * been established. */
079:
080: public static boolean waitForURLConnection(URL url, int timeout,
081: int retryTime) {
082: Connect connect = new Connect(url, retryTime);
083: Thread t = new Thread(connect);
084: t.start();
085: try {
086: t.join(timeout);
087: } catch (InterruptedException ie) {
088: }
089: if (t.isAlive()) {
090: connect.finishLoop();
091: t.interrupt();//for thread deadlock
092: }
093: return connect.getStatus();
094: }
095:
096: public static String issueGetRequest(URL url) {
097: BufferedReader in = null;
098: StringBuffer input = new StringBuffer();
099: try {
100: in = new BufferedReader(new InputStreamReader(url
101: .openStream()));
102: String inputLine;
103: while ((inputLine = in.readLine()) != null) {
104: input.append(inputLine);
105: input.append("\n"); // NOI18N
106: }
107: return input.toString();
108: } catch (Exception e) {
109: //e.printStackTrace();
110: return null;
111: } finally {
112: if (in != null)
113: try {
114: in.close();
115: } catch (IOException e) {
116: //e.printStackTrace();
117: }
118: }
119: }
120:
121: private static class Connect implements Runnable {
122:
123: URL url = null;
124: int retryTime;
125: boolean status = false;
126: boolean loop = true;
127:
128: public Connect(URL url, int retryTime) {
129: this .url = url;
130: this .retryTime = retryTime;
131: }
132:
133: public void finishLoop() {
134: loop = false;
135: }
136:
137: public void run() {
138: try {
139: InetAddress.getByName(url.getHost());
140: } catch (UnknownHostException e) {
141: return;
142: }
143: while (loop) {
144: try {
145: Socket socket = new Socket(url.getHost(), url
146: .getPort());
147: socket.close();
148: status = true;
149: break;
150: } catch (UnknownHostException e) {//nothing to do
151: } catch (IOException e) {//nothing to do
152: }
153: try {
154: Thread.currentThread().sleep(retryTime);
155: } catch (InterruptedException ie) {
156: }
157: }
158: }
159:
160: boolean getStatus() {
161: return status;
162: }
163: }
164:
165: // following block is copy/pasted code from
166: // org.netbeans.modules.j2ee.common.Util
167:
168: /**
169: * Returns Java source groups for all source packages in given project.<br>
170: * Doesn't include test packages.
171: *
172: * @param project Project to search
173: * @return Array of SourceGroup. It is empty if any probelm occurs.
174: */
175: public static SourceGroup[] getJavaSourceGroups(Project project) {
176: SourceGroup[] sourceGroups = ProjectUtils
177: .getSources(project)
178: .getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
179: Set testGroups = getTestSourceGroups(project, sourceGroups);
180: List result = new ArrayList();
181: for (int i = 0; i < sourceGroups.length; i++) {
182: if (!testGroups.contains(sourceGroups[i])) {
183: result.add(sourceGroups[i]);
184: }
185: }
186: return (SourceGroup[]) result.toArray(new SourceGroup[result
187: .size()]);
188: }
189:
190: private static Set/*<SourceGroup>*/getTestSourceGroups(
191: Project project, SourceGroup[] sourceGroups) {
192: Map foldersToSourceGroupsMap = createFoldersToSourceGroupsMap(sourceGroups);
193: Set testGroups = new HashSet();
194: for (int i = 0; i < sourceGroups.length; i++) {
195: testGroups.addAll(getTestTargets(sourceGroups[i],
196: foldersToSourceGroupsMap));
197: }
198: return testGroups;
199: }
200:
201: private static Map createFoldersToSourceGroupsMap(
202: final SourceGroup[] sourceGroups) {
203: Map result;
204: if (sourceGroups.length == 0) {
205: result = Collections.EMPTY_MAP;
206: } else {
207: result = new HashMap(2 * sourceGroups.length, .5f);
208: for (int i = 0; i < sourceGroups.length; i++) {
209: SourceGroup sourceGroup = sourceGroups[i];
210: result.put(sourceGroup.getRootFolder(), sourceGroup);
211: }
212: }
213: return result;
214: }
215:
216: private static List/*<FileObject>*/getFileObjects(URL[] urls) {
217: List result = new ArrayList();
218: for (int i = 0; i < urls.length; i++) {
219: FileObject sourceRoot = URLMapper.findFileObject(urls[i]);
220: if (sourceRoot != null) {
221: result.add(sourceRoot);
222: } else {
223: if (Logger.getLogger("global").isLoggable(Level.FINE)) {
224: Logger.getLogger("global").log(
225: Level.FINE,
226: null,
227: new IllegalStateException(
228: "No FileObject found for the following URL: "
229: + urls[i]));
230: }
231: }
232: }
233: return result;
234: }
235:
236: private static List/*<SourceGroup>*/getTestTargets(
237: SourceGroup sourceGroup, Map foldersToSourceGroupsMap) {
238: final URL[] rootURLs = UnitTestForSourceQuery
239: .findUnitTests(sourceGroup.getRootFolder());
240: if (rootURLs.length == 0) {
241: return new ArrayList();
242: }
243: List result = new ArrayList();
244: List sourceRoots = getFileObjects(rootURLs);
245: for (int i = 0; i < sourceRoots.size(); i++) {
246: FileObject sourceRoot = (FileObject) sourceRoots.get(i);
247: SourceGroup srcGroup = (SourceGroup) foldersToSourceGroupsMap
248: .get(sourceRoot);
249: if (srcGroup != null) {
250: result.add(srcGroup);
251: }
252: }
253: return result;
254: }
255:
256: /** Parsing to get Set of Strings that correpond to tagName valeus inside elName, e.g.:
257: * to get all <servlet-name> values inside the <servlet> elements (in web.xml)
258: */
259: public static Set getTagValues(java.io.InputStream is,
260: String elName, String tagName) throws java.io.IOException,
261: SAXException {
262: return getTagValues(is, new String[] { elName }, tagName);
263: }
264:
265: /** Parsing to get Set of Strings that correpond to tagName valeus inside elNames, e.g.:
266: * to get all <name> values inside the <tag> and <tag-file> elements (in TLD)
267: */
268: public static Set getTagValues(java.io.InputStream is,
269: String[] elNames, String tagName)
270: throws java.io.IOException, SAXException {
271: javax.xml.parsers.SAXParserFactory fact = javax.xml.parsers.SAXParserFactory
272: .newInstance();
273: fact.setValidating(false);
274: try {
275: javax.xml.parsers.SAXParser parser = fact.newSAXParser();
276: XMLReader reader = parser.getXMLReader();
277: TLDVersionHandler handler = new TLDVersionHandler(elNames,
278: tagName);
279: reader.setContentHandler(handler);
280: try {
281: reader.parse(new InputSource(is));
282: } catch (SAXException ex) {
283: String message = ex.getMessage();
284: }
285: return handler.getValues();
286: } catch (javax.xml.parsers.ParserConfigurationException ex) {
287: return new java.util.HashSet();
288: }
289: }
290:
291: private static class TLDVersionHandler extends
292: org.xml.sax.helpers.DefaultHandler {
293: private String tagName;
294: private Set elNames;
295: private Set values;
296: private boolean insideEl, insideTag;
297:
298: TLDVersionHandler(String[] elNames, String tagName) {
299: this .elNames = new java.util.HashSet();
300: for (int i = 0; i < elNames.length; i++) {
301: this .elNames.add(elNames[i]);
302: }
303: this .tagName = tagName;
304: values = new HashSet();
305: }
306:
307: public void startElement(String uri, String localName,
308: String rawName, Attributes atts) throws SAXException {
309: if (elNames.contains(rawName))
310: insideEl = true;
311: else if (tagName.equals(rawName) && insideEl) { //NOI18N
312: insideTag = true;
313: }
314: }
315:
316: public void endElement(String uri, String localName,
317: String rawName) throws SAXException {
318: if (elNames.contains(rawName))
319: insideEl = false;
320: else if (tagName.equals(rawName) && insideEl) { //NOI18N
321: insideTag = false;
322: }
323: }
324:
325: public void characters(char[] ch, int start, int length)
326: throws SAXException {
327: if (insideTag) {
328: values.add(String.valueOf(ch, start, length).trim());
329: }
330: }
331:
332: public Set getValues() {
333: return values;
334: }
335: }
336:
337: }
|