Author: fhanik Date: Thu Jul 5 06:45:42 2007 New Revision: 553505 URL: http://svn.apache.org/viewvc?view=rev&rev=553505 Log: Backport fix for windows problem with Math.floor
Modified: tomcat/container/branches/tc5.0.x/catalina/src/share/org/apache/naming/resources/ResourceCache.java Modified: tomcat/container/branches/tc5.0.x/catalina/src/share/org/apache/naming/resources/ResourceCache.java URL: http://svn.apache.org/viewvc/tomcat/container/branches/tc5.0.x/catalina/src/share/org/apache/naming/resources/ResourceCache.java?view=diff&rev=553505&r1=553504&r2=553505 ============================================================================== --- tomcat/container/branches/tc5.0.x/catalina/src/share/org/apache/naming/resources/ResourceCache.java (original) +++ tomcat/container/branches/tc5.0.x/catalina/src/share/org/apache/naming/resources/ResourceCache.java Thu Jul 5 06:45:42 2007 @@ -1,42 +1,50 @@ /* * Copyright 1999,2004 The Apache Software Foundation. - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - */ + */ package org.apache.naming.resources; import java.util.HashMap; +import java.util.Random; + /** * Implements a special purpose cache. - * + * * @author <a href="mailto:[EMAIL PROTECTED]">Remy Maucherat</a> * @version $Revision$ */ public class ResourceCache { - - + + // ----------------------------------------------------------- Constructors - - + + public ResourceCache() { } - - + + // ----------------------------------------------------- Instance Variables - - + + + /** + * Random generator used to determine elements to free. + */ + protected Random random = new Random(); + + /** * Cache. * Path -> Cache entry. @@ -98,7 +106,7 @@ /** * Return the access count. - * Note: Update is not synced, so the number may not be completely + * Note: Update is not synced, so the number may not be completely * accurate. */ public long getAccessCount() { @@ -148,7 +156,7 @@ /** * Return the number of cache hits. - * Note: Update is not synced, so the number may not be completely + * Note: Update is not synced, so the number may not be completely * accurate. */ public long getHitsCount() { @@ -227,11 +235,9 @@ // Randomly select an entry in the array int entryPos = -1; boolean unique = false; - int count = 0; while (!unique) { unique = true; - entryPos = (int) Math.floor(Math.random() - * (cache.length - 1)); + entryPos = random.nextInt(cache.length) ; // Guarantee uniqueness for (int i = 0; i < entriesFound; i++) { if (toRemove[i] == entryPos) { @@ -239,7 +245,7 @@ } } } - long entryAccessRatio = + long entryAccessRatio = ((cache[entryPos].accessCount * 100) / accessCount); if (entryAccessRatio < desiredEntryAccessRatio) { toRemove[entriesFound] = entryPos; @@ -289,6 +295,7 @@ if ((pos != -1) && (name.equals(currentCache[pos].name))) { cacheEntry = currentCache[pos]; } + if (cacheEntry == null) { try { cacheEntry = (CacheEntry) notFoundCache.get(name); @@ -306,11 +313,14 @@ public void load(CacheEntry entry) { if (entry.exists) { - insertCache(entry); + if (insertCache(entry)) { + cacheSize += entry.size; + } } else { + int sizeIncrement = (notFoundCache.get(entry.name) == null) ? 1 : 0; notFoundCache.put(entry.name, entry); + cacheSize += sizeIncrement; } - cacheSize += entry.size; } @@ -401,13 +411,12 @@ if ((pos != -1) && (name.equals(oldCache[pos].name))) { CacheEntry[] newCache = new CacheEntry[cache.length - 1]; System.arraycopy(oldCache, 0, newCache, 0, pos); - System.arraycopy(oldCache, pos + 1, newCache, pos, + System.arraycopy(oldCache, pos + 1, newCache, pos, oldCache.length - pos - 1); cache = newCache; return oldCache[pos]; } return null; } - } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]