1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package org.jrobin.core;
27
28 import java.io.IOException;
29
30
31
32
33
34
35
36
37
38
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);
58 step = new RrdLong(this, true);
59 dsCount = new RrdInt(this, true);
60 arcCount = new RrdInt(this, true);
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
86
87
88
89
90
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
111
112
113
114
115 public long getLastUpdateTime() throws IOException {
116 return lastUpdateTime.get();
117 }
118
119
120
121
122
123
124
125 public long getStep() throws IOException {
126 return step.get();
127 }
128
129
130
131
132
133
134
135 public int getDsCount() throws IOException {
136 return dsCount.get();
137 }
138
139
140
141
142
143
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
173
174
175
176
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
190
191
192
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
211
212
213
214 public RrdAllocator getRrdAllocator() {
215 return parentDb.getRrdAllocator();
216 }
217 }