diff --git a/source/java/org/alfresco/repo/domain/locale/AbstractLocaleDAOImpl.java b/source/java/org/alfresco/repo/domain/locale/AbstractLocaleDAOImpl.java index e91ee95549..bb6f9141e4 100644 --- a/source/java/org/alfresco/repo/domain/locale/AbstractLocaleDAOImpl.java +++ b/source/java/org/alfresco/repo/domain/locale/AbstractLocaleDAOImpl.java @@ -81,21 +81,25 @@ public abstract class AbstractLocaleDAOImpl implements LocaleDAO this.localeEntityCache = new EntityLookupCache(new LocaleEntityCallbackDAO()); } + /** + * {@inheritDoc} + */ public Pair getLocalePair(Locale locale) { - if (locale == null) - { - throw new IllegalArgumentException("Locale cannot be null"); - } - return getLocalePairImpl(locale); } + /** + * {@inheritDoc} + */ public Pair getDefaultLocalePair() { return getLocalePairImpl(null); } + /** + * {@inheritDoc} + */ public Pair getLocalePair(Long id) { if (id == null) @@ -108,42 +112,44 @@ public abstract class AbstractLocaleDAOImpl implements LocaleDAO { throw new DataIntegrityViolationException("No locale exists for ID " + id); } - + String localeStr = entityPair.getSecond(); // Convert the locale string to a locale - Locale locale = DefaultTypeConverter.INSTANCE.convert(Locale.class, entityPair.getSecond()); + Locale locale = null; + if (LocaleEntity.DEFAULT_LOCALE_SUBSTITUTE.equals(localeStr)) + { + locale = I18NUtil.getLocale(); + } + else + { + locale = DefaultTypeConverter.INSTANCE.convert(Locale.class, entityPair.getSecond()); + } + return new Pair(id, locale); } + /** + * {@inheritDoc} + */ public Pair getOrCreateLocalePair(Locale locale) { - if (locale == null) - { - throw new IllegalArgumentException("Locale cannot be null"); - } - - String localeStr = DefaultTypeConverter.INSTANCE.convert(String.class, locale); - Pair entityPair = localeEntityCache.getOrCreateByValue(localeStr); - if (entityPair == null) - { - throw new RuntimeException("Locale should have been created."); - } - return new Pair(entityPair.getFirst(), locale); + return getOrCreateLocalePairImpl(locale); } + /** + * {@inheritDoc} + */ public Pair getOrCreateDefaultLocalePair() { - Pair localePair = getDefaultLocalePair(); - if (localePair != null) - { - return localePair; - } - - LocaleEntity localeEntity = new LocaleEntity(); - localeEntity.setLocale(null); - - return getOrCreateLocalePair(localeEntity.getLocale()); + return getOrCreateLocalePairImpl(null); } + /** + * Find the locale pair + * + * @param locale the locale to get or null to indicate the + * {@link LocaleEntity#DEFAULT_LOCALE_SUBSTITUTE default locale}. + * @return Returns the locale pair (ID, Locale) or null if not found. + */ private Pair getLocalePairImpl(Locale locale) { // Null means look for the default @@ -160,7 +166,7 @@ public abstract class AbstractLocaleDAOImpl implements LocaleDAO if (localeStr == null) { - throw new IllegalArgumentException("Cannot look up entity by null ID."); + throw new IllegalArgumentException("Cannot look up entity by null locale."); } Pair entityPair = localeEntityCache.getByValue(localeStr); @@ -174,6 +180,40 @@ public abstract class AbstractLocaleDAOImpl implements LocaleDAO } } + /** + * Find or create the locale pair + * + * @param locale the locale to get or null to indicate the + * {@link LocaleEntity#DEFAULT_LOCALE_SUBSTITUTE default locale}. + * @return Returns the locale pair (ID, Locale), never null + */ + private Pair getOrCreateLocalePairImpl(Locale locale) + { + // Null means look for the default + final String localeStr; + if (locale == null) + { + localeStr = LocaleEntity.DEFAULT_LOCALE_SUBSTITUTE; + locale = I18NUtil.getLocale(); + } + else + { + localeStr = DefaultTypeConverter.INSTANCE.convert(String.class, locale); + } + + if (localeStr == null) + { + throw new IllegalArgumentException("Cannot look up entity by null locale."); + } + + Pair entityPair = localeEntityCache.getOrCreateByValue(localeStr); + if (entityPair == null) + { + throw new RuntimeException("Locale should have been created."); + } + return new Pair(entityPair.getFirst(), locale); + } + /** * Callback for alf_locale DAO */ diff --git a/source/java/org/alfresco/repo/domain/locale/LocaleDAO.java b/source/java/org/alfresco/repo/domain/locale/LocaleDAO.java index 2f5d2a92af..1efaed3e26 100644 --- a/source/java/org/alfresco/repo/domain/locale/LocaleDAO.java +++ b/source/java/org/alfresco/repo/domain/locale/LocaleDAO.java @@ -26,8 +26,8 @@ package org.alfresco.repo.domain.locale; import java.util.Locale; -import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.util.Pair; +import org.springframework.dao.DataIntegrityViolationException; /** * Data abstraction layer for Locale entities. @@ -39,8 +39,8 @@ public interface LocaleDAO { /** * @param id the unique ID of the entity - * @return the locale (never null) - * @throws AlfrescoRuntimeException if the ID provided is invalid + * @return the locale pair (never null) + * @throws DataIntegrityViolationException if the ID provided is invalid */ Pair getLocalePair(Long id); @@ -62,8 +62,7 @@ public interface LocaleDAO * Gets the locale ID for an existing instance or creates a new entity if * one doesn't exist. * - * @param id the locale to fetch or null to get or create the default - * locale. + * @param locale the locale to fetch or null to get or create the default locale. * @return the locale - never null */ Pair getOrCreateLocalePair(Locale locale); diff --git a/source/java/org/alfresco/repo/domain/locale/LocaleEntity.java b/source/java/org/alfresco/repo/domain/locale/LocaleEntity.java index 72c5898d41..426cce35fb 100644 --- a/source/java/org/alfresco/repo/domain/locale/LocaleEntity.java +++ b/source/java/org/alfresco/repo/domain/locale/LocaleEntity.java @@ -24,13 +24,7 @@ */ package org.alfresco.repo.domain.locale; -import java.util.Locale; -import java.util.concurrent.locks.ReentrantReadWriteLock; -import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock; -import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock; - -import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter; -import org.springframework.extensions.surf.util.I18NUtil; +import org.alfresco.util.EqualsHelper; /** @@ -49,15 +43,8 @@ public class LocaleEntity private Long version; private String localeStr; - private transient ReadLock refReadLock; - private transient WriteLock refWriteLock; - private transient Locale locale; - public LocaleEntity() { - ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); - refReadLock = lock.readLock(); - refWriteLock = lock.writeLock(); } @Override @@ -95,16 +82,7 @@ public class LocaleEntity } public void setLocaleStr(String localeStr) { - refWriteLock.lock(); - try - { - this.localeStr = localeStr; - this.locale = null; - } - finally - { - refWriteLock.unlock(); - } + this.localeStr = localeStr; } @Override @@ -123,77 +101,12 @@ public class LocaleEntity return false; } LocaleEntity that = (LocaleEntity) obj; - return (this.getLocale().equals(that.getLocale())); + return EqualsHelper.nullSafeEquals(this.localeStr, that.localeStr); } @Override public int hashCode() { - return getLocale().hashCode(); - } - - /** - * Lazily constructs a Locale instance referencing this entity - */ - public Locale getLocale() - { - // The default locale cannot be cached as it depends on the running thread's locale - if (localeStr == null || localeStr.equals(DEFAULT_LOCALE_SUBSTITUTE)) - { - return I18NUtil.getLocale(); - } - // first check if it is available - refReadLock.lock(); - try - { - if (locale != null) - { - return locale; - } - } - finally - { - refReadLock.unlock(); - } - // get write lock - refWriteLock.lock(); - try - { - // double check - if (locale == null ) - { - locale = DefaultTypeConverter.INSTANCE.convert(Locale.class, localeStr); - } - return locale; - } - finally - { - refWriteLock.unlock(); - } - } - - /** - * @param locale the locale to set or null to represent the default locale - */ - public void setLocale(Locale locale) - { - refWriteLock.lock(); - try - { - if (locale == null) - { - this.localeStr = DEFAULT_LOCALE_SUBSTITUTE; - this.locale = null; - } - else - { - this.localeStr = DefaultTypeConverter.INSTANCE.convert(String.class, locale); - this.locale = locale; - } - } - finally - { - refWriteLock.unlock(); - } + return localeStr == null ? 0 : localeStr.hashCode(); } }