KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Developers: Sasa Markovic (saxon@jrobin.org)
9  * Arne Vandamme (cobralord@jrobin.org)
10  *
11  * (C) Copyright 2003, by Sasa Markovic.
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 package org.jrobin.core;
26
27 import java.io.IOException JavaDoc;
28
29 /**
30  * <p>Class that represents an object that can be used to perform the actual
31  * opening and closing of RRD files, using different methods. Other objects
32  * like the FetchSourceList representing the Graph datasources
33  * ({@link org.jrobin.graph.FetchSourceList}) use a RrdOpener to retrieve the RrdDb instances of
34  * RRD datasources.</p>
35  * <p>Overriding the RrdOpener class allows finetuned access on the level
36  * of RrdDb retrieval and release. An child class could for example add
37  * log or debug statements, gather statistics on RRD access, or provide
38  * a transparent way to to access RRD datasources in an alternative way
39  * like through a DBMS. </p>
40  *
41  * @author Arne Vandamme (cobralord@jrobin.org)
42  */

43 public class RrdOpener
44 {
45     // ================================================================
46
// -- Members
47
// ================================================================
48
protected RrdDbPool pool;
49
50     protected boolean readOnly = false;
51     protected boolean usePool = false;
52
53
54     // ================================================================
55
// -- Constructors
56
// ================================================================
57
/**
58      * Creates a new RrdOpener that will open RrdDb objects with read/write
59      * access. If the usePool flag is set, the RrdOpener will use the RrdDbPool
60      * to retrieve RrdDb instances.
61      * @param usePool True if the RrdOpener should use the RrdDbPool.
62      */

63     public RrdOpener( boolean usePool )
64     {
65         this.usePool = usePool;
66
67         if ( usePool )
68             pool = RrdDbPool.getInstance();
69     }
70
71     /**
72      * Creates a new RrdOpener that will open RrdDb objects with read/write
73      * or read-only access, depending on the readOnly flag.. If the usePool
74      * flag is set, the RrdOpener will use the RrdDbPool to retrieve RrdDb
75      * instances.
76      * @param usePool True if the RrdOpener should use the RrdDbPool.
77      * @param readOnly True if the RrdOpener should open RrdDb objects as read-only.
78      */

79     public RrdOpener( boolean usePool, boolean readOnly )
80     {
81         this( usePool );
82         this.readOnly = readOnly;
83     }
84
85
86     // ================================================================
87
// -- Public methods
88
// ================================================================
89
/**
90      * Retrieves the RrdDb instance matching a specific RRD datasource name
91      * (usually a file name) and using a specified RrdBackendFactory.
92      *
93      * @param rrdFile Name of the RRD datasource.
94      * @param backendFactory BackendFactory to use for retrieval.
95      * @return RrdDb instance of the datasource.
96      * @throws IOException Thrown in case of I/O error.
97      * @throws RrdException Thrown in case of a JRobin specific error.
98      */

99     public RrdDb getRrd( String JavaDoc rrdFile, RrdBackendFactory backendFactory ) throws IOException JavaDoc, RrdException
100     {
101         if ( pool != null )
102             return pool.requestRrdDb( rrdFile );
103         else
104             return new RrdDb( rrdFile, readOnly, backendFactory );
105     }
106
107     /**
108      * Releases an RrdDb instance. Depending on the settings of the RrdOpener,
109      * this either closes the RrdDb, or releases it back into the RrdDbPool.
110      * @param rrdDb RrdDb object that should be released.
111      * @throws IOException Thrown in case of I/O error.
112      * @throws RrdException Thrown in case of a JRobin specific error.
113      */

114     public void releaseRrd(RrdDb rrdDb) throws IOException JavaDoc, RrdException
115     {
116         if ( pool != null )
117             pool.release(rrdDb);
118         else
119             rrdDb.close();
120     }
121 }
122
Popular Tags