Class RotatableWriter
- java.lang.Object
-
- org.forgerock.audit.events.handlers.writers.RotatableWriter
-
- All Implemented Interfaces:
TextWriter
,RotatableObject
public class RotatableWriter extends Object implements TextWriter, RotatableObject
Supports file rotation and retention.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
RotatableWriter.RolloverLifecycleHook
Callback hooks to allow custom action to be taken before and after the checks for rotation and retention is performed.-
Nested classes/interfaces inherited from interface org.forgerock.audit.events.handlers.writers.TextWriter
TextWriter.Stream
-
-
Field Summary
Fields Modifier and Type Field Description static RotatableWriter.RolloverLifecycleHook
NOOP_ROLLOVER_LIFECYCLE_HOOK
A RolloverLifecycleHook that does nothing.
-
Constructor Summary
Constructors Constructor Description RotatableWriter(File file, FileBasedEventHandlerConfiguration configuration, boolean append)
Constructs aRotatableWriter
given an initial file to manage rotation/retention, and aFileBasedEventHandlerConfiguration
.RotatableWriter(File file, FileBasedEventHandlerConfiguration configuration, boolean append, RotatableWriter.RolloverLifecycleHook rolloverLifecycleHook)
Constructs aRotatableWriter
given an initial file to manage rotation/retention, a aFileBasedEventHandlerConfiguration
and aRotatableWriter.RolloverLifecycleHook
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Closes the rotatable file.void
flush()
Flushes any buffered contents of the output stream.boolean
forceRotation()
Forces a rotation of the writer.long
getBytesWritten()
Retrieves the number of bytes written by this writer.OffsetDateTime
getLastRotationTime()
Retrieves the last time the file was rotated.void
registerRotationHooks(RotationHooks rotationHooks)
Registers hooks into the rotation process.boolean
rotateIfNeeded()
Rotate the log file if any of the configured rotation policies determine that rotation is required.void
shutdown()
Releases any resources held by the writer.void
write(String str)
Writes some text to the output stream.
-
-
-
Field Detail
-
NOOP_ROLLOVER_LIFECYCLE_HOOK
public static final RotatableWriter.RolloverLifecycleHook NOOP_ROLLOVER_LIFECYCLE_HOOK
A RolloverLifecycleHook that does nothing.
-
-
Constructor Detail
-
RotatableWriter
public RotatableWriter(File file, FileBasedEventHandlerConfiguration configuration, boolean append) throws IOException
Constructs aRotatableWriter
given an initial file to manage rotation/retention, and aFileBasedEventHandlerConfiguration
.- Parameters:
file
- The initial file to manage rotation/retention.configuration
- The configuration of the rotation and retention policies.append
- Whether to append to the rotatable file or not.- Throws:
IOException
- If a problem occurs.
-
RotatableWriter
public RotatableWriter(File file, FileBasedEventHandlerConfiguration configuration, boolean append, RotatableWriter.RolloverLifecycleHook rolloverLifecycleHook) throws IOException
Constructs aRotatableWriter
given an initial file to manage rotation/retention, a aFileBasedEventHandlerConfiguration
and aRotatableWriter.RolloverLifecycleHook
.- Parameters:
file
- The initial file to manage rotation/retention.configuration
- The configuration of the rotation and retention policies.append
- Whether to append to the rotatable file or not.rolloverLifecycleHook
- Hook to use before and after rotation/retention checks.- Throws:
IOException
- If a problem occurs.
-
-
Method Detail
-
rotateIfNeeded
public boolean rotateIfNeeded() throws IOException
Rotate the log file if any of the configured rotation policies determine that rotation is required.- Specified by:
rotateIfNeeded
in interfaceRotatableObject
- Returns:
true
if rotation took place.- Throws:
IOException
- If unable to rotate the log file.
-
getBytesWritten
public long getBytesWritten()
Description copied from interface:TextWriter
Retrieves the number of bytes written by this writer.- Specified by:
getBytesWritten
in interfaceRotatableObject
- Specified by:
getBytesWritten
in interfaceTextWriter
- Returns:
- the number of bytes written by this writer.
-
getLastRotationTime
public OffsetDateTime getLastRotationTime()
Description copied from interface:RotatableObject
Retrieves the last time the file was rotated. If a file rotation never occurred, this value will be the time the server started.- Specified by:
getLastRotationTime
in interfaceRotatableObject
- Returns:
- The last time file rotation occurred.
-
close
public void close() throws IOException
Description copied from interface:RotatableObject
Closes the rotatable file.- Specified by:
close
in interfaceRotatableObject
- Throws:
IOException
- If an exception occurs while closing.
-
shutdown
public void shutdown()
Description copied from interface:TextWriter
Releases any resources held by the writer.- Specified by:
shutdown
in interfaceTextWriter
-
registerRotationHooks
public void registerRotationHooks(RotationHooks rotationHooks)
Description copied from interface:RotatableObject
Registers hooks into the rotation process.- Specified by:
registerRotationHooks
in interfaceRotatableObject
- Parameters:
rotationHooks
- TheRotationHooks
into the rotation process.
-
write
public void write(String str) throws IOException
Description copied from interface:TextWriter
Writes some text to the output stream.- Specified by:
write
in interfaceTextWriter
- Parameters:
str
- The text to write- Throws:
IOException
- If a problem occurs.
-
forceRotation
public boolean forceRotation() throws IOException
Forces a rotation of the writer.- Returns:
true
if rotation was done,false
otherwise.- Throws:
IOException
- If an error occurs
-
flush
public void flush() throws IOException
Description copied from interface:TextWriter
Flushes any buffered contents of the output stream.- Specified by:
flush
in interfaceTextWriter
- Throws:
IOException
- If a problem occurs.
-
-