Skip to content

Commit

Permalink
Deprecating methods in TranslationMessage; adding new method getMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
jessiejuachon committed Apr 23, 2020
1 parent b805d36 commit 17bc16c
Show file tree
Hide file tree
Showing 17 changed files with 301 additions and 32 deletions.
5 changes: 4 additions & 1 deletion src/main/java/com/vmware/vipclient/i18n/VIPCfg.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class VIPCfg {

// define the global parameters
private boolean pseudo;
@Deprecated
private boolean collectSource;
private boolean cleanCache;
private long cacheExpiredTime;
Expand Down Expand Up @@ -320,10 +321,12 @@ public void setPseudo(boolean pseudo) {
this.pseudo = pseudo;
}

@Deprecated
public boolean isCollectSource() {
return collectSource;
}

@Deprecated
public void setCollectSource(boolean collectSource) {
this.collectSource = collectSource;
}
Expand Down Expand Up @@ -427,5 +430,5 @@ public List<DataSourceEnum> getMsgOriginsQueue() {
public void setMsgOriginsQueue(List<DataSourceEnum> msgOriginsQueue) {
this.msgOriginsQueue = msgOriginsQueue;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ public MessageCacheItem (Map<String, String> dataMap) {
private long timestamp;
private Long maxAgeMillis = 86400000l;

public final Map<String, String> cachedData = new HashMap<String, String>();
private final Map<String, String> cachedData = new HashMap<String, String>();

This comment has been minimized.

Copy link
@Xiaochao8

Xiaochao8 Apr 23, 2020

Contributor

Suggest to use ConcurrentHashMap to guarantee concurrency safety. Remove keyword 'synchronized' on methods related to 'cachedData'.

This comment has been minimized.

Copy link
@jessiejuachon

jessiejuachon Apr 30, 2020

Author Contributor

There is some logic inside the setter methods that needs to be thread-safe, that is why they are synchronized. See setCacheItem.
Getter methods do not need to be synchronized, but code scan is complaining about it.


public void addCacheData(String key, String value) {

This comment has been minimized.

Copy link
@Xiaochao8

Xiaochao8 Apr 23, 2020

Contributor

Suggest to change name to addSingleData

this.cachedData.put(key, value);
}

public boolean isCachedDataEmpty() {
return this.cachedData.isEmpty();
}

public synchronized void addCachedData(Map<String, String> cachedData) {

This comment has been minimized.

Copy link
@Xiaochao8

Xiaochao8 Apr 23, 2020

Contributor

Suggest to change name to mergeData or updateData.

if (cachedData != null)
Expand Down Expand Up @@ -94,5 +102,5 @@ public boolean isExpired() {

return System.currentTimeMillis() - responseTimeStamp > maxAgeMillis;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.vmware.vipclient.i18n.VIPCfg;
import com.vmware.vipclient.i18n.common.ConstantsMsg;
import com.vmware.vipclient.i18n.messages.api.opt.SourceOpt;
import com.vmware.vipclient.i18n.messages.dto.MessagesDTO;
import com.vmware.vipclient.i18n.messages.service.ComponentService;
import com.vmware.vipclient.i18n.messages.service.ComponentsService;
Expand Down Expand Up @@ -48,6 +49,43 @@ public TranslationMessage() {
super();
}

public String getMessage(final Locale locale, final String component, final SourceOpt sourceOpt,
final String key, final String comment, final Object... args) {
String message = null;
String source = sourceOpt.getMessage(key);

MessagesDTO dto = new MessagesDTO(component, comment, key, null, locale.toLanguageTag(), this.cfg);
StringService s = new StringService();
message = s.getString(dto);

if (message == null || message.isEmpty()) {
message = source;
} else {
// If the source message is not equal to the cached source (loaded from remote or from offline bundle file),
// it means that this source message hasn't been collected for localization, so return the source message
if (source != null && !source.isEmpty() && !VIPCfg.getInstance().isPseudo()) {
MessagesDTO defaultLocaleDTO = new MessagesDTO(component, comment, key, source,
LocaleUtility.defaultLocale.toLanguageTag(), this.cfg);
String cachedDefaultLocaleMsg = s.getString(defaultLocaleDTO);
if (!source.equals(cachedDefaultLocaleMsg) ||
cachedDefaultLocaleMsg == null || cachedDefaultLocaleMsg.isEmpty()) {
message = source;
}
}
}

if (VIPCfg.getInstance().isPseudo() && message.equals(source) && source != null) {
message = ConstantsKeys.PSEUDOCHAR2 + source + ConstantsKeys.PSEUDOCHAR2;
}

if (message.equals(source)) {
message = FormatUtils.format(message, LocaleUtility.defaultLocale, args);
} else {
message = FormatUtils.format(message, locale, args);
}
return message;
}

/**
* get a translation under the component of the configured product
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright 2019 VMware, Inc.
* SPDX-License-Identifier: EPL-2.0
*/
package com.vmware.vipclient.i18n.messages.api.opt;

import com.vmware.vipclient.i18n.base.cache.MessageCacheItem;

public interface SourceOpt {
public void getComponentMessages(MessageCacheItem cacheItem);
public String getMessage(String key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,11 @@ public void getComponentMessages(MessageCacheItem cacheItem) {

@Override
public String getString() {
JSONObject jo = this.getComponentMessages();
String k = dto.getKey();
String v = "";
if (jo != null) {
v = jo.get(k) == null ? "" : v;
}
return v;
MessageCacheItem cacheItem = new MessageCacheItem();
this.getComponentMessages(cacheItem);
String message = cacheItem.getCachedData().get(dto.getKey()) == null ?
"" : cacheItem.getCachedData().get(dto.getKey());
return message;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2019 VMware, Inc.
* SPDX-License-Identifier: EPL-2.0
*/
package com.vmware.vipclient.i18n.messages.api.opt.source;

import java.util.Enumeration;
import java.util.Locale;
import java.util.ResourceBundle;

import com.vmware.vipclient.i18n.base.cache.MessageCacheItem;
import com.vmware.vipclient.i18n.messages.api.opt.SourceOpt;

public class ResourceBundleSrcOpt implements SourceOpt {

private ResourceBundle rb;

public ResourceBundleSrcOpt(String bundle, Locale locale) {
this.rb = ResourceBundle.getBundle(bundle, locale);
}

@Override
public void getComponentMessages(MessageCacheItem cacheItem) {
Enumeration<String> keys = rb.getKeys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
cacheItem.addCacheData(key, rb.getString(key));
}
}

@Override
public String getMessage(String key) {
return rb.getString(key);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void getMessages(final MessageCacheItem cacheItem, ListIterator msgSource
DataSourceEnum dataSource = (DataSourceEnum) msgSourceQueueIter.next();
dataSource.createMessageOpt(dto).getComponentMessages(cacheItem);

// If failed to get messages from the source, try the next source in the queue
// If failed to get messages from the dataSource, try the next dataSource in the queue
if (cacheItem.getCachedData().isEmpty()) {
getMessages(cacheItem, msgSourceQueueIter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public String getString(MessagesDTO dto) {

// The MessageCacheItem for the requested locale will be a reference
// to the MessageCacheItem of the default locale
if (!cacheItem.cachedData.isEmpty()) {
if (!cacheItem.isCachedDataEmpty()) {
CacheService cacheService = new CacheService(dto);
cacheService.addCacheOfComponent(cacheItem);
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/vmware/vipclient/i18n/util/FormatUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public static String format(String pattern, Object... arguments) {
}

public static String format(String pattern, Locale locale, Object... arguments) {
MessageFormat messageFormat = new MessageFormat(pattern, locale);
return messageFormat.format(arguments, new StringBuilder(), new FieldPosition(0)).toString();
if (pattern != null && !pattern.isEmpty() && arguments != null && arguments.length > 0) {
MessageFormat messageFormat = new MessageFormat(pattern, locale);
return messageFormat.format(arguments, new StringBuilder(), new FieldPosition(0)).toString();
}
return pattern;
}
}
2 changes: 1 addition & 1 deletion src/test/java/com/vmware/vip/i18n/HttpRequesterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void addHeaderParamsTest() {

TranslationMessage tm = (TranslationMessage) I18nFactory.getInstance()
.getMessageInstance(TranslationMessage.class);
tm.getString2("default", "JAVA", new Locale("zh", "Hans"), "table.host");
tm.getString2("default", "messages", new Locale("zh", "Hans"), "table.host");

WireMock.verify(WireMock.getRequestedFor(WireMock.urlMatching(url)).withHeader(key1, WireMock.equalTo(value1))
.withHeader(key2, WireMock.equalTo(value2)));
Expand Down
Loading

0 comments on commit 17bc16c

Please sign in to comment.