1 25 26 package org.jrobin.core; 27 28 import java.io.IOException ; 29 30 40 public class Header implements RrdUpdater { 41 static final String SIGNATURE = "JRobin, version 0.1"; 42 static final String 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 { 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 , RrdException { 69 this(parentDb, (RrdDef) null); 70 String 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 89 public String getSignature() throws IOException { 90 return signature.get(); 91 } 92 93 99 public long getLastUpdateTime() throws IOException { 100 return lastUpdateTime.get(); 101 } 102 103 109 public long getStep() throws IOException { 110 return step.get(); 111 } 112 113 119 public int getDsCount() throws IOException { 120 return dsCount.get(); 121 } 122 123 129 public int getArcCount() throws IOException { 130 return arcCount.get(); 131 } 132 133 void setLastUpdateTime(long lastUpdateTime) throws IOException { 134 this.lastUpdateTime.set(lastUpdateTime); 135 } 136 137 String dump() throws IOException { 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 { 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 160 public void copyStateTo(RrdUpdater other) throws IOException , 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 174 public RrdBackend getRrdBackend() { 175 return parentDb.getRrdBackend(); 176 } 177 178 boolean isJRobinHeader() throws IOException { 179 return signature.get().equals(SIGNATURE); 180 } 181 182 void validateHeader() throws IOException , RrdException { 183 if(!isJRobinHeader()) { 184 throw new RrdException("Not a JRobin RRD!"); 185 } 186 } 187 188 192 public RrdAllocator getRrdAllocator() { 193 return parentDb.getRrdAllocator(); 194 } 195 } 196 | Popular Tags |