Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

G11n java client [BUG 754] Performance impact on the very first L3 fetch for a non-supported locale #823

Merged
Merged
Show file tree
Hide file tree
Changes from 215 commits
Commits
Show all changes
224 commits
Select commit Hold shift + click to select a range
f4c1214
Loading configuration from the JSON configuration file
jessiejuachon Mar 11, 2020
1a4d95e
Removing code that is not yet ready to be used from the sample applic…
jessiejuachon Mar 11, 2020
5e72b73
fixing failing code scan
jessiejuachon Mar 11, 2020
73d85ca
Adding a cache properties map to hold cache details such as etag and …
jessiejuachon Mar 12, 2020
2a38107
Removing white space
jessiejuachon Mar 12, 2020
c7f5d4f
Fixing failing unit test
jessiejuachon Mar 12, 2020
056891a
Parse headers, response code, response message from an HTTP response …
jessiejuachon Mar 12, 2020
a2ba98b
Storing response headers in Map of cache properties
jessiejuachon Mar 17, 2020
17978bd
Fixing failing unit test due to NullPointerException when doing Map.p…
jessiejuachon Mar 17, 2020
6c69170
Changes after code review.
jessiejuachon Mar 17, 2020
41a8aa1
Removing exception handling for now
jessiejuachon Mar 18, 2020
dd2455d
Deprecating VIPCfg.initialize; cleaning up code
jessiejuachon Mar 19, 2020
e26d372
Storing the http response code in the cache
jessiejuachon Mar 20, 2020
01ab69e
Using cached etag as if-none-match request header value in the reques…
jessiejuachon Mar 25, 2020
750c9d2
Handling 404 response from Singleton service
jessiejuachon Mar 26, 2020
529e5cc
Using HTTP repsonse's Cache-Control max-age and timestamp to dertermi…
jessiejuachon Mar 26, 2020
b40fcf9
Moving constants to URLUtils
jessiejuachon Mar 26, 2020
fa123f9
Fixing failing test
jessiejuachon Mar 27, 2020
30b2054
Fixing failing code scan
jessiejuachon Mar 27, 2020
0c69787
Merge branch 'g11n-java-client' into g11n-java-client
Xiaochao8 Mar 27, 2020
ffc0a74
Cleaning up import in ComponentService,java
jessiejuachon Mar 30, 2020
d8450e4
Cleaning up code - caching/expiry
jessiejuachon Mar 30, 2020
d7f7641
Merge branch 'g11n-java-client' of https://github.com/jessiejuachon/s…
jessiejuachon Mar 30, 2020
0dcd827
Adding tests for new caching workflow; enabling old caching workflow …
jessiejuachon Mar 31, 2020
d06adb3
Fixing a failing test
jessiejuachon Mar 31, 2020
48c8558
Adding mock server response
jessiejuachon Mar 31, 2020
4e45bed
Removing unused file sampleconfig.json
jessiejuachon Mar 31, 2020
6a40dc8
Changes from code review
jessiejuachon Mar 31, 2020
4c3c12d
Value of VIPCfg.cacheExpiredTime is -1 when value is not set in confi…
jessiejuachon Mar 31, 2020
7e0d939
Changing a LinkedHashMap to a HashMap because insertion order does no…
jessiejuachon Apr 2, 2020
baef0d0
Adding CacheItem object to contain map of data and a another map for …
jessiejuachon Apr 3, 2020
02a96e9
Fixing failing unit test
jessiejuachon Apr 3, 2020
78d88f7
Fixing code scan issues
jessiejuachon Apr 3, 2020
655c223
Code cleanup
jessiejuachon Apr 3, 2020
8746915
Lazily instantiating cacheProps; code clean up and comments
jessiejuachon Apr 5, 2020
897a03c
Changes after code review
jessiejuachon Apr 7, 2020
3a4e183
Adding header to CacheItem.java
jessiejuachon Apr 7, 2020
b08feb4
Fixing code san issue. Synchronize on a final field
jessiejuachon Apr 7, 2020
8f57e24
Fixing code scan bug
jessiejuachon Apr 7, 2020
72c8799
Changes after code review
jessiejuachon Apr 7, 2020
92edfc8
Updating CasheService.isContainComponent to return false if cache key…
jessiejuachon Apr 7, 2020
2042554
HttpRequester if-none-match header and other customized headers are a…
jessiejuachon Apr 8, 2020
ae707ff
Code clean up; keep old caching expiration logic
jessiejuachon Apr 8, 2020
82818d5
Fixing code scan bug
jessiejuachon Apr 8, 2020
31d4ff6
Merge branch 'g11n-java-client' into g11n-java-client
jessiejuachon Apr 8, 2020
c9f83d7
code clean up
jessiejuachon Apr 9, 2020
78dcc1c
Removing response code and response msg from cache; Passing cacheItem…
jessiejuachon Apr 9, 2020
9281c64
Changes after code review
jessiejuachon Apr 10, 2020
2657762
Adding header to CacheItem.java file
jessiejuachon Apr 10, 2020
9e80f09
Fixing failing unit test
jessiejuachon Apr 10, 2020
68467f3
Changes after code review
jessiejuachon Apr 10, 2020
d67c72e
Fixing code scan bugs
jessiejuachon Apr 10, 2020
8591973
Not storing anything in cache if response is neither 200 nor 304
jessiejuachon Apr 13, 2020
22a5b29
Loading messages from specified offline resource bundles
jessiejuachon Apr 15, 2020
ce2488b
Merge branch 'g11n-java-client' of https://github.com/vmware/singleto…
jessiejuachon Apr 15, 2020
1092963
Adding header
jessiejuachon Apr 15, 2020
35d1d97
Fixing failing unie test
jessiejuachon Apr 15, 2020
ada17f1
Code changes after review; code clean up
jessiejuachon Apr 16, 2020
fc60eef
Using java.nio.file.Paths
jessiejuachon Apr 17, 2020
3e70449
Offline mode as fallback when service fetch fails
jessiejuachon Apr 17, 2020
609d8e4
Resetting VIPCfg and I18nFactory instances for each test
jessiejuachon Apr 17, 2020
dd19b1c
Resetting VIPCfg and I18nFactory after test
jessiejuachon Apr 17, 2020
7389f8b
Fixing tests
jessiejuachon Apr 17, 2020
5b92e44
Limit to wiremock test logging
jessiejuachon Apr 20, 2020
dfb41b5
Decreasing unit test log
jessiejuachon Apr 20, 2020
21a7105
this test is timing out and trying to connect 70+ times
jessiejuachon Apr 21, 2020
84343d5
Adding comments to code
jessiejuachon Apr 21, 2020
be8e443
Merge branch 'g11n-java-client' of https://github.com/vmware/singleto…
jessiejuachon Apr 21, 2020
b805d36
Adding timestamp to cache fromn local bundle
jessiejuachon Apr 21, 2020
17bc16c
Deprecating methods in TranslationMessage; adding new method getMessage
jessiejuachon Apr 23, 2020
15da010
Added tests for TranslationMessage.getMessage
jessiejuachon Apr 24, 2020
631de86
Fixing test
jessiejuachon Apr 24, 2020
ffd9adb
Fixing test
jessiejuachon Apr 24, 2020
c00f7ae
Fixing test
jessiejuachon Apr 24, 2020
d0504c4
Removing comment field because source collection is not supported any…
jessiejuachon Apr 24, 2020
79b18d0
Code clean up
jessiejuachon Apr 24, 2020
ec9f5db
Changes from code review
jessiejuachon Apr 24, 2020
2e8bd36
Code clean up
jessiejuachon Apr 24, 2020
4e44a8c
Optional SourceOpt in initialization
jessiejuachon Apr 26, 2020
de5c4ee
Fixing test
jessiejuachon Apr 27, 2020
406cb2a
Adding comments to code
jessiejuachon Apr 28, 2020
c80696a
Adding comment for javadoc
jessiejuachon Apr 28, 2020
79d111e
Cleaning up code
jessiejuachon Apr 28, 2020
24d8d5d
Code clean up
jessiejuachon Apr 28, 2020
23e62b6
Fixing test
jessiejuachon Apr 28, 2020
369643f
Removing source message fallback from this PR
jessiejuachon Apr 29, 2020
4e67e25
Merge branch 'g11n-java-client' into g11n-java-client
jessiejuachon Apr 29, 2020
636b5b4
Default locale in config file instead of hardcoded; get supported loc…
jessiejuachon May 5, 2020
71a87e5
Merge branch 'g11n-java-client' into g11n-java-client
jessiejuachon May 5, 2020
b9a90e3
Fixing test
jessiejuachon May 5, 2020
060666d
Fixing tests
jessiejuachon May 5, 2020
52e3695
Default locale to Locale.ENGLISH if not set in config file
jessiejuachon May 5, 2020
2858ebc
Changed after code review: code clean up, bug fix
jessiejuachon May 6, 2020
bbe3047
Update src/main/java/com/vmware/vipclient/i18n/messages/api/opt/local…
jessiejuachon May 8, 2020
fa56388
Update src/main/java/com/vmware/vipclient/i18n/messages/api/opt/local…
jessiejuachon May 8, 2020
08cd96d
code clean up, error logging, bug fix
jessiejuachon May 8, 2020
e89ff56
Merge branch 'g11n-java-client' of https://github.com/vmware/singleto…
jessiejuachon May 8, 2020
119aebd
code clean up
jessiejuachon May 8, 2020
e8a937d
code clean up
jessiejuachon May 11, 2020
9d95cd1
Using messages_source.json for source locale messages.
jessiejuachon May 12, 2020
06b60bb
code clean up
jessiejuachon May 12, 2020
51b4dfa
Merge branch 'g11n-java-client' of https://github.com/vmware/singleto…
jessiejuachon May 13, 2020
778a7a1
Commenting out a test
jessiejuachon May 13, 2020
077f19b
code clean up
jessiejuachon May 13, 2020
9424346
code clean up
jessiejuachon May 13, 2020
c25a676
Error logging when fetch failed
jessiejuachon May 13, 2020
db641b9
bug fix
jessiejuachon May 13, 2020
5cd06d6
code clean up
jessiejuachon May 14, 2020
6915f11
Updating sample application
jessiejuachon May 15, 2020
a64be3f
Merge branch 'g11n-java-client' into g11n-java-client
jessiejuachon May 17, 2020
7128194
fixing failing smoke test
jessiejuachon May 19, 2020
c4a388c
Merge branch 'g11n-java-client' of https://github.com/jessiejuachon/s…
jessiejuachon May 19, 2020
5ecea93
code clean up
jessiejuachon May 19, 2020
4c5b0a1
fixing code scan
jessiejuachon May 19, 2020
067d19d
Fixing code scan
jessiejuachon May 19, 2020
711ce80
Adding prodMode configuration
jessiejuachon May 19, 2020
b7dabdb
Merge branch 'g11n-java-client' into g11n-java-client
jessiejuachon May 19, 2020
d8543ec
code clean up
jessiejuachon May 20, 2020
e8094f4
Setting default prodMode to true
jessiejuachon May 21, 2020
9a5ef77
Removing prodMode configuration and logic. Throwing exception if sour…
jessiejuachon Jun 17, 2020
5011c38
Updating javadoc
jessiejuachon Jun 17, 2020
33432b1
Fixing bug: Fallback shall apply to both TranslationMessage.getMessag…
jessiejuachon Jun 17, 2020
c9d3cae
Adding test for Translateion.getMessages, locale not supported
jessiejuachon Jun 17, 2020
adb40ec
code comments
jessiejuachon Jun 17, 2020
7ab5c59
code clean up
jessiejuachon Jun 19, 2020
3c10089
Fix issue: https://github.com/vmware/singleton/issues/622
jessiejuachon Jun 19, 2020
76105b7
code clean up
jessiejuachon Jun 29, 2020
49c5648
code clean up
jessiejuachon Jun 29, 2020
d543e99
code clean up
jessiejuachon Jun 29, 2020
3ecf958
code clean up
jessiejuachon Jun 29, 2020
e6a07a1
code clean up
jessiejuachon Jun 29, 2020
89ff088
code clean up
jessiejuachon Jun 29, 2020
f1356ab
code clean up
jessiejuachon Jun 29, 2020
c63bc2d
Merge branch 'g11n-java-client' of https://github.com/vmware/singleto…
jessiejuachon Jul 15, 2020
beab148
Fix for https://github.com/vmware/singleton/issues/661
jessiejuachon Jul 15, 2020
2cd7713
adding license header
jessiejuachon Jul 16, 2020
e13b694
changes after code review
jessiejuachon Jul 16, 2020
f1f70b3
Update src/main/java/com/vmware/vipclient/i18n/messages/api/opt/local…
jessiejuachon Jul 20, 2020
fe535dc
Update src/main/java/com/vmware/vipclient/i18n/messages/service/Produ…
jessiejuachon Jul 20, 2020
0d22b9f
changes after code review
jessiejuachon Jul 20, 2020
4870782
changes after code review
jessiejuachon Jul 20, 2020
f451436
fix for issues: 664, 662, 686
jessiejuachon Jul 22, 2020
89d7b0d
code clean up
jessiejuachon Jul 23, 2020
ec6e693
code clean up
jessiejuachon Jul 24, 2020
65f1d08
Adding LocaleDTO
jessiejuachon Jul 24, 2020
60fb3b3
code clean up
jessiejuachon Jul 24, 2020
a698f20
code clean up
jessiejuachon Jul 24, 2020
eede3b5
changes after code review
jessiejuachon Jul 31, 2020
2dbfc72
Bug fix https://github.com/vmware/singleton/issues/604, https://githu…
jessiejuachon Aug 3, 2020
e63ab94
Merge branch 'g11n-java-client' into g11n-java-client
jessiejuachon Aug 3, 2020
ae1dccd
code clean up
jessiejuachon Aug 3, 2020
6fbaab4
Mapping to the fallback locale's cache
jessiejuachon Aug 5, 2020
ff99a37
code clean up
jessiejuachon Aug 5, 2020
271ee08
changes after code review
jessiejuachon Aug 7, 2020
230e21c
code clean up
jessiejuachon Aug 12, 2020
55764a7
code clean up
jessiejuachon Aug 12, 2020
e9214cb
code clean up
jessiejuachon Aug 12, 2020
beaa5ef
code clean up
jessiejuachon Aug 12, 2020
673b8fd
code clean up
jessiejuachon Aug 12, 2020
e8bb5b7
Fixing bug on reading offline bundles in jar file.
jessiejuachon Aug 19, 2020
fe82e31
Merge branch 'g11n-java-client' of https://github.com/vmware/singleto…
jessiejuachon Aug 19, 2020
57aeb9b
sample shared library
jessiejuachon Aug 21, 2020
a7f6d36
sample application with shared library
jessiejuachon Aug 21, 2020
423090c
Merge branch 'g11n-java-client' of https://github.com/vmware/singleto…
jessiejuachon Aug 21, 2020
4723dbf
Adding license header
jessiejuachon Aug 21, 2020
203dbb8
Fix for https://github.com/vmware/singleton/issues/746 MsgOriginsQueu…
jessiejuachon Aug 31, 2020
0a755b1
Merge branch 'g11n-java-client' of https://github.com/vmware/singleto…
jessiejuachon Aug 31, 2020
ed60327
Merge branch 'g11n-java-client' of https://github.com/vmware/singleto…
jessiejuachon Sep 1, 2020
b76346c
Fix for bug 781 - L3's CacheService.getSupportedLocalesFromCache is i…
jessiejuachon Sep 14, 2020
a6aa09e
code clean up
jessiejuachon Sep 15, 2020
550de8c
code clean up
jessiejuachon Sep 15, 2020
1599f5c
Fix for issue 781: CacheService.getCacheOfComponent cannot get "match…
jessiejuachon Sep 17, 2020
8f0a9dd
adding test
jessiejuachon Sep 17, 2020
24b8e3a
code clean up
jessiejuachon Sep 17, 2020
22b2159
code clean up
jessiejuachon Sep 17, 2020
4247c99
code clean up
jessiejuachon Sep 17, 2020
88a0a4c
code clean up
jessiejuachon Sep 17, 2020
3d82802
code clean up
jessiejuachon Sep 17, 2020
444b0bf
code clean up
jessiejuachon Sep 17, 2020
d975d97
adding test
jessiejuachon Sep 18, 2020
d7a9a44
fixing test
jessiejuachon Sep 18, 2020
534137a
fixing test
jessiejuachon Sep 18, 2020
7e540e9
fixing test
jessiejuachon Sep 18, 2020
8899bfe
fixing test
jessiejuachon Sep 18, 2020
5ce2453
code clean up
jessiejuachon Sep 18, 2020
c5e9620
code clean up
jessiejuachon Sep 18, 2020
d2905e3
code cleanup
jessiejuachon Sep 18, 2020
8b1e4e9
code clean up
jessiejuachon Sep 18, 2020
303957e
Update src/main/java/com/vmware/vipclient/i18n/messages/service/Cache…
jessiejuachon Sep 18, 2020
f238d95
adding comments to code
jessiejuachon Sep 19, 2020
d5dffd6
clean up code
jessiejuachon Sep 21, 2020
f096330
Merge branch 'g11n-java-client-781' of github.com:jessiejuachon/singl…
jessiejuachon Sep 21, 2020
ef3c67f
code clean up
jessiejuachon Sep 22, 2020
0c6f313
code clean up
jessiejuachon Sep 22, 2020
cfdc289
Adding list of supported locales used by L3 in cache
jessiejuachon Sep 23, 2020
67d6c89
Merge branch 'g11n-java-client' of github.com:vmware/singleton into g…
jessiejuachon Sep 23, 2020
23d7ec8
Merge branch 'g11n-java-client' of github.com:jessiejuachon/singleton…
jessiejuachon Sep 23, 2020
7d51498
Removing logic of adding a cacheItem with an empty dataMap to cache
jessiejuachon Sep 25, 2020
943bb14
Merge branch 'g11n-java-client' of github.com:jessiejuachon/singleton…
jessiejuachon Sep 25, 2020
3f488e0
Merge branch 'g11n-java-client' of github.com:jessiejuachon/singleton…
jessiejuachon Sep 25, 2020
680aab4
code clean up
jessiejuachon Sep 25, 2020
b10eec5
code clean up
jessiejuachon Sep 25, 2020
610ffd0
code clean up
jessiejuachon Sep 25, 2020
01e8222
Removing locale property from MessageCacheItem
jessiejuachon Sep 25, 2020
e78a92e
Update src/main/java/com/vmware/vipclient/i18n/messages/service/Compo…
jessiejuachon Sep 29, 2020
ce6cd77
Merge branch 'g11n-java-client' of github.com:jessiejuachon/singleton…
jessiejuachon Sep 29, 2020
138e1f9
changes based on review comments
jessiejuachon Sep 29, 2020
b1a824a
Enablling locale fallback for matched locale
jessiejuachon Sep 29, 2020
6c4bb64
merging changes
jessiejuachon Sep 30, 2020
a4ddbdd
Merge branch 'g11n-java-client' into g11n-java-client-754
jessiejuachon Oct 5, 2020
a97ccfc
Merge branch 'g11n-java-client' of github.com:vmware/singleton into g…
jessiejuachon Oct 13, 2020
eb8b625
Fixing bug 754 - Performance impact on the very first L3 fetch for a …
jessiejuachon Oct 13, 2020
dd03ac8
Merge branch 'g11n-java-client-754' of github.com:jessiejuachon/singl…
jessiejuachon Oct 13, 2020
f8ce5c1
more test cases
jessiejuachon Oct 14, 2020
c2b1fd9
fixing intermittently failing test
jessiejuachon Oct 14, 2020
6bbdfff
code clean up for readability
jessiejuachon Oct 16, 2020
d8e70b7
fixing incorrect if statement
jessiejuachon Oct 16, 2020
7d8f943
code optimization
jessiejuachon Oct 16, 2020
b3bb30a
Update src/main/java/com/vmware/vipclient/i18n/util/LocaleUtility.java
jessiejuachon Oct 20, 2020
bdbae09
rolling back change
jessiejuachon Oct 20, 2020
03bf400
changing Chinese locale matching rule
jessiejuachon Oct 20, 2020
71404e3
Merge branch 'g11n-java-client' into g11n-java-client-754
jessiejuachon Oct 21, 2020
236ab56
correcting incorrect merge
jessiejuachon Oct 21, 2020
7c2535c
Do Locale matching first in Componentservice.getMessages
jessiejuachon Oct 23, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
package com.vmware.vipclient.i18n.messages.service;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.Locale;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

import com.vmware.vipclient.i18n.VIPCfg;
import com.vmware.vipclient.i18n.base.DataSourceEnum;
Expand Down Expand Up @@ -108,38 +105,48 @@ public MessageCacheItem getMessages(Iterator<Locale> fallbackLocalesIter) {
}
} else { // Item is not in cache.
ProductService ps = new ProductService(dto);
if (!ps.isSupportedLocale(Locale.forLanguageTag(dto.getLocale()))) { // Requested locale is not supported
Locale matchedLocale = LocaleUtility.pickupLocaleFromList(new LinkedList<>(ps.getSupportedLocales()), Locale.forLanguageTag(dto.getLocale()));
if (ps.isSupportedLocale(matchedLocale)) { // Requested locale matches a supported locale (eg. requested locale "fr_CA matches supported locale "fr")
Locale locale = Locale.forLanguageTag(dto.getLocale());
if (ps.isSupportedLocale(locale) || VIPCfg.getInstance().isPseudo()) {
cacheItem = createCacheItem(); // Fetch for the requested locale from data store, create cacheItem and store in cache
if (cacheItem.getCachedData().isEmpty()) // Failed to fetch messages for the requested locale
cacheItem = getFallbackLocaleMessages(fallbackLocalesIter);
} else { // Requested locale is not supported
Locale matchedLocale = LocaleUtility.pickupLocaleFromList(ps.getSupportedLocales(), locale);
if (matchedLocale != null) { // Requested locale matches a supported locale (eg. requested locale "fr_CA matches supported locale "fr")
MessagesDTO matchedLocaleDTO = new MessagesDTO(dto.getComponent(), matchedLocale.toLanguageTag(), dto.getProductID(), dto.getVersion());
return new ComponentService(matchedLocaleDTO).getMessages();
}
cacheItem = new ComponentService(matchedLocaleDTO).getMessages();
} else // Requested locale is not supported and does not match any supported locales
cacheItem = getFallbackLocaleMessages(fallbackLocalesIter);
}

//Create and store cacheItem for the requested locale
cacheItem = createCacheItem(fallbackLocalesIter);
}
return cacheItem;
}

/**
* Gets the messages in the next fallback locale by passing the next fallback locale DTO to a new instance of ComponentService
* and then invoking {@link #getMessages(Iterator)}.
* @param fallbackLocalesIter The fallback locale queue to use in case of failure. If null, no locale fallback will be applied.
*/
private MessageCacheItem getFallbackLocaleMessages(Iterator<Locale> fallbackLocalesIter) {
if (!dto.getLocale().equals(ConstantsKeys.SOURCE) && fallbackLocalesIter != null && fallbackLocalesIter.hasNext()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to judge dto locale is same as the fallback locale?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not necessary, but it is an improvement. If we do not check for dto.getLocale == fallback and it happens, it will just try to fetch for the dto locale again and will fail again, and move on to the next item in the queue. Nonetheless, I have added the check for optimization.

Moreover, I hsve removed the following restriction to allow falling back to SOURCE messages:
!dto.getLocale().equals(ConstantsKeys.SOURCE)

// Use MessageCacheItem of the next fallback locale.
MessagesDTO fallbackLocaleDTO = new MessagesDTO(dto.getComponent(), fallbackLocalesIter.next().toLanguageTag(), dto.getProductID(), dto.getVersion());
return new ComponentService(fallbackLocaleDTO).getMessages(fallbackLocalesIter);
}
return new MessageCacheItem();
}

/**
* Creates a new MessageCacheItem for the DTO and stores it in cache.
*
* @param fallbackLocalesIter The fallback locale queue to use in case of failure. If null, no locale fallback will be applied.
*
*/
private MessageCacheItem createCacheItem(Iterator<Locale> fallbackLocalesIter) {
private MessageCacheItem createCacheItem() {
CacheService cacheService = new CacheService(dto);
// Create a new cacheItem object to be stored in cache
MessageCacheItem cacheItem = new MessageCacheItem();

refreshCacheItem(cacheItem, VIPCfg.getInstance().getMsgOriginsQueue().iterator());
if (!cacheItem.getCachedData().isEmpty()) {
cacheService.addCacheOfComponent(cacheItem);
} else if (!dto.getLocale().equals(ConstantsKeys.SOURCE) && fallbackLocalesIter != null && fallbackLocalesIter.hasNext()) {
// If failed to fetch message for the requested DTO, use MessageCacheItem of the next fallback locale.
MessagesDTO fallbackLocaleDTO = new MessagesDTO(dto.getComponent(), fallbackLocalesIter.next().toLanguageTag(), dto.getProductID(), dto.getVersion());
cacheItem = new ComponentService(fallbackLocaleDTO).getMessages(fallbackLocalesIter);
}

return cacheItem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public Set<Locale> getSupportedLocales() {
public Set<String> getSupportedLanguageTags() {
Iterator<DataSourceEnum> msgSourceQueueIter = VIPCfg.getInstance().getMsgOriginsQueue().iterator();
Set<String> supportedLangTags = new HashSet<>();
while(msgSourceQueueIter.hasNext() && supportedLangTags.isEmpty()) {
supportedLangTags = getSupportedLanguageTags(msgSourceQueueIter.next());
while(msgSourceQueueIter.hasNext()) {
supportedLangTags.addAll(getSupportedLanguageTags(msgSourceQueueIter.next()));
}
return supportedLangTags;
}
Expand Down
52 changes: 11 additions & 41 deletions src/main/java/com/vmware/vipclient/i18n/util/LocaleUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@
*/
package com.vmware.vipclient.i18n.util;

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;

public class LocaleUtility {
Expand Down Expand Up @@ -147,43 +142,18 @@ public static boolean isSameLocale(String locale1, String locale2) {
/*
* pick up the matched locale from a locale list
*/
public static Locale pickupLocaleFromList(List<Locale> locales,
Locale preferredLocale) {
Locale langLocale = null;
preferredLocale = fmtToMappedLocale(preferredLocale);

// Use the first locale from the browser's list of preferred languages
// for the matching, so that it can keep the same way of getting locale
// with other VMware products, like vSphere Web Client, etc.
for (Locale configuredLocale : locales) {
// Language is matched
configuredLocale = fmtToMappedLocale(configuredLocale);
if (configuredLocale.getLanguage().equals(
preferredLocale.getLanguage())) {
String configuredScript = configuredLocale.getScript();
String preferredScript = preferredLocale.getScript();
// Country is matched
if (((preferredScript.equalsIgnoreCase("")) && (configuredScript
.equalsIgnoreCase("")))
|| ((!preferredScript.equalsIgnoreCase("")) && (preferredScript
.equalsIgnoreCase(configuredScript)))) {
return configuredLocale;
}
langLocale = langLocale == null ? configuredLocale : langLocale;
public static Locale pickupLocaleFromList(Set<Locale> locales,
Locale preferredLocale) {
Locale bestMatch = Locale.lookup(Arrays.asList(new Locale.LanguageRange(fmtToMappedLocale(preferredLocale).toLanguageTag())), locales);
jessiejuachon marked this conversation as resolved.
Show resolved Hide resolved

// For any Chinese locale (zh-*) that is not supported (except for zh-Hans and zh-Hant), use the fallback locale even if "zh" is supported.
if (preferredLocale.getLanguage().equals("zh")) {
Locale zhLocale = fmtToMappedLocale(preferredLocale);
if (!locales.contains(zhLocale) && !zhLocale.toLanguageTag().equals("zh-Hans") && !zhLocale.toLanguageTag().equals("zh-Hant")) {
return null;
}
}

// With Chinese locale which is not configured/supported in web.xml, it
// will return 'en_US' as default to meet the usage custom of Chinese,
// e.g. for 'zh-HK' from client(browser) which is not
// configured/supported yet, it will return 'en_US';
// Other locale, like 'de-DE' 'ja-JP' etc.,
// it will return 'de' 'ja'(main/parent language).
if (langLocale != null
&& (!langLocale.getLanguage().equalsIgnoreCase("zh"))) {
return new Locale(langLocale.getLanguage());
}
return preferredLocale;
return bestMatch;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/vmware/vip/i18n/HttpRequesterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void addHeaderParamsTest_() {
// Exception is expected
}

WireMock.verify(WireMock.getRequestedFor(WireMock.urlMatching(url)).withHeader(key1, WireMock.equalTo(value1))
WireMock.verify(WireMock.getRequestedFor(WireMock.urlMatching(url2+"|"+url)).withHeader(key1, WireMock.equalTo(value1))
.withHeader(key2, WireMock.equalTo(value2)));
}

Expand All @@ -100,7 +100,7 @@ public void addHeaderParamsTest() {
.getMessageInstance(TranslationMessage.class);
tm.getString2("default", "messages", new Locale("zh", "Hans"), "table.host");

WireMock.verify(WireMock.getRequestedFor(WireMock.urlMatching(url)).withHeader(key1, WireMock.equalTo(value1))
WireMock.verify(WireMock.getRequestedFor(WireMock.urlMatching(url2+"|"+url)).withHeader(key1, WireMock.equalTo(value1))
.withHeader(key2, WireMock.equalTo(value2)));
}

Expand Down
77 changes: 69 additions & 8 deletions src/test/java/com/vmware/vip/i18n/LocaleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.*;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
Expand All @@ -46,34 +42,99 @@ public void init() {
public void testPickupLocaleFromList() {
Locale[] supportedLocales = { Locale.forLanguageTag("de"),
Locale.forLanguageTag("es"), Locale.forLanguageTag("fr"),
Locale.forLanguageTag("fr-CA"),
Locale.forLanguageTag("ja"), Locale.forLanguageTag("ko"),
Locale.forLanguageTag("zh"),
Locale.forLanguageTag("zh-Hans"),
Locale.forLanguageTag("zh-Hant")

};
Locale[] testLocales = { Locale.forLanguageTag("de"),
Locale.forLanguageTag("es"), Locale.forLanguageTag("fr"),
Locale.forLanguageTag("fr-CA"), Locale.forLanguageTag("fr-FR"),
Locale.forLanguageTag("ja"), Locale.forLanguageTag("ko"),
Locale.forLanguageTag("zh"), Locale.forLanguageTag("zh-CN"),
Locale.forLanguageTag("zh"),
Locale.forLanguageTag("zh-CN"),
Locale.forLanguageTag("zh-TW"),
Locale.forLanguageTag("zh-HANS-CN"),
Locale.forLanguageTag("zh-HANT-TW"),
Locale.forLanguageTag("zh-HANS"),
Locale.forLanguageTag("zh-HANT") };

String[] expectedLocales = { "de", "es", "fr", "ja", "ko", "zh",
String[] expectedLocales = { "de", "es", "fr", "fr-CA", "fr", "ja", "ko", "zh",
"zh-Hans", "zh-Hant", "zh-Hans", "zh-Hant", "zh-Hans", "zh-Hant" };

for (int i = 0; i < testLocales.length; i++) {
String matchedLanguageTag = LocaleUtility.pickupLocaleFromList(
Arrays.asList(supportedLocales), testLocales[i])
new HashSet<>(Arrays.asList(supportedLocales)), testLocales[i])
.toLanguageTag();

logger.debug(matchedLanguageTag + "-----" + expectedLocales[i]);
Assert.assertEquals(expectedLocales[i], matchedLanguageTag);
}
}

@Test
public void testPickupLocaleFromListNotFound() {
Locale[] supportedLocales = { Locale.forLanguageTag("de"),
Locale.forLanguageTag("es"), Locale.forLanguageTag("fr"),
Locale.forLanguageTag("fr-CA"),
Locale.forLanguageTag("ja"), Locale.forLanguageTag("ko"),
Locale.forLanguageTag("zh-Hans"),
Locale.forLanguageTag("zh-Hant")

};
Assert.assertNull(LocaleUtility.pickupLocaleFromList(new HashSet<>(Arrays.asList(supportedLocales)), Locale.forLanguageTag("fil")));
}

@Test
public void testPickupLocaleFromListZh() {
Locale[] supportedLocales = {
Locale.forLanguageTag("zh")
};
Locale[] testLocales = {
Locale.forLanguageTag("zh"),
Locale.forLanguageTag("zh-CN"),
Locale.forLanguageTag("zh-TW"),
Locale.forLanguageTag("zh-HANS-CN"),
Locale.forLanguageTag("zh-HANT-TW"),
Locale.forLanguageTag("zh-HANS"),
Locale.forLanguageTag("zh-HANT") };

String[] expectedLocales = {"zh" ,"zh", "zh", "zh", "zh", "zh", "zh"};

for (int i = 0; i < testLocales.length; i++) {
String matchedLanguageTag = LocaleUtility.pickupLocaleFromList(
new HashSet<>(Arrays.asList(supportedLocales)), testLocales[i])
.toLanguageTag();

logger.debug(matchedLanguageTag + "-----" + expectedLocales[i]);
Assert.assertEquals(expectedLocales[i], matchedLanguageTag);
}
}

/**
* For any Chinese locale (zh-*) that is not supported,
* return null so that fallback locale will be used even if "zh" is supported.
* For any non-Chinese locale, return the best match (e.g. 'de' if 'de-DE' is not supported).
*/
@Test
public void testPickupLocaleFromList_special_case_zh_HK() {
Copy link
Contributor

@Xiaochao8 Xiaochao8 Oct 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add one more test case: zh is supported, zh-Hans and zh-Hant aren't supported. Request zh-Hant will return null rather than zh.

Locale[] supportedLocales = {
Locale.forLanguageTag("zh"),
Locale.forLanguageTag("zh-Hans"),
Locale.forLanguageTag("zh-Hant")

};
Assert.assertNull(LocaleUtility.pickupLocaleFromList(new HashSet<>(Arrays.asList(supportedLocales)), Locale.forLanguageTag("zh-HK")));
Assert.assertEquals("zh", LocaleUtility.pickupLocaleFromList(new HashSet<>(Arrays.asList(supportedLocales)),
Locale.forLanguageTag("zh")).toLanguageTag());
Assert.assertEquals("zh-Hans", LocaleUtility.pickupLocaleFromList(new HashSet<>(Arrays.asList(supportedLocales)),
Locale.forLanguageTag("zh-CN")).toLanguageTag());
Assert.assertEquals("zh-Hant", LocaleUtility.pickupLocaleFromList(new HashSet<>(Arrays.asList(supportedLocales)),
Locale.forLanguageTag("zh-TW")).toLanguageTag());
}

@Test
public void normalizeToLanguageTag() {
String[] testLocaleStrs = { "de", "es", "fr", "ja", "ko", "en-US", "zh-CN", "zh-TW",
Expand Down