1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  package net.sf.urlchecker.v2.communication;
21  
22  import java.text.MessageFormat;
23  import java.util.regex.Pattern;
24  
25  import net.sf.urlchecker.communication.GenericCommunicationFactory;
26  import net.sf.urlchecker.communication.configurers.ExceptionHandlersConfigurer;
27  import net.sf.urlchecker.communication.configurers.RetriesConfigurer;
28  import net.sf.urlchecker.communication.configurers.URLPatternConfigurer;
29  import net.sf.urlchecker.communication.configurers.ValidCodesConfigurer;
30  import net.sf.urlchecker.v2.communication.configurers.ClientConfigurer;
31  import net.sf.urlchecker.v2.communication.configurers.MethodsConfigurer;
32  
33  import org.apache.commons.configuration.ConfigurationException;
34  import org.apache.commons.configuration.XMLConfiguration;
35  import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
36  import org.apache.http.client.HttpClient;
37  import org.apache.http.client.methods.HttpRequestBase;
38  import org.apache.http.conn.ClientConnectionManager;
39  import org.apache.http.impl.client.DefaultHttpClient;
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  public class CommunicationFactory
52          extends
53          GenericCommunicationFactory<HttpClient, ClientConnectionManager, HTTPMethods, HttpRequestBase> {
54  
55      
56      private static final CommunicationFactory FACTORY = new CommunicationFactory();
57  
58      
59  
60  
61  
62  
63      public static CommunicationFactory getInstance() {
64          return FACTORY;
65      }
66  
67      
68  
69  
70      private CommunicationFactory() {
71          configured = false;
72          client = null;
73          multithreaded = false;
74  
75      }
76  
77      
78  
79  
80  
81  
82  
83  
84      
85      @Override
86      public HttpClient configureClient(boolean localThreadSceme)
87              throws ConfigurationException {
88          synchronized (this) {
89              if (null == client) {
90  
91                  multithreaded = localThreadSceme;
92  
93                  XMLConfiguration config = null;
94  
95                  try {
96                      config = new XMLConfiguration(fileName);
97  
98                  } catch (final ConfigurationException e) {
99                      LOGGER.warn(MessageFormat.format(WARNINGS[0], new Object[] {
100                             fileName, FILE_NOT_FOUND }));
101                     try {
102                         config = new XMLConfiguration(
103                                 HTTPCONFIGURATION_FILENAME);
104                     } catch (final ConfigurationException e1) {
105                         LOGGER.warn(MessageFormat.format(WARNINGS[0],
106                                 new Object[] { HTTPCONFIGURATION_FILENAME,
107                                         NO_CONFIG }));
108 
109                     }
110                 } finally {
111                     if (config == null) {
112                         throw new ConfigurationException("no configuration");
113                     }
114                 }
115                 final ClientConfigurer current = new ClientConfigurer(
116                         multithreaded);
117                 client = current.configureWith(config);
118 
119                 
120                 ((DefaultHttpClient) client)
121                         .setHttpRequestRetryHandler(new RequestRetryHandler());
122                 
123                 final RetriesConfigurer current1 = new RetriesConfigurer();
124                 maxretries = current1.configureWith(config);
125                 final MethodsConfigurer current2 = new MethodsConfigurer();
126                 methods = current2.configureWith(config);
127                 final ExceptionHandlersConfigurer current3 = new ExceptionHandlersConfigurer();
128                 exceptionHandlers = current3.configureWith(config);
129                 final ValidCodesConfigurer current4 = new ValidCodesConfigurer();
130                 validCodes = current4.configureWith(config);
131                 final URLPatternConfigurer current5 = new URLPatternConfigurer();
132                 final String pattern = current5.configureWith(config);
133                 urlpattern = null == pattern ? null : Pattern.compile(pattern);
134                 
135                 configured = true;
136             } else {
137                 
138                 
139                 
140                 if (localThreadSceme != multithreaded) {
141                     throw new ConfigurationException(
142                             "Builder can only return one type of configuration until shutdown");
143                 }
144             }
145             return client;
146         }
147     }
148 
149     
150 
151 
152 
153 
154 
155 
156     
157     @Override
158     public HttpClient configureClient(boolean localThreadSceme, String FileName)
159             throws ConfigurationException {
160         synchronized (this) {
161             fileName = FileName;
162 
163             return configureClient(localThreadSceme);
164         }
165     }
166 
167     
168 
169 
170 
171 
172 
173 
174     
175     @Override
176     public HttpRequestBase getMethod(String source) {
177         if (!configured) {
178             throw new IllegalStateException();
179         }
180         HTTPMethods type = getMethodType(source);
181         if (null == type) {
182             type = HTTPMethods.HEAD;
183         }
184         final HttpRequestBase method = type.getMethod(source);
185         
186 
187 
188 
189 
190         return method;
191     }
192 
193     
194 
195 
196 
197 
198 
199 
200     
201     @Override
202     public HTTPMethods getMethodType(String source) {
203         if (!configured) {
204             throw new IllegalStateException();
205         }
206         synchronized (this) {
207             HTTPMethods retValue = methods.get(source);
208             if (retValue == null) {
209                 retValue = methods.get(null);
210             }
211 
212             return retValue;
213         }
214     }
215 
216     
217 
218 
219 
220 
221 
222     
223     @Override
224     public void shutdown() {
225         synchronized (this) {
226             if (multithreaded) {
227                 MultiThreadedHttpConnectionManager.shutdownAll();
228                 multithreaded = false;
229             }
230             if (null != client) {
231                 client.getConnectionManager().shutdown();
232                 client = null;
233             }
234             configured = false;
235         }
236     }
237 
238 }