View Javadoc

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-2005, 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   *
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;
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   * <p/>
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 int SIGNATURE_LENGTH = 2;
42  	static final String SIGNATURE = "JR";
43  
44  	static final String DEFAULT_SIGNATURE = "JRobin, version 0.1";
45  	static final String RRDTOOL_VERSION = "0001";
46  
47  	private RrdDb parentDb;
48  
49  	private RrdString signature;
50  	private RrdLong step;
51  	private RrdInt dsCount, arcCount;
52  	private RrdLong lastUpdateTime;
53  
54  	Header(RrdDb parentDb, RrdDef rrdDef) throws IOException {
55  		boolean shouldInitialize = rrdDef != null;
56  		this.parentDb = parentDb;
57  		signature = new RrdString(this);			 // NOT constant, may be cached
58  		step = new RrdLong(this, true);			 // constant, may be cached
59  		dsCount = new RrdInt(this, true);			 // constant, may be cached
60  		arcCount = new RrdInt(this, true);			 // constant, may be cached
61  		lastUpdateTime = new RrdLong(this);
62  		if (shouldInitialize) {
63  			signature.set(DEFAULT_SIGNATURE);
64  			step.set(rrdDef.getStep());
65  			dsCount.set(rrdDef.getDsCount());
66  			arcCount.set(rrdDef.getArcCount());
67  			lastUpdateTime.set(rrdDef.getStartTime());
68  		}
69  	}
70  
71  	Header(RrdDb parentDb, DataImporter reader) throws IOException, RrdException {
72  		this(parentDb, (RrdDef) null);
73  		String version = reader.getVersion();
74  		if (!version.equals(RRDTOOL_VERSION)) {
75  			throw new RrdException("Could not unserilalize xml version " + version);
76  		}
77  		signature.set(DEFAULT_SIGNATURE);
78  		step.set(reader.getStep());
79  		dsCount.set(reader.getDsCount());
80  		arcCount.set(reader.getArcCount());
81  		lastUpdateTime.set(reader.getLastUpdateTime());
82  	}
83  
84  	/**
85  	 * Returns RRD signature. Initially, the returned string will be
86  	 * of the form <b><i>JRobin, version x.x</i></b>. Note: RRD format did not
87  	 * change since Jrobin 1.0.0 release (and probably never will).
88  	 *
89  	 * @return RRD signature
90  	 * @throws IOException Thrown in case of I/O error
91  	 */
92  	public String getSignature() throws IOException {
93  		return signature.get();
94  	}
95  
96  	public String getInfo() throws IOException {
97  		return getSignature().substring(SIGNATURE_LENGTH);
98  	}
99  
100 	public void setInfo(String info) throws IOException {
101 		if (info != null && info.length() > 0) {
102 			signature.set(SIGNATURE + info);
103 		}
104 		else {
105 			signature.set(SIGNATURE);
106 		}
107 	}
108 
109 	/**
110 	 * Returns the last update time of the RRD.
111 	 *
112 	 * @return Timestamp (Unix epoch, no milliseconds) corresponding to the last update time.
113 	 * @throws IOException Thrown in case of I/O error
114 	 */
115 	public long getLastUpdateTime() throws IOException {
116 		return lastUpdateTime.get();
117 	}
118 
119 	/**
120 	 * Returns primary RRD time step.
121 	 *
122 	 * @return Primary time step in seconds
123 	 * @throws IOException Thrown in case of I/O error
124 	 */
125 	public long getStep() throws IOException {
126 		return step.get();
127 	}
128 
129 	/**
130 	 * Returns the number of datasources defined in the RRD.
131 	 *
132 	 * @return Number of datasources defined
133 	 * @throws IOException Thrown in case of I/O error
134 	 */
135 	public int getDsCount() throws IOException {
136 		return dsCount.get();
137 	}
138 
139 	/**
140 	 * Returns the number of archives defined in the RRD.
141 	 *
142 	 * @return Number of archives defined
143 	 * @throws IOException Thrown in case of I/O error
144 	 */
145 	public int getArcCount() throws IOException {
146 		return arcCount.get();
147 	}
148 
149 	void setLastUpdateTime(long lastUpdateTime) throws IOException {
150 		this.lastUpdateTime.set(lastUpdateTime);
151 	}
152 
153 	String dump() throws IOException {
154 		return "== HEADER ==\n" +
155 				"signature:" + getSignature() +
156 				" lastUpdateTime:" + getLastUpdateTime() +
157 				" step:" + getStep() +
158 				" dsCount:" + getDsCount() +
159 				" arcCount:" + getArcCount() + "\n";
160 	}
161 
162 	void appendXml(XmlWriter writer) throws IOException {
163 		writer.writeComment(signature.get());
164 		writer.writeTag("version", RRDTOOL_VERSION);
165 		writer.writeComment("Seconds");
166 		writer.writeTag("step", step.get());
167 		writer.writeComment(Util.getDate(lastUpdateTime.get()));
168 		writer.writeTag("lastupdate", lastUpdateTime.get());
169 	}
170 
171 	/**
172 	 * Copies object's internal state to another Header object.
173 	 *
174 	 * @param other New Header object to copy state to
175 	 * @throws IOException  Thrown in case of I/O error
176 	 * @throws RrdException Thrown if supplied argument is not a Header object
177 	 */
178 	public void copyStateTo(RrdUpdater other) throws IOException, RrdException {
179 		if (!(other instanceof Header)) {
180 			throw new RrdException(
181 					"Cannot copy Header object to " + other.getClass().getName());
182 		}
183 		Header header = (Header) other;
184 		header.signature.set(signature.get());
185 		header.lastUpdateTime.set(lastUpdateTime.get());
186 	}
187 
188 	/**
189 	 * Returns the underlying storage (backend) object which actually performs all
190 	 * I/O operations.
191 	 *
192 	 * @return I/O backend object
193 	 */
194 	public RrdBackend getRrdBackend() {
195 		return parentDb.getRrdBackend();
196 	}
197 
198 	boolean isJRobinHeader() throws IOException {
199 		return signature.get().startsWith(SIGNATURE);
200 	}
201 
202 	void validateHeader() throws IOException, RrdException {
203 		if (!isJRobinHeader()) {
204 			String msg = "Invalid file header. File [" + parentDb.getCanonicalPath() + "] is not a JRobin RRD file";
205 			throw new RrdException(msg);
206 		}
207 	}
208 
209 	/**
210 	 * Required to implement RrdUpdater interface. You should never call this method directly.
211 	 *
212 	 * @return Allocator object
213 	 */
214 	public RrdAllocator getRrdAllocator() {
215 		return parentDb.getRrdAllocator();
216 	}
217 }