1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package net.sf.urlchecker.communication.configurers;
21
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.apache.commons.configuration.HierarchicalConfiguration;
28 import org.apache.commons.configuration.XMLConfiguration;
29 import org.apache.log4j.Logger;
30
31
32
33
34
35
36
37
38
39
40
41 public class ValidCodesConfigurer implements Configurer<Map<String, Integer[]>> {
42
43
44 private static final Logger LOGGER = Logger.getLogger(ValidCodesConfigurer.class
45 .getName());
46
47
48
49
50
51
52
53
54
55 public Map<String, Integer[]> configureWith(XMLConfiguration config) {
56 final Map<String, Integer[]> validCodes = new HashMap<String, Integer[]>();
57 validCodes.clear();
58 validCodes.put("*", new Integer[] { 200 });
59 if (config.containsKey("return_codes")) {
60 final List<HierarchicalConfiguration> validConfigs = config
61 .configurationsAt("return_codes");
62 if (null != validConfigs) {
63 for (final HierarchicalConfiguration valid : validConfigs) {
64 valid.setListDelimiter(',');
65 final String source = valid.getString("source");
66 final List<Integer> codesList = new ArrayList<Integer>();
67 for (final Object code : valid.getList("codes.code")) {
68 try {
69 codesList.add(Integer.valueOf(code.toString()));
70 } catch (final NumberFormatException e) {
71 LOGGER.error(
72 "One of the configured returned codes is not a valid signed integer, it will be skipped:"
73 + code.toString(), e);
74 }
75 }
76 if (!codesList.isEmpty()) {
77 validCodes.put(source,
78 codesList.toArray(new Integer[] {}));
79 }
80 }
81 } else {
82 LOGGER.warn("No valid return codes are configured, using defaults:"
83 + validCodes.values().toString());
84 }
85 }
86 return validCodes;
87 }
88
89 }