Author: vgritsenko Date: Fri Mar 18 08:49:48 2005 New Revision: 158096 URL: http://svn.apache.org/viewcvs?view=rev&rev=158096 Log: Bugfix: wrong usage of SourceValidity.isValid methods
Modified: cocoon/trunk/src/java/org/apache/cocoon/i18n/XMLResourceBundle.java cocoon/trunk/src/java/org/apache/cocoon/i18n/XMLResourceBundleFactory.java cocoon/trunk/status.xml Modified: cocoon/trunk/src/java/org/apache/cocoon/i18n/XMLResourceBundle.java URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/i18n/XMLResourceBundle.java?view=diff&r1=158095&r2=158096 ============================================================================== --- cocoon/trunk/src/java/org/apache/cocoon/i18n/XMLResourceBundle.java (original) +++ cocoon/trunk/src/java/org/apache/cocoon/i18n/XMLResourceBundle.java Fri Mar 18 08:49:48 2005 @@ -1,5 +1,5 @@ /* - * Copyright 1999-2004 The Apache Software Foundation. + * Copyright 1999-2005 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. @@ -57,7 +57,7 @@ * are instances of the [EMAIL PROTECTED] ParamSaxBuffer} class. * * @author <a href="mailto:dev@cocoon.apache.org">Apache Cocoon Team</a> - * @version CVS $Id$ + * @version $Id$ */ public class XMLResourceBundle extends AbstractLogEnabled implements Bundle, Serviceable { @@ -91,7 +91,7 @@ /** * Bundle validity */ - private SourceValidity validity = null; + private SourceValidity validity; /** * Locale of the bundle @@ -287,20 +287,23 @@ */ protected void load(String sourceURL) throws IOException, ProcessingException, SAXException { - Source source = null; SourceResolver resolver = null; try { - resolver = (SourceResolver)manager.lookup(SourceResolver.ROLE); - source = resolver.resolveURI(sourceURL); - SourceValidity sourceValidity = source.getValidity(); - if (validity == null || validity.isValid( sourceValidity ) == SourceValidity.INVALID) { - HashMap values = new HashMap(); - SourceUtil.toSAX(source, new SAXContentHandler(values)); - this.validity = sourceValidity; - this.values = values; - if (getLogger().isDebugEnabled()) { - getLogger().debug("Loaded XML bundle: " + name + ", locale: " + locale); + int valid = this.validity == null? SourceValidity.INVALID: this.validity.isValid(); + if (valid != SourceValidity.VALID) { + // Saved validity is not valid, get new source and validity + resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE); + source = resolver.resolveURI(sourceURL); + SourceValidity sourceValidity = source.getValidity(); + if (valid == SourceValidity.INVALID || this.validity.isValid(sourceValidity) != SourceValidity.VALID) { + HashMap values = new HashMap(); + SourceUtil.toSAX(source, new SAXContentHandler(values)); + this.validity = sourceValidity; + this.values = values; + if (getLogger().isDebugEnabled()) { + getLogger().debug("Loaded XML bundle: " + this.name + ", locale: " + this.locale); + } } } } catch (ServiceException e) { @@ -311,7 +314,7 @@ if (source != null) { resolver.release(source); } - manager.release(resolver); + this.manager.release(resolver); } } @@ -339,7 +342,7 @@ * @return the locale */ public Locale getLocale() { - return locale; + return this.locale; } /** @@ -372,7 +375,7 @@ return null; } - Object value = values.get(key); + Object value = this.values.get(key); if (value != null) { return value.toString(); } @@ -390,21 +393,20 @@ * @return the enumeration of keys */ public Set keySet() { - return Collections.unmodifiableSet(values.keySet()); + return Collections.unmodifiableSet(this.values.keySet()); } /** * Reload this bundle if URI's timestam is newer than ours * * @param sourceURL source URL of the XML bundle - **/ - public void update(String sourceURL) - { + */ + public void update(String sourceURL) { try { - load(sourceURL); - } - catch (Exception e) { - getLogger().info("Resource update failed. " + name + ", locale: " + locale + " Exception: " + e.getMessage()); + load(sourceURL); + } catch (Exception e) { + getLogger().info("Resource update failed. " + this.name + ", locale: " + this.locale + + " Exception: " + e); } } } Modified: cocoon/trunk/src/java/org/apache/cocoon/i18n/XMLResourceBundleFactory.java URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/i18n/XMLResourceBundleFactory.java?view=diff&r1=158095&r2=158096 ============================================================================== --- cocoon/trunk/src/java/org/apache/cocoon/i18n/XMLResourceBundleFactory.java (original) +++ cocoon/trunk/src/java/org/apache/cocoon/i18n/XMLResourceBundleFactory.java Fri Mar 18 08:49:48 2005 @@ -1,5 +1,5 @@ /* - * Copyright 1999-2004 The Apache Software Foundation. + * Copyright 1999-2005 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. @@ -47,12 +47,20 @@ * @author <a href="mailto:[EMAIL PROTECTED]">Neeme Praks</a> * @author <a href="mailto:[EMAIL PROTECTED]">Oleg Podolsky</a> * @author <a href="mailto:[EMAIL PROTECTED]">Konstantin Piroumian</a> - * @version CVS $Id$ + * @version $Id$ */ public class XMLResourceBundleFactory implements BundleFactory, Serviceable, Configurable, Disposable, ThreadSafe, LogEnabled { - protected Map cache = Collections.synchronizedMap(new HashMap()); + /** + * Cache of the bundles by file name + */ + protected final Map cache = Collections.synchronizedMap(new HashMap()); + + /** + * Cache for the file names of the bundles that were not found + */ + protected final Map cacheNotFound = new HashMap(); /** * Should we load bundles to cache on startup or not? @@ -65,11 +73,6 @@ protected String directory; /** - * Cache for the names of the bundles that were not found - */ - protected final Map cacheNotFound = new HashMap(); - - /** * The logger */ private Logger logger; @@ -77,7 +80,7 @@ /** * Service Manager */ - protected ServiceManager manager = null; + protected ServiceManager manager; /** * Source resolver @@ -139,7 +142,7 @@ if (getLogger().isDebugEnabled()) { getLogger().debug("Configured with: cacheAtStartup = " + - cacheAtStartup + ", directory = '" + directory + "'"); + this.cacheAtStartup + ", directory = '" + this.directory + "'"); } } @@ -195,7 +198,8 @@ * @return the bundle * @exception Exception if a bundle is not found */ - public Bundle select(String directory, String name, String localeName) throws Exception { + public Bundle select(String directory, String name, String localeName) + throws Exception { return select(directory, name, new Locale(localeName, localeName)); } @@ -209,7 +213,8 @@ * @return the bundle * @exception Exception if a bundle is not found */ - public Bundle select(String directory, String name, Locale locale) throws Exception { + public Bundle select(String directory, String name, Locale locale) + throws Exception { String []directories = new String[1]; directories[0] = directory; return select(directories, name, locale); @@ -226,7 +231,7 @@ * @exception Exception if a bundle is not found */ public Bundle select(String[] directories, String name, Locale locale) - throws Exception { + throws Exception { Bundle bundle = _select(directories, 0, name, locale); if (bundle == null) { throw new Exception("Unable to locate resource: " + name); @@ -245,7 +250,7 @@ private XMLResourceBundle _select(String[] directories, int index, String name, Locale locale) { if (getLogger().isDebugEnabled()) { - getLogger().debug("selecting from: " + name + ", locale: " + locale + + getLogger().debug("Selecting from: " + name + ", locale: " + locale + ", directory: " + directories[index]); } String cacheKey = getCacheKey(directories, index, name, locale); @@ -303,10 +308,10 @@ return bundle; } catch (ResourceNotFoundException e) { getLogger().info("Resource not found: " + name + ", locale: " + locale + - ", bundleName: " + fileName + ". Exception: " + e.toString()); + ", bundleName: " + fileName + ". Exception: " + e); } catch (SourceNotFoundException e) { getLogger().info("Resource not found: " + name + ", locale: " + locale + - ", bundleName: " + fileName + ". Exception: " + e.toString()); + ", bundleName: " + fileName + ". Exception: " + e); } catch (SAXParseException e) { getLogger().error("Incorrect resource format", e); } catch (Exception e) { @@ -410,7 +415,7 @@ */ protected XMLResourceBundle selectCached(String fileName) { XMLResourceBundle bundle = null; - bundle = (XMLResourceBundle)cache.get(fileName); + bundle = (XMLResourceBundle) this.cache.get(fileName); if (bundle != null) { bundle.update(fileName); if (getLogger().isDebugEnabled()) { @@ -433,7 +438,7 @@ * otherwise, false. */ protected boolean isNotFoundBundle(String fileName) { - String result = (String) (cacheNotFound.get(fileName)); + String result = (String) this.cacheNotFound.get(fileName); if (result != null) { if (getLogger().isDebugEnabled()) { getLogger().debug("Returning from not_found_cache: " + fileName); @@ -456,7 +461,7 @@ if (getLogger().isDebugEnabled()) { getLogger().debug("Updating not_found_cache: " + fileName); } - cacheNotFound.put(fileName, fileName); + this.cacheNotFound.put(fileName, fileName); } else { if (getLogger().isDebugEnabled()) { getLogger().debug("Updating cache: " + fileName); Modified: cocoon/trunk/status.xml URL: http://svn.apache.org/viewcvs/cocoon/trunk/status.xml?view=diff&r1=158095&r2=158096 ============================================================================== --- cocoon/trunk/status.xml (original) +++ cocoon/trunk/status.xml Fri Mar 18 08:49:48 2005 @@ -429,6 +429,9 @@ </action> </release> <release version="2.1.7" date="TBD"> + <action dev="VG" type="fix"> + Fix caching of i18n bundles with expires validities. + </action> <action dev="VG" type="add"> Implemented error handling for the internal requests. Error handling for the internal requests configured using <code>when</code> attribute on