KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > graph > FetchSourceList


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.graph;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 import org.jrobin.core.RrdException;
32 import org.jrobin.core.RrdDb;
33 import org.jrobin.core.RrdOpener;
34
35 /**
36  * <p>A FetchSourceList represents a number of RRD datasources,
37  * to be used with RrdGraphDef for Graph generation.</p>
38  *
39  * @author Arne Vandamme (cobralord@jrobin.org)
40  */

41 public class FetchSourceList
42 {
43     // ================================================================
44
// -- Members
45
// ================================================================
46
private HashMap JavaDoc map;
47     private ArrayList JavaDoc list;
48
49     private int defCount;
50
51     private boolean persistent;
52     private boolean openerLocked;
53     private boolean opened;
54
55     private RrdOpener rrdOpener;
56
57
58     // ================================================================
59
// -- Constructors
60
// ================================================================
61
/**
62      * Creates a new FetchSourceList with the specified default size.
63      * The size of the actual list is not limited to this number, and
64      * the list will expand automatically if necessary. The default size
65      * should be a ballpark figure for the number of different RrdDb
66      * that will be used (usually a RrdDb corresponds with a single RRD file).
67      *
68      * @param defaultSize Default size of the FetchSourceList.
69      */

70     public FetchSourceList( int defaultSize )
71     {
72         this( defaultSize, false, false );
73     }
74
75     /**
76      * Creates a new FetchSourceList with the specified default size.
77      * The size of the actual list is not limited to this number, and
78      * the list will expand automatically if necessary. The default size
79      * should be a ballpark figure for the number of different RrdDb
80      * that will be used (usually a RrdDb corresponds with a single RRD file).
81      *
82      * @param defaultSize Default size of the FetchSourceList.
83      * @param persistent True if the list is persistent, false if not.
84      */

85     public FetchSourceList( int defaultSize, boolean persistent, boolean lockOpener )
86     {
87         map = new HashMap JavaDoc( defaultSize );
88         list = new ArrayList JavaDoc( defaultSize );
89
90         opened = false;
91         this.persistent = persistent;
92         this.openerLocked = lockOpener;
93     }
94
95     /**
96      * Creates a new FetchSourceList with the specified default size.
97      * The size of the actual list is not limited to this number, and
98      * the list will expand automatically if necessary. The default size
99      * should be a ballpark figure for the number of different RrdDb
100      * that will be used (usually a RrdDb corresponds with a single RRD file).
101      *
102      * @param defaultSize Default size of the FetchSourceList.
103      * @param persistent True if the list is persistent, false if not.
104      * @param rrdOpener Reference to the RrdOpener object that will be used
105      * for RrdDb retrieval.
106      */

107     public FetchSourceList( int defaultSize, boolean persistent, boolean lockOpener, RrdOpener rrdOpener )
108     {
109         this( defaultSize, persistent , lockOpener);
110
111         this.rrdOpener = rrdOpener;
112     }
113
114
115     // ================================================================
116
// -- Public methods
117
// ================================================================
118
/**
119      * Sets the internal RrdOpener object to use for RrdDb retrieval.
120      *
121      * @param rrdOpener Reference to the corresponding RrdOpener instance.
122      */

123     public void setRrdOpener( RrdOpener rrdOpener )
124     {
125         // Only allow RrdOpener change if not persistent
126
if ( !persistent && !openerLocked )
127             this.rrdOpener = rrdOpener;
128     }
129
130     public RrdOpener getRrdOpener() {
131         return rrdOpener;
132     }
133
134     /**
135      * Sets the persistency state of the FetchSourceList.
136      * If the list is set as persistent, RrdDb's can be opened
137      * and retrieved, but not released, and the RrdOpener reference
138      * can not be changed. This is useful to avoid premature closing
139      * and reopening of datasources for performance reasons.
140      *
141      * Setting a FetchSourceList as persistent requires you to manually
142      * control releasing all datasources (all calls to openAll() will
143      * still succeed).
144      *
145      * @param persistent True if the list should behave as persistent.
146      */

147     public void setPersistent( boolean persistent )
148     {
149         this.persistent = persistent;
150     }
151
152     /**
153      * This locks the RrdOpener in the FetchSourceList. Subsequent call so
154      * setRrdOpener() will be ignored until it is unlocked.
155      */

156     public void lockOpener() {
157         openerLocked = true;
158     }
159
160     /**
161      * Unlocks the RrdOpener object, means calls to setRrdOpener() can change
162      * the internal rrdOpener object.
163      */

164     public void unlockOpener() {
165         openerLocked = false;
166     }
167
168     /**
169      * Returns the number of FetchSources hold in the list.
170      * @return Number of different FetchSources.
171      */

172     public int size() {
173         return list.size();
174     }
175
176     /**
177      * Returns the number of Defs represented by the
178      * different FetchSources.
179      * @return Number of Def definitions.
180      */

181     public int defCount() {
182         return defCount;
183     }
184
185     /**
186      * Retrieves (opens) all RrdDb instances related to the
187      * different FetchSources.
188      * It is safe to call this method multiple times in a row.
189      *
190      * @throws IOException Thrown in case of fetching I/O error.
191      * @throws RrdException Thrown in case of a JRobin specific error.
192      */

193     public void openAll() throws RrdException, IOException JavaDoc
194     {
195         if ( opened ) return;
196
197         for ( int i = 0; i < size(); i++ )
198             get(i).openRrd();
199
200         opened = true;
201     }
202
203     /**
204      * Releases all RrdDb instances for the FetchSources.
205      * It is safe to call this method multiple times in a row.
206      * In case of a persistent list, this method does nothing
207      * until persistency is removed.
208      *
209      * @throws IOException Thrown in case of fetching I/O error.
210      * @throws RrdException Thrown in case of a JRobin specific error.
211      */

212     public void releaseAll() throws RrdException, IOException JavaDoc
213     {
214         if ( persistent ) return; // Do not allow release if this FSList is persistent
215

216         for ( int i = 0; i < size(); i++ )
217             get(i).release();
218
219         opened = false;
220     }
221
222     /**
223      * Clears up the FetchSourceList for new use.
224      * This removes persistency, releases all RrdDb instances, and
225      * clears the internal list of FetchSources. After clear()
226      * the list is empty.
227      *
228      * @throws IOException Thrown in case of fetching I/O error.
229      * @throws RrdException Thrown in case of a JRobin specific error.
230      */

231     public void clear() throws RrdException, IOException JavaDoc
232     {
233         persistent = false;
234
235         releaseAll();
236
237         map.clear();
238         list.clear();
239     }
240
241     /**
242      * Returns the highest last update time in seconds of the datasources
243      * represented by the list. If the update time differs for different
244      * datasources, the highest overall timestamp will be returned.
245      *
246      * @return Last update time in seconds.
247      * @throws IOException Thrown in case of fetching I/O error.
248      * @throws RrdException Thrown in case of a JRobin specific error.
249      */

250     public long getLastUpdateTime() throws RrdException, IOException JavaDoc
251     {
252         RrdDb rrd;
253
254         long maxUpdateTime = 0;
255         long lastUpdateTime = 0;
256
257         for ( int i = 0; i < size(); i++ )
258         {
259             rrd = get(i).getRrd();
260
261             lastUpdateTime = rrd.getLastUpdateTime();
262             if ( lastUpdateTime > maxUpdateTime )
263                 maxUpdateTime = lastUpdateTime;
264         }
265
266         return maxUpdateTime;
267     }
268
269     /**
270      * Adds a datasource for graphing purposes to the list, see
271      * {@link RrdGraphDef#datasource( java.lang.String, java.lang.String,
272      * java.lang.String, java.lang.String, java.lang.String )}.
273
274      * @param name Internal datasource name, to be used in GraphDefs.
275      * @param file Path to RRD file.
276      * @param dsName Data source name defined in the RRD file.
277      * @param consolFunc Consolidation function that will be used to extract data from the RRD
278      * file ("AVERAGE", "MIN", "MAX" or "LAST").
279      * @param backend Name of the RrdBackendFactory that should be used for this RrdDb.
280      * @throws RrdException Thrown in case of a JRobin specific error.
281      */

282     public void add( String JavaDoc name, String JavaDoc file, String JavaDoc dsName, String JavaDoc consolFunc, String JavaDoc backend ) throws RrdException
283     {
284         if ( map.containsKey(file) )
285         {
286             FetchSource rf = (FetchSource) map.get(file);
287             rf.addSource( consolFunc, dsName, name );
288         }
289         else
290         {
291             FetchSource fs = new FetchSource( file, consolFunc, dsName, name, backend, this );
292             map.put( file, fs );
293             list.add( fs );
294         }
295
296         defCount++;
297     }
298
299     /**
300      * Adds a datasource for graphing purposes to the list, see
301      * {@link RrdGraphDef#datasource( java.lang.String, java.lang.String,
302      * java.lang.String, java.lang.String )}.
303      *
304      * @param name Internal datasource name, to be used in GraphDefs.
305      * @param file Path to RRD file.
306      * @param dsName Data source name defined in the RRD file.
307      * @param consolFunc Consolidation function that will be used to extract data from the RRD
308      * file ("AVERAGE", "MIN", "MAX" or "LAST").
309      * @throws RrdException Thrown in case of a JRobin specific error.
310      */

311     public void add( String JavaDoc name, String JavaDoc file, String JavaDoc dsName, String JavaDoc consolFunc ) throws RrdException
312     {
313         if ( map.containsKey(file) )
314         {
315             FetchSource rf = (FetchSource) map.get(file);
316             rf.addSource( consolFunc, dsName, name );
317         }
318         else
319         {
320             FetchSource fs = new FetchSource( file, consolFunc, dsName, name, this );
321             map.put( file, fs );
322             list.add( fs );
323         }
324
325         defCount++;
326     }
327
328     // ================================================================
329
// -- Protected (package) methods
330
// ================================================================
331
/**
332      * Returns the FetchSource for the given index.
333      *
334      * @param index Index of the FetchSource in the list.
335      * @return FetchSource instance.
336      */

337     protected FetchSource get( int index )
338     {
339         return (FetchSource) list.get(index);
340     }
341 }
342
Popular Tags