001    /* ============================================================
002     * JRobin : Pure java implementation of RRDTool's functionality
003     * ============================================================
004     *
005     * Project Info:  http://www.jrobin.org
006     * Project Lead:  Sasa Markovic (saxon@jrobin.org);
007     *
008     * (C) Copyright 2003-2005, by Sasa Markovic.
009     *
010     * Developers:    Sasa Markovic (saxon@jrobin.org)
011     *
012     *
013     * This library is free software; you can redistribute it and/or modify it under the terms
014     * of the GNU Lesser General Public License as published by the Free Software Foundation;
015     * either version 2.1 of the License, or (at your option) any later version.
016     *
017     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
018     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
019     * See the GNU Lesser General Public License for more details.
020     *
021     * You should have received a copy of the GNU Lesser General Public License along with this
022     * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
023     * Boston, MA 02111-1307, USA.
024     */
025    
026    package org.jrobin.core;
027    
028    import java.io.IOException;
029    
030    /**
031     * Factory class which creates actual {@link RrdNioBackend} objects. This is the default factory since
032     * 1.4.0 version
033     */
034    public class RrdNioBackendFactory extends RrdFileBackendFactory {
035            /**
036             * factory name, "NIO"
037             */
038            public static final String NAME = "NIO";
039    
040            /**
041             * Period in seconds between consecutive synchronizations when
042             * sync-mode is set to SYNC_BACKGROUND. By default in-memory cache will be
043             * transferred to the disc every 300 seconds (5 minutes). Default value can be
044             * changed via {@link #setSyncPeriod(int)} method.
045             */
046            public static final int DEFAULT_SYNC_PERIOD = 300; // seconds
047    
048            private static int syncPeriod = DEFAULT_SYNC_PERIOD;
049    
050            /**
051             * Returns time between two consecutive background synchronizations. If not changed via
052             * {@link #setSyncPeriod(int)} method call, defaults to {@link #DEFAULT_SYNC_PERIOD}.
053             * See {@link #setSyncPeriod(int)} for more information.
054             *
055             * @return Time in seconds between consecutive background synchronizations.
056             */
057            public static int getSyncPeriod() {
058                    return syncPeriod;
059            }
060    
061            /**
062             * Sets time between consecutive background synchronizations.
063             *
064             * @param syncPeriod Time in seconds between consecutive background synchronizations.
065             */
066            public static void setSyncPeriod(int syncPeriod) {
067                    RrdNioBackendFactory.syncPeriod = syncPeriod;
068            }
069    
070            /**
071             * Creates RrdNioBackend object for the given file path.
072             *
073             * @param path   File path
074             * @param readOnly True, if the file should be accessed in read/only mode.
075             *                 False otherwise.
076             * @return RrdNioBackend object which handles all I/O operations for the given file path
077             * @throws IOException Thrown in case of I/O error.
078             */
079            protected RrdBackend open(String path, boolean readOnly) throws IOException {
080                    return new RrdNioBackend(path, readOnly, syncPeriod);
081            }
082    
083            /**
084             * Returns the name of this factory.
085             *
086             * @return Factory name (equals to string "NIO")
087             */
088            public String getFactoryName() {
089                    return NAME;
090            }
091    }