diff --git a/common/src/main/java/com/genexus/BaseProvider.java b/common/src/main/java/com/genexus/BaseProvider.java index 35a020a86..63ae17577 100644 --- a/common/src/main/java/com/genexus/BaseProvider.java +++ b/common/src/main/java/com/genexus/BaseProvider.java @@ -2,7 +2,6 @@ import java.io.File; import java.io.IOException; -import java.nio.charset.StandardCharsets; import java.util.Date; import java.util.List; import java.util.ListIterator; @@ -20,6 +19,8 @@ import com.genexus.common.interfaces.SpecificImplementation; import com.genexus.util.GXDirectory; import com.genexus.util.GXFileCollection; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; @@ -53,76 +54,65 @@ private void loadQueryTables() { String path = SpecificImplementation.Application.getModelContext().getHttpContext().getDefaultPath(); String configurationDirectoryPath = path + File.separatorChar + "Metadata" + File.separatorChar + "TableAccess"; - - ConcurrentHashMap> qTables = new ConcurrentHashMap(); - loadQueryTablesPlatform(configurationDirectoryPath, qTables); + ConcurrentHashMap> qTables = new ConcurrentHashMap>(); + if (ApplicationContext.getInstance().isSpringBootApp()) + loadQueryTablesSpringBoot(configurationDirectoryPath, qTables); + else + loadQueryTablesFileSystem(configurationDirectoryPath, qTables); startupDate = CommonUtil.now(false,false); queryTables = qTables; } } - public void loadQueryTablesPlatform(String configurationDirectoryPath, ConcurrentHashMap> qTables) { - if (ApplicationContext.getInstance().isSpringBootApp()) - loadQueryTablesSpringBoot(configurationDirectoryPath, qTables); - else - loadQueryTablesNone(configurationDirectoryPath, qTables); - - } - - public void loadQueryTablesNone(String configurationDirectoryPath, ConcurrentHashMap> qTables) { + private void loadQueryTablesFileSystem(String configurationDirectoryPath, ConcurrentHashMap> qTables){ GXDirectory configurationDirectory = new GXDirectory(configurationDirectoryPath); GXFileCollection files = configurationDirectory.getFiles(); XMLReader reader = new XMLReader(); - short ok; - boolean anyTable=false; - for(int i=1; i <= files.getItemCount(); i++) { - Vector lst = new Vector(); - lst.add(FORCED_INVALIDATE); // Caso en que se invalido el cache manualmente + boolean anyTables=false; + for(int i=1; i <= files.getItemCount(); i++) + { AbstractGXFile xmlFile = files.item(i); - reader.open(xmlFile.getAbsoluteName()); - ok = reader.readType(1, "Table"); - while (ok == 1) { - anyTable=true; - lst.add(normalizeKey(reader.getAttributeByName("name"))); - ok = reader.readType(1, "Table"); - } - reader.close(); - if (anyTable) { - qTables.put(normalizeKey(xmlFile.getNameNoExt()), lst); - } + String xmlFileName = xmlFile.getAbsoluteName(); + String xmlFileNameNoExt = xmlFile.getNameNoExt(); + anyTables = processXMLFile(reader, anyTables, xmlFileName, xmlFileNameNoExt, qTables); } } - public void loadQueryTablesSpringBoot(String configurationDirectoryPath, ConcurrentHashMap> qTables) { + private void loadQueryTablesSpringBoot(String configurationDirectoryPath, ConcurrentHashMap> qTables){ + PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); try { - Resource[] resources = new PathMatchingResourcePatternResolver().getResources(configurationDirectoryPath + "/*.xml"); + Resource[] resources = resolver.getResources("classpath:" + configurationDirectoryPath + "/*"); XMLReader reader = new XMLReader(); - reader.setDocEncoding("UTF8"); - short ok; - boolean anyTable=false; - String xmlContent; - for (int i = 0; i < resources.length; i++) { - Vector lst = new Vector(); - lst.add(FORCED_INVALIDATE); - xmlContent = resources[i].getContentAsString(StandardCharsets.UTF_8); - if (!xmlContent.startsWith("<")) - xmlContent = xmlContent.substring(1); //Avoid BOM - reader.openFromString(xmlContent); - ok = reader.readType(1, "Table"); - while (ok == 1) { - anyTable=true; - lst.add(normalizeKey(reader.getAttributeByName("name"))); - ok = reader.readType(1, "Table"); - } - reader.close(); - if (anyTable) { - qTables.put(normalizeKey(resources[i].getFilename().substring(0, resources[i].getFilename().lastIndexOf("."))), lst); - } + boolean anyTables=false; + for (Resource resource : resources) { + String xmlFileName = resource.getFilename(); + String xmlFileNameNoExt = xmlFileName.substring(0, xmlFileName.lastIndexOf('.')); + xmlFileName = resource instanceof FileSystemResource? ((FileSystemResource) resource).getPath() : ((ClassPathResource) resource).getPath(); + anyTables = processXMLFile(reader, anyTables, xmlFileName, xmlFileNameNoExt, qTables); } } - catch (IOException e) { - logger.error("Error reading Table Access metadata", e); + catch (IOException e){ + logger.error("Error loading Query Tables ", e); + } + } + + private boolean processXMLFile(XMLReader reader, boolean anyTables, String xmlFileName, String xmlFileNameNoExt, ConcurrentHashMap> qTables) { + Vector lst = new Vector<>(); + lst.add(FORCED_INVALIDATE); // Caso en que se invalido el cache manualmente + reader.open(xmlFileName); + short ok = reader.readType(1, "Table"); + boolean anyLocalTables = false; + while (ok == 1) + { + anyLocalTables = true; + lst.add(normalizeKey(reader.getAttributeByName("name"))); + ok = reader.readType(1, "Table"); + } + reader.close(); + if (anyTables || anyLocalTables) { + qTables.put(normalizeKey(xmlFileNameNoExt), lst); } + return anyTables || anyLocalTables; } public ConcurrentHashMap> queryTables() {