| 1 23 package org.archive.crawler.deciderules; 24 25 import java.util.logging.Logger ; 26 27 import javax.management.AttributeNotFoundException ; 28 29 import org.archive.crawler.settings.SimpleType; 30 31 39 public class ExternalImplDecideRule 40 extends PredicatedDecideRule { 41 42 private static final long serialVersionUID = 7727715263469524372L; 43 44 private static final Logger LOGGER = 45 Logger.getLogger(ExternalImplDecideRule.class.getName()); 46 static final String ATTR_IMPLEMENTATION = "implementation-class"; 47 private ExternalImplInterface implementation = null; 48 49 52 public ExternalImplDecideRule(String name) { 53 super(name); 54 setDescription("ExternalImplDecideRule. Rule that " + 55 "instantiates implementations of the ExternalImplInterface. " + 56 "The implementation needs to be present on the classpath. " + 57 "On initialization, the implementation is instantiated (" + 58 "assumption is that there is public default constructor)."); 59 addElementToDefinition(new SimpleType(ATTR_IMPLEMENTATION, 60 "Name of implementation of ExternalImplInterface class to " + 61 "instantiate.", "")); 62 } 63 64 boolean evaluate(Object obj) { 65 ExternalImplInterface impl = getConfiguredImplementation(obj); 66 return (impl != null)? impl.evaluate(obj): false; 67 } 68 69 77 protected synchronized ExternalImplInterface 78 getConfiguredImplementation(Object o) { 79 if (this.implementation != null) { 80 return this.implementation; 81 } 82 ExternalImplInterface result = null; 83 try { 84 String className = 85 (String )getAttribute(o, ATTR_IMPLEMENTATION); 86 if (className != null && className.length() != 0) { 87 Object obj = Class.forName(className).newInstance(); 88 if (!(obj instanceof ExternalImplInterface)) { 89 LOGGER.severe("Implementation " + className + 90 " does not implement ExternalImplInterface"); 91 } 92 result = (ExternalImplInterface)obj; 93 this.implementation = result; 94 } 95 } catch (AttributeNotFoundException e) { 96 LOGGER.severe(e.getMessage()); 97 } catch (InstantiationException e) { 98 LOGGER.severe(e.getMessage()); 99 } catch (IllegalAccessException e) { 100 LOGGER.severe(e.getMessage()); 101 } catch (ClassNotFoundException e) { 102 LOGGER.severe(e.getMessage()); 103 } 104 return result; 105 } 106 } | Popular Tags |