com.love320.templateparser.factory.impl.BeanFactoryCacheImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.love320.templateparser.factory.impl.BeanFactoryCacheImpl.java

Source

/**
* Copyright (c) 2010-2011 love320.com
* 
* Licensed under the Apache License, Version 2.0 (the "License");
* 
* Founder admin@love320.com
*/
package com.love320.templateparser.factory.impl;

import java.util.List;

import org.dom4j.Element;
import org.dom4j.Node;

import com.love320.templateparser.cache.Cache;
import com.love320.templateparser.exception.FactoryException;
import com.love320.templateparser.factory.BeanFactory;
import com.love320.templateparser.factory.entity.BeanString;
import com.love320.templateparser.util.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @ClassName: BeanFactoryImpl 
* @Description: TODO
* @author love320.com
* @date 2012-4-15 ?05:01:08 
*  
*/
public class BeanFactoryCacheImpl implements BeanFactory {

    private final static Logger logger = LoggerFactory.getLogger(BeanFactoryCacheImpl.class);

    private Element docroot;
    private Cache cache;
    private String cacheKey = "WWW.LOVE320.COMKeyksdjfksdjglksjdfkdhsgksdjfkljsdlkfj<bean>:";// ()

    public BeanString getBeanString(String name) {
        BeanString bs = (BeanString) cache.getObject(cacheKey + name);
        if (bs == null) {
            try {
                bs = action(name);
            } catch (Exception e) {
                logger.error("Exception", e);
                FactoryException fe = new FactoryException(new String[] { name });
                logger.error("FactoryException", fe);
            }
            bs = action(name);
            cache.putObject(cacheKey + name, bs);
        }
        return bs;
    }

    private BeanString action(String name) {
        BeanString beanString = new BeanString();//bean?
        beanString.setName(name);//bean?name
        Node node = docroot.selectSingleNode("/beans/bean[@id='" + name + "']/@class"); //?idnameclass??
        beanString.setClassName(node.getText());//bean?className
        Node beanScope = docroot.selectSingleNode("/beans/bean[@id='" + name + "']/@scope"); //?idnamescope??
        if (beanScope != null)
            beanString.setScope(beanScope.getText());//bean?scope

        List ls = docroot.selectNodes("/beans/bean[@id='" + name + "']/property");//???
        for (int i = 0; i < ls.size(); i++) {
            Element element = (Element) ls.get(i);
            String propertyName = element.attributeValue("name");//beanName

            Node propertyBeanNode = docroot.selectSingleNode(
                    "/beans/bean[@id='" + name + "']/property[@name='" + propertyName + "']/ref/@bean");
            if (propertyBeanNode != null) {
                //?? ???beanId
                String[] refs = { null, null };
                refs[0] = propertyName;
                refs[1] = propertyBeanNode.getText();
                beanString.getRefList().add(refs);
            }

            Node propertyValueNode = docroot.selectSingleNode(
                    "/beans/bean[@id='" + name + "']/property[@name='" + propertyName + "']/value");
            if (propertyValueNode != null) {
                //?? ????
                String[] values = { null, null };
                values[0] = propertyName;
                values[1] = propertyValueNode.getText();
                beanString.getValueList().add(values);
            }

        }
        return beanString;
    }

    public void setDocroot(Element docroot) {
        this.docroot = docroot;
    }

    public void setCache(Cache cache) {
        this.cache = cache;
    }

}