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/URLStreamOpeningThread.java
Modified:
trunk/common/src/main/org/jboss/portal/common/util/URLTools.java
trunk/wsrp/src/main/org/jboss/portal/wsrp/services/RemoteSOAPInvokerServiceFactory.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/URLStreamOpeningThread.java
===================================================================
--- trunk/common/src/main/org/jboss/portal/common/util/URLStreamOpeningThread.java
(rev 0)
+++
trunk/common/src/main/org/jboss/portal/common/util/URLStreamOpeningThread.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 <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @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 = null;
+
+ public URLStreamOpeningThread(URL url)
+ {
+ ParameterValidation.throwIllegalArgExceptionIfNull(url, "URL");
+
+ this.url = url;
+ }
+
+ public void run()
+ {
+ try
+ {
+ inputStream = url.openStream();
+ if (inputStream == null)
+ {
+ throw new IllegalArgumentException("Cannot open stream from [" +
url + "]");
+ }
+ }
+ catch (IOException e)
+ {
+ exception = e;
+ }
+ }
+
+ public boolean isConnected()
+ {
+ return inputStream != null;
+ }
+
+ public boolean isError()
+ {
+ return exception != null;
+ }
+
+ public InputStream getInputStream()
+ {
+ return inputStream;
+ }
+
+ public IOException getException()
+ {
+ return exception;
+ }
+}
Property changes on:
trunk/common/src/main/org/jboss/portal/common/util/URLStreamOpeningThread.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
===================================================================
--- trunk/common/src/main/org/jboss/portal/common/util/URLTools.java 2007-03-06 18:15:10
UTC (rev 6557)
+++ trunk/common/src/main/org/jboss/portal/common/util/URLTools.java 2007-03-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 resource by trying to
open a stream from it. NOTE:
- * This is a potentially blocking method since by default there is no timeout on the
connection... See
- *
http://jira.jboss.com/jira/browse/JBPORTAL-1279
+ * Determines that the specified URL corresponds to an existing resource by trying to
open a stream from it. Same as
+ * <code>exists(url, 1000)</code>
*
* @param url
* @return
@@ -121,19 +118,49 @@
{
ParameterValidation.throwIllegalArgExceptionIfNull(url, "URL");
- InputStream in = 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 timing out, 0
meaning never timing out.
+ * @return
+ * @since 2.4.2
+ */
+ public static boolean exists(URL url, long waitForMillis)
+ {
+ URLStreamOpeningThread thread = new URLStreamOpeningThread(url);
+ thread.start();
+
try
{
- in = url.openStream();
- return true;
+ // Wait for the thread to finish but don't wait longer than the specified
time
+ thread.join(waitForMillis);
+
+ if (thread.isAlive())
+ {
+ // Timeout occurred; thread has not finished
+ throw new RuntimeException("Couldn't connect to " + url +
" within " + 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 @@
* <code>false</code>, <code>false</code>
to throw an {@link IllegalArgumentException} is the
* given URL is <code>null</code>.
* @return
- * @since 2.6
+ * @since 2.4.2
*/
public static boolean exists(String urlAsString, boolean allowNull)
{
@@ -155,7 +182,7 @@
try
{
URL url = new URL(urlAsString);
- return exists(url);
+ return exists((URL)url);
}
catch (MalformedURLException e)
{
Modified:
trunk/wsrp/src/main/org/jboss/portal/wsrp/services/RemoteSOAPInvokerServiceFactory.java
===================================================================
---
trunk/wsrp/src/main/org/jboss/portal/wsrp/services/RemoteSOAPInvokerServiceFactory.java 2007-03-06
18:15:10 UTC (rev 6557)
+++
trunk/wsrp/src/main/org/jboss/portal/wsrp/services/RemoteSOAPInvokerServiceFactory.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 = null;
-
- public URLStreamOpeningThread(URL url)
- {
- ParameterValidation.throwIllegalArgExceptionIfNull(url, "URL");
-
- this.url = url;
- }
-
- public void run()
- {
- try
- {
- inputStream = url.openStream();
- if (inputStream == null)
- {
- throw new IllegalArgumentException("Cannot open stream from [" +
url + "]");
- }
- }
- catch (IOException e)
- {
- exception = e;
- }
- }
-
- public boolean isConnected()
- {
- return inputStream != null;
- }
-
- public boolean isError()
- {
- return exception != 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.");
}