KickJava   Java API By Example, From Geeks To Geeks.

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


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  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * Developers: Sasa Markovic (saxon@jrobin.org)
15  * Arne Vandamme (cobralord@jrobin.org)
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  * Class to represent RRD header. Header information is mainly static (once set, it
32  * cannot be changed), with the exception of last update time (this value is changed whenever
33  * RRD gets updated).<p>
34  *
35  * Normally, you don't need to manipulate the Header object directly - JRobin framework
36  * does it for you.<p>
37  *
38  * @author <a HREF="mailto:saxon@jrobin.org">Sasa Markovic</a>*
39  */

40 public class Header implements RrdUpdater {
41     static final String JavaDoc SIGNATURE = "JRobin, version 0.1";
42     static final String JavaDoc RRDTOOL_VERSION = "0001";
43
44     private RrdDb parentDb;
45
46     private RrdString signature;
47     private RrdLong step;
48     private RrdInt dsCount, arcCount;
49     private RrdLong lastUpdateTime;
50
51     Header(RrdDb parentDb, RrdDef rrdDef) throws IOException JavaDoc {
52         boolean shouldInitialize = rrdDef != null;
53         this.parentDb = parentDb;
54         signature = new RrdString(this);
55         step = new RrdLong(this);
56         dsCount = new RrdInt(this);
57         arcCount = new RrdInt(this);
58         lastUpdateTime = new RrdLong(this);
59         if(shouldInitialize) {
60             signature.set(SIGNATURE);
61             step.set(rrdDef.getStep());
62             dsCount.set(rrdDef.getDsCount());
63             arcCount.set(rrdDef.getArcCount());
64             lastUpdateTime.set(rrdDef.getStartTime());
65         }
66     }
67
68     Header(RrdDb parentDb, DataImporter reader) throws IOException JavaDoc, RrdException {
69         this(parentDb, (RrdDef) null);
70         String JavaDoc version = reader.getVersion();
71         if(!version.equals(RRDTOOL_VERSION)) {
72             throw new RrdException("Could not unserilalize xml version " + version);
73         }
74         signature.set(SIGNATURE);
75         step.set(reader.getStep());
76         dsCount.set(reader.getDsCount());
77         arcCount.set(reader.getArcCount());
78         lastUpdateTime.set(reader.getLastUpdateTime());
79     }
80
81     /**
82      * Returns RRD signature. The returned string will be always
83      * of the form <b><i>JRobin, version x.x</i></b>. Note: RRD format did not
84      * change since Jrobin 1.0.0 release (and probably never will).
85      *
86      * @return RRD signature
87      * @throws IOException Thrown in case of I/O error
88      */

89     public String JavaDoc getSignature() throws IOException JavaDoc {
90         return signature.get();
91     }
92
93     /**
94      * Returns the last update time of the RRD.
95      *
96      * @return Timestamp (Unix epoch, no milliseconds) corresponding to the last update time.
97      * @throws IOException Thrown in case of I/O error
98      */

99     public long getLastUpdateTime() throws IOException JavaDoc {
100         return lastUpdateTime.get();
101     }
102
103     /**
104      * Returns primary RRD time step.
105      *
106      * @return Primary time step in seconds
107      * @throws IOException Thrown in case of I/O error
108      */

109     public long getStep() throws IOException JavaDoc {
110         return step.get();
111     }
112
113     /**
114      * Returns the number of datasources defined in the RRD.
115      *
116      * @return Number of datasources defined
117      * @throws IOException Thrown in case of I/O error
118      */

119     public int getDsCount() throws IOException JavaDoc {
120         return dsCount.get();
121     }
122
123     /**
124      * Returns the number of archives defined in the RRD.
125      *
126      * @return Number of archives defined
127      * @throws IOException Thrown in case of I/O error
128      */

129     public int getArcCount() throws IOException JavaDoc {
130         return arcCount.get();
131     }
132
133     void setLastUpdateTime(long lastUpdateTime) throws IOException JavaDoc {
134         this.lastUpdateTime.set(lastUpdateTime);
135     }
136
137     String JavaDoc dump() throws IOException JavaDoc {
138         return "== HEADER ==\n" +
139             "signature:" + getSignature() +
140             " lastUpdateTime:" + getLastUpdateTime() +
141             " step:" + getStep() +
142             " dsCount:" + getDsCount() +
143             " arcCount:" + getArcCount() + "\n";
144     }
145
146     void appendXml(XmlWriter writer) throws IOException JavaDoc {
147         writer.writeTag("version", RRDTOOL_VERSION);
148         writer.writeComment("Seconds");
149         writer.writeTag("step", step.get());
150         writer.writeComment(Util.getDate(lastUpdateTime.get()));
151         writer.writeTag("lastupdate", lastUpdateTime.get());
152     }
153
154     /**
155      * Copies object's internal state to another Header object.
156      * @param other New Header object to copy state to
157      * @throws IOException Thrown in case of I/O error
158      * @throws RrdException Thrown if supplied argument is not a Header object
159      */

160     public void copyStateTo(RrdUpdater other) throws IOException JavaDoc, RrdException {
161         if(!(other instanceof Header)) {
162             throw new RrdException(
163                 "Cannot copy Header object to " + other.getClass().getName());
164         }
165         Header header = (Header) other;
166         header.lastUpdateTime.set(lastUpdateTime.get());
167     }
168
169     /**
170      * Returns the underlying storage (backend) object which actually performs all
171      * I/O operations.
172      * @return I/O backend object
173      */

174     public RrdBackend getRrdBackend() {
175         return parentDb.getRrdBackend();
176     }
177
178     boolean isJRobinHeader() throws IOException JavaDoc {
179         return signature.get().equals(SIGNATURE);
180     }
181
182     void validateHeader() throws IOException JavaDoc, RrdException {
183         if(!isJRobinHeader()) {
184             throw new RrdException("Not a JRobin RRD!");
185         }
186     }
187
188     /**
189      * Required to implement RrdUpdater interface. You should never call this method directly.
190      * @return Allocator object
191      */

192     public RrdAllocator getRrdAllocator() {
193         return parentDb.getRrdAllocator();
194     }
195 }
196
Popular Tags