Skip to content

Commit

Permalink
drop logFile access; rather use a normal logger for pdf-A conversion
Browse files Browse the repository at this point in the history
- it's bad practice to enforce IO in the code, rather than by
configuration
  • Loading branch information
ivy-rew committed Oct 31, 2023
1 parent 5f75f94 commit b117221
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.junit.jupiter.api.Test;

import ch.ivyteam.ivy.addons.docfactory.exception.DocFactoryException;
import ch.ivyteam.ivy.addons.docfactory.log.DocFactoryLogDirectoryRetriever;
import ch.ivyteam.ivy.addons.docfactory.pdf.PdfAType;
import ch.ivyteam.ivy.addons.docfactory.pdfbox.PdfAValidator;

Expand Down Expand Up @@ -48,7 +47,6 @@ public static void setupClass() throws URISyntaxException {
@BeforeEach
public void setup() throws Exception {
asposePdfFactory = new AsposePdfFactory(false);
asposePdfFactory.setLogDirectoryRetriever(new LogDirectoryRetriever());
}

@Test
Expand Down Expand Up @@ -209,16 +207,4 @@ private java.io.File makePDFToConvert(String pdfToConvertPath) throws IOExceptio
return pdfToConvert;
}

private class LogDirectoryRetriever implements DocFactoryLogDirectoryRetriever {

@Override
public java.io.File getLogDirectory() {
java.io.File logDir = new java.io.File("test/logs");
if (!logDir.isDirectory()) {
logDir.mkdir();
}
return new java.io.File("test/logs");
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

import com.aspose.pdf.ConvertErrorAction;
Expand All @@ -18,20 +15,15 @@
import ch.ivyteam.api.API;
import ch.ivyteam.ivy.addons.docfactory.PdfFactory;
import ch.ivyteam.ivy.addons.docfactory.exception.DocFactoryException;
import ch.ivyteam.ivy.addons.docfactory.log.DocFactoryLogDirectoryRetriever;
import ch.ivyteam.ivy.addons.docfactory.log.IvyLogDirectoryRetriever;
import ch.ivyteam.ivy.addons.docfactory.pdf.PdfAType;
import ch.ivyteam.ivy.environment.Ivy;
import ch.ivyteam.ivy.scripting.objects.File;
import ch.ivyteam.log.Logger;

public class AsposePdfFactory extends PdfFactory {

private static final long PDF_FACTORY_LOG_MAX_BYTE_SIZE = 5 * 1024;
private static final Logger LOGGER = Logger.getLogger(AsposePdfFactory.class);
private static final String PDF_EXTENSION = ".pdf";
private static final String PDF_FACTORY_LOG_FILENAME = "pdfFactory.log";
private static final String JAVA_IO_TMPDIR = "java.io.tmpdir";

private DocFactoryLogDirectoryRetriever logDirectoryRetriever;

public AsposePdfFactory() {
init();
Expand All @@ -56,8 +48,7 @@ private void init() {
}

@Override
public File appendPdfFiles(String resultFilePath, List<java.io.File> pdfsToAppend)
throws DocFactoryException {
public File appendPdfFiles(String resultFilePath, List<java.io.File> pdfsToAppend) throws DocFactoryException {
API.checkNotEmpty(resultFilePath, "resultFileName");
API.checkNotNull(pdfsToAppend, "pdfsToAppend");
API.checkNotEmpty(pdfsToAppend, "pdfsToAppend");
Expand All @@ -66,8 +57,7 @@ public File appendPdfFiles(String resultFilePath, List<java.io.File> pdfsToAppen
Document pdfDocument = new Document(new FileInputStream(pdfsToAppend.get(0)));
return appendFilesToDocument(pdfDocument, pdfsToAppend.subList(1, pdfsToAppend.size()), resultFilePath);
} catch (Exception ex) {
throw new DocFactoryException("An error occurred while generating the pdf file. " + ex.getMessage(),
ex);
throw new DocFactoryException("An error occurred while generating the pdf file. " + ex.getMessage(), ex);
}
}

Expand All @@ -86,33 +76,15 @@ public void convertToPdfA(String filePath, PdfAType pdfAType)
} catch (Exception ex) {
throw new DocFactoryException(ex);
} finally {
try {
var logs = new String(bos.toByteArray());
if (!logs.isBlank()) {
var file = getAsposePdfFactoryLogFile().toPath();
Files.writeString(file, logs, StandardOpenOption.APPEND);
}
} catch (IOException ex) {
throw new RuntimeException(ex);
var logs = new String(bos.toByteArray());
if (!logs.isBlank()) {
logs.lines().forEachOrdered(logLine -> {
LOGGER.error(logLine);
});
}
}
}

public DocFactoryLogDirectoryRetriever getLogDirectoryRetriever() {
if (this.logDirectoryRetriever == null) {
this.setLogDirectoryRetriever(new IvyLogDirectoryRetriever());
}
return logDirectoryRetriever;
}

/**
* visible for unit tests
* @param logDirectoryRetriever
*/
protected void setLogDirectoryRetriever(DocFactoryLogDirectoryRetriever logDirectoryRetriever) {
this.logDirectoryRetriever = logDirectoryRetriever;
}

private PdfFormat getAsposePDFAType(PdfAType pdfAType) {
switch (pdfAType) {
case PDF_A_1A:
Expand All @@ -136,23 +108,6 @@ private PdfFormat getAsposePDFAType(PdfAType pdfAType) {
}
}

private java.io.File getAsposePdfFactoryLogFile() throws IOException {
java.io.File logDir = this.getLogDirectoryRetriever().getLogDirectory();
if (logDir == null) {
logDir = new java.io.File(System.getProperty(JAVA_IO_TMPDIR));
}
java.io.File logFile = new java.io.File(logDir, PDF_FACTORY_LOG_FILENAME);
if (!logFile.exists()) {
logFile.createNewFile();
} else if (FileUtils.sizeOf(logFile) > PDF_FACTORY_LOG_MAX_BYTE_SIZE) {
FileUtils.moveFile(logFile,
new java.io.File(logFile.getParentFile(), logFile.getName() + System.nanoTime()));
logFile.createNewFile();
}

return logFile;
}

/**
* Only visible for testing
*/
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit b117221

Please sign in to comment.