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.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   * A factory for creating Communication objects.
43   * 
44   * <p>
45   * <b> $Id: CommunicationFactory.java 183 2010-12-17 19:10:21Z georgosn $</b>
46   * </p>
47   * 
48   * @author $LastChangedBy: georgosn $
49   * @version $LastChangedRevision: 183 $
50   */
51  public class CommunicationFactory
52          extends
53          GenericCommunicationFactory<HttpClient, ClientConnectionManager, HTTPMethods, HttpRequestBase> {
54  
55      /** The Constant FACTORY. */
56      private static final CommunicationFactory FACTORY = new CommunicationFactory();
57  
58      /**
59       * Gets the single instance of CommunicationFactory.
60       * 
61       * @return single instance of CommunicationFactory
62       */
63      public static CommunicationFactory getInstance() {
64          return FACTORY;
65      }
66  
67      /**
68       * Instantiates a new communication FACTORY.
69       */
70      private CommunicationFactory() {
71          configured = false;
72          client = null;
73          multithreaded = false;
74  
75      }
76  
77      /*
78       * (non-Javadoc)
79       * 
80       * @see
81       * net.sf.urlchecker.communication.GenericCommunicationFactory#configureClient
82       * (boolean)
83       */
84      /** {@inheritDoc} */
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                 // Configure retries
120                 ((DefaultHttpClient) client)
121                         .setHttpRequestRetryHandler(new RequestRetryHandler());
122                 // ((DefaultHttpClient)client).setHttpConnectionManager(manager);
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                 // last statement always
135                 configured = true;
136             } else {
137                 // in case a new client is requested with a different threading
138                 // scheme than the one that is active, throw a configuration
139                 // exception
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      * (non-Javadoc)
151      * 
152      * @see
153      * net.sf.urlchecker.communication.GenericCommunicationFactory#configureClient
154      * (boolean, java.lang.String)
155      */
156     /** {@inheritDoc} */
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      * (non-Javadoc)
169      * 
170      * @see
171      * net.sf.urlchecker.communication.GenericCommunicationFactory#getMethod
172      * (java.lang.String)
173      */
174     /** {@inheritDoc} */
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          * method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new
187          * RetryHandler());
188          */
189 
190         return method;
191     }
192 
193     /*
194      * (non-Javadoc)
195      * 
196      * @see
197      * net.sf.urlchecker.communication.GenericCommunicationFactory#getMethodType
198      * (java.lang.String)
199      */
200     /** {@inheritDoc} */
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      * (non-Javadoc)
218      * 
219      * @see
220      * net.sf.urlchecker.communication.GenericCommunicationFactory#shutdown()
221      */
222     /** {@inheritDoc} */
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 }