181 } catch (NumberFormatException e) {
182 return new int[0];
183 }
184 }
185
186 private static void fail(String msg, Object... args) {
187 Formatter f = new Formatter();
188 f.format(msg, args);
189 throw new RuntimeException(f.out().toString());
190 }
191
192 // loads rules from the given file
193 // Each non-blank/non-comment line must have the format:
194 // ("bind" | "connect") 1*LWSP-char (hostname | ipaddress["/" prefix])
195 // 1*LWSP-char ("*" | port) [ "-" ("*" | port) ]
196 private static List<Rule> loadRulesFromFile(String file)
197 throws IOException
198 {
199 Scanner scanner = new Scanner(new File(file));
200 try {
201 List<Rule> result = new ArrayList<Rule>();
202 while (scanner.hasNextLine()) {
203 String line = scanner.nextLine().trim();
204
205 // skip blank lines and comments
206 if (line.length() == 0 || line.charAt(0) == '#')
207 continue;
208
209 // must have 3 fields
210 String[] s = line.split("\\s+");
211 if (s.length != 3) {
212 fail("Malformed line '%s'", line);
213 continue;
214 }
215
216 // first field is the action ("bind" or "connect")
217 Action action = null;
218 for (Action a: Action.values()) {
219 if (s[0].equalsIgnoreCase(a.name())) {
220 action = a;
221 break;
|
181 } catch (NumberFormatException e) {
182 return new int[0];
183 }
184 }
185
186 private static void fail(String msg, Object... args) {
187 Formatter f = new Formatter();
188 f.format(msg, args);
189 throw new RuntimeException(f.out().toString());
190 }
191
192 // loads rules from the given file
193 // Each non-blank/non-comment line must have the format:
194 // ("bind" | "connect") 1*LWSP-char (hostname | ipaddress["/" prefix])
195 // 1*LWSP-char ("*" | port) [ "-" ("*" | port) ]
196 private static List<Rule> loadRulesFromFile(String file)
197 throws IOException
198 {
199 Scanner scanner = new Scanner(new File(file));
200 try {
201 List<Rule> result = new ArrayList<>();
202 while (scanner.hasNextLine()) {
203 String line = scanner.nextLine().trim();
204
205 // skip blank lines and comments
206 if (line.length() == 0 || line.charAt(0) == '#')
207 continue;
208
209 // must have 3 fields
210 String[] s = line.split("\\s+");
211 if (s.length != 3) {
212 fail("Malformed line '%s'", line);
213 continue;
214 }
215
216 // first field is the action ("bind" or "connect")
217 Action action = null;
218 for (Action a: Action.values()) {
219 if (s[0].equalsIgnoreCase(a.name())) {
220 action = a;
221 break;
|