com.gzj.tulip.jade.statement.cached.CachedStatement.java Source code

Java tutorial

Introduction

Here is the source code for com.gzj.tulip.jade.statement.cached.CachedStatement.java

Source

/*
 * 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 com.gzj.tulip.jade.statement.cached;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.gzj.tulip.jade.annotation.Cache;
import com.gzj.tulip.jade.annotation.CacheDelete;
import com.gzj.tulip.jade.statement.Statement;
import com.gzj.tulip.jade.annotation.SQLType;
import com.gzj.tulip.jade.statement.StatementMetaData;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanWrapperImpl;

/**
 * {@link CachedStatement} ??Cache
 * 
 * @author  [qieqie.wang@gmail.com]
 * 
 */
public class CachedStatement implements Statement {

    /**
     * Statemenet???
     */
    private final Statement realStatement;

    /**
     * DAO {@link Cache}null
     * <p>
     * ?????DAO {@link Cache} ??null
     */
    private final Cache cacheAnnotation;

    /**
     * ? {@link CacheDelete}null
     * <p>
     * 
     * ?????(???Cache?)
     */
    private final CacheDelete cacheDeleteAnnotation;

    /**
     * cache??
     */
    private final CacheProvider cacheProvider;

    /**
     * 
     * @param cacheProvider
     * @param realStatement
     */
    public CachedStatement(CacheProvider cacheProvider, Statement realStatement) {
        this.realStatement = realStatement;
        this.cacheProvider = cacheProvider;
        StatementMetaData metaData = realStatement.getMetaData();
        SQLType sqlType = metaData.getSQLType();
        cacheDeleteAnnotation = metaData.getMethod().getAnnotation(CacheDelete.class);
        Cache cacheAnnotation = metaData.getMethod().getAnnotation(Cache.class);
        if (sqlType == SQLType.READ) {
            this.cacheAnnotation = cacheAnnotation;
        } else {
            this.cacheAnnotation = null;
            if (cacheAnnotation != null) {
                Log logger = LogFactory.getLog(CachedStatement.class);
                logger.warn("@" + Cache.class.getName() + " is invalid for a " //
                        + sqlType + " SQL:" + metaData.getSQL());
            }
        }
    }

    @Override
    public StatementMetaData getMetaData() {
        return realStatement.getMetaData();
    }

    @Override
    public Object execute(Map<String, Object> parameters) {
        Object value = null;
        if (cacheAnnotation == null) {
            value = realStatement.execute(parameters);
        } else {
            CacheInterface cache = cacheProvider.getCacheByPool(//
                    getMetaData(), cacheAnnotation.pool());
            String cacheKey = buildKey(cacheAnnotation.key(), parameters);
            value = cache.get(cacheKey);
            if (value == null) {
                value = realStatement.execute(parameters);
                cache.set(cacheKey, value, cacheAnnotation.expiry());
            }
        }
        if (cacheDeleteAnnotation != null) {
            CacheInterface cache = cacheProvider.getCacheByPool(//
                    getMetaData(), cacheDeleteAnnotation.pool());
            for (String key : cacheDeleteAnnotation.key()) {
                String cacheKey = buildKey(key, parameters);
                cache.delete(cacheKey);
            }
        }
        return value;
    }

    // ??
    private static final Pattern PATTERN = Pattern.compile("\\:([a-zA-Z0-9_\\.]*)");

    /**
     * ? KEY  :name, :name.property ???
     * 
     * @param key - ? KEY
     * @param parameters - ?
     * 
     * @return  KEY
     * @author  in355hz@gmail.com
     */
    private static String buildKey(String key, Map<String, Object> parameters) {
        // ??  :name ??
        Matcher matcher = PATTERN.matcher(key);
        if (matcher.find()) {

            StringBuilder builder = new StringBuilder();

            int index = 0;

            do {
                // ??????
                final String name = matcher.group(1).trim();

                Object value = null;

                // ?  a.b.c ?? 
                int find = name.indexOf('.');
                if (find >= 0) {

                    //   BeanWrapper ?
                    Object bean = parameters.get(name.substring(0, find));
                    if (bean != null) {
                        value = new BeanWrapperImpl(bean).getPropertyValue(name.substring(find + 1));
                    }

                } else {

                    // ??
                    value = parameters.get(name);
                }

                // ?
                builder.append(key.substring(index, matcher.start()));
                builder.append(value);

                index = matcher.end();

            } while (matcher.find());

            // ?
            builder.append(key.substring(index));

            return builder.toString();
        }

        return key;
    }
}