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.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import net.sf.urlchecker.exception.handling.AlwaysFalse;
27  import net.sf.urlchecker.exception.handling.AlwaysTrue;
28  import net.sf.urlchecker.exception.handling.ExceptionHandler;
29  
30  import org.apache.commons.configuration.HierarchicalConfiguration;
31  import org.apache.commons.configuration.XMLConfiguration;
32  import org.apache.commons.httpclient.ConnectTimeoutException;
33  import org.apache.commons.httpclient.NoHttpResponseException;
34  import org.apache.log4j.Logger;
35  
36  /**
37   * The ExceptionHandlers Configurer Class .
38   * 
39   * <p>
40   * <b> $Id: ExceptionHandlersConfigurer.java 180 2010-12-12 18:47:56Z georgosn
41   * $</b>
42   * </p>
43   * 
44   * @author $LastChangedBy: georgosn $
45   * @version $LastChangedRevision: 184 $
46   */
47  public class ExceptionHandlersConfigurer implements
48          Configurer<Map<String, ExceptionHandler<?>>> {
49  
50      /** The Constant EXCEPTION_HANDLER_NOT_INTO_ACCOUNT. */
51      private static final String EXCEPTION_HANDLER_NOT_INTO_ACCOUNT = "!!!The configuration will continue but the exception handler will not be taken into account!!!";
52  
53      /** The Constant LOGGER. */
54      private static final Logger LOGGER                             = Logger.getLogger(ExceptionHandlersConfigurer.class
55                                                                             .getName());
56  
57      /*
58       * (non-Javadoc)
59       * 
60       * @see
61       * net.sf.urlchecker.communication.configurers.Configurer#configureWith(org
62       * .apache.commons.configuration.XMLConfiguration, java.lang.Object)
63       */
64      /** {@inheritDoc} */
65      public Map<String, ExceptionHandler<?>> configureWith(
66              final XMLConfiguration config) {
67          final Map<String, ExceptionHandler<?>> exceptionHandlers = new HashMap<String, ExceptionHandler<?>>();
68          final List<HierarchicalConfiguration> handlersConfig = config
69                  .configurationsAt("handlers.handler");
70          if (null != handlersConfig) {
71              exceptionHandlers.clear();
72              for (final HierarchicalConfiguration handler : handlersConfig) {
73                  try {
74                      final Class<ExceptionHandler> exceptionHandler = (Class<ExceptionHandler>) Class
75                              .forName(handler.getString("[@class]"));
76  
77                      exceptionHandlers.put(handler.getString("[@exception]"),
78                              exceptionHandler.newInstance());
79                  } catch (final InstantiationException e) {
80                      LOGGER.error("Configured class "
81                              + handler.getString("name")
82                              + " can not be instantiated. Check that the class has a no arguments constructor");
83                      LOGGER.warn(EXCEPTION_HANDLER_NOT_INTO_ACCOUNT, e);
84                  } catch (final IllegalAccessException e) {
85                      LOGGER.error("Configured class "
86                              + handler.getString("name")
87                              + " can not be accessed. Make sure the class has a PUBLIC no arguments constructor.");
88                      LOGGER.warn(EXCEPTION_HANDLER_NOT_INTO_ACCOUNT, e);
89                  } catch (final ClassNotFoundException e) {
90                      LOGGER.error("Configured class "
91                              + handler.getString("name")
92                              + " can not be found. Make sure the class is in the classpath.");
93                      LOGGER.warn(EXCEPTION_HANDLER_NOT_INTO_ACCOUNT, e);
94                  }
95              }
96          } else {
97              LOGGER.info("No exception handler is configured. The default ones will be used for NoHttpResponse and ConnectTimeoutException");
98              exceptionHandlers.put(NoHttpResponseException.class.getName(),
99                      new AlwaysTrue());
100             exceptionHandlers.put(ConnectTimeoutException.class.getName(),
101                     new AlwaysFalse());
102         }
103 
104         return exceptionHandlers;
105     }
106 }