From portal-commits at lists.jboss.org Tue Mar 6 14:01:40 2007 Content-Type: multipart/mixed; boundary="===============6485440089201415231==" MIME-Version: 1.0 From: portal-commits at lists.jboss.org To: portal-commits at lists.jboss.org Subject: [portal-commits] JBoss Portal SVN: r6558 - in trunk: wsrp/src/main/org/jboss/portal/wsrp/services and 1 other directory. Date: Tue, 06 Mar 2007 14:01:39 -0500 Message-ID: --===============6485440089201415231== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Author: chris.laprun(a)jboss.com Date: 2007-03-06 14:01:39 -0500 (Tue, 06 Mar 2007) New Revision: 6558 Added: trunk/common/src/main/org/jboss/portal/common/util/URLStreamOpeningThrea= d.java Modified: trunk/common/src/main/org/jboss/portal/common/util/URLTools.java trunk/wsrp/src/main/org/jboss/portal/wsrp/services/RemoteSOAPInvokerServ= iceFactory.java Log: JBPORTAL-1279: - Extracted URLStreamOpeningThread to its own class and moved it to common. - URLTools.exists uses URLStreamOpeningThread with a default timeout of 1s. - URLTools.exists(URL, long) to control timeout. Added: trunk/common/src/main/org/jboss/portal/common/util/URLStreamOpeningT= hread.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- trunk/common/src/main/org/jboss/portal/common/util/URLStreamOpeningThre= ad.java (rev 0) +++ trunk/common/src/main/org/jboss/portal/common/util/URLStreamOpeningThre= ad.java 2007-03-06 19:01:39 UTC (rev 6558) @@ -0,0 +1,90 @@ +/* +* JBoss, a division of Red Hat +* Copyright 2006, Red Hat Middleware, LLC, and individual contributors as = indicated +* by the @authors tag. See the copyright.txt in the distribution for a +* full listing of individual contributors. +* +* This is free software; you can redistribute it and/or modify it +* under the terms of the GNU Lesser General Public License as +* published by the Free Software Foundation; either version 2.1 of +* the License, or (at your option) any later version. +* +* This software is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this software; if not, write to the Free +* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +* 02110-1301 USA, or see the FSF site: http://www.fsf.org. +*/ + +package org.jboss.portal.common.util; + + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; + +/** + * A class that opens an InputStream to a URL in a thread so that it doesn= 't block normal operations and can be timed + * out if needed. + * + * @author Chris Laprun + * @version $Revision$ + * @since 2.4.2 + */ +public class URLStreamOpeningThread extends Thread +{ + volatile private URL url; + + /** */ + private InputStream inputStream; + + /** Exception in the event a connection error occurs */ + private IOException exception =3D null; + + public URLStreamOpeningThread(URL url) + { + ParameterValidation.throwIllegalArgExceptionIfNull(url, "URL"); + + this.url =3D url; + } + + public void run() + { + try + { + inputStream =3D url.openStream(); + if (inputStream =3D=3D null) + { + throw new IllegalArgumentException("Cannot open stream from ["= + url + "]"); + } + } + catch (IOException e) + { + exception =3D e; + } + } + + public boolean isConnected() + { + return inputStream !=3D null; + } + + public boolean isError() + { + return exception !=3D null; + } + + public InputStream getInputStream() + { + return inputStream; + } + + public IOException getException() + { + return exception; + } +} Property changes on: trunk/common/src/main/org/jboss/portal/common/util/URL= StreamOpeningThread.java ___________________________________________________________________ Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native Modified: trunk/common/src/main/org/jboss/portal/common/util/URLTools.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- trunk/common/src/main/org/jboss/portal/common/util/URLTools.java 2007-0= 3-06 18:15:10 UTC (rev 6557) +++ trunk/common/src/main/org/jboss/portal/common/util/URLTools.java 2007-0= 3-06 19:01:39 UTC (rev 6558) @@ -22,8 +22,6 @@ *************************************************************************= *****/ package org.jboss.portal.common.util; = -import java.io.IOException; -import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; @@ -110,9 +108,8 @@ } = /** - * Determines that the specified URL corresponds to an existing resourc= e by trying to open a stream from it. NOTE: - * This is a potentially blocking method since by default there is no t= imeout on the connection... See - * http://jira.jboss.com/jira/browse/JBPORTAL-1279 + * Determines that the specified URL corresponds to an existing resourc= e by trying to open a stream from it. Same as + * exists(url, 1000) * * @param url * @return @@ -121,19 +118,49 @@ { ParameterValidation.throwIllegalArgExceptionIfNull(url, "URL"); = - InputStream in =3D null; + return exists(url, 1000); + } + + /** + * Determines if the specified URL corresponds to an existing resource = by trying to open a stream from it. The + * connection attempt is made from a different thread on which we wait = for the specified amount of time before timing + * out. + * + * @param url the URL to be tested + * @param waitForMillis the number of milliseconds to wait before timin= g out, 0 meaning never timing out. + * @return + * @since 2.4.2 + */ + public static boolean exists(URL url, long waitForMillis) + { + URLStreamOpeningThread thread =3D new URLStreamOpeningThread(url); + thread.start(); + try { - in =3D url.openStream(); - return true; + // Wait for the thread to finish but don't wait longer than the s= pecified time + thread.join(waitForMillis); + + if (thread.isAlive()) + { + // Timeout occurred; thread has not finished + throw new RuntimeException("Couldn't connect to " + url + " wi= thin " + waitForMillis / 1000 + + " seconds. Check your connection parameters or the URL."); + } + else + { + // Finished + return thread.isConnected() && !thread.isError(); + } } - catch (IOException e) + catch (InterruptedException e) { - return false; + // Thread was interrupted + throw new RuntimeException("Connection thread to " + url + " was = interrupted!", e); } finally { - Tools.safeClose(in); + Tools.safeClose(thread.getInputStream()); } } = @@ -143,7 +170,7 @@ * false, false to throw a= n {@link IllegalArgumentException} is the * given URL is null. * @return - * @since 2.6 + * @since 2.4.2 */ public static boolean exists(String urlAsString, boolean allowNull) { @@ -155,7 +182,7 @@ try { URL url =3D new URL(urlAsString); - return exists(url); + return exists((URL)url); } catch (MalformedURLException e) { Modified: trunk/wsrp/src/main/org/jboss/portal/wsrp/services/RemoteSOAPInvo= kerServiceFactory.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- trunk/wsrp/src/main/org/jboss/portal/wsrp/services/RemoteSOAPInvokerSer= viceFactory.java 2007-03-06 18:15:10 UTC (rev 6557) +++ trunk/wsrp/src/main/org/jboss/portal/wsrp/services/RemoteSOAPInvokerSer= viceFactory.java 2007-03-06 19:01:39 UTC (rev 6558) @@ -26,6 +26,7 @@ import org.jboss.logging.Logger; import org.jboss.portal.common.util.ParameterValidation; import org.jboss.portal.common.util.Tools; +import org.jboss.portal.common.util.URLStreamOpeningThread; import org.xml.sax.InputSource; = import javax.wsdl.Definition; @@ -184,60 +185,6 @@ } = = - static class URLStreamOpeningThread extends Thread - { - volatile private URL url; - - /** */ - private InputStream inputStream; - - /** Exception in the event a connection error occurs */ - private IOException exception =3D null; - - public URLStreamOpeningThread(URL url) - { - ParameterValidation.throwIllegalArgExceptionIfNull(url, "URL"); - - this.url =3D url; - } - - public void run() - { - try - { - inputStream =3D url.openStream(); - if (inputStream =3D=3D null) - { - throw new IllegalArgumentException("Cannot open stream from= [" + url + "]"); - } - } - catch (IOException e) - { - exception =3D e; - } - } - - public boolean isConnected() - { - return inputStream !=3D null; - } - - public boolean isError() - { - return exception !=3D null; - } - - public InputStream getInputStream() - { - return inputStream; - } - - public IOException getException() - { - return exception; - } - } - /** A WSDLLocator that can handle wsdl imports */ public static class WSDLLocatorImpl implements WSDLLocator { @@ -268,7 +215,8 @@ if (thread.isAlive()) { // Timeout occurred; thread has not finished - // todo: do we need clean up here? + // todo: do we need more clean up here? + Tools.safeClose(thread.getInputStream()); throw new RuntimeException("Couldn't connect to " + wsdlURL= + " within " + delayMillis / 1000 + " seconds. Check your connection parameters or the URL= ."); } --===============6485440089201415231==--