com.taobao.itest.tb.tfs.mock.TfsManagerXmlStoreImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.taobao.itest.tb.tfs.mock.TfsManagerXmlStoreImpl.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.tfs.mock;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import com.taobao.common.tfs.TfsManager;

/**
 * TFSXML Store?
 * 
 * @author leijuan
 */
public class TfsManagerXmlStoreImpl implements TfsManager {
    /**
     * store
     */
    private Map<String, TfsFile> tfsStore = new HashMap<String, TfsFile>();

    /**
     * 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> fileElements = doc.getRootElement().elements("file");
            for (Element fileElement : fileElements) {
                TfsFile tfsFile = new TfsFile();
                tfsFile.setKey(fileElement.attributeValue("key"));
                tfsFile.setHref(fileElement.attributeValue("href"));
                tfsFile.setSuffix(fileElement.attributeValue("suffix"));
                tfsFile.setDesc(fileElement.attributeValue("desc"));
                if (StringUtils.isNotEmpty(tfsFile.getHref())) {
                    tfsFile.setRawContent(
                            IOUtils.toByteArray(this.getClass().getResourceAsStream(tfsFile.getHref())));
                } else {
                    tfsFile.setRawContent(fileElement.getTextTrim().getBytes());
                }
                tfsStore.put(tfsFile.getKey(), tfsFile);
            }
        }

    }

    /**
     * ??
     * 
     * @return ?
     */
    public boolean isEnable() {
        return true;
    }

    /**
     * ?master ip?
     */
    public int setMasterIP(String ipaddr) {
        return 0;
    }

    /**
     * ?IP
     * 
     * @return master ip
     */
    public String getMasterIP() {
        return "127.0.0.1";
    }

    /**
     * ?tfs??tfs?prefix???prefix???
     * 
     * @param suffix
     *            ???
     * @return tfs file name
     */
    public String newTfsFileName(String suffix) {
        return "MOCK-" + RandomStringUtils.random(8);
    }

    /**
     * ?tfs
     * 
     * @param tfsFileName
     *            tfs file name
     * @param suffix
     *            suffix
     * @param localFileName
     *            local file name
     * @return ?
     */
    public boolean fetchFile(String tfsFileName, String suffix, String localFileName) {
        if (tfsStore.containsKey(tfsFileName) && tfsStore.get(tfsFileName).isSuffixSame(suffix)) {
            try {
                FileOutputStream fileInputOutput = new FileOutputStream(new File(localFileName));
                IOUtils.write(tfsStore.get(tfsFileName).getRawContent(), fileInputOutput);
                IOUtils.closeQuietly(fileInputOutput);
                return true;
            } catch (Exception ignore) {
                return false;
            }

        }
        return false;
    }

    /**
     * ?
     * 
     * @param localFileName
     *            local file name
     * @param tfsFileName
     *            tfs file name
     * @param suffix
     *            ???
     * @return ??
     */
    public String saveFile(String localFileName, String tfsFileName, String suffix) {
        try {
            return saveFile(IOUtils.toByteArray(new FileInputStream(localFileName)), tfsFileName, suffix);
        } catch (Exception ignore) {
            return null;
        }
    }

    /**
     * ?byte[]TFS
     * 
     * @param data
     *            ??
     * @param tfsFileName
     *            tfs??
     * @param suffix
     *            ?
     * @return ??TFS??
     */
    public String saveFile(byte[] data, String tfsFileName, String suffix) {
        TfsFile tfsFile = new TfsFile();
        tfsFile.setKey(tfsFileName);
        tfsFile.setSuffix(suffix);
        tfsFile.setRawContent(data);
        tfsStore.put(tfsFile.getKey(), tfsFile);
        return tfsFile.getKey();
    }

    /**
     * 
     * 
     * @param tfsFileName
     *            tfs file name
     * @param suffix
     *            ???
     * @return 
     */
    public boolean unlinkFile(String tfsFileName, String suffix) {
        tfsStore.remove(tfsFileName);
        return true;
    }

    /**
     * ?tfsoutput
     * 
     * @param tfsFileName
     *            tfs file name
     * @param suffix
     *            ???
     * @param output
     *            output stream
     * @return ??
     */
    public boolean fetchFile(String tfsFileName, String suffix, OutputStream output) {
        if (tfsStore.containsKey(tfsFileName) && tfsStore.get(tfsFileName).isSuffixSame(suffix)) {
            try {
                output.write(tfsStore.get(tfsFileName).getRawContent());
                return true;
            } catch (Exception ignore) {
                return false;
            }
        }
        return false;
    }

    /**
     * ????
     * 
     * @param localFileName
     *            local file name
     * @param tfsFileName
     *            tfs file name
     * @param suffix
     *            ???
     * @return file name
     */
    public String saveUniqueFile(String localFileName, String tfsFileName, String suffix) {
        File localFile = new File(localFileName);
        if (!localFile.exists()) {
            return saveFile(localFileName, tfsFileName, suffix);
        }
        return null;
    }

    /**
     * ????(byte[])TFS
     * 
     * @param data
     *            ??
     * @param tfsFileName
     *            tfs??
     * @param suffix
     *            ?
     * @return ??TFS??
     */
    public String saveUniqueFile(byte[] data, String tfsFileName, String suffix) {
        try {
            File tempFile = File.createTempFile(RandomStringUtils.random(6), suffix);
            FileOutputStream outputStream = new FileOutputStream(tempFile);
            fetchFile(tfsFileName, suffix, outputStream);
            IOUtils.closeQuietly(outputStream);
            return tempFile.getAbsolutePath();
        } catch (Exception ignore) {
            return null;
        }
    }

    /**
     * ???
     * 
     * @param tfsFileName
     *            tfs file name
     * @param suffix
     *            ???
     * @return ???
     */
    public int unlinkUniqueFile(String tfsFileName, String suffix) {
        tfsStore.remove(tfsFileName);
        return 0;
    }

    /**
     * ??
     * 
     * @param fileName
     *            file
     * @param suffix
     *            suffix
     * @param option
     *            1 ?? 0 ??
     * @return ?
     */
    public boolean hideFile(String fileName, String suffix, int option) {
        // 
        return true;
    }

    /**
     * TFS file object
     * 
     * @author leijuan
     */
    private class TfsFile {
        /**
         * tfs key
         */
        private String key;
        /**
         * TFS ???
         */
        private String suffix;
        /**
         * 
         */
        private String href;
        /**
         * ??
         */
        private String desc;
        /**
         * 
         */
        private byte[] rawContent;

        /**
         * ? tfs key
         * 
         * @return tfs key
         */
        public String getKey() {
            return key;
        }

        /**
         *  tfs key
         * 
         * @param key
         *            tfs key
         */
        public void setKey(String key) {
            this.key = key;
        }

        /**
         * ? TFS ???
         * 
         * @return TFS ???
         */
        @SuppressWarnings("unused")
        public String getSuffix() {
            return suffix;
        }

        /**
         *  TFS ???
         * 
         * @param suffix
         *            TFS ???
         */
        public void setSuffix(String suffix) {
            this.suffix = suffix;
        }

        public String getHref() {
            return href;
        }

        public void setHref(String href) {
            this.href = href;
        }

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }

        /**
         * ? 
         * 
         * @return 
         */
        public byte[] getRawContent() {
            return rawContent;
        }

        /**
         *  
         * 
         * @param rawContent
         *            
         */
        public void setRawContent(byte[] rawContent) {
            this.rawContent = rawContent;
        }

        /**
         * ????
         * 
         * @param suffix
         *            ???
         * @return 
         */
        public boolean isSuffixSame(String suffix) {
            return suffix != null ? suffix.equals(this.suffix) : this.suffix == null;
        }
    }

}