KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HashMap JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 /**
32  * Base (abstract) backend factory class which holds references to all concrete
33  * backend factories and defines abstract methods which must be implemented in
34  * all concrete factory implementations.<p>
35  *
36  * Factory classes are used to create concrete {@link RrdBackend} implementations.
37  * Each factory creates unlimited number of specific backend objects.
38  *
39  * JRobin supports three different backend types (backend factories) out of the box:<p>
40  * <ul>
41  * <li>{@link RrdFileBackend}: objects of this class are created from the
42  * {@link RrdFileBackendFactory} class. This was the default backend used in all
43  * JRobin releases before 1.4.0 release. It uses java.io.* package and RandomAccessFile class to store
44  * RRD data in files on the disk.
45  *
46  * <li>{@link RrdNioBackend}: objects of this class are created from the
47  * {@link RrdNioBackendFactory} class. The backend uses java.io.* and java.nio.*
48  * classes (mapped ByteBuffer) to store RRD data in files on the disk. This is the default backend
49  * since 1.4.0 release.
50  *
51  * <li>{@link RrdMemoryBackend}: objects of this class are created from the
52  * {@link RrdMemoryBackendFactory} class. This backend stores all data in memory. Once
53  * JVM exits, all data gets lost. The backend is extremely fast and memory hungry.
54  * </ul>
55  *
56  * Each backend factory is identifed by its {@link #getFactoryName() name}. Constructors
57  * are provided in the {@link RrdDb} class to create RrdDb objects (RRD databases)
58  * backed with a specific backend.<p>
59  *
60  * See javadoc for {@link RrdBackend} to find out how to create your custom backends.
61  */

62 public abstract class RrdBackendFactory {
63     private static final HashMap JavaDoc factories = new HashMap JavaDoc();
64     private static RrdBackendFactory defaultFactory;
65
66     static {
67         try {
68             RrdFileBackendFactory fileFactory = new RrdFileBackendFactory();
69             registerFactory(fileFactory);
70             RrdMemoryBackendFactory memoryFactory = new RrdMemoryBackendFactory();
71             registerFactory(memoryFactory);
72             RrdNioBackendFactory nioFactory = new RrdNioBackendFactory();
73             registerFactory(nioFactory);
74
75             // Here is the default backend factory
76
defaultFactory = nioFactory;
77
78         } catch (RrdException e) {
79             throw new RuntimeException JavaDoc("FATAL: Cannot register RRD backend factories: " + e);
80         }
81     }
82
83     /**
84      * Returns backend factory for the given backend factory name.
85      * @param name Backend factory name. Initially supported names are:<p>
86      * <ul>
87      * <li><b>FILE</b>: Default factory which creates backends based on the
88      * java.io.* package. RRD data is stored in files on the disk
89      * <li><b>NIO</b>: Factory which creates backends based on the
90      * java.nio.* package. RRD data is stored in files on the disk
91      * <li><b>MEMORY</b>: Factory which creates memory-oriented backends.
92      * RRD data is stored in memory, it gets lost as soon as JVM exits.
93      * </ul>
94      * @return Backend factory for the given factory name
95      * @throws RrdException Thrown if no factory with the given name
96      * is available.
97      */

98     public static synchronized RrdBackendFactory getFactory(String JavaDoc name) throws RrdException {
99         RrdBackendFactory factory = (RrdBackendFactory) factories.get(name);
100         if(factory != null) {
101             return factory;
102         }
103         else {
104             throw new RrdException("No backend factory found with the name specified [" + name + "]");
105         }
106     }
107
108     /**
109      * Registers new (custom) backend factory within the JRobin framework.
110      * @param factory Factory to be registered
111      * @throws RrdException Thrown if the name of the specified factory is already
112      * used.
113      */

114     public static synchronized void registerFactory(RrdBackendFactory factory)
115         throws RrdException {
116         String JavaDoc name = factory.getFactoryName();
117         if(!factories.containsKey(name)) {
118             factories.put(name, factory);
119         }
120         else {
121             throw new RrdException("Backend factory of this name2 (" + name +
122                 ") already exists and cannot be registered");
123         }
124     }
125
126     /**
127      * Registers new (custom) backend factory within the JRobin framework and sets this
128      * factory as the default.
129      * @param factory Factory to be registered and set as default
130      * @throws RrdException Thrown if the name of the specified factory is already
131      * used.
132      */

133     public static synchronized void registerAndSetAsDefaultFactory(RrdBackendFactory factory)
134             throws RrdException {
135         registerFactory(factory);
136         setDefaultFactory(factory.getFactoryName());
137     }
138
139     /**
140      * Returns the defaul backend factory. This factory is used to construct
141      * {@link RrdDb} objects if no factory is specified in the RrdDb constructor.
142      * @return Default backend factory.
143      */

144     public static RrdBackendFactory getDefaultFactory() {
145         return defaultFactory;
146     }
147
148     /**
149      * Replaces the default backend factory with a new one. This method must be called before
150      * the first RRD gets created. <p>
151      * @param factoryName Name of the default factory. Out of the box, JRobin supports three
152      * different RRD backends: "FILE" (java.io.* based), "NIO" (java.nio.* based) and "MEMORY"
153      * (byte[] based).
154      * @throws RrdException Thrown if invalid factory name is supplied or not called before
155      * the first RRD is created.
156      */

157     public static void setDefaultFactory(String JavaDoc factoryName) throws RrdException {
158         // We will allow this only if no RRDs are created
159
if(RrdBackend.getCount() == 0) {
160             defaultFactory = getFactory(factoryName);
161         }
162         else {
163             throw new RrdException("Could not change the default backend factory. " +
164                     "This method must be called before the first RRD gets created");
165         }
166     }
167
168     /**
169      * Creates RrdBackend object for the given storage path.
170      * @param path Storage path
171      * @param readOnly True, if the storage should be accessed in read/only mode.
172      * False otherwise.
173      * @param lockMode One of the following constants: {@link RrdDb#NO_LOCKS},
174      * {@link RrdDb#EXCEPTION_IF_LOCKED} or {@link RrdDb#WAIT_IF_LOCKED}.
175      * @return Backend object which handles all I/O operations for the given storage path
176      * @throws IOException Thrown in case of I/O error.
177      */

178     protected abstract RrdBackend open(String JavaDoc path, boolean readOnly, int lockMode)
179             throws IOException JavaDoc;
180
181     /**
182      * Method to determine if a storage with the given path already exists.
183      * @param path Storage path
184      * @return True, if such storage exists, false otherwise.
185      */

186     protected abstract boolean exists(String JavaDoc path) throws IOException JavaDoc;
187
188     /**
189      * Returns the name (primary ID) for the factory.
190      * @return Name of the factory.
191      */

192     public abstract String JavaDoc getFactoryName();
193 }
194
Popular Tags