KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > core > RrdNioBackendFactory


1 /* ============================================================
2  * JRobin : Pure java implementation of RRDTool's functionality
3  * ============================================================
4  *
5  * Project Info: http://www.jrobin.org
6  * Project Lead: Sasa Markovic (saxon@jrobin.org);
7  *
8  * (C) Copyright 2003, by Sasa Markovic.
9  *
10  * Developers: Sasa Markovic (saxon@jrobin.org)
11  * Arne Vandamme (cobralord@jrobin.org)
12  *
13  * This library is free software; you can redistribute it and/or modify it under the terms
14  * of the GNU Lesser General Public License as published by the Free Software Foundation;
15  * either version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License along with this
22  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */

25
26 package org.jrobin.core;
27
28 import java.io.IOException JavaDoc;
29
30 /**
31  * Factory class which creates actual {@link RrdNioBackend} objects. This is the default factory since
32  * 1.4.0 version
33  */

34 public class RrdNioBackendFactory extends RrdFileBackendFactory{
35     /** factory name, "NIO" */
36     public static final String JavaDoc NAME = "NIO";
37
38     /** See {@link #setSyncMode(int)} for explanation */
39     public static final int SYNC_ONCLOSE = 0; // will sync() only on close()
40
/** See {@link #setSyncMode(int)} for explanation */
41     public static final int SYNC_BEFOREUPDATE = 1;
42     /** See {@link #setSyncMode(int)} for explanation */
43     public static final int SYNC_AFTERUPDATE = 2;
44     /** See {@link #setSyncMode(int)} for explanation */
45     public static final int SYNC_BEFOREFETCH = 3;
46     /** See {@link #setSyncMode(int)} for explanation */
47     public static final int SYNC_AFTERFETCH = 4;
48     /** See {@link #setSyncMode(int)} for explanation */
49     public static final int SYNC_BACKGROUND = 5;
50     /**
51      * Period in seconds between consecutive synchronizations when
52      * sync-mode is set to SYNC_BACKGROUND. By default in-memory cache will be
53      * transferred to the disc every 300 seconds (5 minutes). Default value can be
54      * changed via {@link #setSyncPeriod(int)} method.
55      */

56     public static final int DEFAULT_SYNC_PERIOD = 300; // seconds
57

58     private static int syncMode = SYNC_BACKGROUND;
59     private static int syncPeriod = DEFAULT_SYNC_PERIOD;
60
61     /**
62      * Returns the current synchronization mode between backend data in memory and data
63      * in the persistent storage (disk file).
64      *
65      * @return Integer representing current synchronization mode (SYNC_ONCLOSE,
66      * SYNC_BEFOREUPDATE, SYNC_AFTERUPDATE, SYNC_BEFOREFETCH, SYNC_AFTERFETCH or
67      * SYNC_BACKGROUND). See {@link #setSyncMode(int)} for full explanation of these return values.
68      */

69     public static int getSyncMode() {
70         return syncMode;
71     }
72
73     /**
74      * Sets the current synchronization mode between backend data in memory (backend cache) and
75      * RRD data in the persistant storage (disk file).<p>
76      * @param syncMode Desired synchronization mode. Possible values are:<p>
77      * <ul>
78      * <li>SYNC_ONCLOSE: synchronization will be performed only when {@link RrdDb#close()}
79      * is called (RRD file is closed) or when {@link RrdDb#sync()} method is called.
80      * <li>SYNC_BEFOREUPDATE: synchronization will be performed before each {@link Sample#update()}
81      * call (right before RRD file is about to be updated).
82      * <li>SYNC_AFTERUPDATE: synchronization will be performed after each {@link Sample#update()}
83      * call (right after RRD file is updated).
84      * <li>SYNC_BEFOREFETCH: synchronization will be performed before each
85      * {@link FetchRequest#fetchData()} call (right before data is about to be fetched from a RRD file,
86      * for example for graph creation)
87      * <li>SYNC_AFTERFETCH: synchronization will be performed after each
88      * {@link FetchRequest#fetchData()} call (right after data is fetched from a RRD file)
89      * <li>SYNC_BACKGROUND (<b>default</b>): synchronization will be performed automatically
90      * from a separate thread on a regular basis. Period of time between the two consecutive
91      * synchronizations can be controlled with {@link #setSyncPeriod(int)}.
92      * </ul>
93      */

94     public static void setSyncMode(int syncMode) {
95         RrdNioBackendFactory.syncMode = syncMode;
96     }
97
98     /**
99      * Returns time between two consecutive background synchronizations. If not changed via
100      * {@link #setSyncPeriod(int)} method call, defaults to {@link #DEFAULT_SYNC_PERIOD}.
101      * See {@link #setSyncPeriod(int)} for more information.
102      * @return Time in seconds between consecutive background synchronizations.
103      */

104     public static int getSyncPeriod() {
105         return syncPeriod;
106     }
107
108     /**
109      * Sets time between consecutive background synchronizations. Method is effective only if
110      * synchronization mode is set to SYNC_BACKGROUND.
111      * @param syncPeriod Time in seconds between consecutive background synchronizations.
112      */

113     public static void setSyncPeriod(int syncPeriod) {
114         RrdNioBackendFactory.syncPeriod = syncPeriod;
115     }
116
117     /**
118      * Creates RrdNioBackend object for the given file path.
119      * @param path File path
120      * @param readOnly True, if the file should be accessed in read/only mode.
121      * False otherwise.
122      * @param lockMode One of the following constants: {@link RrdDb#NO_LOCKS},
123      * {@link RrdDb#EXCEPTION_IF_LOCKED} or {@link RrdDb#WAIT_IF_LOCKED}.
124      * @return RrdNioBackend object which handles all I/O operations for the given file path
125      * @throws IOException Thrown in case of I/O error.
126      */

127     protected RrdBackend open(String JavaDoc path, boolean readOnly, int lockMode) throws IOException JavaDoc {
128         return new RrdNioBackend(path, readOnly, lockMode, syncMode, syncPeriod);
129     }
130
131     /**
132      * Returns the name of this factory.
133      * @return Factory name (equals to string "NIO")
134      */

135     public String JavaDoc getFactoryName() {
136         return NAME;
137     }
138 }
139
Popular Tags