View Javadoc

1   /*
2    * (C)opyright 2010, Nikolaos Georgosopoulos
3    *
4    * This file is part of URLChecker.
5   
6       URLChecker is free software: you can redistribute it and/or modify
7       it under the terms of the Lesser General Public License as published by
8       the Free Software Foundation, either version 3 of the License, or
9       (at your option) any later version.
10  
11      URLChecker is distributed in the hope that it will be useful,
12      but WITHOUT ANY WARRANTY; without even the implied warranty of
13      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14      GNU General Public License for more details.
15  
16      You should have received a copy of the Lesser General Public License
17      along with URLChecker.  If not, see <http://www.gnu.org/licenses/>.
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   * The ValidCodes Configurer Class .
33   * 
34   * <p>
35   * <b> $Id: ValidCodesConfigurer.java 182 2010-12-13 22:51:53Z georgosn $</b>
36   * </p>
37   * 
38   * @author $LastChangedBy: georgosn $
39   * @version $LastChangedRevision: 182 $
40   */
41  public class ValidCodesConfigurer implements Configurer<Map<String, Integer[]>> {
42  
43      /** The Constant LOGGER. */
44      private static final Logger LOGGER = Logger.getLogger(ValidCodesConfigurer.class
45                                                 .getName());
46  
47      /*
48       * (non-Javadoc)
49       * 
50       * @see
51       * net.sf.urlchecker.communication.configurers.Configurer#configureWith(org
52       * .apache.commons.configuration.XMLConfiguration, java.lang.Object)
53       */
54      /** {@inheritDoc} */
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  }