com.baifendian.swordfish.common.hadoop.ConfigurationUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.baifendian.swordfish.common.hadoop.ConfigurationUtil.java

Source

/*
 * Copyright (C) 2017 Baifendian Corporation
 *
 * 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.baifendian.swordfish.common.hadoop;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.Properties;
import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ResourceUtils;

/**
 * ?? <p>
 */
public class ConfigurationUtil {

    /**
     * logger
     */
    private static final Logger logger = LoggerFactory.getLogger(ConfigurationUtil.class);

    /**
     * {@link Configuration}
     */
    private static volatile Configuration configuration;

    /**
     * {@link Properties}
     */
    private static final Properties PROPERTIES = new Properties();

    private static final Properties STORM_PROPERTIES = new Properties();

    static {
        InputStream is = null;
        try {
            File dataSourceFile = ResourceUtils.getFile("classpath:common/hadoop/hadoop.properties");
            is = new FileInputStream(dataSourceFile);
            PROPERTIES.load(is);

            File stormDataSourceFile = ResourceUtils.getFile("classpath:common/storm.properties");
            is = new FileInputStream(stormDataSourceFile);
            STORM_PROPERTIES.load(is);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        } finally {
            IOUtils.closeQuietly(is);
        }
    }

    /**
     * ? Configuration <p>
     *
     * @return {@link Configuration}
     */
    public static Configuration getConfiguration() {
        init();
        return configuration;
    }

    /**
     * ? web app ?
     */
    public static String getWebappAddress(String appId) {
        init();
        return String.format(configuration.get("yarn.resourcemanager.webapp.address"), appId);
    }

    /**
     * ? Storm web app ?
     */
    public static String getStormAppAddress(String appId) {
        init();
        return MessageFormat.format("{0}/{1}{2}", configuration.get("storm.rest.url"),
                configuration.get("storm.rest.topology"), appId);
    }

    /**
     * ? appid ? url
     */
    public static String getApplicationStatusAddress(String appId) {
        init();
        return String.format(configuration.get("yarn.application.status.address"), appId);
    }

    /**
     * ??
     */
    private static void init() {
        if (configuration == null) {
            synchronized (ConfigurationUtil.class) {
                if (configuration == null) {
                    configuration = new Configuration();
                    initConfiguration();
                }
            }
        }
    }

    /**
     * ?? <p>
     */
    private static void initConfiguration() {
        configuration.setBoolean("mapreduce.app-submission.cross-platform",
                Boolean.parseBoolean(PROPERTIES.getProperty("mapreduce.app-submission.cross-platform")));
        configuration.set("fs.defaultFS", PROPERTIES.getProperty("fs.defaultFS"));
        configuration.set("mapreduce.framework.name", PROPERTIES.getProperty("mapreduce.framework.name"));
        configuration.set("yarn.resourcemanager.address", PROPERTIES.getProperty("yarn.resourcemanager.address"));
        configuration.set("yarn.resourcemanager.scheduler.address",
                PROPERTIES.getProperty("yarn.resourcemanager.scheduler.address"));
        configuration.set("mapreduce.jobhistory.address", PROPERTIES.getProperty("mapreduce.jobhistory.address"));
        configuration.set("yarn.resourcemanager.webapp.address",
                PROPERTIES.getProperty("yarn.resourcemanager.webapp.address"));
        configuration.set("yarn.application.status.address",
                PROPERTIES.getProperty("yarn.application.status.address"));

        configuration.set("storm.rest.url", STORM_PROPERTIES.getProperty("storm.rest.url"));
        configuration.set("storm.rest.topology", STORM_PROPERTIES.getProperty("storm.rest.topology"));
    }
}