org.shaf.core.util.ConfigUtils.java Source code

Java tutorial

Introduction

Here is the source code for org.shaf.core.util.ConfigUtils.java

Source

/**
 * Copyright 2014-2015 SHAF-WORK
 * 
 * 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 org.shaf.core.util;

import java.util.Iterator;
import java.util.Map.Entry;
import java.util.regex.Pattern;

import org.apache.hadoop.conf.Configuration;
import org.shaf.core.process.config.ProcessConfiguration;

/**
 * The configurations utilities.
 * 
 * @author Mykola Galushka
 */
public class ConfigUtils {

    /**
     * Defines a logger.
     */
    private static final Log LOG = Log.forClass(ConfigUtils.class);

    /**
     * Exports properties with the specified pattern from
     * {@link ProcessConfiguration process configuration} object to the
     * {@link Configuration Hadoop configuration} object.
     * 
     * @param source
     *            the process configuration object.
     * @param target
     *            the Hadoop configuration object.
     * @param pattern
     *            the pattern for selecting properties.
     */
    public final static void export(final ProcessConfiguration source, final Configuration target,
            final String pattern) {
        Iterator<String> keys = source.getKeys();
        while (keys.hasNext()) {
            String key = keys.next();
            if (Pattern.matches(pattern, key)) {
                String value = source.getString(key);
                target.set(key, value);

                LOG.trace("Exports from Process to Hadoop configuration property: " + key + "=" + value);
            }
        }
    }

    /**
     * Exports all properties from {@link ProcessConfiguration process
     * configuration} object to the {@link Configuration Hadoop configuration}
     * object.
     * 
     * @param source
     *            the process configuration object.
     * @param target
     *            the Hadoop configuration object.
     */
    public final static void export(final ProcessConfiguration source, final Configuration target) {
        export(source, target, ".*");
    }

    /**
     * Exports properties with the specified pattern from {@link Configuration
     * Hadoop configuration} object to the {@link ProcessConfiguration process
     * configuration} object.
     * 
     * @param source
     *            the Hadoop configuration object.
     * @param target
     *            the process configuration object.
     * @param pattern
     *            the pattern for selecting properties.
     */
    public final static void export(final Configuration source, final ProcessConfiguration target,
            final String pattern) {
        Iterator<Entry<String, String>> entries = source.iterator();
        while (entries.hasNext()) {
            Entry<String, String> entry = entries.next();
            if (Pattern.matches(pattern, entry.getKey())) {
                target.addProperty(entry.getKey(), entry.getValue());

                LOG.trace("Exports from Process to Hadoop configuration property: " + entry.getKey() + "="
                        + entry.getValue());
            }
        }
    }

    /**
     * Exports all properties from {@link Configuration Hadoop configuration}
     * object to the {@link ProcessConfiguration process configuration} object.
     * 
     * @param source
     *            the Hadoop configuration object.
     * @param target
     *            the process configuration object.
     */
    public final static void export(final Configuration source, final ProcessConfiguration target) {
        export(source, target, ".*");
    }
}