1 25 package org.jrobin.demo.graph; 26 27 import org.jrobin.graph.RrdExport; 28 import org.jrobin.graph.RrdExportDefTemplate; 29 import org.jrobin.graph.ExportData; 30 import org.jrobin.core.RrdException; 31 32 import java.io.BufferedReader ; 33 import java.io.IOException ; 34 import java.io.File ; 35 import java.io.InputStreamReader ; 36 37 43 public class ExportTemplate 44 { 45 private static int maxRows = 400; 47 private static String templateFile, dumpFile; 48 49 private static void die( String msg ) 50 { 51 System.err.println( msg ); 52 System.exit( -1 ); 53 } 54 55 private static void parseArguments( String [] args ) 56 { 57 int rpos = args.length - 1; 58 59 templateFile = args[rpos]; 61 62 if ( rpos % 2 > 0 ) 64 die( "Invalid number of arguments." ); 65 66 for ( int i = 0; i < rpos; i += 2 ) 67 { 68 String arg = args[i]; 69 String val = args[i + 1]; 70 71 try 72 { 73 if ( arg.equalsIgnoreCase("-m") ) 74 maxRows = Integer.parseInt(val); 75 else if ( arg.equalsIgnoreCase("-f") ) 76 dumpFile = val; 77 } 78 catch ( Exception e ) { 79 die( "Error with option '" + arg + "': " + e.getMessage() ); 80 } 81 } 82 } 83 84 private static String readVariable( BufferedReader in, String name ) throws IOException 85 { 86 System.out.print( "Variable '" + name + "' = " ); 87 88 return in.readLine(); 89 } 90 91 public static void main( String [] args ) 92 { 93 if ( args.length < 1 ) 94 { 95 System.out.println( "Usage: ExportTemplate [-m maxRows] [-f <dump_file>] <template_file>" ); 96 System.exit(0); 97 } 98 99 parseArguments( args ); 100 101 try 102 { 103 System.out.println( ">>> Reading XML template" ); 105 RrdExportDefTemplate template = new RrdExportDefTemplate( new File (templateFile) ); 106 107 System.out.println( ">>> Setting template variables" ); 109 if ( template.hasVariables() ) 110 { 111 BufferedReader in = new BufferedReader ( new InputStreamReader (System.in) ); 112 113 String [] variables = template.getVariables(); 114 for ( int i = 0; i < variables.length; i++ ) 115 template.setVariable( variables[i], readVariable( in, variables[i] ) ); 116 } 117 118 System.out.println( ">>> Exporting data..." ); 119 120 long start = System.currentTimeMillis(); 121 122 RrdExport export = new RrdExport( template.getRrdExportDef() ); 124 ExportData data = export.fetch( maxRows ); 125 126 if ( dumpFile != null ) 127 data.exportXml( dumpFile ); 128 else 129 data.exportXml( System.out ); 130 131 long stop = System.currentTimeMillis(); 132 133 System.out.println( ">>> Data exported in " + (stop - start) + " milliseconds" ); 134 } 135 catch ( RrdException rrde ) { 136 die( "RrdException occurred: " + rrde.getMessage() ); 137 } 138 catch ( IOException ioe ) { 139 die( "IOException occurred: " + ioe.getMessage() ); 140 } 141 } 142 } | Popular Tags |