java动态选择要执行的类

时间:2014-10-07 18:34:05

标签: java parsing dynamic reflection classloader

我很确定这对于专家来说非常基础,但对我来说,作为一个新手,它给我带来了困难。

我有3个解析器,每个解析器都有自己的功能,将来会有更多的解析器。现在我想做的是:我希望我的应用程序根据要出现的页面在运行时选择正确的解析器。

为实现这一目标,我做了以下工作:我有一个接口(IWebParser):

public interface IWebParser {
    public abstract Object execute(String page, URL url);
    public abstract List<SimpleWebPosting> parse(String page, URL url, List<String> tokens);

    public abstract Boolean canExecute(URL url);
}

我的每个解析器都实现了这个接口。我有另一个名为ParserControl的类,其中有一个方法提交(String page,URL url) - 这是我的程序总是调用的,只要有一个页面要解析。此类ParserControl从xml文件获取可用的解析器,并尝试(在while语句中)任何解析器是否可以解析相关页面。这是通过canExecute(URL url)方法完成的。现在,在canExecute上收到true后,我想执行该特定的解析器。

我的类ParserControl看起来像这样:

public class ParserControl {
    private static final Logger logger = Logger.getLogger("de.comlineag.snc.parser.ParserControl");
// the list of operational web parser as taken from the properties file is stored within this structure
private static List<IWebParser> webParser; 
// the ParserControl instance - used during instantiation of the class and later to retrieve the list 
private static ParserControl pc = null;

// ParserControl is not to be directly instantiated by other classes
private ParserControl() {
    try {
        webParser = getAllParser();
    } catch (XPathExpressionException | IOException
            | ParserConfigurationException | SAXException e) {
        logger.error("EXCEPTION :: error during parser execution " + e.getMessage());
        e.printStackTrace();
    }
};

// Static 'instance' method - this method is called every time
// the submit method is called but can also be called implicitely to get
// an instance of ParserControl
public static ParserControl getInstance() throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {
    if (pc == null) {pc = new ParserControl();}
    return pc;
}


public static List<SimpleWebPosting> submit(String page, URL url, ArrayList<String> tTerms) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException{
    logger.trace("ParserControl called");
    pc = getInstance();

    while (pc.webParser.iterator().hasNext()) {
        logger.trace("trying parser " + pc.webParser.iterator().getClass().getSimpleName().toString());
        if (((IWebParser) pc.webParser.iterator().getClass().getClassLoader()).canExecute(url)) {
            return ((IWebParser) pc.webParser.iterator().getClass().getClassLoader()).parse(page, url, tTerms);
        } else {
            logger.trace("parser " + pc.webParser.iterator().getClass().getSimpleName().toString() + " returned false to canExecute()" );
        }
    }

    return null;
}


// retrieves all configured parser from the properties file and creates the parser list 
@SuppressWarnings("unchecked")
private <T> ArrayList<T> getAllParser() throws IOException, ParserConfigurationException, SAXException, XPathExpressionException {
    String fileName = "webapp/WEB-INF/properties/webparser.xml";
    ArrayList<T> ar = new ArrayList<T>();

    File file = new File(fileName);
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(file);

    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();

    String expression = "//parser[@type='webparser']/value";
    NodeList nodeList= (NodeList) xpath.compile(expression).evaluate(doc, XPathConstants.NODESET);
    for (int i = 0 ; i < nodeList.getLength() ; i++) {
        ar.add((T) nodeList.item(i).getTextContent());
        logger.trace("found parser " + nodeList.item(i).getTextContent().toString() + " in configuration file " + fileName);
    }
    return ar;
}
}

现在,经过这个冗长的介绍,我的问题是:执行此操作时,我无法实例化解析器类,而是获得NullPointerException。 while循环中的logger.trace返回:

TRACE ParserControl - trying parser Itr   <--- I would expect the class name here!!!
    ERROR SimpleWebCrawler - WEBCRAWLER-Crawler Exception java.lang.NullPointerException

谁能告诉我,我在这里做错了什么?

1 个答案:

答案 0 :(得分:5)

你有一些奇怪的东西在这里发生。我看到的问题:

  • 你有一些单例设计模式,但仍然使用静态变量。
  • 您使用的迭代器错误
  • 你在仿制药中不必使用仿制药
  • 您的IWebParser界面可以返回Boolean,该值可以为空。它应该能够返回null吗?或者它应该是原始类型(boolean)。如果它返回null并且你在if语句中有它,那么你将得到一个NPE。 EG:Boolean b=null; if(b) {} // NPE!!

修正:

  • static变量
  • 中删除webParser
  • getAllParser中删除泛型,以便构造函数读取List<IWebParser> getAllParser()(同样您可以看到我将ArrayList替换为List)。
  • 修复你的迭代器使用,现在你获得迭代器的ClassLoader并尝试将其转换为IWebParser,显然不会工作。这是循环的工作版本,你可以看到我在外面声明了迭代器并使用.next()来获取循环中的下一个IWebParser

Iterator<IWebParser> it = pc.webParser.iterator();
while (it.hasNext()) {
    IWebParser parser = it.next();
    logger.trace("trying parser " + parser.getClass().getSimpleName().toString());
    if (parser.canExecute(url)) {
        return parser.parse(page, url, tTerms);
    } else {
        logger.trace("parser " + parser.getClass().getSimpleName().toString() + " returned false to canExecute()" );
    }
}

想象一下,Iterator是一个对象,其指针位于排序列表中的某个位置。当您致电webParser.iterator()时,它会构建一个指向列表开头的新Iterator。现在,如果你试图遍历这些并继续调用webParser.iterator(),你将总是得到一个指向第一个元素的迭代器。这就是为什么在你的循环之外声明你的Iterator并在里面重用同一个循环的重要性。还值得注意的是,当你想将指针移动到下一个索引时,你只需要在迭代器上调用.next(),这就是为什么我声明parser变量并将其设置为while循环内的下一个变量的原因


Awnsers评论

为什么这是一个单例设计模式?

单例是一种对象,其中应用程序中应该只创建一个且只有一个实例。在Java中,这通常通过使用private构造函数来获得,该构造函数具有通常名为getInstance()的公共静态方法。然后getInstance()方法将创建自己的实例(如果尚未创建和存储它)或返回存储的实例,这通常通过使用静态变量来存储该类的唯一实例来完成。

当您使用面向对象编程时,重要的是充分利用类和类的实例意味着什么。当您合并静态变量和方法时,您应该始终考虑它们为什么应该是静态的。我认为始终可以开始使用非静态变量,只有在需要时才能使其静态化。在这种情况下,List webParser实际上属于类实例而不是每个人,它在类的构造函数中初始化,然后仅在类的非静态实例中使用...因此为什么要将它静态化?你也使用单例模式,这意味着无论如何只有一个实例!

getAllParsers()

中的错误

我假设你传递了一些解析器的类名来添加到这个ParserControl类。在这种情况下,您可以使用Class.forName(className).newInstance()

替换第r.add((T) nodeList.item(i).getTextContent());行 行r.add((IWebParser)Class.forName(nodeList.item(i).getTextContent()).newInstance());

您需要将完整路径传递给该类。 EG:com.me.parsers.IFrameParser,如果你在班级中有一个班级使用$来指定班级,那么也是FYI,EG:com.me.parsers.ParserClass$InnerClassParser

相关问题