1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package org.jrobin.cmd;
27
28 import org.jrobin.core.RrdDb;
29 import org.jrobin.core.RrdDbPool;
30 import org.jrobin.core.RrdDef;
31 import org.jrobin.core.RrdException;
32
33 import java.io.IOException;
34
35 abstract class RrdToolCmd {
36
37 private RrdCmdScanner cmdScanner;
38
39 abstract String getCmdType();
40
41 abstract Object execute() throws RrdException, IOException;
42
43 Object executeCommand(String command) throws RrdException, IOException {
44 cmdScanner = new RrdCmdScanner(command);
45 return execute();
46 }
47
48 String getOptionValue(String shortForm, String longForm) throws RrdException {
49 return cmdScanner.getOptionValue(shortForm, longForm);
50 }
51
52 String getOptionValue(String shortForm, String longForm, String defaultValue) throws RrdException {
53 return cmdScanner.getOptionValue(shortForm, longForm, defaultValue);
54 }
55
56 String[] getMultipleOptionValues(String shortForm, String longForm) throws RrdException {
57 return cmdScanner.getMultipleOptions(shortForm, longForm);
58 }
59
60 boolean getBooleanOption(String shortForm, String longForm) {
61 return cmdScanner.getBooleanOption(shortForm, longForm);
62 }
63
64 String[] getRemainingWords() {
65 return cmdScanner.getRemainingWords();
66 }
67
68 static boolean rrdDbPoolUsed = true;
69 static boolean standardOutUsed = true;
70
71 static boolean isRrdDbPoolUsed() {
72 return rrdDbPoolUsed;
73 }
74
75 static void setRrdDbPoolUsed(boolean rrdDbPoolUsed) {
76 RrdToolCmd.rrdDbPoolUsed = rrdDbPoolUsed;
77 }
78
79 static boolean isStandardOutUsed() {
80 return standardOutUsed;
81 }
82
83 static void setStandardOutUsed(boolean standardOutUsed) {
84 RrdToolCmd.standardOutUsed = standardOutUsed;
85 }
86
87 static long parseLong(String value) throws RrdException {
88 try {
89 return Long.parseLong(value);
90 }
91 catch (NumberFormatException nfe) {
92 throw new RrdException(nfe);
93 }
94 }
95
96 static int parseInt(String value) throws RrdException {
97 try {
98 return Integer.parseInt(value);
99 }
100 catch (NumberFormatException nfe) {
101 throw new RrdException(nfe);
102 }
103 }
104
105 static double parseDouble(String value) throws RrdException {
106 if (value.equals("U")) {
107 return Double.NaN;
108 }
109 try {
110 return Double.parseDouble(value);
111 }
112 catch (NumberFormatException nfe) {
113 throw new RrdException(nfe);
114 }
115 }
116
117 static void print(String s) {
118 if (standardOutUsed) {
119 System.out.print(s);
120 }
121 }
122
123 static void println(String s) {
124 if (standardOutUsed) {
125 System.out.println(s);
126 }
127 }
128
129 static RrdDb getRrdDbReference(String path) throws IOException, RrdException {
130 if (rrdDbPoolUsed) {
131 return RrdDbPool.getInstance().requestRrdDb(path);
132 }
133 else {
134 return new RrdDb(path);
135 }
136 }
137
138 static RrdDb getRrdDbReference(String path, String xmlPath) throws IOException, RrdException {
139 if (rrdDbPoolUsed) {
140 return RrdDbPool.getInstance().requestRrdDb(path, xmlPath);
141 }
142 else {
143 return new RrdDb(path, xmlPath);
144 }
145 }
146
147 static RrdDb getRrdDbReference(RrdDef rrdDef) throws IOException, RrdException {
148 if (rrdDbPoolUsed) {
149 return RrdDbPool.getInstance().requestRrdDb(rrdDef);
150 }
151 else {
152 return new RrdDb(rrdDef);
153 }
154 }
155
156 static void releaseRrdDbReference(RrdDb rrdDb) throws IOException, RrdException {
157 if (rrdDbPoolUsed) {
158 RrdDbPool.getInstance().release(rrdDb);
159 }
160 else {
161 rrdDb.close();
162 }
163 }
164 }