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

org.openstack4j.openstack.heat.utils.Environment Maven / Gradle / Ivy

There is a newer version: 3.2.0
Show newest version
package org.openstack4j.openstack.heat.utils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.dataformat.yaml.snakeyaml.Yaml;
import com.google.common.base.Charsets;
import com.google.common.io.Resources;

public class Environment {
        
    private String envContent;
    private Map files = new HashMap();
    private URL baseUrl;
    
    public Environment(URL environmentRes) throws JsonParseException, IOException, URISyntaxException{
        setEnvContent(Resources.toString(environmentRes, Charsets.UTF_8));
        setBaseUrl(TemplateUtils.baseUrl(environmentRes.toString()));
        getFileContent();
    }
    
    public Environment(String environmentLoc) 
            throws JsonParseException, MalformedURLException, 
                   UnsupportedEncodingException, IOException, URISyntaxException {
        this(TemplateUtils.normaliseFilePathToUrl(environmentLoc));
    }
    
    
    @SuppressWarnings("unchecked")
    private Map getResourceRegistry(){
        // FIXME find alternative implementation not importing com.fasterxml.jackson.dataformat.yaml.snakeyaml package
        // this package is not visible in OSGi
        Yaml yaml = new Yaml();
        Map content = (Map) yaml.load(getEnvContent());
        return (Map) content.get("resource_registry");
    }

    /*
     * Processing the template files located in the resource_registry part in the environment file
     */
    private void getFileContent() 
            throws JsonParseException, MalformedURLException, UnsupportedEncodingException, IOException, URISyntaxException {
        Map rr = getResourceRegistry();
        if (rr == null ) return;
        if(rr.get("base_url") != null ) setBaseUrl(new URL(rr.get("base_url")));
        for(String resourceType: rr.keySet()) {
            if(resourceType == "base_url") continue;
            
            URL tplUrl = new URL(baseUrl, rr.get(resourceType));
            
            Template tpl = new Template(tplUrl);
            files.put(rr.get(resourceType), tpl.getTplContent());
            Map fileFromTemplate = tpl.getFiles();
            for(String file : fileFromTemplate.keySet()) {
                if(! files.containsKey(file)) {
                    files.put(file, fileFromTemplate.get(file));
                }
            }
            
        }
    }

    public String getEnvContent() {
        return envContent;
    }

    public void setEnvContent(String envContent) {
        this.envContent = envContent;
    }

    public Map getFiles() {
        return files;
    }

    public void setFiles(Map files) {
        this.files = files;
    }

    public URL getBaseUrl() {
        return baseUrl;
    }

    public void setBaseUrl(URL baseUrl) {
        this.baseUrl = baseUrl;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy