KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > convertor > Convertor


1 package org.jrobin.convertor;
2
3 import org.jrobin.core.RrdDb;
4 import org.jrobin.core.RrdException;
5
6 import java.io.*;
7
8 class Convertor {
9     static final String JavaDoc SUFFIX = ".jrb";
10     static final String JavaDoc SEPARATOR = System.getProperty("file.separator");
11     static final Runtime JavaDoc RUNTIME = Runtime.getRuntime();
12
13     private String JavaDoc rrdtoolBinary;
14     private String JavaDoc workingDirectory;
15     private String JavaDoc suffix;
16
17     private int okCount, badCount;
18
19     private Convertor(String JavaDoc rrdtoolBinary, String JavaDoc workingDirectory, String JavaDoc suffix) {
20         this.rrdtoolBinary = rrdtoolBinary;
21         this.workingDirectory = workingDirectory;
22         this.suffix = suffix;
23     }
24
25     private void convert() {
26         println("Converting RRDTool files to JRobin native format");
27         println("Converted files will be placed in the same directory, with " +
28             suffix + " suffix appended");
29         println("==========================================");
30         long start = System.currentTimeMillis();
31         if(!workingDirectory.endsWith(SEPARATOR)) {
32             workingDirectory += SEPARATOR;
33         }
34         File parent = new File(workingDirectory);
35         if(parent.isDirectory() && parent.exists()) {
36             // directory
37
FileFilter filter = new FileFilter() {
38                 public boolean accept(File f) {
39                     try {
40                         return !f.isDirectory() && f.getCanonicalPath().endsWith(".rrd");
41                     } catch (IOException e) {
42                         return false;
43                     }
44                 }
45             };
46             File[] files = parent.listFiles(filter);
47             for(int i = 0; i < files.length; i++) {
48                 print("[" + i + "/" + files.length + "] ");
49                 convertFile(files[i]);
50             }
51         }
52         else if(!parent.isDirectory() && parent.exists()) {
53             // single file
54
convertFile(parent);
55         }
56         else {
57             println("Nothing to do");
58         }
59         println("Conversion finished, " + okCount + " files ok, " + badCount + " files bad");
60         long secs = (System.currentTimeMillis() - start + 500L) / 1000L;
61         long mins = secs / 60;
62         secs %= 60;
63         println("Time elapsed: " + mins + ":" + ((secs < 10)? "0": "") + secs);
64     }
65
66     private long convertFile(File rrdFile) {
67         long start = System.currentTimeMillis();
68         String JavaDoc xmlPath = null, destPath = null;
69         try {
70             String JavaDoc sourcePath = rrdFile.getCanonicalPath();
71             xmlPath = sourcePath + ".xml";
72             destPath = sourcePath + suffix;
73             print(rrdFile.getName() + " ");
74             xmlDump(sourcePath, xmlPath);
75             RrdDb rrd = new RrdDb(destPath, xmlPath);
76             rrd.close();
77             rrd = null;
78             System.gc();
79             okCount++;
80             long elapsed = System.currentTimeMillis() - start;
81             println("[OK, " + (elapsed / 1000.0) + "]");
82             return elapsed;
83         } catch (IOException e) {
84             removeFile(destPath);
85             badCount++;
86             println("[IO ERROR]");
87             return -1;
88         } catch (RrdException e) {
89             removeFile(destPath);
90             badCount++;
91             println("[RRD ERROR]");
92             return -2;
93         }
94         finally {
95             removeFile(xmlPath);
96         }
97     }
98
99     private static boolean removeFile(String JavaDoc filePath) {
100         if(filePath != null) {
101             return new File(filePath).delete();
102         }
103         return true;
104     }
105
106     private void xmlDump(String JavaDoc sourcePath, String JavaDoc xmlPath) throws IOException {
107         String JavaDoc[] cmd = new String JavaDoc[] { rrdtoolBinary, "dump", sourcePath };
108         Process JavaDoc p = RUNTIME.exec(cmd);
109         OutputStream outStream = new BufferedOutputStream(new FileOutputStream(xmlPath, false));
110         transportStream(p.getInputStream(), outStream);
111         transportStream(p.getErrorStream(), null);
112         try {
113             p.waitFor();
114         }
115         catch(InterruptedException JavaDoc ie) {
116             // NOP
117
}
118     }
119
120     public static void main(String JavaDoc[] args) {
121         if(args.length < 2 || args.length > 3) {
122             println("Usage: java -jar convertor.jar " +
123                 "<path to RRDTool binary> <RRD directory/file path> [converted file suffix]");
124         }
125         else {
126             Convertor c = new Convertor(args[0], args[1], args.length == 3? args[2]: SUFFIX);
127             c.convert();
128         }
129     }
130
131     private final static void println(String JavaDoc msg) {
132         System.out.println(msg);
133     }
134
135     private final static void print(String JavaDoc msg) {
136         System.out.print(msg);
137     }
138
139     private static void transportStream(InputStream in, OutputStream out) throws IOException {
140         try {
141             int b;
142             while((b = in.read()) != -1) {
143                 if(out != null) {
144                     out.write(b);
145                 }
146             }
147         }
148         finally {
149             in.close();
150             if(out != null) {
151                 out.flush();
152                 out.close();
153             }
154         }
155     }
156 }
157
Popular Tags