001    /* ============================================================
002     * JRobin : Pure java implementation of RRDTool's functionality
003     * ============================================================
004     *
005     * Project Info:  http://www.jrobin.org
006     * Project Lead:  Sasa Markovic (saxon@jrobin.org);
007     *
008     * (C) Copyright 2003-2005, by Sasa Markovic.
009     *
010     * This library is free software; you can redistribute it and/or modify it under the terms
011     * of the GNU Lesser General Public License as published by the Free Software Foundation;
012     * either version 2.1 of the License, or (at your option) any later version.
013     *
014     * Developers:    Sasa Markovic (saxon@jrobin.org)
015     *
016     *
017     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
018     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
019     * See the GNU Lesser General Public License for more details.
020     *
021     * You should have received a copy of the GNU Lesser General Public License along with this
022     * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
023     * Boston, MA 02111-1307, USA.
024     */
025    
026    package org.jrobin.core;
027    
028    import java.io.IOException;
029    
030    /**
031     * Class to represent RRD header. Header information is mainly static (once set, it
032     * cannot be changed), with the exception of last update time (this value is changed whenever
033     * RRD gets updated).<p>
034     * <p/>
035     * Normally, you don't need to manipulate the Header object directly - JRobin framework
036     * does it for you.<p>
037     *
038     * @author <a href="mailto:saxon@jrobin.org">Sasa Markovic</a>*
039     */
040    public class Header implements RrdUpdater {
041            static final int SIGNATURE_LENGTH = 2;
042            static final String SIGNATURE = "JR";
043    
044            static final String DEFAULT_SIGNATURE = "JRobin, version 0.1";
045            static final String RRDTOOL_VERSION = "0001";
046    
047            private RrdDb parentDb;
048    
049            private RrdString signature;
050            private RrdLong step;
051            private RrdInt dsCount, arcCount;
052            private RrdLong lastUpdateTime;
053    
054            Header(RrdDb parentDb, RrdDef rrdDef) throws IOException {
055                    boolean shouldInitialize = rrdDef != null;
056                    this.parentDb = parentDb;
057                    signature = new RrdString(this);                         // NOT constant, may be cached
058                    step = new RrdLong(this, true);                  // constant, may be cached
059                    dsCount = new RrdInt(this, true);                        // constant, may be cached
060                    arcCount = new RrdInt(this, true);                       // constant, may be cached
061                    lastUpdateTime = new RrdLong(this);
062                    if (shouldInitialize) {
063                            signature.set(DEFAULT_SIGNATURE);
064                            step.set(rrdDef.getStep());
065                            dsCount.set(rrdDef.getDsCount());
066                            arcCount.set(rrdDef.getArcCount());
067                            lastUpdateTime.set(rrdDef.getStartTime());
068                    }
069            }
070    
071            Header(RrdDb parentDb, DataImporter reader) throws IOException, RrdException {
072                    this(parentDb, (RrdDef) null);
073                    String version = reader.getVersion();
074                    if (!version.equals(RRDTOOL_VERSION)) {
075                            throw new RrdException("Could not unserilalize xml version " + version);
076                    }
077                    signature.set(DEFAULT_SIGNATURE);
078                    step.set(reader.getStep());
079                    dsCount.set(reader.getDsCount());
080                    arcCount.set(reader.getArcCount());
081                    lastUpdateTime.set(reader.getLastUpdateTime());
082            }
083    
084            /**
085             * Returns RRD signature. Initially, the returned string will be
086             * of the form <b><i>JRobin, version x.x</i></b>. Note: RRD format did not
087             * change since Jrobin 1.0.0 release (and probably never will).
088             *
089             * @return RRD signature
090             * @throws IOException Thrown in case of I/O error
091             */
092            public String getSignature() throws IOException {
093                    return signature.get();
094            }
095    
096            public String getInfo() throws IOException {
097                    return getSignature().substring(SIGNATURE_LENGTH);
098            }
099    
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    }