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;
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public abstract class GenericCommunicationFactory<T, M, S, E> {
54
55
56
57
58
59
60
61 public static final Pattern DEFAULT_PATTERN = Pattern
62 .compile("((https?|ftp|gopher):((//)|(\\\\))+([\\w\\d:#@%/;$\\(\\)~_?\\+-=\\\\.]|&(?!(lt;|gt;|amp;|apos;|quot;)))*)");
63
64
65 private static final long serialVersionUID = 1L;
66
67
68 protected static final String HTTPCONFIGURATION_FILENAME = "httpconfiguration.xml";
69
70
71 protected static final String[] WARNINGS = { "The configuration {0} was not found. {1}" };
72
73
74 protected static final String NO_CONFIG = "DEFAULT values will be left on httpclient";
75
76
77 protected static final String FILE_NOT_FOUND = "Trying the DEFAULT configuration file";
78
79
80 protected static final Logger LOGGER = Logger.getLogger(CommunicationFactory.class
81 .getName());
82
83
84 transient protected boolean configured;
85
86
87 transient protected T client;
88
89
90 protected transient M manager;
91
92
93 protected boolean multithreaded;
94
95
96 protected Map<String, Integer[]> validCodes;
97
98
99 protected Map<String, ExceptionHandler<?>> exceptionHandlers;
100
101
102 protected int maxretries;
103
104
105 protected Map<String, S> methods;
106
107
108 protected String fileName;
109
110
111 protected Pattern urlpattern;
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130 public abstract T configureClient(final boolean localThreadSceme)
131 throws ConfigurationException;
132
133
134
135
136
137
138
139
140
141
142
143
144 public abstract T configureClient(final boolean localThreadSceme,
145 String FileName) throws ConfigurationException;
146
147
148
149
150
151
152 public synchronized Map<String, ExceptionHandler<?>> getExceptionHandlers() {
153 if (!configured) {
154 throw new IllegalStateException();
155 }
156 return exceptionHandlers;
157 }
158
159
160
161
162
163
164 public synchronized int getMaxretries() {
165 if (!configured) {
166 throw new IllegalStateException();
167 }
168 return maxretries;
169 }
170
171
172
173
174
175
176
177
178 public abstract E getMethod(final String source);
179
180
181
182
183
184
185
186
187 public abstract S getMethodType(final String source);
188
189
190
191
192
193
194
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
215
216
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
227
228
229
230 public synchronized Map<String, Integer[]> getValidCodes() {
231 if (!configured) {
232 throw new IllegalStateException();
233 }
234 return validCodes;
235 }
236
237
238
239
240
241
242 public boolean isConfigured() {
243 return configured;
244 }
245
246
247
248
249
250
251 public synchronized boolean isMultiThreaded() {
252 if (!configured) {
253 throw new IllegalStateException();
254 }
255 return multithreaded;
256 }
257
258
259
260
261
262
263
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
284
285 public abstract void shutdown();
286 }