Skip to content

Commit

Permalink
Merge branch 'main' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
dixitdeepak authored Dec 2, 2024
2 parents e29dd4e + a4acb92 commit 1c5ab25
Show file tree
Hide file tree
Showing 28 changed files with 185 additions and 346 deletions.
12 changes: 5 additions & 7 deletions framework/service/org/moqui/impl/InstanceServices.xml
Original file line number Diff line number Diff line change
Expand Up @@ -723,15 +723,14 @@ along with this software (see the LICENSE.md file). If not, see
dbUserExists = false
javax.sql.XAConnection testXaCon = null
java.sql.Connection testCon = null
try {
testXaCon = ec.entity.getConfConnection(adminMap)
testCon = testXaCon.getConnection()
dbConnectSuccess = true
try (java.sql.Connection testCon = testXaCon.getConnection()) {
dbConnectSuccess = true
}
} catch (Exception e) {
logger.warn("Test connection failed", e)
} finally {
if (testCon != null) testCon.close()
if (testXaCon != null) testXaCon.close()
}
Expand Down Expand Up @@ -804,15 +803,14 @@ along with this software (see the LICENSE.md file). If not, see
dbUserExists = false
javax.sql.XAConnection testXaCon = null
java.sql.Connection testCon = null
try {
testXaCon = ec.entity.getConfConnection(adminMap)
testCon = testXaCon.getConnection()
try (java.sql.Connection testCon = testXaCon.getConnection()) {
dbConnectSuccess = true
}
} catch (Exception e) {
logger.warn("Test connection to Postgres failed", e)
} finally {
if (testCon != null) testCon.close()
if (testXaCon != null) testXaCon.close()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class ExecutionContextFactoryImpl implements ExecutionContextFactory {
// get the MoquiInit.properties file
Properties moquiInitProperties = new Properties()
URL initProps = this.class.getClassLoader().getResource("MoquiInit.properties")
if (initProps != null) { InputStream is = initProps.openStream(); moquiInitProperties.load(is); is.close() }
if (initProps != null) { try(InputStream is = initProps.openStream()) { moquiInitProperties.load(is);} }

// if there is a system property use that, otherwise from the properties file
runtimePath = System.getProperty("moqui.runtime")
Expand Down Expand Up @@ -345,12 +345,15 @@ class ExecutionContextFactoryImpl implements ExecutionContextFactory {

URL defaultConfUrl = this.class.getClassLoader().getResource("MoquiDefaultConf.xml")
if (defaultConfUrl == null) throw new IllegalArgumentException("Could not find MoquiDefaultConf.xml file on the classpath")
MNode newConfigXmlRoot = MNode.parse(defaultConfUrl.toString(), defaultConfUrl.newInputStream())
//MNode newConfigXmlRoot = MNode.parse(defaultConfUrl.toString(), defaultConfUrl.newInputStream())
try (InputStream is = defaultConfUrl.newInputStream()) {
MNode newConfigXmlRoot = MNode.parse(defaultConfUrl.toString(), is)

// just merge the component configuration, needed before component init is done
mergeConfigComponentNodes(newConfigXmlRoot, runtimeConfXmlRoot)

return newConfigXmlRoot
}
}
protected void initComponents(MNode baseConfigNode) {
File versionJsonFile = new File(runtimePath + "/version.json")
Expand Down Expand Up @@ -517,9 +520,10 @@ class ExecutionContextFactoryImpl implements ExecutionContextFactory {
try {
if (confSaveFile.exists()) confSaveFile.delete()
if (!confSaveFile.parentFile.exists()) confSaveFile.parentFile.mkdirs()
FileWriter fw = new FileWriter(confSaveFile)
try (FileWriter fw = new FileWriter(confSaveFile)) {
fw.write(confXmlRoot.toString())
fw.close()
}

} catch (Exception e) {
logger.warn("Could not save ${confSaveFile.absolutePath} file: ${e.toString()}")
}
Expand Down Expand Up @@ -1346,8 +1350,7 @@ class ExecutionContextFactoryImpl implements ExecutionContextFactory {
String targetDirLocation = zipFile.getParent()
logger.info("Expanding component archive ${zipRr.getFileName()} to ${targetDirLocation}")

ZipInputStream zipIn = new ZipInputStream(zipRr.openStream())
try {
try (ZipInputStream zipIn = new ZipInputStream(zipRr.openStream())) {
ZipEntry entry = zipIn.getNextEntry()
// iterates over entries in the zip file
while (entry != null) {
Expand All @@ -1357,14 +1360,13 @@ class ExecutionContextFactoryImpl implements ExecutionContextFactory {
File dir = new File(filePath)
dir.mkdir()
} else {
OutputStream os = new FileOutputStream(filePath)
try (OutputStream os = new FileOutputStream(filePath)) {
ObjectUtilities.copyStream(zipIn, os)
}
}
zipIn.closeEntry()
entry = zipIn.getNextEntry()
}
} finally {
zipIn.close()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,6 @@ class TransactionCache implements Synchronization {
}

void flushCache(boolean clearRead) {
Map<String, Connection> connectionByGroup = new HashMap<>()
try {
int writeInfoListSize = writeInfoList.size()
if (writeInfoListSize > 0) {
Expand All @@ -456,12 +455,7 @@ class TransactionCache implements Synchronization {
for (int i = 0; i < writeInfoListSize; i++) {
EntityWriteInfo ewi = (EntityWriteInfo) writeInfoList.get(i)
String groupName = ewi.evb.getEntityDefinition().getEntityGroupName()
Connection con = connectionByGroup.get(groupName)
if (con == null) {
con = efi.getConnection(groupName)
connectionByGroup.put(groupName, con)
}

try (Connection con = efi.getConnection(groupName)) {
if (ewi.writeMode.is(WriteMode.CREATE)) {
ewi.evb.basicCreate(con)
createCount++
Expand All @@ -472,6 +466,7 @@ class TransactionCache implements Synchronization {
ewi.evb.basicUpdate(con)
updateCount++
}
}
}
if (logger.isDebugEnabled()) logger.debug("Flushed TransactionCache in ${System.currentTimeMillis() - startTime}ms: ${createCount} creates, ${updateCount} updates, ${deleteCount} deletes, ${readOneCache.size()} read entries, ${readListCache.size()} entities with list cache")
}
Expand All @@ -489,9 +484,6 @@ class TransactionCache implements Synchronization {
} catch (Throwable t) {
logger.error("Error writing values from TransactionCache: ${t.toString()}", t)
throw new XAException("Error writing values from TransactionCache: + ${t.toString()}")
} finally {
// now close connections
for (Connection con in connectionByGroup.values()) con.close()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -791,9 +791,14 @@ class UserFacadeImpl implements UserFacade {
String hashedKey = eci.ecfi.getSimpleHash(loginKey, "", eci.ecfi.getLoginKeyHashType(), false)
Timestamp fromDate = getNowTimestamp()
long thruTime = fromDate.getTime() + Math.round(expireHours * 60*60*1000)
/*
TODO:deepak: Set the require new transaction false, we are getting lock wait timeout exception at second login attempt
Will check and discuss this with community
*/

eci.serviceFacade.sync().name("create", "moqui.security.UserLoginKey")
.parameters([loginKey:hashedKey, userId:userId, fromDate:fromDate, thruDate:new Timestamp(thruTime)])
.disableAuthz().requireNewTransaction(true).call()
.disableAuthz().requireNewTransaction(false).call()

// clean out expired keys
eci.entity.find("moqui.security.UserLoginKey").condition("userId", userId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -827,23 +827,17 @@ class WebFacadeImpl implements WebFacade {
response.setHeader("Content-Disposition", "attachment; filename=\"${rr.getFileName()}\"; filename*=utf-8''${StringUtilities.encodeAsciiFilename(rr.getFileName())}")
}
if (contentType == null || contentType.isEmpty() || ResourceReference.isBinaryContentType(contentType)) {
InputStream is = rr.openStream()
try (InputStream is = rr.openStream()) {
if (is == null) {
logger.warn("Sending not found response, openStream returned null for location: ${location}")
response.sendError(HttpServletResponse.SC_NOT_FOUND, "Resource not found at ${location}")
return
}

try {
OutputStream os = response.outputStream
try {
try (OutputStream os = response.outputStream) {
int totalLen = ObjectUtilities.copyStream(is, os)
logger.info("Streamed ${totalLen} bytes from location ${location}")
} finally {
os.close()
}
} finally {
is.close()
}
}
} else {
String rrText = rr.getText()
Expand Down Expand Up @@ -1386,8 +1380,7 @@ class WebFacadeImpl implements WebFacade {
// first send the empty image
response.setContentType('image/png')
response.setHeader("Content-Disposition", "inline")
OutputStream os = response.outputStream
try { os.write(trackingPng) } finally { os.close() }
try (OutputStream os = response.outputStream) { os.write(trackingPng) }
// mark the message viewed
try {
String emailMessageId = (String) eci.contextStack.get("emailMessageId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,17 @@ private Template makeTemplate(final String location, boolean hasVersion) {
}

Template newTemplate;
Reader templateReader = null;

InputStream is = ecfi.resourceFacade.getLocationStream(location);
try (InputStream is = ecfi.resourceFacade.getLocationStream(location)) {
if (is == null) throw new BaseArtifactException("Template not found at " + location);

try {
templateReader = new InputStreamReader(is, StandardCharsets.UTF_8);
try (Reader templateReader = new InputStreamReader(is, StandardCharsets.UTF_8);) {
newTemplate = new Template(location, templateReader, getFtlConfiguration());
} catch (Exception e) {
throw new BaseArtifactException("Error while initializing template at " + location, e);
} finally {
if (templateReader != null) {
try { templateReader.close(); }
catch (Exception e) { logger.error("Error closing template reader", e); }
}
}
} catch (IOException e) {
throw new BaseArtifactException("Template not found at " + location);
}

if (!hasVersion) templateFtlLocationCache.put(location, newTemplate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,11 @@ class GStringTemplateRenderer implements TemplateRenderer {
if (theTemplate) return theTemplate

Template newTemplate = null
Reader templateReader = null
try {
templateReader = new InputStreamReader(ecfi.resourceFacade.getLocationStream(location))
try (Reader templateReader = new InputStreamReader(ecfi.resourceFacade.getLocationStream(location))) {
GStringTemplateEngine gste = new GStringTemplateEngine()
newTemplate = gste.createTemplate(templateReader)
} catch (Exception e) {
throw new BaseArtifactException("Error while initializing template at [${location}]", e)
} finally {
if (templateReader != null) templateReader.close()
}

if (newTemplate) templateGStringLocationCache.put(location, newTemplate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,11 @@ class XmlActionsScriptRunner implements ScriptRunner {

String templateLocation = ecfi.confXmlRoot.first("resource-facade").attribute("xml-actions-template-location")
Template newTemplate = null
Reader templateReader = null
try {
templateReader = new InputStreamReader(ecfi.resourceFacade.getLocationStream(templateLocation))
try (Reader templateReader = new InputStreamReader(ecfi.resourceFacade.getLocationStream(templateLocation))) {
newTemplate = new Template(templateLocation, templateReader,
ecfi.resourceFacade.ftlTemplateRenderer.getFtlConfiguration())
} catch (Exception e) {
logger.error("Error while initializing XMLActions template at [${templateLocation}]", e)
} finally {
if (templateReader != null) templateReader.close()
}
xmlActionsTemplate = newTemplate
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,12 @@ class EntityDataDocument {
efi.ecfi.getEci().message.addError(efi.ecfi.resource.expand('File ${filename} already exists.','',[filename:filename]))
return 0
}

PrintWriter pw = new PrintWriter(outFile)

int valuesWritten
try (PrintWriter pw = new PrintWriter(outFile)) {
pw.write("[\n")
int valuesWritten = writeDocumentsToWriter(pw, dataDocumentIds, condition, fromUpdateStamp, thruUpdatedStamp, prettyPrint)
valuesWritten = writeDocumentsToWriter(pw, dataDocumentIds, condition, fromUpdateStamp, thruUpdatedStamp, prettyPrint)
pw.write("{}\n]\n")
pw.close()
}
efi.ecfi.getEci().message.addMessage(efi.ecfi.resource.expand('Wrote ${valuesWritten} documents to file ${filename}','',[valuesWritten:valuesWritten,filename:filename]))
return valuesWritten
}
Expand All @@ -85,11 +84,11 @@ class EntityDataDocument {
}
outFile.createNewFile()

PrintWriter pw = new PrintWriter(outFile)
try (PrintWriter pw = new PrintWriter(outFile)) {
pw.write("[\n")
valuesWritten += writeDocumentsToWriter(pw, [dataDocumentId], condition, fromUpdateStamp, thruUpdatedStamp, prettyPrint)
pw.write("{}\n]\n")
pw.close()
}
efi.ecfi.getEci().message.addMessage(efi.ecfi.resource.expand('Wrote ${valuesWritten} records to file ${filename}','',[valuesWritten:valuesWritten, filename:filename]))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class EntityDataLoaderImpl implements EntityDataLoader {
char csvDelimiter = ','
char csvCommentStart = '#'
char csvQuoteChar = '"'
char csvEscapeChar = '\\'

String csvEntityName = null
List<String> csvFieldNames = null
Expand Down Expand Up @@ -115,6 +116,7 @@ class EntityDataLoaderImpl implements EntityDataLoader {
@Override EntityDataLoader csvDelimiter(char delimiter) { this.csvDelimiter = delimiter; return this }
@Override EntityDataLoader csvCommentStart(char commentStart) { this.csvCommentStart = commentStart; return this }
@Override EntityDataLoader csvQuoteChar(char quoteChar) { this.csvQuoteChar = quoteChar; return this }
@Override EntityDataLoader csvEscapeChar(char escapeChar) { this.csvEscapeChar = escapeChar; return this }

@Override EntityDataLoader csvEntityName(String entityName) {
if (!efi.isEntityDefined(entityName) && !sfi.isServiceDefined(entityName))
Expand Down Expand Up @@ -293,21 +295,15 @@ class EntityDataLoaderImpl implements EntityDataLoader {

// load the CSV text in its own transaction
if (this.csvText) {
InputStream csvInputStream = new ByteArrayInputStream(csvText.getBytes("UTF-8"))
try {
try (InputStream csvInputStream = new ByteArrayInputStream(csvText.getBytes("UTF-8"))) {
tf.runUseOrBegin(transactionTimeout, "Error loading CSV entity data", { ech.loadFile("csvText", csvInputStream) })
} finally {
if (csvInputStream != null) csvInputStream.close()
}
}

// load the JSON text in its own transaction
if (this.jsonText) {
InputStream jsonInputStream = new ByteArrayInputStream(jsonText.getBytes("UTF-8"))
try {
try (InputStream jsonInputStream = new ByteArrayInputStream(jsonText.getBytes("UTF-8"))) {
tf.runUseOrBegin(transactionTimeout, "Error loading JSON entity data", { ejh.loadFile("jsonText", jsonInputStream) })
} finally {
if (jsonInputStream != null) jsonInputStream.close()
}
}

Expand All @@ -334,12 +330,11 @@ class EntityDataLoaderImpl implements EntityDataLoader {
TransactionFacade tf = efi.ecfi.transactionFacade
boolean beganTransaction = tf.begin(transactionTimeout)
try {
InputStream inputStream = null
try {

try (InputStream inputStream = efi.ecfi.resourceFacade.getLocationStream(location)) {
logger.info("Loading entity data from ${location}")
long beforeTime = System.currentTimeMillis()

inputStream = efi.ecfi.resourceFacade.getLocationStream(location)
if (inputStream == null) throw new BaseException("Data file not found at ${location}")

long recordsLoaded = 0
Expand Down Expand Up @@ -419,8 +414,6 @@ class EntityDataLoaderImpl implements EntityDataLoader {
}
} catch (TypeToSkipException e) {
// nothing to do, this just stops the parsing when we know the file is not in the types we want
} finally {
if (inputStream != null) inputStream.close()
}
} catch (Throwable t) {
tf.rollback(beganTransaction, "Error loading entity data", t)
Expand Down Expand Up @@ -925,6 +918,7 @@ class EntityDataLoaderImpl implements EntityDataLoader {
.withSkipHeaderRecord(true) // TODO: remove this? does it even do anything?
.withIgnoreEmptyLines(true)
.withIgnoreSurroundingSpaces(true)
.withEscape(edli.csvEscapeChar)
.parse(reader)

Iterator<CSVRecord> iterator = parser.iterator()
Expand Down
Loading

0 comments on commit 1c5ab25

Please sign in to comment.