user3806245
user3806245

Reputation:

How can I limit the scope of System.setProperty to only to the method that sets it?

I am working on an export that requires the files to be stored in a folder inside the tmp folder, and each folder must be different for different exports.

So my export() method does the below:

System.setProperty("java.io.tmpdir", System.getProperty("java.io.tmpdir")+pathSpecificToFirstExport);

the createTempFile method makes use of System.getProperty("java.io.tmpdir") to store the files in it.

While the above method is running, another call to export() sets the new System.getProperty("java.io.tmpdir") to System.getProperty("java.io.tmpdir")+pathSpecificToFirstExport+pathSpecificToSecondExport while what I really want is only System.getProperty("java.io.tmpdir")+pathSpecificToSecondExport.

I cannot hardcode System.getProperty("java.io.tmpdir") instead of appending new path to it everytime as System.getProperty("java.io.tmpdir") changes for different environments. I cannot change the way temp file is created as it is not done by me, but by write() of SXSSFWorkbook.java:

File tmplFile = TempFile.createTempFile("poi-sxssf-template", ".xlsx");

What I am looking for is to limit the scope of System.getProperty("java.io.tmpdir") only to the instance of method export()

Any thoughts?

Upvotes: 0

Views: 1274

Answers (4)

Stephen C
Stephen C

Reputation: 718946

You can't do that. The System properties object is effectively global, and there is no scoping mechanism applicable to it.

What you need to do is use a different mechanism for creating temporary files that doesn't depend on the "java.io.tmpdir". Solution: use createTempFile(String prefix, String suffix, File directory), and keep track of the "current" temporary directory using (for example) thread locals.

Better still, use the equivalent method in java.nio.Files.


Unfortunately I cannot make the above change since the createTempDirectory is done by another method that I cannot change (SXSSFWorkbook does that for me).

So I took a look at SXSSFWorkbook, and here is where it is creating temporary files:

/**
 * Write out this workbook to an Outputstream.
 *
 * @param stream - the java OutputStream you wish to write to
 * @exception IOException if anything can't be written.
 */
public void write(OutputStream stream) throws IOException {
    for (SXSSFSheet sheet : _xFromSxHash.values()) {
        sheet.flushRows();
    }

    //Save the template
    File tmplFile = File.createTempFile("poi-sxssf-template", ".xlsx");
    tmplFile.deleteOnExit();
    FileOutputStream os = new FileOutputStream(tmplFile);
    _wb.write(os);
    os.close();

    //Substitute the template entries with the generated sheet data files
    injectData(tmplFile, stream);
    tmplFile.delete();
}

First of all, Apache-POI is open source, and that means that you are free to modify it, if you need to. In this case, modifying the write method would be better than trying to make it behave differently by messing around with the global temporary directory.

But this begs the question: Why are you trying to do this? Looking at the code of write, it is pretty clear that the method is designed to clean up after itself. If write terminates normally, the temporary file is deleted before the method returns. If it terminates abnormally, the file should be cleaned up when the JVM exits.

And if, despite the above, temporary files are still "leaking", then it should be a simple matter to write an external script that periodically finds and deletes them.

Upvotes: 4

deathyr
deathyr

Reputation: 423

If you can run the jobs from maven commands you can try

mvn <command to execute job> -Djava.io.tmpdir=absolutePathSpecificToFirstExport

and run a mvn command for each job.

Upvotes: -2

elhefe
elhefe

Reputation: 3494

You can't. setProperty is always global. What you should do instead is something like

Files.createTempDirectory(System.getProperty("java.io.tempdir") + pathSpecificToFirstExport);

Upvotes: 0

deathyr
deathyr

Reputation: 423

Can you save the first value of "java.io.tmpdir" in a temp variable and the set it back once you finish?

String defaultDir = System.getProperty("java.io.tmpdir")
System.setProperty("java.io.tmpdir", System.getProperty("java.io.tmpdir")+pathSpecificToFirstExport);
//createTempFile method
System.setProperty("java.io.tmpdir",defaultDir);

Upvotes: 0

Related Questions