org.apache.tika.io
Class TemporaryResources

java.lang.Object
  extended by org.apache.tika.io.TemporaryResources
All Implemented Interfaces:
Closeable

public class TemporaryResources
extends Object
implements Closeable

Utility class for tracking and ultimately closing or otherwise disposing a collection of temporary resources.

Note that this class is not thread-safe.

Since:
Apache Tika 0.10

Constructor Summary
TemporaryResources()
           
 
Method Summary
 void addResource(Closeable resource)
          Adds a new resource to the set of tracked resources that will all be closed when the close() method is called.
 void close()
          Closes all tracked resources.
 File createTemporaryFile()
          Creates and returns a temporary file that will automatically be deleted when the close() method is called.
 void dispose()
          Calls the close() method and wraps the potential IOException into a TikaException for convenience when used within Tika.
<T extends Closeable>
T
getResource(Class<T> klass)
          Returns the latest of the tracked resources that implements or extends the given interface or class.
 void setTemporaryFileDirectory(File tmp)
          Sets the directory to be used for the temporary files created by the createTemporaryFile() method.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TemporaryResources

public TemporaryResources()
Method Detail

setTemporaryFileDirectory

public void setTemporaryFileDirectory(File tmp)
Sets the directory to be used for the temporary files created by the createTemporaryFile() method.

Parameters:
tmp - temporary file directory, or null for the system default

createTemporaryFile

public File createTemporaryFile()
                         throws IOException
Creates and returns a temporary file that will automatically be deleted when the close() method is called.

Returns:
Throws:
IOException

addResource

public void addResource(Closeable resource)
Adds a new resource to the set of tracked resources that will all be closed when the close() method is called.

Parameters:
resource - resource to be tracked

getResource

public <T extends Closeable> T getResource(Class<T> klass)
Returns the latest of the tracked resources that implements or extends the given interface or class.

Parameters:
klass - interface or class
Returns:
matching resource, or null if not found

close

public void close()
           throws IOException
Closes all tracked resources. The resources are closed in reverse order from how they were added.

Any thrown exceptions from managed resources are collected and then re-thrown only once all the resources have been closed.

Specified by:
close in interface Closeable
Throws:
IOException - if one or more of the tracked resources could not be closed

dispose

public void dispose()
             throws TikaException
Calls the close() method and wraps the potential IOException into a TikaException for convenience when used within Tika.

Throws:
TikaException - if one or more of the tracked resources could not be closed


Copyright © 2007-2012 The Apache Software Foundation. All Rights Reserved.