Skip to content

Commit

Permalink
G11n java client - Fix for expiration-related issues (issues 604, 664…
Browse files Browse the repository at this point in the history
…, and 662) (vmware#699)

Fix for expiration-related issues (issues 604, 664, and 662) (vmware#699)
  • Loading branch information
jessiejuachon authored and huihuiw01 committed Aug 19, 2020
1 parent 2899759 commit 7a03823
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,77 +14,60 @@ public class MessageCacheItem implements CacheItem {
public MessageCacheItem() {

}

public MessageCacheItem (Map<String, String> dataMap, String etag, long timestamp, Long maxAgeMillis) {
super();
this.addCachedData(dataMap);
this.etag = etag;
this.timestamp = timestamp;
this.maxAgeMillis = maxAgeMillis;
}

public MessageCacheItem (Map<String, String> dataMap) {
super();

public MessageCacheItem(Map<String, String> dataMap) {
if (dataMap != null)
this.addCachedData(dataMap);
this.cachedData.putAll(dataMap);
}


public MessageCacheItem (String locale, Map<String, String> dataMap, String etag, long timestamp, Long maxAgeMillis) {
this.setCacheItem(locale, dataMap, etag, timestamp, maxAgeMillis);
}

private String locale;
private String etag;
private long timestamp;
private Long maxAgeMillis = 86400000l;

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

public void addCacheData(String key, String value) {
this.cachedData.put(key, value);
private final Map<String, String> cachedData = new HashMap<>();

public synchronized void setCacheItem(String locale, Map<String, String> dataToCache, String etag, long timestamp, Long maxAgeMillis) {
if (dataToCache != null)
this.cachedData.putAll(dataToCache);
this.setCacheItem(locale, etag, timestamp, maxAgeMillis);
}

public synchronized void addCachedData(Map<String, String> cachedData) {
if (cachedData != null)
this.cachedData.putAll(cachedData);
public synchronized void setCacheItem(String locale, String etag, long timestamp, Long maxAgeMillis) {
this.locale = locale;
if (etag != null && !etag.isEmpty())
this.etag = etag;
this.timestamp = timestamp;
if (maxAgeMillis != null)
this.maxAgeMillis = maxAgeMillis;
}

public synchronized void setCacheItem (MessageCacheItem cacheItem) {
// Do not update cacheItem if timestamp is earlier than current.
// An older timestamp comes from an old thread that was blocked.
if (cacheItem.getTimestamp() < this.timestamp)
return;
this.addCachedData(cacheItem.getCachedData());
this.etag = cacheItem.etag;
this.timestamp = cacheItem.timestamp;
this.maxAgeMillis = cacheItem.maxAgeMillis;
this.setCacheItem(cacheItem.getLocale(), cacheItem.getCachedData(), cacheItem.getEtag(), cacheItem.getTimestamp(), cacheItem.getMaxAgeMillis());
}

public synchronized String getEtag() {
public String getEtag() {
return etag;
}

public synchronized void setEtag(String etag) {
this.etag = etag;
}

public synchronized long getTimestamp() {
public long getTimestamp() {
return timestamp;
}

public synchronized void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}

public Map<String, String> getCachedData() {
return cachedData;
}

public synchronized Long getMaxAgeMillis() {
public Long getMaxAgeMillis() {
return maxAgeMillis;
}

public synchronized void setMaxAgeMillis(Long maxAgeMillis) {
this.maxAgeMillis = maxAgeMillis;
}
public String getLocale() { return locale; }

public boolean isExpired() {
public synchronized boolean isExpired() {
// If offline mode only, cache never expires.
if (VIPCfg.getInstance().getVipServer() == null) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ public void getComponentMessages(MessageCacheItem cacheItem) {
path = Paths.get(uri);
messages = JSONBundleUtil.getMessages(path);
}

cacheItem.addCachedData(messages);
cacheItem.setTimestamp(System.currentTimeMillis());
cacheItem.setCacheItem(dto.getLocale(), messages, null, System.currentTimeMillis(), null);
} catch (Exception e) {
logger.debug(e.getMessage());
// Do not update cacheItem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
import java.util.List;
import java.util.Map;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vmware.vipclient.i18n.VIPCfg;
import com.vmware.vipclient.i18n.base.cache.MessageCacheItem;
import com.vmware.vipclient.i18n.messages.api.opt.BaseOpt;
Expand All @@ -23,7 +18,10 @@
import com.vmware.vipclient.i18n.messages.api.url.V2URL;
import com.vmware.vipclient.i18n.messages.dto.MessagesDTO;
import com.vmware.vipclient.i18n.util.ConstantsKeys;
import com.vmware.vipclient.i18n.util.LocaleUtility;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ComponentBasedOpt extends BaseOpt implements Opt, MessageOpt {
private final Logger logger = LoggerFactory.getLogger(ComponentBasedOpt.class.getName());
Expand Down Expand Up @@ -52,33 +50,32 @@ public void getComponentMessages(MessageCacheItem cacheItem) {

if (responseCode != null && (responseCode.equals(HttpURLConnection.HTTP_OK) ||
responseCode.equals(HttpURLConnection.HTTP_NOT_MODIFIED))) {

// If already in cache (timestamp > 0), always extend the timestamp.
if (cacheItem.getTimestamp() != 0 && response.get(URLUtils.RESPONSE_TIMESTAMP) != null)
cacheItem.setTimestamp((long) response.get(URLUtils.RESPONSE_TIMESTAMP) );
long timestamp = 0;
String etag = null;
Long maxAgeMillis = null;

if (response.get(URLUtils.RESPONSE_TIMESTAMP) != null)
timestamp = (long) response.get(URLUtils.RESPONSE_TIMESTAMP);
if (response.get(URLUtils.HEADERS) != null)
cacheItem.setEtag(URLUtils.createEtagString((Map<String, List<String>>) response.get(URLUtils.HEADERS)));
etag = URLUtils.createEtagString((Map<String, List<String>>) response.get(URLUtils.HEADERS));
if (response.get(URLUtils.MAX_AGE_MILLIS) != null)
cacheItem.setMaxAgeMillis((Long) response.get(URLUtils.MAX_AGE_MILLIS));
maxAgeMillis = (Long) response.get(URLUtils.MAX_AGE_MILLIS);

if (responseCode.equals(HttpURLConnection.HTTP_OK)) {
JSONObject respObj = (JSONObject) JSONValue.parse((String) response.get(URLUtils.BODY));
try {
if (getResponseCode(respObj) == 200){
// If not yet in cache (timestamp = 0), store the timestamp only when successful (business code 200).
if (cacheItem.getTimestamp() == 0 && response.get(URLUtils.RESPONSE_TIMESTAMP) != null) {
cacheItem.setTimestamp((long) response.get(URLUtils.RESPONSE_TIMESTAMP) );
}
if (getResponseCode(respObj) == 200) {
Map<String,String> messages = this.getMsgsJson(response);
if (messages != null) {
cacheItem.addCachedData(messages);
cacheItem.setCacheItem(this.dto.getLocale(), messages, etag, timestamp, maxAgeMillis);
}
}
} catch (Exception e) {
logger.error("Failed to get messages");
}
}

} else {
cacheItem.setCacheItem(this.dto.getLocale(), etag, timestamp, maxAgeMillis);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ public JSONObject queryFromServer(final Set<String> components, final Set<String
Map<String, Object> response = VIPCfg.getInstance().getVipService().getHttpRequester().request(url, ConstantsKeys.GET,
requestData);
this.responseStr = (String) response.get(URLUtils.BODY);
cacheItem.setEtag(URLUtils.createEtagString((Map<String, List<String>>) response.get(URLUtils.HEADERS)));
cacheItem.setTimestamp((long) response.get(URLUtils.RESPONSE_TIMESTAMP));
cacheItem.setMaxAgeMillis((Long) response.get(URLUtils.MAX_AGE_MILLIS));
String etag = URLUtils.createEtagString((Map<String, List<String>>) response.get(URLUtils.HEADERS));
long timestamp = (long) response.get(URLUtils.RESPONSE_TIMESTAMP);
Long maxAgeMillis = (Long) response.get(URLUtils.MAX_AGE_MILLIS);
cacheItem.setCacheItem(null, etag, timestamp, maxAgeMillis);
if (StringUtil.isEmpty(this.responseStr))
throw new VIPJavaClientException(ConstantsMsg.SERVER_RETURN_EMPTY);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
package com.vmware.vipclient.i18n.messages.service;

import java.util.*;

import com.vmware.vipclient.i18n.VIPCfg;
import com.vmware.vipclient.i18n.base.cache.Cache;
import com.vmware.vipclient.i18n.base.cache.FormatCacheItem;
Expand All @@ -12,12 +14,6 @@
import com.vmware.vipclient.i18n.util.ConstantsKeys;
import com.vmware.vipclient.i18n.util.LocaleUtility;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

public class CacheService {
private MessagesDTO dto;

Expand Down
Loading

0 comments on commit 7a03823

Please sign in to comment.