diff options
author | John Ross | 2012-02-16 00:20:02 +0000 |
---|---|---|
committer | John Ross | 2012-02-16 23:33:15 +0000 |
commit | 55ab63744dc6fc94cdd73b1e099817b373322e52 (patch) | |
tree | ca84bb8884219a30a50d787494e31f2ab4638c6b | |
parent | eb1acae0736de5106cfef9295fc81b2d865ed909 (diff) | |
download | rt.equinox.bundles-55ab63744dc6fc94cdd73b1e099817b373322e52.tar.gz rt.equinox.bundles-55ab63744dc6fc94cdd73b1e099817b373322e52.tar.xz rt.equinox.bundles-55ab63744dc6fc94cdd73b1e099817b373322e52.zip |
Bug 371557: Don't instantiate resource bundle until the first time a translation is needed.v20120216-2333
4 files changed, 155 insertions, 128 deletions
diff --git a/bundles/org.eclipse.equinox.metatype/src/org/eclipse/equinox/metatype/impl/AttributeDefinitionImpl.java b/bundles/org.eclipse.equinox.metatype/src/org/eclipse/equinox/metatype/impl/AttributeDefinitionImpl.java index 1adcbd212..afc095059 100644 --- a/bundles/org.eclipse.equinox.metatype/src/org/eclipse/equinox/metatype/impl/AttributeDefinitionImpl.java +++ b/bundles/org.eclipse.equinox.metatype/src/org/eclipse/equinox/metatype/impl/AttributeDefinitionImpl.java @@ -44,6 +44,7 @@ public class AttributeDefinitionImpl extends LocalizationElement implements Equi } private AttributeDefinitionImpl(String id, String name, String description, int type, int cardinality, Object min, Object max, boolean isRequired, String localization, LogService logger, ExtendableHelper helper) { + super(localization); this._id = id; this._name = name; this._description = description; @@ -52,7 +53,6 @@ public class AttributeDefinitionImpl extends LocalizationElement implements Equi this._minValue = min; this._maxValue = max; this._isRequired = isRequired; - this._localization = localization; this.logger = logger; this.helper = helper; } @@ -62,7 +62,7 @@ public class AttributeDefinitionImpl extends LocalizationElement implements Equi */ public synchronized Object clone() { - AttributeDefinitionImpl ad = new AttributeDefinitionImpl(_id, _name, _description, _dataType, _cardinality, _minValue, _maxValue, _isRequired, _localization, logger, helper); + AttributeDefinitionImpl ad = new AttributeDefinitionImpl(_id, _name, _description, _dataType, _cardinality, _minValue, _maxValue, _isRequired, getLocalization(), logger, helper); if (_defaults != null) { ad.setDefaultValue(_defaults.clone()); diff --git a/bundles/org.eclipse.equinox.metatype/src/org/eclipse/equinox/metatype/impl/LocalizationElement.java b/bundles/org.eclipse.equinox.metatype/src/org/eclipse/equinox/metatype/impl/LocalizationElement.java index 46dce3155..fd4659dfb 100644 --- a/bundles/org.eclipse.equinox.metatype/src/org/eclipse/equinox/metatype/impl/LocalizationElement.java +++ b/bundles/org.eclipse.equinox.metatype/src/org/eclipse/equinox/metatype/impl/LocalizationElement.java @@ -10,35 +10,140 @@ *******************************************************************************/ package org.eclipse.equinox.metatype.impl; -import java.util.MissingResourceException; -import java.util.ResourceBundle; - -public class LocalizationElement { +import java.io.IOException; +import java.net.URL; +import java.util.*; +import org.osgi.framework.Bundle; +public abstract class LocalizationElement { public static final char KEY_SIGN = '%'; - String _localization = null; - ResourceBundle _rb; + public static final char LOCALE_SEP = '_'; - /** - * Internal method + /* + * Internal Method - to get resource bundle. */ - void setResourceBundle(ResourceBundle rb) { - this._rb = rb; + private static ResourceBundle getResourceBundle(String localization, String locale, Bundle bundle) { + // Determine the base name of the bundle localization property files. + // If the <MetaData> 'localization' attribute was not specified, + // use the Bundle-Localization manifest header value instead if it exists. + String resourceBase = localization != null ? localization : MetaTypeProviderImpl.getBundleLocalization(bundle); + + // There are seven searching candidates possible: + // baseName + + // "_" + language1 + "_" + country1 + "_" + variation1 + ".properties" + // or "_" + language1 + "_" + country1 + ".properties" + // or "_" + language1 + ".properties" + // or "_" + language2 + "_" + country2 + "_" + variation2 + ".properties" + // or "_" + language2 + "_" + country2 + ".properties" + // or "_" + language2 + ".properties" + // or "" + ".properties" + // + // Where language1[_country1[_variation1]] is the requested locale, + // and language2[_country2[_variation2]] is the default locale. + + String[] searchCandidates = new String[7]; + + // Candidates from passed locale: + if (locale != null && locale.length() > 0) { + int idx1_first = locale.indexOf(LOCALE_SEP); + if (idx1_first == -1) { + // locale has only language. + searchCandidates[2] = MetaTypeProviderImpl.RESOURCE_FILE_CONN + locale; + } else { + // locale has at least language and country. + searchCandidates[2] = MetaTypeProviderImpl.RESOURCE_FILE_CONN + locale.substring(0, idx1_first); + int idx1_second = locale.indexOf(LOCALE_SEP, idx1_first + 1); + if (idx1_second == -1) { + // locale just has both language and country. + searchCandidates[1] = MetaTypeProviderImpl.RESOURCE_FILE_CONN + locale; + } else { + // locale has language, country, and variation all. + searchCandidates[1] = MetaTypeProviderImpl.RESOURCE_FILE_CONN + locale.substring(0, idx1_second); + searchCandidates[0] = MetaTypeProviderImpl.RESOURCE_FILE_CONN + locale; + } + } + } + + // Candidates from Locale.getDefault(): + String defaultLocale = Locale.getDefault().toString(); + int idx2_first = defaultLocale.indexOf(LOCALE_SEP); + int idx2_second = defaultLocale.indexOf(LOCALE_SEP, idx2_first + 1); + if (idx2_second != -1) { + // default-locale is format of [language]_[country]_variation. + searchCandidates[3] = MetaTypeProviderImpl.RESOURCE_FILE_CONN + defaultLocale; + if (searchCandidates[3].equalsIgnoreCase(searchCandidates[0])) { + searchCandidates[3] = null; + } + } + if ((idx2_first != -1) && (idx2_second != idx2_first + 1)) { + // default-locale is format of [language]_country[_variation]. + searchCandidates[4] = MetaTypeProviderImpl.RESOURCE_FILE_CONN + ((idx2_second == -1) ? defaultLocale : defaultLocale.substring(0, idx2_second)); + if (searchCandidates[4].equalsIgnoreCase(searchCandidates[1])) { + searchCandidates[4] = null; + } + } + if ((idx2_first == -1) && (defaultLocale.length() > 0)) { + // default-locale has only language. + searchCandidates[5] = MetaTypeProviderImpl.RESOURCE_FILE_CONN + defaultLocale; + } else if (idx2_first > 0) { + // default-locale is format of language_[...]. + searchCandidates[5] = MetaTypeProviderImpl.RESOURCE_FILE_CONN + defaultLocale.substring(0, idx2_first); + } + if (searchCandidates[5] != null && searchCandidates[5].equalsIgnoreCase(searchCandidates[2])) { + searchCandidates[5] = null; + } + + // The final candidate. + searchCandidates[6] = ""; //$NON-NLS-1$ + + URL resourceUrl = null; + URL[] urls = null; + + for (int idx = 0; (idx < searchCandidates.length) && (resourceUrl == null); idx++) { + urls = (searchCandidates[idx] == null ? null : FragmentUtils.findEntries(bundle, resourceBase + searchCandidates[idx] + MetaTypeProviderImpl.RESOURCE_FILE_EXT)); + if (urls != null && urls.length > 0) + resourceUrl = urls[0]; + } + + if (resourceUrl != null) { + try { + return new PropertyResourceBundle(resourceUrl.openStream()); + } catch (IOException ioe) { + // Exception when creating PropertyResourceBundle object. + } + } + return null; + } + + private final String localization; + + // @GuardedBy("this") + private Bundle bundle; + // @GuardedBy("this") + private String locale; + // @GuardedBy("this") + private ResourceBundle resourceBundle; + + public LocalizationElement(String localization) { + this.localization = localization; + } + + String getLocalization() { + return localization; } /** * Method to get the localized text of inputed String. */ - String getLocalized(String key) { - + protected String getLocalized(String key) { if (key == null) { return null; } - if ((key.length() > 1) && (key.charAt(0) == KEY_SIGN)) { - if (_rb != null) { + ResourceBundle rb = getResourceBundle(); + if (rb != null) { try { - String transfered = _rb.getString(key.substring(1)); + String transfered = rb.getString(key.substring(1)); if (transfered != null) { return transfered; } @@ -52,4 +157,24 @@ public class LocalizationElement { } return key; } + + /* + * This method must be (and currently is) called after setLocaleAndBundle(String, Bundle). + * If the bundle is not set, an NPE will be generated by getResourceBundle(String, String, Bundle). + */ + protected synchronized ResourceBundle getResourceBundle() { + if (resourceBundle == null) { + resourceBundle = getResourceBundle(localization, locale, bundle); + } + return resourceBundle; + } + + /* + * This method must be (and currently is) called before getResourceBundle(). + * If the bundle is not set, an NPE will be generated by getResourceBundle(String, String, Bundle). + */ + protected synchronized void setLocaleAndBundle(String locale, Bundle bundle) { + this.locale = locale; + this.bundle = bundle; + } } diff --git a/bundles/org.eclipse.equinox.metatype/src/org/eclipse/equinox/metatype/impl/MetaTypeProviderImpl.java b/bundles/org.eclipse.equinox.metatype/src/org/eclipse/equinox/metatype/impl/MetaTypeProviderImpl.java index c814c03b5..bf45a886a 100644 --- a/bundles/org.eclipse.equinox.metatype/src/org/eclipse/equinox/metatype/impl/MetaTypeProviderImpl.java +++ b/bundles/org.eclipse.equinox.metatype/src/org/eclipse/equinox/metatype/impl/MetaTypeProviderImpl.java @@ -147,8 +147,8 @@ public class MetaTypeProviderImpl implements MetaTypeProvider { if (locale == null || locale.length() == 0) return false; - int idx_first = locale.indexOf(ObjectClassDefinitionImpl.LOCALE_SEP); - int idx_second = locale.lastIndexOf(ObjectClassDefinitionImpl.LOCALE_SEP); + int idx_first = locale.indexOf(LocalizationElement.LOCALE_SEP); + int idx_second = locale.lastIndexOf(LocalizationElement.LOCALE_SEP); if (idx_first == -1 && locale.length() == 2) // It is format of only language. return false; @@ -171,15 +171,17 @@ public class MetaTypeProviderImpl implements MetaTypeProvider { Enumeration<ObjectClassDefinitionImpl> ocds = _allPidOCDs.elements(); while (ocds.hasMoreElements()) { ObjectClassDefinitionImpl ocd = ocds.nextElement(); - if (ocd._localization != null && !localizationFiles.contains(ocd._localization)) - localizationFiles.add(ocd._localization); + String localization = ocd.getLocalization(); + if (localization != null && !localizationFiles.contains(localization)) + localizationFiles.add(localization); } // get all the localization resources for FPIDS ocds = _allFPidOCDs.elements(); while (ocds.hasMoreElements()) { ObjectClassDefinitionImpl ocd = ocds.nextElement(); - if (ocd._localization != null && !localizationFiles.contains(ocd._localization)) - localizationFiles.add(ocd._localization); + String localization = ocd.getLocalization(); + if (localization != null && !localizationFiles.contains(localization)) + localizationFiles.add(localization); } if (localizationFiles.size() == 0) localizationFiles.add(getBundleLocalization(_bundle)); diff --git a/bundles/org.eclipse.equinox.metatype/src/org/eclipse/equinox/metatype/impl/ObjectClassDefinitionImpl.java b/bundles/org.eclipse.equinox.metatype/src/org/eclipse/equinox/metatype/impl/ObjectClassDefinitionImpl.java index 57411278e..df7e9f374 100644 --- a/bundles/org.eclipse.equinox.metatype/src/org/eclipse/equinox/metatype/impl/ObjectClassDefinitionImpl.java +++ b/bundles/org.eclipse.equinox.metatype/src/org/eclipse/equinox/metatype/impl/ObjectClassDefinitionImpl.java @@ -22,8 +22,6 @@ import org.osgi.framework.Bundle; * Implementation of ObjectClassDefinition */ public class ObjectClassDefinitionImpl extends LocalizationElement implements EquinoxObjectClassDefinition, Cloneable { - public static final char LOCALE_SEP = '_'; - private static final Comparator<Icon> iconComparator = new Comparator<Icon>() { public int compare(Icon icon1, Icon icon2) { return icon1.getIconSize().compareTo(icon2.getIconSize()); @@ -52,11 +50,11 @@ public class ObjectClassDefinitionImpl extends LocalizationElement implements Eq * Constructor of class ObjectClassDefinitionImpl. */ public ObjectClassDefinitionImpl(String name, String description, String id, int type, String localization, ExtendableHelper helper) { + super(localization); this._name = name; this._id = id; this._description = description; this._type = type; - this._localization = localization; this.helper = helper; } @@ -65,7 +63,7 @@ public class ObjectClassDefinitionImpl extends LocalizationElement implements Eq */ public synchronized Object clone() { - ObjectClassDefinitionImpl ocd = new ObjectClassDefinitionImpl(_name, _description, _id, _type, _localization, helper); + ObjectClassDefinitionImpl ocd = new ObjectClassDefinitionImpl(_name, _description, _id, _type, getLocalization(), helper); for (int i = 0; i < _required.size(); i++) { AttributeDefinitionImpl ad = _required.elementAt(i); ocd.addAttributeDefinition((AttributeDefinitionImpl) ad.clone(), true); @@ -220,116 +218,18 @@ public class ObjectClassDefinitionImpl extends LocalizationElement implements Eq * Method to set the resource bundle for this OCD and all its ADs. */ void setResourceBundle(String assignedLocale, Bundle bundle) { - - _rb = getResourceBundle(assignedLocale, bundle); - + setLocaleAndBundle(assignedLocale, bundle); Enumeration<AttributeDefinitionImpl> allADReqs = _required.elements(); while (allADReqs.hasMoreElements()) { AttributeDefinitionImpl ad = allADReqs.nextElement(); - ad.setResourceBundle(_rb); + ad.setLocaleAndBundle(assignedLocale, bundle); } Enumeration<AttributeDefinitionImpl> allADOpts = _optional.elements(); while (allADOpts.hasMoreElements()) { AttributeDefinitionImpl ad = allADOpts.nextElement(); - ad.setResourceBundle(_rb); - } - } - - /* - * Internal Method - to get resource bundle. - */ - private ResourceBundle getResourceBundle(String locale, final Bundle bundle) { - // Determine the base name of the bundle localization property files. - // If the <MetaData> 'localization' attribute was not specified, - // use the Bundle-Localization manifest header value instead if it exists. - String resourceBase = _localization != null ? _localization : MetaTypeProviderImpl.getBundleLocalization(bundle); - - // There are seven searching candidates possible: - // baseName + - // "_" + language1 + "_" + country1 + "_" + variation1 + ".properties" - // or "_" + language1 + "_" + country1 + ".properties" - // or "_" + language1 + ".properties" - // or "_" + language2 + "_" + country2 + "_" + variation2 + ".properties" - // or "_" + language2 + "_" + country2 + ".properties" - // or "_" + language2 + ".properties" - // or "" + ".properties" - // - // Where language1[_country1[_variation1]] is the requested locale, - // and language2[_country2[_variation2]] is the default locale. - - String[] searchCandidates = new String[7]; - - // Candidates from passed locale: - if (locale != null && locale.length() > 0) { - int idx1_first = locale.indexOf(LOCALE_SEP); - if (idx1_first == -1) { - // locale has only language. - searchCandidates[2] = MetaTypeProviderImpl.RESOURCE_FILE_CONN + locale; - } else { - // locale has at least language and country. - searchCandidates[2] = MetaTypeProviderImpl.RESOURCE_FILE_CONN + locale.substring(0, idx1_first); - int idx1_second = locale.indexOf(LOCALE_SEP, idx1_first + 1); - if (idx1_second == -1) { - // locale just has both language and country. - searchCandidates[1] = MetaTypeProviderImpl.RESOURCE_FILE_CONN + locale; - } else { - // locale has language, country, and variation all. - searchCandidates[1] = MetaTypeProviderImpl.RESOURCE_FILE_CONN + locale.substring(0, idx1_second); - searchCandidates[0] = MetaTypeProviderImpl.RESOURCE_FILE_CONN + locale; - } - } - } - - // Candidates from Locale.getDefault(): - String defaultLocale = Locale.getDefault().toString(); - int idx2_first = defaultLocale.indexOf(LOCALE_SEP); - int idx2_second = defaultLocale.indexOf(LOCALE_SEP, idx2_first + 1); - if (idx2_second != -1) { - // default-locale is format of [language]_[country]_variation. - searchCandidates[3] = MetaTypeProviderImpl.RESOURCE_FILE_CONN + defaultLocale; - if (searchCandidates[3].equalsIgnoreCase(searchCandidates[0])) { - searchCandidates[3] = null; - } - } - if ((idx2_first != -1) && (idx2_second != idx2_first + 1)) { - // default-locale is format of [language]_country[_variation]. - searchCandidates[4] = MetaTypeProviderImpl.RESOURCE_FILE_CONN + ((idx2_second == -1) ? defaultLocale : defaultLocale.substring(0, idx2_second)); - if (searchCandidates[4].equalsIgnoreCase(searchCandidates[1])) { - searchCandidates[4] = null; - } - } - if ((idx2_first == -1) && (defaultLocale.length() > 0)) { - // default-locale has only language. - searchCandidates[5] = MetaTypeProviderImpl.RESOURCE_FILE_CONN + defaultLocale; - } else if (idx2_first > 0) { - // default-locale is format of language_[...]. - searchCandidates[5] = MetaTypeProviderImpl.RESOURCE_FILE_CONN + defaultLocale.substring(0, idx2_first); - } - if (searchCandidates[5] != null && searchCandidates[5].equalsIgnoreCase(searchCandidates[2])) { - searchCandidates[5] = null; - } - - // The final candidate. - searchCandidates[6] = ""; //$NON-NLS-1$ - - URL resourceUrl = null; - URL[] urls = null; - - for (int idx = 0; (idx < searchCandidates.length) && (resourceUrl == null); idx++) { - urls = (searchCandidates[idx] == null ? null : FragmentUtils.findEntries(bundle, resourceBase + searchCandidates[idx] + MetaTypeProviderImpl.RESOURCE_FILE_EXT)); - if (urls != null && urls.length > 0) - resourceUrl = urls[0]; + ad.setLocaleAndBundle(assignedLocale, bundle); } - - if (resourceUrl != null) { - try { - return new PropertyResourceBundle(resourceUrl.openStream()); - } catch (IOException ioe) { - // Exception when creating PropertyResourceBundle object. - } - } - return null; } public Map<String, String> getExtensionAttributes(String schema) { |