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;
21
22 import java.util.Map;
23 import java.util.regex.Pattern;
24
25 import net.sf.urlchecker.commands.Result;
26 import net.sf.urlchecker.exception.handling.ExceptionHandler;
27
28 import org.apache.commons.configuration.ConfigurationException;
29 import org.apache.commons.lang.ArrayUtils;
30 import org.apache.log4j.Logger;
31
32 /**
33 * A factory for creating GenericCommunication objects. This class was created
34 * to serve the two different httpClients and may be removed in future versions
35 * of the library.
36 *
37 * @param <T>
38 * the generic type
39 * @param <M>
40 * the generic type
41 * @param <S>
42 * the generic type
43 * @param <E>
44 * the element type
45 * <p>
46 * <b> $Id: GenericCommunicationFactory.java 180 2010-12-12 18:47:56Z
47 * georgosn $</b>
48 * </p>
49 *
50 * @author $LastChangedBy: georgosn $
51 * @version $LastChangedRevision: 184 $
52 */
53 public abstract class GenericCommunicationFactory<T, M, S, E> {
54 /**
55 * The Constant PATTERN to be used if no URL pattern is configured. Value
56 *
57 * <pre>
58 * ((https?|ftp|gopher):((//)|(\\\\))+([\\w\\d:#@%/;$\\(\\)~_?\\+-=\\\\.]|&(?!(lt;|gt;|amp;|apos;|quot;)))*)
59 * </pre>
60 */
61 public static final Pattern DEFAULT_PATTERN = Pattern
62 .compile("((https?|ftp|gopher):((//)|(\\\\))+([\\w\\d:#@%/;$\\(\\)~_?\\+-=\\\\.]|&(?!(lt;|gt;|amp;|apos;|quot;)))*)");
63
64 /** The Constant serialVersionUID. */
65 private static final long serialVersionUID = 1L;
66
67 /** The Constant HTTPCONFIGURATION_FILENAME. */
68 protected static final String HTTPCONFIGURATION_FILENAME = "httpconfiguration.xml";
69
70 /** The Constant WARNINGS. */
71 protected static final String[] WARNINGS = { "The configuration {0} was not found. {1}" };
72
73 /** The Constant NO_CONFIG. */
74 protected static final String NO_CONFIG = "DEFAULT values will be left on httpclient";
75
76 /** The Constant FILE_NOT_FOUND. */
77 protected static final String FILE_NOT_FOUND = "Trying the DEFAULT configuration file";
78
79 /** The Constant LOGGER. */
80 protected static final Logger LOGGER = Logger.getLogger(CommunicationFactory.class
81 .getName());
82
83 /** The configured. */
84 transient protected boolean configured;
85
86 /** The client. */
87 transient protected T client;
88
89 /** The manager. */
90 protected transient M manager;
91
92 /** The multithreaded. */
93 protected boolean multithreaded;
94
95 /** The valid codes. */
96 protected Map<String, Integer[]> validCodes;
97
98 /** The exception handlers. */
99 protected Map<String, ExceptionHandler<?>> exceptionHandlers;
100
101 /** The maxretries. */
102 protected int maxretries;
103
104 /** The methods. */
105 protected Map<String, S> methods;
106
107 /** The file name. */
108 protected String fileName;
109
110 /** The urlpattern. */
111 protected Pattern urlpattern;
112
113 /**
114 * Configure client.
115 *
116 * @param localThreadSceme
117 * the multi threaded
118 * @return the http client
119 * @throws org.apache.commons.configuration.ConfigurationException
120 * the configuration exception
121 * @param <T>
122 * a T object.
123 * @param <M>
124 * a M object.
125 * @param <S>
126 * a S object.
127 * @param <E>
128 * a E object.
129 */
130 public abstract T configureClient(final boolean localThreadSceme)
131 throws ConfigurationException;
132
133 /**
134 * Configure client.
135 *
136 * @param localThreadSceme
137 * the local thread sceme
138 * @param FileName
139 * the file name
140 * @return the t
141 * @throws org.apache.commons.configuration.ConfigurationException
142 * the configuration exception
143 */
144 public abstract T configureClient(final boolean localThreadSceme,
145 String FileName) throws ConfigurationException;
146
147 /**
148 * Returns the exception handlers.
149 *
150 * @return the exceptionHandlers
151 */
152 public synchronized Map<String, ExceptionHandler<?>> getExceptionHandlers() {
153 if (!configured) {
154 throw new IllegalStateException();
155 }
156 return exceptionHandlers;
157 }
158
159 /**
160 * Returns the maxretries.
161 *
162 * @return the maxretries
163 */
164 public synchronized int getMaxretries() {
165 if (!configured) {
166 throw new IllegalStateException();
167 }
168 return maxretries;
169 }
170
171 /**
172 * Returns the method.
173 *
174 * @param source
175 * the source
176 * @return the method
177 */
178 public abstract E getMethod(final String source);
179
180 /**
181 * Returns the method type.
182 *
183 * @param source
184 * the source
185 * @return the method
186 */
187 public abstract S getMethodType(final String source);
188
189 /**
190 * Returns the only valid codes.
191 *
192 * @param source
193 * the source
194 * @return the only valid codes
195 */
196 public Integer[] getOnlyValidCodes(String source) {
197 if (!configured) {
198 throw new IllegalStateException();
199 }
200 Integer[] ret = new Integer[] {};
201 synchronized (validCodes) {
202 if (validCodes.containsKey(source)) {
203 ret = validCodes.get(source);
204 } else {
205 if (validCodes.containsKey("*")) {
206 ret = validCodes.get("*");
207 }
208 }
209 return ret;
210 }
211 }
212
213 /**
214 * Returns the urlpattern.
215 *
216 * @return the urlpattern
217 */
218 public synchronized Pattern getUrlpattern() {
219 if (!configured) {
220 throw new IllegalStateException();
221 }
222 return null == urlpattern ? DEFAULT_PATTERN : urlpattern;
223 }
224
225 /**
226 * Returns the valid codes.
227 *
228 * @return the validCodes
229 */
230 public synchronized Map<String, Integer[]> getValidCodes() {
231 if (!configured) {
232 throw new IllegalStateException();
233 }
234 return validCodes;
235 }
236
237 /**
238 * Checks if client is configured.
239 *
240 * @return true, if is configured
241 */
242 public boolean isConfigured() {
243 return configured;
244 }
245
246 /**
247 * Checks if client connection manager is multi threaded.
248 *
249 * @return true, if is multi threaded
250 */
251 public synchronized boolean isMultiThreaded() {
252 if (!configured) {
253 throw new IllegalStateException();
254 }
255 return multithreaded;
256 }
257
258 /**
259 * Checks if a response is valid .
260 *
261 * @param result
262 * the result
263 * @return true, if is valid response
264 */
265 public synchronized boolean isValidResponse(final Result result) {
266 if (!configured) {
267 throw new IllegalStateException();
268 }
269 boolean ret = false;
270 synchronized (validCodes) {
271 if (validCodes.containsKey(result.getTarget())) {
272 ret = ArrayUtils.contains(validCodes.get(result.getTarget()),
273 result.getResult());
274 } else {
275 ret = ArrayUtils.contains(validCodes.get("*"),
276 result.getResult());
277 }
278 }
279 return ret;
280 }
281
282 /**
283 * Shuts down the client.
284 */
285 public abstract void shutdown();
286 }