Skip to content

Commit

Permalink
MOSIP-35914
Browse files Browse the repository at this point in the history
Signed-off-by: Jayesh Kharode <[email protected]>
  • Loading branch information
Jayesh Kharode committed Nov 21, 2024
1 parent 9fa39ca commit 2b3af37
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 211 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class ContextController {
try {
if (personaConfigPath != null && !personaConfigPath.equals(""))
DataProviderConstants.RESOURCE = personaConfigPath;
VariableManager.Init(contextKey);
// VariableManager.Init(contextKey);

return contextUtils.createUpdateServerContext(contextProperties, contextKey);
} catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public PersonaController(@Lazy PacketSyncService packetSyncService) {
if (personaConfigPath != null && !personaConfigPath.equals("")) {
DataProviderConstants.RESOURCE = personaConfigPath;
}
VariableManager.Init(contextKey);
// VariableManager.Init(contextKey);

return packetSyncService.updatePersonaData(personaRequestDto, contextKey);

Expand Down Expand Up @@ -111,7 +111,7 @@ public PersonaController(@Lazy PacketSyncService packetSyncService) {
// clear all tokens
// VariableManager.setVariableValue("urlSwitched", "true");

return packetSyncService.generateResidentData(residentRequestDto, contextKey).toString();
return packetSyncService.generateResidentData(count, residentRequestDto, contextKey).toString();

} catch (Exception ex) {
logger.error("generateResidentData", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public TestDataController(@Lazy PacketSyncService packetSyncService, @Lazy Packe
// clear all tokens
// VariableManager.setVariableValue("urlSwitched", "true");

return packetSyncService.generateResidentData(residentRequestDto, contextKey).toString();
return packetSyncService.generateResidentData(count, residentRequestDto, contextKey).toString();

} catch (Exception ex) {
logger.error("generateResidentData", ex);
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void loadServerContextProperties(String contextKey) {
public String downloadCard(String personaPath, String uin, String context) throws Exception{

loadServerContextProperties(context);
ResidentModel resident = ResidentModel.readPersona(personaPath);
ResidentModel resident = ResidentModel.readPersona(personaPath,context);
RegistrationSteps steps = new RegistrationSteps();
String resp = steps.downloadCard(resident, uin,context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ Boolean generateCBEFF(ResidentModel resident, List<String> bioAttrib, String out
bos.close();
}
}
resident.save();
resident.save(contextKey);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import io.mosip.testrig.dslrig.dataprovider.util.CommonUtil;
import io.mosip.testrig.dslrig.dataprovider.util.Gender;
import io.mosip.testrig.dslrig.dataprovider.util.RestClient;
import io.mosip.testrig.dslrig.dataprovider.variables.VariableManager;
import lombok.Data;
import java.io.*;
import java.io.FileInputStream;
Expand Down Expand Up @@ -118,8 +120,11 @@ public String toJSONString() {
return jsonStr;
}

public void save() throws IOException {
Files.write(Paths.get(path), this.toJSONString().getBytes());
public void save(String contextKey) throws IOException {
if(RestClient.isDebugEnabled(contextKey)) {
Files.write(Paths.get(path), this.toJSONString().getBytes());
}
VariableManager.setVariableValue(contextKey, path, this.toJSONString());
}


Expand All @@ -132,29 +137,31 @@ public void save() throws IOException {
// }
// }

public static ResidentModel readPersona(String filePath) throws IOException {

ObjectMapper mapper = new ObjectMapper();
//mapper.registerModule(new SimpleModule().addDeserializer(Pair.class,new PairDeserializer()));
// mapper.registerModule(new SimpleModule().addSerializer(Pair.class, new PairSerializer()));
byte[] bytes = CommonUtil.read(filePath);
ResidentModel model = mapper.readValue(bytes, ResidentModel.class);
model.setPath(filePath);
public static ResidentModel readPersona(String filePath, String contextKey) throws IOException {

ResidentModel model = null;
ObjectMapper mapper = new ObjectMapper();
try {
byte[] bytes = VariableManager.getVariableValue(contextKey, filePath).toString().getBytes();
// byte[] bytes = CommonUtil.read(filePath);
model = mapper.readValue(bytes, ResidentModel.class);
model.setPath(filePath);
} catch (Exception e) {
logger.error(e.getMessage());
}

return model;
}

}

// public void writePersona(String filePath) throws IOException {
// ObjectMapper mapper = new ObjectMapper();
// try (OutputStream outputStream = new FileOutputStream(filePath)) {
// mapper.writeValue(outputStream, this.toJSONString().getBytes());
//
// }
// }

public void writePersona(String filePath) throws IOException {
Files.write(Paths.get(filePath), this.toJSONString().getBytes());
public void writePersona(String filePath, String contextKey) throws IOException {

if(RestClient.isDebugEnabled(contextKey)) {
Files.write(Paths.get(filePath), this.toJSONString().getBytes());
}
VariableManager.setVariableValue(contextKey, filePath, this.toJSONString());
}

public static void main(String [] args) {

ResidentModel model = new ResidentModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,58 +35,50 @@ public final class VariableManager {
static boolean bInit = false;

static {
Init(NS_DEFAULT);
Init();
}

public static boolean isInit() {
return bInit;
}

public static void Init(String contextKey) {

if (bInit)
return;
// resolve a cache manager
CachingProvider cachingProvider = Caching.getCachingProvider();
cacheManager = cachingProvider.getCacheManager();

// configure the cache
cacheConfig = new MutableConfiguration<String, Object>().setTypes(String.class, Object.class)
.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(Duration.ONE_DAY)).setStatisticsEnabled(true);

// create the cache
if (varNameSpaces == null) {
varNameSpaces = new Hashtable<String, Cache<String, Object>>();
// load predefined variables

Cache<String, Object> cache = cacheManager.createCache(contextKey, cacheConfig);
varNameSpaces.put(contextKey, cache);

}
CONFIG_PATH = DataProviderConstants.RESOURCE + "config/";
Boolean bret = loadNamespaceFromPropertyFile(CONFIG_PATH + "default.properties", NS_DEFAULT);
bInit = bret;

}

static Cache<String, Object> createNameSpace(String contextKey) {
Cache<String, Object> ht = null;
try {
ht = varNameSpaces.get(contextKey);
} catch (Exception e) {
}
if (ht == null) {
ht = cacheManager.createCache(contextKey, cacheConfig);
varNameSpaces.put(contextKey, ht);
}
return ht;
}

public static Object setVariableValue(String contextKey, String varName, Object value) {
Cache<String, Object> ht = createNameSpace(contextKey);
ht.put(varName, value);
return value;
}
public static synchronized void Init() {
synchronized (VariableManager.class) {
if (bInit) return;
// resolve a cache manager
CachingProvider cachingProvider = Caching.getCachingProvider();
cacheManager = cachingProvider.getCacheManager();
// configure the cache
cacheConfig = new MutableConfiguration<String, Object>()
.setTypes(String.class, Object.class)
.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(Duration.ONE_DAY))
.setStatisticsEnabled(true);
// create the cache
if (varNameSpaces == null) {
varNameSpaces = new Hashtable<String, Cache<String, Object>>();
Cache<String, Object> cache = cacheManager.createCache(NS_DEFAULT, cacheConfig);
varNameSpaces.put(NS_DEFAULT, cache);
}
CONFIG_PATH = DataProviderConstants.RESOURCE + "config/";
Boolean bret = loadNamespaceFromPropertyFile(CONFIG_PATH + "default.properties", NS_DEFAULT);
bInit = bret;
}
}

static Cache<String, Object> createNameSpace(String contextKey) {
Cache<String, Object> ht = varNameSpaces.get(contextKey);
if (ht == null) {
ht = cacheManager.createCache(contextKey, cacheConfig);
varNameSpaces.put(contextKey, ht);
}
return ht;
}

public static Object setVariableValue(String contextKey, String varName, Object value) {
Cache<String, Object> ht = createNameSpace(contextKey);
ht.put(varName, value);
return value;
}

public static String[] findVariables(String text) {

Expand All @@ -109,32 +101,25 @@ public static String[] findVariables(String text) {
return set.toArray(a);
}

public static Object getVariableValue(String contextKey, String varName) {

if (!bInit)
Init(contextKey);

Cache<String, ?> ht = null;
Object ret = null; // To do ---- new Object()

try {
ht = varNameSpaces.get(contextKey);
if (ht != null) {
ret = ht.get(varName);
if (ret == null && contextKey.equalsIgnoreCase(NS_DEFAULT)) {
// Cache expired , reloading the default namespace
loadNamespaceFromPropertyFile(VariableManager.CONFIG_PATH + "default.properties",
VariableManager.NS_DEFAULT);
ht = varNameSpaces.get(contextKey);
ret = ht.get(varName);
}
return ret;
}
} catch (Exception e) {
logger.error(e.getMessage());
}
return ret;
}
public static Object getVariableValue(String contextKey, String varName) {
if (!bInit) Init();
Cache<String, ?> ht = varNameSpaces.get(contextKey);
Object ret = null;
try {
if (ht != null) {
ret = ht.get(varName);
if (ret == null && contextKey.equalsIgnoreCase(NS_DEFAULT)) {
// Cache expired, reloading the default namespace
loadNamespaceFromPropertyFile(CONFIG_PATH + "default.properties", NS_DEFAULT);
ht = varNameSpaces.get(contextKey);
ret = ht.get(varName);
}
}
} catch (Exception e) {
logger.error(e.getMessage());
}
return ret;
}

public static Boolean loadNamespaceFromPropertyFile(String propFile, String contextKey) {
Boolean bRet = false;
Expand All @@ -155,21 +140,21 @@ public static Boolean loadNamespaceFromPropertyFile(String propFile, String cont
return bRet;
}

public static String deleteNameSpace(String contextKey) {
try {
printAllContents();
Cache<String, Object> cache = varNameSpaces.remove(contextKey);
if (cache != null) {
synchronized (cacheManager) {
cacheManager.destroyCache(contextKey);
}
}
} catch (Exception e) {
logger.error(e.getMessage());
return "false";
}
return "true";
}
public static String deleteNameSpace(String contextKey) {
try {
printAllContents();
Cache<String, Object> cache = varNameSpaces.remove(contextKey);
if (cache != null) {
synchronized (cacheManager) {
cacheManager.destroyCache(contextKey);
}
}
} catch (Exception e) {
logger.error(e.getMessage());
return "false";
}
return "true";
}

public static void printAllContents() {
StringBuffer s = new StringBuffer();
Expand Down Expand Up @@ -224,4 +209,4 @@ public static void testJCache() {

}

}
}

0 comments on commit 2b3af37

Please sign in to comment.