You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by jd...@apache.org on 2009/04/11 10:47:52 UTC

svn commit: r764186 - in /wicket/trunk/wicket/src/main/java/org/apache/wicket/util: resource/UrlResourceStream.java watch/ModificationWatcher.java

Author: jdonnerstag
Date: Sat Apr 11 08:47:51 2009
New Revision: 764186

URL: http://svn.apache.org/viewvc?rev=764186&view=rev
Log:
fixed WICKET-2212 Resource lookup/caching in OSGI environment spams errors for gone resources
Issue: WICKET-2212

Modified:
    wicket/trunk/wicket/src/main/java/org/apache/wicket/util/resource/UrlResourceStream.java
    wicket/trunk/wicket/src/main/java/org/apache/wicket/util/watch/ModificationWatcher.java

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/util/resource/UrlResourceStream.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/util/resource/UrlResourceStream.java?rev=764186&r1=764185&r2=764186&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/util/resource/UrlResourceStream.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/util/resource/UrlResourceStream.java Sat Apr 11 08:47:51 2009
@@ -40,8 +40,8 @@
  * @author Jonathan Locke
  */
 public class UrlResourceStream extends AbstractResourceStream
-		implements
-			IFixedLocationResourceStream
+	implements
+		IFixedLocationResourceStream
 {
 	private static final long serialVersionUID = 1L;
 
@@ -54,9 +54,7 @@
 	/** The URL to this resource. */
 	private final URL url;
 
-	/**
-	 * the handle to the file if it is a file resource
-	 */
+	/** the handle to the file if it is a file resource */
 	private File file;
 
 	/** Length of stream. */
@@ -92,7 +90,7 @@
 			catch (Exception ex)
 			{
 				log.debug("cannot convert url: " + url + " to file (" + ex.getMessage() +
-						"), falling back to the inputstream for polling");
+					"), falling back to the inputstream for polling");
 			}
 			if (file != null && !file.exists())
 			{
@@ -105,7 +103,7 @@
 			// couldn't have been constructed. But we re-throw with details
 			// anyway.
 			final IllegalArgumentException illegalArgumentException = new IllegalArgumentException(
-					"Invalid URL parameter " + url);
+				"Invalid URL parameter " + url);
 			illegalArgumentException.initCause(ex);
 			throw illegalArgumentException;
 		}
@@ -150,6 +148,7 @@
 	/**
 	 * @return The content type of this resource, such as "image/jpeg" or "text/html"
 	 */
+	@Override
 	public String getContentType()
 	{
 		testContentType();
@@ -170,7 +169,7 @@
 				// TODO Post 1.2: General: For non webapplication another method
 				// should be implemented (getMimeType on application?)
 				contentType = ((WebApplication)application).getServletContext().getMimeType(
-						url.getFile());
+					url.getFile());
 				if (contentType == null)
 				{
 					contentType = URLConnection.getFileNameMap().getContentTypeFor(url.getFile());
@@ -198,7 +197,7 @@
 			catch (IOException e)
 			{
 				throw new ResourceStreamNotFoundException("Resource " + url +
-						" could not be opened", e);
+					" could not be opened", e);
 			}
 		}
 
@@ -217,10 +216,17 @@
 	 * @see org.apache.wicket.util.watch.IModifiable#lastModifiedTime()
 	 * @return The last time this resource was modified
 	 */
+	@Override
 	public Time lastModifiedTime()
 	{
 		if (file != null)
 		{
+			// In case the file has been removed by now
+			if (file.exists() == false)
+			{
+				return null;
+			}
+
 			long lastModified = file.lastModified();
 			if (lastModified != this.lastModified)
 			{
@@ -255,6 +261,7 @@
 					close = true;
 					lastModified = urlConnection.getLastModified();
 				}
+
 				// update the last modified time.
 				if (lastModified != this.lastModified)
 				{
@@ -274,8 +281,11 @@
 				}
 				else
 				{
-					log.error("getLastModified for " + url + " failed: " + e.getMessage());
+					log.warn("getLastModified for " + url + " failed: " + e.getMessage());
 				}
+
+				// Allow modification watcher to detect the problem
+				return null;
 			}
 			finally
 			{
@@ -306,6 +316,7 @@
 	/**
 	 * @see java.lang.Object#toString()
 	 */
+	@Override
 	public String toString()
 	{
 		return url.toString();
@@ -314,6 +325,7 @@
 	/**
 	 * @see org.apache.wicket.util.resource.IResourceStream#length()
 	 */
+	@Override
 	public long length()
 	{
 		return contentLength;

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/util/watch/ModificationWatcher.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/util/watch/ModificationWatcher.java?rev=764186&r1=764185&r2=764186&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/util/watch/ModificationWatcher.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/util/watch/ModificationWatcher.java Sat Apr 11 08:47:51 2009
@@ -33,8 +33,8 @@
 
 
 /**
- * Monitors one or more <code>IModifiable</code> objects, calling a
- * {@link IChangeListener IChangeListener} when a given object's modification time changes.
+ * Monitors one or more <code>IModifiable</code> objects, calling a {@link IChangeListener
+ * IChangeListener} when a given object's modification time changes.
  * 
  * @author Jonathan Locke
  * @since 1.2.6
@@ -84,14 +84,14 @@
 	}
 
 	/**
-	 * Adds an <code>IModifiable</code> object and an <code>IChangeListener</code> object to
-	 * call when the modifiable object is modified.
+	 * Adds an <code>IModifiable</code> object and an <code>IChangeListener</code> object to call
+	 * when the modifiable object is modified.
 	 * 
 	 * @param modifiable
 	 *            an <code>IModifiable</code> object to monitor
 	 * @param listener
-	 *            an <code>IChangeListener</code> to call if the <code>IModifiable</code> object
-	 *            is modified
+	 *            an <code>IChangeListener</code> to call if the <code>IModifiable</code> object is
+	 *            modified
 	 * @return <code>true</code> if the set did not already contain the specified element
 	 */
 	public final boolean add(final IModifiable modifiable, final IChangeListener listener)
@@ -102,13 +102,14 @@
 		// Found it?
 		if (entry == null)
 		{
-			if (modifiable.lastModifiedTime() != null)
+			Time lastModifiedTime = modifiable.lastModifiedTime();
+			if (lastModifiedTime != null)
 			{
 				// Construct new entry
 				final Entry newEntry = new Entry();
 
 				newEntry.modifiable = modifiable;
-				newEntry.lastModifiedTime = modifiable.lastModifiedTime();
+				newEntry.lastModifiedTime = lastModifiedTime;
 				newEntry.listeners.add(listener);
 
 				// Put in map
@@ -161,20 +162,19 @@
 		{
 			public void run(final Logger log)
 			{
-				// Iterate over a copy of the list of entries to avoid
-				// concurrent
-				// modification problems without the associated liveness issues
-				// of holding a lock while potentially polling file times!
-				for (final Iterator<Entry> iterator = new ArrayList<Entry>(modifiableToEntry.values()).iterator(); iterator.hasNext();)
+				// Iterate over a copy of the list of entries to avoid concurrent modification
+				// problems without the associated liveness issues of holding a lock while
+				// potentially polling file times!
+				Iterator<Entry> iter = new ArrayList<Entry>(modifiableToEntry.values()).iterator();
+				while (iter.hasNext())
 				{
-					// Get next entry
-					final Entry entry = iterator.next();
+					final Entry entry = iter.next();
 
 					// If the modifiable has been modified after the last known
 					// modification time
 					final Time modifiableLastModified = entry.modifiable.lastModifiedTime();
-
-					if (modifiableLastModified.after(entry.lastModifiedTime))
+					if ((modifiableLastModified != null) &&
+						modifiableLastModified.after(entry.lastModifiedTime))
 					{
 						// Notify all listeners that the modifiable was modified
 						entry.listeners.notifyListeners();