1 25 package org.archive.crawler.settings; 26 27 import java.io.File ; 28 import java.lang.reflect.Constructor ; 29 import java.lang.reflect.InvocationTargetException ; 30 import java.text.ParseException ; 31 import java.util.Collection ; 32 import java.util.Collections ; 33 import java.util.HashMap ; 34 import java.util.HashSet ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.Map ; 38 import java.util.Set ; 39 import java.util.logging.Level ; 40 41 import javax.management.AttributeNotFoundException ; 42 import javax.management.InvalidAttributeValueException ; 43 44 import org.archive.crawler.datamodel.CrawlOrder; 45 import org.archive.crawler.settings.refinements.Refinement; 46 import org.archive.net.UURI; 47 import org.archive.util.ArchiveUtils; 48 49 58 public abstract class SettingsHandler { 59 60 private SettingsCache settingsCache = 61 new SettingsCache(new CrawlerSettings(this, null)); 62 63 64 private CrawlOrder order; 65 66 private Set <ValueErrorHandler> valueErrorHandlers 67 = Collections.synchronizedSet(new HashSet <ValueErrorHandler>()); 68 private int errorReportingLevel = Level.ALL.intValue(); 69 70 71 final static String INTEGER = "integer"; 72 final static String LONG = "long"; 73 final static String FLOAT = "float"; 74 final static String DOUBLE = "double"; 75 final static String BOOLEAN = "boolean"; 76 final static String STRING = "string"; 77 final static String TEXT = "text"; 78 final static String OBJECT = "object"; 79 final static String TIMESTAMP = "timestamp"; 80 final static String MAP = "map"; 81 final static String INTEGER_LIST = "integerList"; 82 final static String LONG_LIST = "longList"; 83 final static String FLOAT_LIST = "floatList"; 84 final static String DOUBLE_LIST = "doubleList"; 85 final static String STRING_LIST = "stringList"; 86 private final static String names[][] = new String [][] { 87 { INTEGER, "java.lang.Integer"}, 88 { LONG, "java.lang.Long"}, 89 { FLOAT, "java.lang.Float"}, 90 { DOUBLE, "java.lang.Double"}, 91 { BOOLEAN, "java.lang.Boolean"}, 92 { STRING, "java.lang.String"}, 93 { TEXT, "org.archive.crawler.settings.TextField"}, 94 { OBJECT, "org.archive.crawler.settings.ModuleType"}, 95 { TIMESTAMP, "java.util.Date"}, 96 { MAP, "org.archive.crawler.settings.MapType"}, 97 { INTEGER_LIST, 98 "org.archive.crawler.settings.IntegerList"}, 99 { LONG_LIST, "org.archive.crawler.settings.LongList"}, 100 { FLOAT_LIST, "org.archive.crawler.settings.FloatList"}, 101 { DOUBLE_LIST, "org.archive.crawler.settings.DoubleList"}, 102 { STRING_LIST, "org.archive.crawler.settings.StringList"}}; 103 private final static Map <String ,String > name2class 104 = new HashMap <String ,String >(); 105 private final static Map <String ,String > class2name 106 = new HashMap <String ,String >(); 107 static { 108 for (int i = 0; i < names.length; i++) { 109 name2class.put(names[i][0], names[i][1]); 110 class2name.put(names[i][1], names[i][0]); 111 } 112 } 113 114 118 public SettingsHandler() throws InvalidAttributeValueException { 119 order = new CrawlOrder(); 120 order.setAsOrder(this); 121 } 122 123 127 public void initialize() { 128 readSettingsObject(settingsCache.getGlobalSettings()); 129 } 130 131 public void cleanup() { 132 this.settingsCache = null; 133 if (this.order != null) { 134 this.order.setController(null); 135 } 136 this.order = null; 137 } 138 139 144 protected String getParentScope(String scope) { 145 int split = scope.indexOf('.'); 146 return (split == -1)? null: scope.substring(split + 1); 147 } 148 149 157 public ModuleType getModule(String name) { 158 return settingsCache.getGlobalSettings().getModule(name); 159 } 160 161 173 public ComplexType getComplexTypeByAbsoluteName( 174 CrawlerSettings settings, String absoluteName) 175 throws AttributeNotFoundException { 176 177 settings = settings == null ? settingsCache.getGlobalSettings() : settings; 178 179 DataContainer data = settings.getData(absoluteName); 180 if (data == null) { 181 CrawlerSettings parentSettings = settings.getParent(); 182 if (parentSettings == null) { 183 throw new AttributeNotFoundException (absoluteName); 184 } 185 return getComplexTypeByAbsoluteName(parentSettings, absoluteName); 186 } 187 return data.getComplexType(); 188 } 189 190 protected static String getTypeName(String className) { 191 return (String ) class2name.get(className); 192 } 193 194 protected static String getClassName(String typeName) { 195 return (String ) name2class.get(typeName); 196 } 197 198 206 protected static Object StringToType(String stringValue, String typeName) { 207 Object value; 208 if (typeName == SettingsHandler.STRING) { 209 value = stringValue; 210 } else if (typeName == SettingsHandler.TEXT) { 211 value = new TextField(stringValue); 212 } else if (typeName == SettingsHandler.INTEGER) { 213 value = Integer.decode(stringValue); 214 } else if (typeName == SettingsHandler.LONG) { 215 value = Long.decode(stringValue); 216 } else if (typeName == SettingsHandler.BOOLEAN) { 217 value = Boolean.valueOf(stringValue); 218 } else if (typeName == SettingsHandler.DOUBLE) { 219 value = Double.valueOf(stringValue); 220 } else if (typeName == SettingsHandler.FLOAT) { 221 value = Float.valueOf(stringValue); 222 } else if (typeName == SettingsHandler.TIMESTAMP) { 223 try { 224 value = ArchiveUtils.parse14DigitDate(stringValue); 225 } catch (ParseException e) { 226 throw new ClassCastException ( 227 "Cannot convert '" 228 + stringValue 229 + "' to type '" 230 + typeName 231 + "'"); 232 } 233 } else { 234 throw new ClassCastException ( 235 "Cannot convert '" 236 + stringValue 237 + "' to type '" 238 + typeName 239 + "'"); 240 } 241 return value; 242 } 243 244 255 public CrawlerSettings getSettings(String host) { 256 return getRefinementsForSettings(getSettingsForHost(host), null); 257 } 258 259 273 public CrawlerSettings getSettings(String host, UURI uuri) { 274 return getRefinementsForSettings(getSettingsForHost(host), uuri); 275 } 276 277 protected CrawlerSettings getSettingsForHost(String host) { 278 CrawlerSettings settings = settingsCache.getSettings(host, null); 279 280 if (settings == null) { 281 String tmpHost = host; 282 settings = getSettingsObject(tmpHost); 283 while (settings == null && tmpHost != null) { 284 tmpHost = getParentScope(tmpHost); 285 settings = getSettingsObject(tmpHost); 286 } 287 288 settingsCache.putSettings(host, settings); 289 } 290 291 return settings; 292 } 293 294 private CrawlerSettings getRefinementsForSettings(CrawlerSettings settings, 295 UURI uri) { 296 if (settings.hasRefinements()) { 297 for(Iterator it = settings.refinementsIterator(); it.hasNext();) { 298 Refinement refinement = (Refinement) it.next(); 299 if (refinement.isWithinRefinementBounds(uri)) { 300 settings = getSettingsObject(settings.getScope(), 301 refinement.getReference()); 302 } 303 } 304 } 305 306 return settings; 307 } 308 309 321 public CrawlerSettings getSettingsObject(String scope) { 322 return getSettingsObject(scope, null); 323 } 324 325 333 public CrawlerSettings getSettingsObject(String scope, String refinement) { 334 CrawlerSettings settings = 335 settingsCache.getSettingsObject(scope, refinement); 336 337 if (settings == null) { 338 settings = new CrawlerSettings(this, scope, refinement); 340 settings = readSettingsObject(settings); 343 if (settings != null) { 344 settingsCache.putSettings(scope, settings); 345 } 346 } 347 return settings; 348 } 349 350 361 public CrawlerSettings getOrCreateSettingsObject(String scope) { 362 return getOrCreateSettingsObject(scope, null); 363 } 364 365 public CrawlerSettings getOrCreateSettingsObject(String scope, 366 String refinement) { 367 CrawlerSettings settings; 368 settings = getSettingsObject(scope, refinement); 369 if (settings == null) { 370 scope = scope.intern(); 371 372 settings = new CrawlerSettings(this, scope, refinement); 374 settingsCache.refreshHostToSettings(); 375 settingsCache.putSettings(scope, settings); 376 } 377 return settings; 378 } 379 380 384 public abstract void writeSettingsObject(CrawlerSettings settings); 385 386 393 protected abstract CrawlerSettings readSettingsObject(CrawlerSettings settings); 394 395 399 public void deleteSettingsObject(CrawlerSettings settings) { 400 settingsCache.deleteSettingsObject(settings); 401 } 402 403 407 public CrawlOrder getOrder() { 408 return order; 409 } 410 411 419 public static ModuleType instantiateModuleTypeFromClassName( 420 String name, String className) 421 throws InvocationTargetException { 422 423 Class cl; 424 try { 425 cl = Class.forName(className); 426 } catch (ClassNotFoundException e) { 427 throw new InvocationTargetException (e); 428 } 429 430 ModuleType module; 431 try { 432 Constructor co = 433 cl.getConstructor(new Class [] { String .class }); 434 module = (ModuleType) co.newInstance(new Object [] { name }); 435 } catch (IllegalArgumentException e) { 436 throw new InvocationTargetException (e); 437 } catch (InstantiationException e) { 438 throw new InvocationTargetException (e); 439 } catch (IllegalAccessException e) { 440 throw new InvocationTargetException (e); 441 } catch (SecurityException e) { 442 throw new InvocationTargetException (e); 443 } catch (NoSuchMethodException e) { 444 throw new InvocationTargetException (e); 445 } 446 return module; 447 } 448 449 457 public abstract File getPathRelativeToWorkingDirectory(String path); 458 459 471 public abstract Collection getDomainOverrides(String rootDomain); 472 473 483 public void unregisterValueErrorHandler(ValueErrorHandler errorHandler) { 484 valueErrorHandlers.remove(errorHandler); 485 } 486 487 501 public void registerValueErrorHandler(ValueErrorHandler errorHandler) { 502 if (errorHandler != null) { 503 valueErrorHandlers.add(errorHandler); 504 } 505 } 506 507 513 boolean fireValueErrorHandlers(Constraint.FailedCheck error) { 514 if (error.getLevel().intValue() >= errorReportingLevel) { 515 for (Iterator it = valueErrorHandlers.iterator(); it.hasNext();) { 516 ((ValueErrorHandler) it.next()).handleValueError(error); 517 } 518 } 519 return valueErrorHandlers.size() > 0; 520 } 521 522 527 public void setErrorReportingLevel(Level level) { 528 errorReportingLevel = level.intValue(); 529 } 530 531 544 public abstract List getListOfAllFiles(); 545 546 551 public void clearPerHostSettingsCache() { 552 settingsCache.clear(); 553 } 554 } 555 | Popular Tags |