aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDirk Fauth2014-04-03 03:52:20 (EDT)
committerDirk Fauth2014-04-03 09:38:23 (EDT)
commite4053c58b0272bc2528dc9c7c995dbfa5608fbd2 (patch)
treefb39fffe8cb013f2a5c18fcd8e3203093c349638
parent9c85295f0b0431a27bf4108ae28cf25fc3f7dec4 (diff)
downloadeclipse.platform.runtime-e4053c58b0272bc2528dc9c7c995dbfa5608fbd2.zip
eclipse.platform.runtime-e4053c58b0272bc2528dc9c7c995dbfa5608fbd2.tar.gz
eclipse.platform.runtime-e4053c58b0272bc2528dc9c7c995dbfa5608fbd2.tar.bz2
Bug 431308 - Invalid locale kills eclipse startuprefs/changes/69/24369/2
Change-Id: I9cfae7152240a1012c89009a4fec6e90c4d4c50a Signed-off-by: Dirk Fauth <dirk.fauth@googlemail.com>
-rw-r--r--bundles/org.eclipse.e4.core.services/src/org/eclipse/e4/core/internal/services/BundleTranslationProvider.java19
-rw-r--r--bundles/org.eclipse.e4.core.services/src/org/eclipse/e4/core/internal/services/ResourceBundleHelper.java47
-rw-r--r--bundles/org.eclipse.e4.core.services/src/org/eclipse/e4/core/internal/services/TranslationObjectSupplier.java11
3 files changed, 54 insertions, 23 deletions
diff --git a/bundles/org.eclipse.e4.core.services/src/org/eclipse/e4/core/internal/services/BundleTranslationProvider.java b/bundles/org.eclipse.e4.core.services/src/org/eclipse/e4/core/internal/services/BundleTranslationProvider.java
index 9b58a57..3d37dab 100644
--- a/bundles/org.eclipse.e4.core.services/src/org/eclipse/e4/core/internal/services/BundleTranslationProvider.java
+++ b/bundles/org.eclipse.e4.core.services/src/org/eclipse/e4/core/internal/services/BundleTranslationProvider.java
@@ -15,6 +15,7 @@ import java.util.ResourceBundle;
import javax.inject.Inject;
import org.eclipse.e4.core.services.translation.ResourceBundleProvider;
import org.eclipse.e4.core.services.translation.TranslationService;
+import org.osgi.service.log.LogService;
public class BundleTranslationProvider extends TranslationService {
@@ -26,9 +27,21 @@ public class BundleTranslationProvider extends TranslationService {
if (provider == null)
return key;
- ResourceBundle resourceBundle = ResourceBundleHelper.getResourceBundleForUri(
- contributorURI, ResourceBundleHelper.toLocale(locale), provider);
- return getResourceString(key, resourceBundle);
+ try {
+ ResourceBundle resourceBundle = ResourceBundleHelper.getResourceBundleForUri(
+ contributorURI, ResourceBundleHelper.toLocale(locale), provider);
+ return getResourceString(key, resourceBundle);
+ } catch (Exception e) {
+ // an error occurred on trying to retrieve the translation for the given key
+ // for improved fault tolerance we will log the error and return the key
+ LogService logService = ServicesActivator.getDefault().getLogService();
+ if (logService != null)
+ logService.log(LogService.LOG_ERROR,
+ "Error retrieving the translation for key=" + key //$NON-NLS-1$
+ + " and contributorURI=" + contributorURI, e); //$NON-NLS-1$
+
+ return key;
+ }
}
}
diff --git a/bundles/org.eclipse.e4.core.services/src/org/eclipse/e4/core/internal/services/ResourceBundleHelper.java b/bundles/org.eclipse.e4.core.services/src/org/eclipse/e4/core/internal/services/ResourceBundleHelper.java
index 8b9f338..1a13c11 100644
--- a/bundles/org.eclipse.e4.core.services/src/org/eclipse/e4/core/internal/services/ResourceBundleHelper.java
+++ b/bundles/org.eclipse.e4.core.services/src/org/eclipse/e4/core/internal/services/ResourceBundleHelper.java
@@ -477,14 +477,18 @@ public class ResourceBundleHelper {
*
* @param str
* the locale String to convert
- * @return a Locale that matches the specified locale String or <code>null</code> if the
- * specified String is <code>null</code>
- * @throws IllegalArgumentException
- * if the String is an invalid format
+ * @return a Locale that matches the specified locale String. If the given input String is
+ * <code>null</code> or can not be parsed because of an invalid format,
+ * {@link Locale#getDefault()} will be returned.
*/
public static Locale toLocale(String str) {
+ LogService logService = ServicesActivator.getDefault().getLogService();
+
if (str == null) {
- return null;
+ if (logService != null)
+ logService.log(LogService.LOG_ERROR, "Given locale String is null"
+ + " - Default Locale will be used instead."); //$NON-NLS-1$
+ return Locale.getDefault();
}
String language = ""; //$NON-NLS-1$
@@ -494,15 +498,24 @@ public class ResourceBundleHelper {
String[] localeParts = str.split("_"); //$NON-NLS-1$
if (localeParts.length == 0 || localeParts.length > 3
|| (localeParts.length == 1 && localeParts[0].length() == 0)) {
- throw new IllegalArgumentException("Invalid locale format: " + str); //$NON-NLS-1$
+ if (logService != null)
+ logService.log(LogService.LOG_ERROR, "Invalid locale format: " + str
+ + " - Default Locale will be used instead."); //$NON-NLS-1$
+ return Locale.getDefault();
} else {
if (localeParts[0].length() == 1 || localeParts[0].length() > 2) {
- throw new IllegalArgumentException("Invalid locale format: " + str); //$NON-NLS-1$
+ if (logService != null)
+ logService.log(LogService.LOG_ERROR, "Invalid locale format: " + str
+ + " - Default Locale will be used instead."); //$NON-NLS-1$
+ return Locale.getDefault();
} else if (localeParts[0].length() == 2) {
char ch0 = localeParts[0].charAt(0);
char ch1 = localeParts[0].charAt(1);
if (ch0 < 'a' || ch0 > 'z' || ch1 < 'a' || ch1 > 'z') {
- throw new IllegalArgumentException("Invalid locale format: " + str); //$NON-NLS-1$
+ if (logService != null)
+ logService.log(LogService.LOG_ERROR, "Invalid locale format: " + str
+ + " - Default Locale will be used instead."); //$NON-NLS-1$
+ return Locale.getDefault();
}
}
@@ -510,12 +523,18 @@ public class ResourceBundleHelper {
if (localeParts.length > 1) {
if (localeParts[1].length() == 1 || localeParts[1].length() > 2) {
- throw new IllegalArgumentException("Invalid locale format: " + str); //$NON-NLS-1$
+ if (logService != null)
+ logService.log(LogService.LOG_ERROR, "Invalid locale format: " + str
+ + " - Only language part will be used to create the Locale."); //$NON-NLS-1$
+ return new Locale(language);
} else if (localeParts[1].length() == 2) {
char ch3 = localeParts[1].charAt(0);
char ch4 = localeParts[1].charAt(1);
if (ch3 < 'A' || ch3 > 'Z' || ch4 < 'A' || ch4 > 'Z') {
- throw new IllegalArgumentException("Invalid locale format: " + str); //$NON-NLS-1$
+ if (logService != null)
+ logService.log(LogService.LOG_ERROR, "Invalid locale format: " + str
+ + " - Only language part will be used to create the Locale."); //$NON-NLS-1$
+ return new Locale(language);
}
}
@@ -524,7 +543,13 @@ public class ResourceBundleHelper {
if (localeParts.length == 3) {
if (localeParts[0].length() == 0 && localeParts[1].length() == 0) {
- throw new IllegalArgumentException("Invalid locale format: " + str); //$NON-NLS-1$
+ if (logService != null)
+ logService
+ .log(LogService.LOG_ERROR,
+ "Invalid locale format: "
+ + str
+ + " - Only language and country part will be used to create the Locale."); //$NON-NLS-1$
+ return new Locale(language, country);
}
variant = localeParts[2];
}
diff --git a/bundles/org.eclipse.e4.core.services/src/org/eclipse/e4/core/internal/services/TranslationObjectSupplier.java b/bundles/org.eclipse.e4.core.services/src/org/eclipse/e4/core/internal/services/TranslationObjectSupplier.java
index 0da507f..a58e01b 100644
--- a/bundles/org.eclipse.e4.core.services/src/org/eclipse/e4/core/internal/services/TranslationObjectSupplier.java
+++ b/bundles/org.eclipse.e4.core.services/src/org/eclipse/e4/core/internal/services/TranslationObjectSupplier.java
@@ -34,7 +34,6 @@ import org.eclipse.e4.core.services.translation.TranslationService;
import org.eclipse.osgi.service.localization.BundleLocalization;
import org.osgi.service.log.LogService;
-@SuppressWarnings({ "restriction" })
public class TranslationObjectSupplier extends ExtendedObjectSupplier {
private static LogService logService = ServicesActivator.getDefault().getLogService();
@@ -88,17 +87,11 @@ public class TranslationObjectSupplier extends ExtendedObjectSupplier {
try {
this.locale = locale == null ? Locale.getDefault() : ResourceBundleHelper
.toLocale(locale);
- } catch (IllegalArgumentException e) {
- // parsing the locale String to a Locale failed because of invalid String, use the
- // default locale
- if (logService != null)
- logService.log(LogService.LOG_ERROR, e.getMessage()
- + " - Default Locale will be used instead."); //$NON-NLS-1$
- this.locale = Locale.getDefault();
} catch (Exception e) {
// parsing the locale String to a Locale failed, so we use the default Locale
if (logService != null)
- logService.log(LogService.LOG_ERROR, "Invalid locale", e); //$NON-NLS-1$
+ logService.log(LogService.LOG_ERROR, "Invalid locale format: " + locale //$NON-NLS-1$
+ + " - Default Locale will be used instead.", e); //$NON-NLS-1$
this.locale = Locale.getDefault();
}