com.taobao.itest.tb.tair.mock.TairManagerXmlStoreImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.taobao.itest.tb.tair.mock.TairManagerXmlStoreImpl.java

Source

/*
 * Copyright 2011 Alibaba Group Holding Limited.
 * All rights reserved.
 *
 * 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 is 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.taobao.itest.tb.tair.mock;

import groovy.lang.Binding;
import groovy.lang.GroovyShell;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.taobao.common.tair.DataEntry;
import com.taobao.common.tair.Result;
import com.taobao.common.tair.ResultCode;
import com.taobao.common.tair.TairManager;

/**
 * tair managerxml store ??Tair
 * 
 * @author leijuan
 */
public class TairManagerXmlStoreImpl implements TairManager {
    /**
     * tair store
     */
    private Map<Integer, Map<Object, Object>> tairStore = new HashMap<Integer, Map<Object, Object>>();

    /**
     * xml?classpath
     * 
     * @param files
     *            
     * @throws Exception
     *             exception
     */
    @SuppressWarnings({ "unchecked" })
    public void setXmlStoreFiles(String files) throws Exception {
        SAXReader reader = new SAXReader();
        for (String filePath : files.split("[,:]")) {
            Document doc = reader.read(this.getClass().getResourceAsStream(filePath.trim()));
            List<Element> entryElements = doc.getRootElement().elements("entry");
            for (Element entryElement : entryElements) {
                int namespace = Integer.valueOf(entryElement.attributeValue("namespace"));
                String key = entryElement.attributeValue("key");
                String content = entryElement.getTextTrim();
                if (entryElement.attributeValue("href") != null) {
                    content = IOUtils
                            .toString(this.getClass().getResourceAsStream(entryElement.attributeValue("href")));
                }
                String language = entryElement.attributeValue("language");
                if ("groovy".equalsIgnoreCase(language)) {
                    getNameSpaceStore(namespace).put(key, evalGroovy(content));
                } else {
                    getNameSpaceStore(namespace).put(key, content);
                }
            }
        }

    }

    /**
     * get name space store
     * 
     * @param namespace
     *            name space id
     * @return name space store
     */
    private Map<Object, Object> getNameSpaceStore(int namespace) {
        Map<Object, Object> namepaceStore = tairStore.get(namespace);
        if (namepaceStore == null) {
            namepaceStore = new HashMap<Object, Object>();
            tairStore.put(namespace, namepaceStore);
        }
        return namepaceStore;
    }

    /**
     * ??
     * 
     * @param namespace
     *            ?namespace
     * @param key
     *            ???key
     * @return result data entry
     */
    public Result<DataEntry> get(int namespace, Object key) {
        if (getNameSpaceStore(namespace).containsKey(key)) {
            return new Result<DataEntry>(ResultCode.SUCCESS,
                    new DataEntry(key, getNameSpaceStore(namespace).get(key)));
        } else {
            return new Result<DataEntry>(ResultCode.DATANOTEXSITS);
        }
    }

    /**
     * ???
     * 
     * @param namespace
     *            ?namespace
     * @param keys
     *            ???key
     * @return ??Map<Key, Value>
     */
    public Result<List<DataEntry>> mget(int namespace, List<Object> keys) {
        List<DataEntry> entries = new ArrayList<DataEntry>();
        for (Object key : keys) {
            entries.add(get(namespace, key).getValue());
        }
        return new Result<List<DataEntry>>(ResultCode.SUCCESS, entries);
    }

    /**
     * ???? 0?? ?
     * 
     * @param namespace
     *            ?namespace
     * @param key
     *            key
     * @param value
     *            value
     * @return result code
     */
    public ResultCode put(int namespace, Object key, Serializable value) {
        getNameSpaceStore(namespace).put(key, value);
        return ResultCode.SUCCESS;
    }

    /**
     * ????
     * 
     * @param namespace
     *            ?namespace
     * @param key
     *            ?key
     * @param value
     *            ?value
     * @param version
     *            ???
     * @return result code
     */
    public ResultCode put(int namespace, Object key, Serializable value, int version) {
        return put(namespace, key, value);
    }

    /**
     * ????
     * 
     * @param namespace
     *            ?namespace
     * @param key
     *            ?key
     * @param value
     *            ?value
     * @param version
     *            ???
     * @param expireTime
     *            ???
     * @return result code
     */
    public ResultCode put(int namespace, Object key, Serializable value, int version, int expireTime) {
        return put(namespace, key, value, version);
    }

    /**
     * key?
     * 
     * @param namespace
     *            ?namespace
     * @param key
     *            ?key
     * @return result code
     */
    public ResultCode delete(int namespace, Object key) {
        getNameSpaceStore(namespace).remove(key);
        return ResultCode.SUCCESS;
    }

    /**
     * ????group?
     * 
     * @param namespace
     *            ?namespace
     * @param key
     *            ?key
     * @return result code
     */
    public ResultCode invalid(int namespace, Object key) {
        return delete(namespace, key);
    }

    /**
     * ?????group?
     * 
     * @param namespace
     *            ?namespace
     * @param keys
     *            ?key
     * @return result code
     */
    public ResultCode minvalid(int namespace, List<? extends Object> keys) {
        for (Object key : keys) {
            invalid(namespace, key);
        }
        return ResultCode.SUCCESS;
    }

    /**
     * ????
     * 
     * @param namespace
     *            ?namespace
     * @param keys
     *            ??key
     * @return result code
     */
    public ResultCode mdelete(int namespace, List<Object> keys) {
        for (Object key : keys) {
            delete(namespace, key);
        }
        return ResultCode.SUCCESS;
    }

    /**
     * key?valuekey??defaultValue
     * key??int
     * 
     * @param namespace
     *            ?namspace
     * @param key
     *            ?key
     * @param value
     *            ?
     * @param defaultValue
     *            ?
     * @return ?
     */
    public Result<Integer> incr(int namespace, Object key, int value, int defaultValue) {
        int newValue = defaultValue;
        Map<Object, Object> nameSpaceStore = getNameSpaceStore(namespace);
        if (nameSpaceStore.get(key) == null) {
            nameSpaceStore.put(key, newValue);
        } else {
            Integer temp = (Integer) nameSpaceStore.get(key);
            newValue = temp + value;
            nameSpaceStore.put(key, newValue);
        }
        return new Result<Integer>(ResultCode.SUCCESS, newValue);
    }

    /**
     * key??valuekey??defaultValue
     * key??int
     * 
     * @param namespace
     *            ?namspace
     * @param key
     *            ?key
     * @param value
     *            ??
     * @param defaultValue
     *            ?
     * @return ?
     */
    public Result<Integer> decr(int namespace, Object key, int value, int defaultValue) {
        return incr(namespace, key, (0 - value), defaultValue);
    }

    /**
     * ?
     */
    public String getVersion() {
        return "1.0.0-Mock";
    }

    /**
     * Groovy?
     * 
     * @param groovyCode
     *            groovy?
     * @return 
     */
    private Object evalGroovy(String groovyCode) {
        Binding binding = new Binding();
        GroovyShell shell = new GroovyShell(binding);
        return shell.evaluate(groovyCode);
    }
}