KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > demo > graph > ExportTemplate


1 /* ============================================================
2  * JRobin : Pure java implementation of RRDTool's functionality
3  * ============================================================
4  *
5  * Project Info: http://www.jrobin.org
6  * Project Lead: Sasa Markovic (saxon@jrobin.org);
7  *
8  * (C) Copyright 2003, by Sasa Markovic.
9  *
10  * Developers: Sasa Markovic (saxon@jrobin.org)
11  * Arne Vandamme (cobralord@jrobin.org)
12  *
13  * This library is free software; you can redistribute it and/or modify it under the terms
14  * of the GNU Lesser General Public License as published by the Free Software Foundation;
15  * either version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License along with this
22  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */

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 JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.File JavaDoc;
35 import java.io.InputStreamReader JavaDoc;
36
37 /**
38  * <p>Simple command line application that allows you to export RRD data by means
39  * of a RrdGraphDefTemplate. Pretty straightforward in use.</p>
40  *
41  * @author Arne Vandamme (cobralord@jrobin.org)
42  */

43 public class ExportTemplate
44 {
45     private static int maxRows = 400; // Maximum number of rows
46

47     private static String JavaDoc templateFile, dumpFile;
48
49     private static void die( String JavaDoc msg )
50     {
51         System.err.println( msg );
52         System.exit( -1 );
53     }
54
55     private static void parseArguments( String JavaDoc[] args )
56     {
57         int rpos = args.length - 1;
58
59         // Last argument should be the templateFile
60
templateFile = args[rpos];
61
62         // Remaining number of parameters should be even
63
if ( rpos % 2 > 0 )
64             die( "Invalid number of arguments." );
65
66         for ( int i = 0; i < rpos; i += 2 )
67         {
68             String JavaDoc arg = args[i];
69             String JavaDoc 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 JavaDoc e ) {
79                 die( "Error with option '" + arg + "': " + e.getMessage() );
80             }
81         }
82     }
83
84     private static String JavaDoc readVariable( BufferedReader JavaDoc in, String JavaDoc name ) throws IOException JavaDoc
85     {
86         System.out.print( "Variable '" + name + "' = " );
87
88         return in.readLine();
89     }
90
91     public static void main( String JavaDoc[] 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             // -- Read the RrdGraphDefTemplate (XML format)
104
System.out.println( ">>> Reading XML template" );
105             RrdExportDefTemplate template = new RrdExportDefTemplate( new File JavaDoc(templateFile) );
106
107             // -- Set the parameters (if there are any)
108
System.out.println( ">>> Setting template variables" );
109             if ( template.hasVariables() )
110             {
111                 BufferedReader JavaDoc in = new BufferedReader JavaDoc( new InputStreamReader JavaDoc(System.in) );
112
113                 String JavaDoc[] 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             // -- Generate the actual graph
123
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 JavaDoc ioe ) {
139             die( "IOException occurred: " + ioe.getMessage() );
140         }
141     }
142 }
Popular Tags