001: /* ExternalImplDecideRule
002: *
003: * Created on May 25, 2005
004: *
005: * Copyright (C) 2005 Internet Archive.
006: *
007: * This file is part of the Heritrix web crawler (crawler.archive.org).
008: *
009: * Heritrix is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU Lesser Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * any later version.
013: *
014: * Heritrix is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU Lesser Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser Public License
020: * along with Heritrix; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: */
023: package org.archive.crawler.deciderules;
024:
025: import java.util.logging.Logger;
026:
027: import javax.management.AttributeNotFoundException;
028:
029: import org.archive.crawler.settings.SimpleType;
030:
031: /**
032: * A rule that can be configured to take alternate implementations
033: * of the ExternalImplInterface.
034: * If no implementation specified, or none found, returns
035: * configured decision.
036: * @author stack
037: * @version $Date: 2007-02-18 21:53:01 +0000 (Sun, 18 Feb 2007) $, $Revision: 4914 $
038: */
039: public class ExternalImplDecideRule extends PredicatedDecideRule {
040:
041: private static final long serialVersionUID = 7727715263469524372L;
042:
043: private static final Logger LOGGER = Logger
044: .getLogger(ExternalImplDecideRule.class.getName());
045: static final String ATTR_IMPLEMENTATION = "implementation-class";
046: private ExternalImplInterface implementation = null;
047:
048: /**
049: * @param name Name of this rule.
050: */
051: public ExternalImplDecideRule(String name) {
052: super (name);
053: setDescription("ExternalImplDecideRule. Rule that "
054: + "instantiates implementations of the ExternalImplInterface. "
055: + "The implementation needs to be present on the classpath. "
056: + "On initialization, the implementation is instantiated ("
057: + "assumption is that there is public default constructor).");
058: addElementToDefinition(new SimpleType(ATTR_IMPLEMENTATION,
059: "Name of implementation of ExternalImplInterface class to "
060: + "instantiate.", ""));
061: }
062:
063: protected boolean evaluate(Object obj) {
064: ExternalImplInterface impl = getConfiguredImplementation(obj);
065: return (impl != null) ? impl.evaluate(obj) : false;
066: }
067:
068: /**
069: * Get implementation, if one specified.
070: * If none specified, will keep trying to find one. Will be messy
071: * if the provided class is not-instantiable or not implementation
072: * of ExternalImplInterface.
073: * @param o A context object.
074: * @return Instance of <code>ExternalImplInterface</code> or null.
075: */
076: protected synchronized ExternalImplInterface getConfiguredImplementation(
077: Object o) {
078: if (this .implementation != null) {
079: return this .implementation;
080: }
081: ExternalImplInterface result = null;
082: try {
083: String className = (String) getAttribute(o,
084: ATTR_IMPLEMENTATION);
085: if (className != null && className.length() != 0) {
086: Object obj = Class.forName(className).newInstance();
087: if (!(obj instanceof ExternalImplInterface)) {
088: LOGGER
089: .severe("Implementation "
090: + className
091: + " does not implement ExternalImplInterface");
092: }
093: result = (ExternalImplInterface) obj;
094: this .implementation = result;
095: }
096: } catch (AttributeNotFoundException e) {
097: LOGGER.severe(e.getMessage());
098: } catch (InstantiationException e) {
099: LOGGER.severe(e.getMessage());
100: } catch (IllegalAccessException e) {
101: LOGGER.severe(e.getMessage());
102: } catch (ClassNotFoundException e) {
103: LOGGER.severe(e.getMessage());
104: }
105: return result;
106: }
107: }
|