com.athena.chameleon.engine.threadpool.task.BaseTask.java Source code

Java tutorial

Introduction

Here is the source code for com.athena.chameleon.engine.threadpool.task.BaseTask.java

Source

/*
 * Copyright 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 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.
 *
 * Revision History
 * Author         Date            Description
 * ---------------   ----------------   ------------
 * Sang-cheon Park   2012. 9. 11.      First Draft.
 */
package com.athena.chameleon.engine.threadpool.task;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.athena.chameleon.engine.threadpool.handler.ChameleonTaskExceptionHandler;

/**
 * <pre>
 * Encoding  task, XML Parsing task ?   task? Runnable interface ? .
 * BaseTask ?? taskRun()   .
 * </pre>
 * 
 * @author Sang-cheon Park
 * @version 1.0
 */
public abstract class BaseTask implements Runnable {

    protected static final Logger logger = LoggerFactory.getLogger(BaseTask.class);
    protected String taskName;

    /**
     * @return the taskName
     */
    public String getTaskName() {
        return taskName == null ? super.toString() : taskName;
    }

    /**
     * @param taskName the taskName to set
     */
    public void setTaskName(String taskName) {
        this.taskName = taskName;
    }

    /**
     * <pre>
     * Constructor
     * </pre>
     * @param taskName
     */
    public BaseTask() {
    }//end of Constructor()

    /**
     * <pre>
     * Constructor
     * </pre>
     * @param taskName
     */
    public BaseTask(String taskName) {
        this.taskName = taskName;
    }//end of Constructor()

    /**
      * <pre>
      * 
      * </pre>
      * @see java.lang.Runnable#run()
      */
    @Override
    public void run() {
        // Thread   UncaughtException?  catch   handler ?.
        Thread.currentThread().setUncaughtExceptionHandler(new ChameleonTaskExceptionHandler());

        logger.debug("[{}] is started.", getTaskName());
        // TODO  ?  beforeRun()    

        taskRun();

        // TODO  ?  afterRun()    
        logger.debug("[{}] is completed.", getTaskName());
    }

    /**
     * <pre>
     * ?? ? ?  .
     * </pre>
     * @param file
     * @return
     */
    protected String fileToString(String file) {
        String result = null;

        try {
            DataInputStream in = null;
            File f = new File(file);
            byte[] buffer = new byte[(int) f.length()];
            in = new DataInputStream(new FileInputStream(f));
            in.readFully(buffer);
            result = new String(buffer);
            IOUtils.closeQuietly(in);
        } catch (IOException e) {
            throw new RuntimeException("IO problem in fileToString", e);
        }

        return result;
    }//end of fileToString()

    @Override
    public String toString() {
        return getTaskName();
    }

    protected abstract void taskRun();

}//end of BaseTask.java