All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.paoding.rose.jade.context.spring.JadeFactoryBean Maven / Gradle / Ivy

/*
 * Copyright 2009-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License i distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.paoding.rose.jade.context.spring;

import net.paoding.rose.jade.context.JadeInvocationHandler;
import net.paoding.rose.jade.dataaccess.DataAccessFactory;
import net.paoding.rose.jade.rowmapper.RowMapperFactory;
import net.paoding.rose.jade.statement.DAOMetaData;
import net.paoding.rose.jade.statement.interpreter.factory.InterpreterFactory;
import net.paoding.rose.jade.statement.StatementWrapperProvider;
import net.paoding.rose.jade.statement.cached.CacheProvider;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

import java.lang.reflect.Proxy;
import java.util.Objects;

/**
 * @author 王志亮 [[email protected]]
 * @author 廖涵 [[email protected]]
 */
@SuppressWarnings("rawtypes")
public class JadeFactoryBean implements FactoryBean, InitializingBean {

    protected Class objectType;

    protected DataAccessFactory dataAccessFactory;

    protected RowMapperFactory rowMapperFactory;

    protected InterpreterFactory interpreterFactory;

    protected CacheProvider cacheProvider;

    protected Object daoObject;

    private ClassLoader classLoader;

    private BeanFactory beanFactory;

    // 可选的
    private StatementWrapperProvider statementWrapperProvider;

    public JadeFactoryBean() {
    }


    @Override
    public Class getObjectType() {
        return objectType;
    }

    public void setObjectType(Class objectType) {
        this.objectType = objectType;
    }

    /**
     * @param dataAccessFactory
     */
    public void setDataAccessFactory(DataAccessFactory dataAccessFactory) {
        this.dataAccessFactory = dataAccessFactory;
    }

    public DataAccessFactory getDataAccessFactory() {
        return dataAccessFactory;
    }

    /**
     * @param rowMapperFactory
     */
    public void setRowMapperFactory(RowMapperFactory rowMapperFactory) {
        this.rowMapperFactory = rowMapperFactory;
    }

    public RowMapperFactory getRowMapperFactory() {
        return rowMapperFactory;
    }

    public ClassLoader getClassLoader() {
        return this.classLoader;
    }

    public void setClassLoader(ClassLoader classLoader) {
        this.classLoader = classLoader;
    }

    /**
     * @param interpreterFactory
     */
    public void setInterpreterFactory(InterpreterFactory interpreterFactory) {
        this.interpreterFactory = interpreterFactory;
    }

    public InterpreterFactory getInterpreterFactory() {
        return interpreterFactory;
    }

    public void setCacheProvider(CacheProvider cacheProvider) {
        this.cacheProvider = cacheProvider;
    }

    public CacheProvider getCacheProvider() {
        return cacheProvider;
    }

    public StatementWrapperProvider getStatementWrapperProvider() {
        return statementWrapperProvider;
    }

    public void setStatementWrapperProvider(StatementWrapperProvider statementWrapperProvider) {
        this.statementWrapperProvider = statementWrapperProvider;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Assert.isTrue(objectType.isInterface(), "not a interface class: " + objectType.getName());

        Assert.notNull(dataAccessFactory, "data access factory is null");
        Assert.notNull(rowMapperFactory, "row mapper factory is null");
        Assert.notNull(interpreterFactory, "interpreter factory is null");
        // cacheProvider可以null,不做assert.notNull判断
    }

    @Override
    public Object getObject() {
        if (daoObject == null) {
            daoObject = createDAO();
            Assert.notNull(daoObject, "dao object is null");
        }
        return daoObject;
    }

    protected Object createDAO() {
        try {
            DAOMetaData daoMetaData = new DAOMetaData(objectType);
            JadeInvocationHandler handler = new JadeInvocationHandler(classLoader, daoMetaData, interpreterFactory,
                    rowMapperFactory, dataAccessFactory, cacheProvider, statementWrapperProvider, beanFactory);
            ClassLoader classLoader = Objects.nonNull(this.classLoader) ? this.classLoader : ClassUtils.getDefaultClassLoader();
            return Proxy.newProxyInstance(classLoader, new Class[]{objectType}, handler);
        } catch (RuntimeException e) {
            throw new IllegalStateException("failed to create bean for "
                    + this.objectType.getName(), e);
        }
    }

    public BeanFactory getBeanFactory() {
        return beanFactory;
    }

    public void setBeanFactory(BeanFactory beanFactory) {
        this.beanFactory = beanFactory;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy