001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2015 ForgeRock AS.
015 */
016package org.forgerock.audit.rotation;
017
018/**
019 * Creates a file size based rotation policy. Once a file is a given size in bytes it is rotated.
020 */
021public class SizeBasedRotationPolicy implements RotationPolicy {
022
023    private final long maxFileSizeInBytes;
024
025    /**
026     * Constructs a SizeBasedRotationPolicy given a max file size in bytes.
027     * @param maxFileSizeInBytes A max file size in bytes.
028     */
029    public SizeBasedRotationPolicy(final long maxFileSizeInBytes) {
030        this.maxFileSizeInBytes = maxFileSizeInBytes;
031    }
032
033    /**
034     * Indicates whether or not a {@link RotatableObject} needs rotation.
035     * @param file The {@link RotatableObject} to be checked.
036     * @return True - If the {@link RotatableObject} needs rotation.
037     *         False - If the {@link RotatableObject} doesn't need rotation.
038     */
039    @Override
040    public boolean shouldRotateFile(RotatableObject file) {
041        return maxFileSizeInBytes > 0L && file.getBytesWritten() >= maxFileSizeInBytes;
042    }
043
044    /**
045     * Gets the maximum size (in bytes) a file may grow to before being rotated.
046     *
047     * @return the maximum file size permitted by this policy.
048     */
049    public long getMaxFileSizeInBytes() {
050        return maxFileSizeInBytes;
051    }
052}