KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > crawler > deciderules > ExternalImplDecideRule


1 /* ExternalImplDecideRule
2  *
3  * Created on May 25, 2005
4  *
5  * Copyright (C) 2005 Internet Archive.
6  *
7  * This file is part of the Heritrix web crawler (crawler.archive.org).
8  *
9  * Heritrix is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * any later version.
13  *
14  * Heritrix is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser Public License
20  * along with Heritrix; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23 package org.archive.crawler.deciderules;
24
25 import java.util.logging.Logger JavaDoc;
26
27 import javax.management.AttributeNotFoundException JavaDoc;
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/01/13 01:31:14 $, $Revision: 1.1.16.1 $
38  */

39 public class ExternalImplDecideRule
40 extends PredicatedDecideRule {
41
42     private static final long serialVersionUID = 7727715263469524372L;
43
44     private static final Logger JavaDoc LOGGER =
45         Logger.getLogger(ExternalImplDecideRule.class.getName());
46     static final String JavaDoc ATTR_IMPLEMENTATION = "implementation-class";
47     private ExternalImplInterface implementation = null;
48
49     /**
50      * @param name Name of this rule.
51      */

52     public ExternalImplDecideRule(String JavaDoc 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 JavaDoc 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 JavaDoc o) {
79         if (this.implementation != null) {
80             return this.implementation;
81         }
82         ExternalImplInterface result = null;
83         try {
84             String JavaDoc className =
85                 (String JavaDoc)getAttribute(o, ATTR_IMPLEMENTATION);
86             if (className != null && className.length() != 0) {
87                 Object JavaDoc 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 JavaDoc e) {
96             LOGGER.severe(e.getMessage());
97         } catch (InstantiationException JavaDoc e) {
98             LOGGER.severe(e.getMessage());
99         } catch (IllegalAccessException JavaDoc e) {
100             LOGGER.severe(e.getMessage());
101         } catch (ClassNotFoundException JavaDoc e) {
102             LOGGER.severe(e.getMessage());
103         }
104         return result;
105     }
106 }
Popular Tags