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.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  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  public class ExceptionHandlersConfigurer implements
48          Configurer<Map<String, ExceptionHandler<?>>> {
49  
50      
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      
54      private static final Logger LOGGER                             = Logger.getLogger(ExceptionHandlersConfigurer.class
55                                                                             .getName());
56  
57      
58  
59  
60  
61  
62  
63  
64      
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 }