com.asakusafw.bulkloader.common.BulkLoaderInitializer.java Source code

Java tutorial

Introduction

Here is the source code for com.asakusafw.bulkloader.common.BulkLoaderInitializer.java

Source

/**
 * Copyright 2011-2016 Asakusa Framework Team.
 *
 * 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.asakusafw.bulkloader.common;

import java.io.File;
import java.util.List;

import org.apache.hadoop.io.nativeio.NativeIO;

import com.asakusafw.bulkloader.exception.BulkLoaderSystemException;
import com.asakusafw.bulkloader.log.Log;
import com.asakusafw.bulkloader.log.LogInitializer;

/**
 * ?????
 */
public final class BulkLoaderInitializer {

    static final Log LOG = new Log(BulkLoaderInitializer.class);

    private BulkLoaderInitializer() {
        return;
    }

    /**
     * DB?????
     * ???????
     * DB?????
     * @param jobflowId ID
     * @param executionId ID
     * @param properties ??(?$ASAKUSA_HOME/bulkloader/conf/?????)
     * @param targetName ??
     * @return ??
     */
    public static boolean initDBServer(String jobflowId, String executionId, List<String> properties,
            String targetName) {
        return initialize(jobflowId, executionId, properties, true, false, true, targetName);
    }

    /**
     * Hadoop Cluster????
     * ???????
     * HadoopCluster????
     * @param jobflowId ID
     * @param executionId ID
     * @param properties ??(?$ASAKUSA_HOME/bulkloader/conf/?????)
     * @return ??
     */
    public static boolean initHadoopCluster(String jobflowId, String executionId, List<String> properties) {
        // Eagerly initializes Hadoop native libraries
        NativeIO.isAvailable();
        return initialize(jobflowId, executionId, properties, false, true, false, null);
    }

    /**
     * ???
     * @param jobflowId ID
     * @param executionId ID
     * @param properties ?
     * @param doDBPropCheck DB?????
     * @param doHCPropCheck ????
     * @param doDbConnInit DB????
     * @param targetName ??DB???????
     * @return ???????{@code true}?????{@code false}
     */
    private static boolean initialize(String jobflowId, String executionId, List<String> properties,
            boolean doDBPropCheck, boolean doHCPropCheck, boolean doDbConnInit, String targetName) {

        try {
            // ???
            ConfigurationLoader.init(properties, doDBPropCheck, doHCPropCheck);
            // ???
            Constants.setSystemColumn();
        } catch (BulkLoaderSystemException e) {
            // ????????
            if (initLog(jobflowId, executionId, targetName)) {
                LOG.log(e);
                return false;
            } else {
                printPropLoadError(jobflowId, executionId, targetName, properties, e);
                return false;
            }
        } catch (Exception e) {
            printPropLoadError(jobflowId, executionId, targetName, properties, e);
            return false;
        }

        // ??
        if (!initLog(jobflowId, executionId, targetName)) {
            return false;
        }

        // DBConnection??
        if (doDbConnInit) {
            try {
                // JDBC?
                ConfigurationLoader.loadJDBCProp(targetName);
                // DBConnection?
                DBConnection.init(ConfigurationLoader.getProperty(Constants.PROP_KEY_JDBC_DRIVER));
            } catch (BulkLoaderSystemException e) {
                LOG.log(e);
                return false;
            }
        }

        return true;
    }

    /**
     * ???
     * @param jobflowId ID
     * @param executionId ID
     * @param targetName ??
     * @return ???????{@code true}?????{@code false}
     */
    private static boolean initLog(String jobflowId, String executionId, String targetName) {
        // ??
        String logConfFilePath = null;
        try {
            logConfFilePath = ConfigurationLoader.getProperty(Constants.PROP_KEY_LOG_CONF_PATH);
            if (new File(logConfFilePath).exists() == false) {
                String home = ConfigurationLoader.getEnvProperty(Constants.ASAKUSA_HOME);
                if (home != null) {
                    File file = new File(new File(home), logConfFilePath);
                    if (file.exists()) {
                        logConfFilePath = file.getPath();
                    }
                }
            }
            LogInitializer.execute(logConfFilePath);
            return true;
        } catch (Exception e) {
            // TODO MessageFormat.format?
            System.err.println("ERROR??????? [" + e.getMessage()
                    + "] ID" + jobflowId + " ID" + executionId
                    + "??" + targetName);
            System.err.println("" + logConfFilePath);
            e.printStackTrace();
            return false;
        }
    }

    /**
     * ??????????
     * @param jobflowId ID
     * @param executionId ID
     * @param targetName ??
     * @param properties ?
     * @param e ??
     */
    private static void printPropLoadError(String jobflowId, String executionId, String targetName,
            List<String> properties, Exception e) {
        // TODO MessageFormat.format?
        System.err.println("ERROR??????? ["
                + e.getMessage() + "] ID" + jobflowId + " ID"
                + executionId + "??" + targetName);
        System.err.println("" + System.getenv());
        if (properties == null) {
            System.err.println("null");
        } else {
            for (int i = 0; i < properties.size(); i++) {
                System.err.println("[" + i + "]" + properties.get(i));
            }
        }
        e.printStackTrace();
    }
}