// Path: com.swookiee.runtime.util/src/main/java/com/swookiee/runtime/util/GuardAgainst.java
// public final class GuardAgainst {
// 
//     private GuardAgainst() {
//     }
// 
//     public static <T> T nullValue(T value) {
//         if (value == null) {
//             throw new IllegalArgumentException();
//         }
//         return value;
//     }
// 
//     public static <T> T nullValue(T value, String message) {
//         if (value == null) {
//             throw new IllegalArgumentException(message);
//         }
//         return value;
//     }
// }


import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.*;
import org.osgi.service.cm.ConfigurationAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.swookiee.runtime.util.GuardAgainst;
/*******************************************************************************
 * Copyright (c) 2014 Lars Pfannenschmidt, Thorsten Krüger and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Lars Pfannenschmidt - initial API and implementation, ongoing development and documentation
 *    Thorsten Krüger - provide better error message on invalid configuration field types
 *******************************************************************************/

package com.swookiee.runtime.util.configuration;




/**
 * This utility class can be used to bridge your configuration information out of your YAML and POJO pair to the
 * ConfigurationAdmin and thereby to the providers of {@link Configuration} implementations.
 * <p>
 * Example:
 * 
 * <pre>
 * YourConfigPojo {
 *   public AdminUserConfiguration adminUserConfiguration;
 * }
 * </pre>
 * 
 * <p>
 * Corresponding YAML file
 * 
 * <pre>
 * adminUserConfiguration:
 *     username: "admin"
 *     password: "admin123"
 * </pre>
 * 
 * 
 */
public final class ConfigurationUtils {

    private static final Logger logger = LoggerFactory.getLogger(ConfigurationUtils.class);
    private final static ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    
    private ConfigurationUtils() {
        //Making sure nobody creates a new instance.
    }

    /**
     * 
     * @param <T> Class of your configuration pojo.
     * @param clazz
     *            Configuration POJO {@link Class}
     * @param configurationFile
     *            URL to your YAML configuration
     * @param configurationAdmin
     *            {@link ConfigurationAdmin} instance
     */
    public static <T> void applyConfiguration(final Class<T> clazz, final URL configurationFile,
            final ConfigurationAdmin configurationAdmin) {
        try {
// Path: com.swookiee.runtime.util/src/main/java/com/swookiee/runtime/util/GuardAgainst.java
// public final class GuardAgainst {
// 
//     private GuardAgainst() {
//     }
// 
//     public static <T> T nullValue(T value) {
//         if (value == null) {
//             throw new IllegalArgumentException();
//         }
//         return value;
//     }
// 
//     public static <T> T nullValue(T value, String message) {
//         if (value == null) {
//             throw new IllegalArgumentException(message);
//         }
//         return value;
//     }
// }


// Path: com.swookiee.runtime.util/src/main/java/com/swookiee/runtime/util/configuration/ConfigurationUtils.java
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.*;
import org.osgi.service.cm.ConfigurationAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.swookiee.runtime.util.GuardAgainst;

/*******************************************************************************
 * Copyright (c) 2014 Lars Pfannenschmidt, Thorsten Krüger and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Lars Pfannenschmidt - initial API and implementation, ongoing development and documentation
 *    Thorsten Krüger - provide better error message on invalid configuration field types
 *******************************************************************************/

package com.swookiee.runtime.util.configuration;




/**
 * This utility class can be used to bridge your configuration information out of your YAML and POJO pair to the
 * ConfigurationAdmin and thereby to the providers of {@link Configuration} implementations.
 * <p>
 * Example:
 * 
 * <pre>
 * YourConfigPojo {
 *   public AdminUserConfiguration adminUserConfiguration;
 * }
 * </pre>
 * 
 * <p>
 * Corresponding YAML file
 * 
 * <pre>
 * adminUserConfiguration:
 *     username: "admin"
 *     password: "admin123"
 * </pre>
 * 
 * 
 */
public final class ConfigurationUtils {

    private static final Logger logger = LoggerFactory.getLogger(ConfigurationUtils.class);
    private final static ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    
    private ConfigurationUtils() {
        //Making sure nobody creates a new instance.
    }

    /**
     * 
     * @param <T> Class of your configuration pojo.
     * @param clazz
     *            Configuration POJO {@link Class}
     * @param configurationFile
     *            URL to your YAML configuration
     * @param configurationAdmin
     *            {@link ConfigurationAdmin} instance
     */
    public static <T> void applyConfiguration(final Class<T> clazz, final URL configurationFile,
            final ConfigurationAdmin configurationAdmin) {
        try {
            GuardAgainst.nullValue(clazz, "Class type can not be null");