Skip to content

Commit

Permalink
- issue #465
Browse files Browse the repository at this point in the history
  • Loading branch information
rathnapandi committed Mar 12, 2024
1 parent 0a03578 commit 9deb6a6
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.axway.apim.lib;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.ehcache.Cache;
import org.ehcache.config.CacheRuntimeConfiguration;
import org.ehcache.spi.loaderwriter.BulkCacheLoadingException;
import org.ehcache.spi.loaderwriter.BulkCacheWritingException;
import org.ehcache.spi.loaderwriter.CacheLoadingException;
import org.ehcache.spi.loaderwriter.CacheWritingException;

public class APIMCLICache<K, V> implements Cache<K, V> {

private final Cache<K, V> cache;
private final String prefix;

public APIMCLICache(Cache<K, V> cache, String prefix) {
super();
this.cache = cache;
this.prefix = prefix;
}

@Override
public boolean containsKey(K key) {
return this.cache.containsKey(getPrefixedKey(key));
}

@Override
public void clear() {
this.cache.clear();
}

@Override
public V get(K key) throws CacheLoadingException {
return this.cache.get(getPrefixedKey(key));
}

@Override
public Map<K, V> getAll(Set<? extends K> arg0) throws BulkCacheLoadingException {
throw new UnsupportedOperationException("Method getAll is not implemented for the APIMCLICache");
}

@Override
public CacheRuntimeConfiguration<K, V> getRuntimeConfiguration() {
return this.cache.getRuntimeConfiguration();
}

@Override
public Iterator<Entry<K, V>> iterator() {
return this.cache.iterator();
}

@Override
public void put(K key, V value) throws CacheWritingException {
this.cache.put(getPrefixedKey(key), value);
}

@Override
public void putAll(Map<? extends K, ? extends V> arg0) throws BulkCacheWritingException {
throw new UnsupportedOperationException("Method putAll is not implemented for the APIMCLICache");
}

@Override
public V putIfAbsent(K key, V value) throws CacheLoadingException, CacheWritingException {
return this.cache.putIfAbsent(getPrefixedKey(key), value);
}

@Override
public void remove(K key) throws CacheWritingException {
this.cache.remove(getPrefixedKey(key));
}

@Override
public boolean remove(K key, V value) throws CacheWritingException {
return this.cache.remove(getPrefixedKey(key), value);
}

@Override
public void removeAll(Set<? extends K> arg0) throws BulkCacheWritingException {
throw new UnsupportedOperationException("Method removeAll is not implemented for the APIMCLICache");
}

@Override
public V replace(K key, V value) throws CacheLoadingException, CacheWritingException {
return this.cache.replace(getPrefixedKey(key), value);
}

@Override
public boolean replace(K key, V value1, V value2) throws CacheLoadingException, CacheWritingException {
return this.cache.replace(getPrefixedKey(key), value1, value2);
}

@SuppressWarnings("unchecked")
private K getPrefixedKey(K key) {
return (K) (prefix+key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,22 @@ public <K, V> Cache<K, V> createCache(String arg0, Builder<? extends CacheConfig

@Override
public <K, V> Cache<K, V> getCache(String alias, Class<K> keyType, Class<V> valueType) {
String cachePrefix;
try {
cachePrefix = CoreParameters.getInstance().getAPIManagerURL().toString();
} catch (AppException e) {
throw new RuntimeException(e);
}
if (this.enabledCaches == null) {
// Caches not specified, return requested cache
// however, cacheManager might be a DoNothingCacheManager if ignoreCache is set
return cacheManager.getCache(alias, keyType, valueType);
Cache<K, V> plainCache = cacheManager.getCache(alias, keyType, valueType);
return new APIMCLICache<>(plainCache, cachePrefix);
} else {
if (this.enabledCaches.contains(alias)) {
LOG.debug("Using cache: {} as it is enabled.", alias);
return cacheManager.getCache(alias, keyType, valueType);
Cache<K, V> plainCache = cacheManager.getCache(alias, keyType, valueType);
return new APIMCLICache<>(plainCache, cachePrefix);
} else {
return new DoNothingCache<>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum ErrorCode {
MISSING_PARAMETER(5, "There is a missing parameter.", false),
INVALID_PARAMETER(6, "There is an invalid parameter.", false),
API_ALREADY_EXISTS(7, "The API already exists for another organization.", false),
BACKEND_API_DEF_NA(8, "The API already exists for another organization.", false),
NO_CHANGE(10, "No change between desired and actual API has been detected.", false, LogLevel.WARN),
EXPORT_FOLDER_EXISTS(12, "Export failed Export-Folder already exists.", false),
UPDATE_ONLY_IS_SET(13, "Creating of a new API fails, when flag updateOnly is set.", false),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public void saveAPILocally(ExportAPI exportAPI, APIResultHandler apiResultHandle
// Skip processing if API definition is not available due to original API cloned and deleted.
if (apiDef == null) {
LOG.error("Backend API Definition is not available for the API : {}, hence use the option -useFEAPIDefinition to export API", exportAPI.getName());
return;
if (params.getId() != null)
throw new AppException("Backend API Definition is not available for the API : " + exportAPI.getName() + ", hence use the option -useFEAPIDefinition to export API", ErrorCode.BACKEND_API_DEF_NA);
}
String targetFile = null;
String configFile;
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<sonar.issue.ignore.multicriteria.e2.ruleKey>java:S106</sonar.issue.ignore.multicriteria.e2.ruleKey>
<sonar.issue.ignore.multicriteria.e2.resourceKey>**/*.java</sonar.issue.ignore.multicriteria.e2.resourceKey>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
<citrus.version>4.0.2</citrus.version>
<citrus.version>4.1.1</citrus.version>
<jackson.version>2.15.3</jackson.version>
<failsafe.version>3.3.2</failsafe.version>
<graphql.version>21.3</graphql.version>
Expand Down

0 comments on commit 9deb6a6

Please sign in to comment.