1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 /***
32 * A rule that can be configured to take alternate implementations
33 * of the ExternalImplInterface.
34 * If no implementation specified, or none found, returns
35 * configured decision.
36 * @author stack
37 * @version $Date: 2007-02-18 21:53:01 +0000 (Sun, 18 Feb 2007) $, $Revision: 4914 $
38 */
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 /***
50 * @param name Name of this rule.
51 */
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 protected boolean evaluate(Object obj) {
65 ExternalImplInterface impl = getConfiguredImplementation(obj);
66 return (impl != null)? impl.evaluate(obj): false;
67 }
68
69 /***
70 * Get implementation, if one specified.
71 * If none specified, will keep trying to find one. Will be messy
72 * if the provided class is not-instantiable or not implementation
73 * of ExternalImplInterface.
74 * @param o A context object.
75 * @return Instance of <code>ExternalImplInterface</code> or null.
76 */
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 }