com.taobao.tanggong.handler.FileLoader.java Source code

Java tutorial

Introduction

Here is the source code for com.taobao.tanggong.handler.FileLoader.java

Source

/*
 * Copyright 2012 Alibaba.com All right reserved. This software is the
 * confidential and proprietary information of Alibaba.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Alibaba.com.
 */
package com.taobao.tanggong.handler;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import org.apache.commons.io.IOUtils;

import com.taobao.tanggong.LifeCycle;
import com.taobao.tanggong.ServerConfiguration;
import com.taobao.tanggong.TangException;

/**
 * FileLoader.java??TODO ??
 * 
 * @author jingjing.zhijj 2012-12-20 ?3:19:52
 */
abstract public class FileLoader implements LifeCycle {

    private ServerConfiguration serverConfiguration;

    protected File dataDirectory;

    protected String workingDirectory;

    protected String filename = "data";

    public ServerConfiguration getServerConfiguration() {
        return serverConfiguration;
    }

    public void setServerConfiguration(ServerConfiguration serverConfiguration) {
        this.serverConfiguration = serverConfiguration;
    }

    public File getDataDirectory() {
        return dataDirectory;
    }

    public void setDataDirectory(File dataDirectory) {
        this.dataDirectory = dataDirectory;
    }

    public String getWorkingDirectory() {
        return workingDirectory;
    }

    public void setWorkingDirectory(String workingDirectory) {
        this.workingDirectory = workingDirectory;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public void init() throws TangException {

        this.dataDirectory = new File(this.serverConfiguration.getBaseDirectory(), this.getWorkingDirectory());
        if (!dataDirectory.exists()) {
            //failed to create directory or not exist
            if (!this.dataDirectory.mkdirs() && !this.dataDirectory.exists()) {
                this.dataDirectory = null;
            }
        }

        File dataFile = new File(this.dataDirectory, this.getFilename());
        if (dataFile.exists()) {

            try {
                List<String> lines = IOUtils.readLines(new FileInputStream(dataFile));
                initLines(lines);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }

    /**
     * @param dataFile
     */
    abstract protected void initLines(List<String> lines);

    protected void save(List<String> lines) {
        if (null != lines && null != this.dataDirectory) {
            File masterFile = new File(this.dataDirectory, this.getFilename());

            File backupFile = new File(this.dataDirectory, this.getFilename() + ".bak");

            if (masterFile.exists()) {
                masterFile.renameTo(backupFile);
            }

            OutputStream output = null;
            try {
                output = new FileOutputStream(masterFile);

                StringBuilder sb = new StringBuilder();
                for (String k : lines) {

                    sb.append(k);
                    sb.append("\n");
                }

                IOUtils.write(sb.toString(), output);

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                IOUtils.closeQuietly(output);
            }
        }
    }

    /**
     * @return
     */

    public void destory() throws TangException {
        // TODO Auto-generated method stub

    }

}