1 25 package org.jrobin.graph; 26 27 import java.io.*; 28 import java.util.ArrayList ; 29 import java.util.Date ; 30 import java.util.GregorianCalendar ; 31 32 import org.jrobin.core.Util; 33 import org.jrobin.core.RrdException; 34 import org.jrobin.core.XmlWriter; 35 36 45 public class RrdExportDef implements Serializable 46 { 47 public final static int STRICT_IMPLICIT_OFF = 0; 51 public final static int STRICT_IMPLICIT_ON = 1; 52 public final static int STRICT_EXPLICIT_OFF = 2; 53 public final static int STRICT_EXPLICIT_ON = 3; 54 55 private long endTime = Util.getTime(); private long startTime = Util.getTime() - 86400L; 57 private long resolution = 1; 59 private int strict = STRICT_IMPLICIT_OFF; 60 61 private int numSdefs = 0; 63 private int numDefs = 0; 65 protected FetchSourceList fetchSources = new FetchSourceList( 10 ); protected ArrayList pdefList = new ArrayList ( 10 ); protected ArrayList cdefList = new ArrayList ( 10 ); protected ArrayList exportList = new ArrayList ( 10 ); protected ArrayList edefList = new ArrayList ( 3 ); 71 public RrdExportDef() { 75 } 76 77 85 public RrdExportDef( long startTime, long endTime ) throws RrdException 86 { 87 setTimePeriod( startTime, endTime ); 88 } 89 90 98 public RrdExportDef( Date start, Date end) throws RrdException 99 { 100 setTimePeriod( start, end ); 101 } 102 103 111 public RrdExportDef( GregorianCalendar start, GregorianCalendar end ) throws RrdException 112 { 113 setTimePeriod( start, end ); 114 } 115 116 127 public void setTimePeriod( long startTime, long endTime ) throws RrdException 128 { 129 if ( startTime < 0 || ( endTime != 0 && endTime <= startTime ) ) 130 throw new RrdException( "Invalid start/end time: " + startTime + "/" + endTime ); 131 132 this.startTime = startTime; 133 this.endTime = endTime; 134 } 135 136 143 public void setTimePeriod( Date start, Date end ) throws RrdException 144 { 145 setTimePeriod( start.getTime() / 1000L, end.getTime() / 1000L ); 146 } 147 148 155 public void setTimePeriod( GregorianCalendar start, GregorianCalendar end ) throws RrdException 156 { 157 setTimePeriod( start.getTime(), end.getTime() ); 158 } 159 160 166 public void setResolution( long resolution ) 167 { 168 this.resolution = resolution; 169 } 170 171 187 public void datasource( String name, String file, String dsName, String consolFunc ) throws RrdException 188 { 189 fetchSources.add( name, file, dsName, consolFunc ); 190 191 numDefs++; 192 } 193 194 211 public void datasource( String name, String file, String dsName, String consolFunc, String backend ) throws RrdException 212 { 213 fetchSources.add( name, file, dsName, consolFunc, backend ); 214 215 numDefs++; 216 } 217 218 224 public void setDatasources( FetchSourceList datasourceList ) 225 { 226 fetchSources = datasourceList; 227 228 numDefs = fetchSources.defCount(); 229 } 230 231 253 public void datasource( String name, String rpn ) 254 { 255 cdefList.add( new Cdef(name, rpn) ); 256 } 257 258 267 public void datasource( String name, String defName, String consolFunc ) throws RrdException 268 { 269 cdefList.add( new Sdef(name, defName, consolFunc) ); 270 numSdefs++; 271 } 272 273 280 public void datasource( String name, Plottable plottable ) 281 { 282 pdefList.add( new Pdef(name, plottable) ); 283 } 284 285 293 public void datasource( String name, Plottable plottable, int index ) 294 { 295 pdefList.add( new Pdef(name, plottable, index) ); 296 } 297 298 306 public void datasource( String name, Plottable plottable, String sourceName ) 307 { 308 pdefList.add( new Pdef(name, plottable, sourceName) ); 309 } 310 311 316 public void addExportData( ExportData edata ) 317 { 318 edefList.add( edata ); 319 } 320 321 327 public void export( String name ) 328 { 329 export( name, "" ); 330 } 331 332 339 public void export( String name, String legend ) 340 { 341 if ( strict == STRICT_IMPLICIT_OFF ) 342 strict = STRICT_IMPLICIT_ON; 343 344 exportList.add( new String [] { name, legend } ); 345 } 346 347 365 public void setStrictExport( boolean strict ) 366 { 367 this.strict = ( strict ? STRICT_EXPLICIT_ON : STRICT_EXPLICIT_OFF ); 368 } 369 370 376 public void exportXmlTemplate( OutputStream stream ) 377 { 378 XmlWriter xml = new XmlWriter( stream ); 379 380 xml.startTag("rrd_export_def"); 381 382 xml.startTag("span"); 384 xml.writeTag("start", getStartTime() ); 385 xml.writeTag("end", getEndTime() ); 386 xml.closeTag(); 388 xml.startTag( "options" ); 390 if ( resolution > 1 ) 391 xml.writeTag( "resolution", resolution ); 392 xml.writeTag( "strict_export", ( strict == STRICT_IMPLICIT_ON || strict == STRICT_EXPLICIT_ON ? "true" : "false" ) ); 393 xml.closeTag(); 394 395 xml.startTag("datasources"); 397 for ( int i = 0; i < fetchSources.size(); i++ ) 399 fetchSources.get( i ).exportXml(xml); 400 for (int i = 0; i < cdefList.size(); i++ ) 402 { 403 Cdef cdef = (Cdef) cdefList.get(i); 404 cdef.exportXml(xml); 405 } 406 xml.closeTag(); 408 xml.startTag("exports"); 410 String [][] list = getExportDatasources(); 411 for ( int i = 0; i < list.length; i++ ) 412 { 413 xml.startTag( "export" ); 414 xml.writeTag( "datasource", list[i][0] ); 415 xml.writeTag( "legend", list[i][1] ); 416 xml.closeTag(); 417 } 418 xml.closeTag(); 420 xml.closeTag(); xml.flush(); 422 423 xml.flush(); 424 } 425 426 433 public String getXmlTemplate() 434 { 435 return exportXmlTemplate(); 436 } 437 438 444 public String exportXmlTemplate() 445 { 446 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 447 exportXmlTemplate(outputStream); 448 return outputStream.toString(); 449 } 450 451 457 public void exportXmlTemplate(String filePath) throws IOException 458 { 459 FileOutputStream outputStream = new FileOutputStream(filePath, false); 460 exportXmlTemplate(outputStream); 461 outputStream.close(); 462 } 463 464 protected long getStartTime() { 468 return startTime; 469 } 470 471 protected long getEndTime() { 472 return endTime; 473 } 474 475 protected long getResolution() { 476 return resolution; 477 } 478 479 protected int getNumDefs() 480 { 481 return numDefs; 482 } 483 484 protected Cdef[] getCdefs() 485 { 486 return (Cdef[]) cdefList.toArray( new Cdef[] {} ); 487 } 488 489 protected Pdef[] getPdefs() 490 { 491 return (Pdef[]) pdefList.toArray( new Pdef[] {} ); 492 } 493 494 protected ExportData[] getExportData() 495 { 496 return (ExportData[]) edefList.toArray( new ExportData[] {} ); 497 } 498 499 protected int getNumSdefs() 500 { 501 return numSdefs; 502 } 503 504 protected FetchSourceList getFetchSources() 505 { 506 return fetchSources; 507 } 508 509 protected boolean isStrict() { 510 return ( strict == STRICT_IMPLICIT_ON || strict == STRICT_EXPLICIT_ON ); 511 } 512 513 protected String [][] getExportDatasources() { 514 return (String [][]) exportList.toArray( new String [0][2] ); 515 } 516 } 517 | Popular Tags |