All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.arpnetworking.play.configuration.ConfigurationHelper Maven / Gradle / Ivy

There is a newer version: 0.9.2
Show newest version
/**
 * Copyright 2014 Groupon.com
 *
 * 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.arpnetworking.play.configuration;

import com.arpnetworking.steno.Logger;
import com.arpnetworking.steno.LoggerFactory;
import com.google.common.base.Throwables;
import play.Application;
import play.Configuration;
import play.Environment;
import scala.concurrent.duration.Duration;
import scala.concurrent.duration.FiniteDuration;

import java.io.File;

/**
 * Utility methods that provide common patterns when interacting with Play's Configuration class.
 *
 * @author Ville Koskela (vkoskela at groupon dot com)
 */
public final class ConfigurationHelper {

    /**
     * Return the value of a configuration key as a File instance.
     *
     * @param configuration Play Configuration instance.
     * @param key The name of the configuration key to interpret as a File reference.
     * @param app Instance of Play Application.
     * @return Instance of File as defined by key in configuration.
     */
    public static File getFile(final Configuration configuration, final String key, final Application app) {
        final String pathAsString = configuration.getString(key);
        if (!pathAsString.startsWith("/")) {
            return app.getFile(pathAsString);
        }
        return new File(pathAsString);
    }

    /**
     * Return the value of a configuration key as a FiniteDuration instance.
     *
     * @param configuration Play Configuration instance.
     * @param key The name of the configuration key to interpret as a FiniteDuration reference.
     * @return Instance of FiniteDuration as defined by key in configuration.
     */
    public static FiniteDuration getFiniteDuration(final Configuration configuration, final String key) {
        final Duration duration = Duration.create(configuration.getString(key));
        return new FiniteDuration(
                duration.length(),
                duration.unit());
    }

    /**
     * Return the value of a configuration key as a Class instance.
     *
     * @param environment Play Environment instance.
     * @param configuration Play Configuration instance.
     * @param key The name of the configuration key to interpret as a Class reference.
     * @param  The type parameter for the Class instance to return.
     * @return Instance of Class as defined by key in configuration.
     */
    public static  Class getType(
            final Environment environment,
            final Configuration configuration,
            final String key) {
        final String className = configuration.getString(key);
        try {
            @SuppressWarnings("unchecked")
            final Class clazz = (Class) environment.classLoader().loadClass(className);
            LOGGER.debug()
                    .setMessage("Loaded class")
                    .addData("classLoader", environment.classLoader())
                    .addData("className", className)
                    .log();
            return clazz;
        } catch (final ClassNotFoundException e) {
            throw Throwables.propagate(e);
        }
    }

    private ConfigurationHelper() {}

    private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationHelper.class);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy