Package net.windwards.dnsfrontend

Source Code of net.windwards.dnsfrontend.ConfigurationImpl

package net.windwards.dnsfrontend;

import com.fasterxml.jackson.annotation.JsonProperty;
import net.windwards.dnsfrontend.api.Configuration;
import net.windwards.dnsfrontend.api.ConfigurationException;
import net.windwards.dnsfrontend.api.NoSuchDomainException;
import org.xbill.DNS.Name;
import org.xbill.DNS.TextParseException;

import java.util.HashMap;
import java.util.Map;


public class ConfigurationImpl implements Configuration {
    public static class DomainConfiguration {
        @JsonProperty
        private String moniker;
        @JsonProperty
        private int ttl;
    }

    @JsonProperty
    private Map<String, DomainConfiguration> domains;
    @JsonProperty
    private short port;
    @JsonProperty
    private int backendTimeout;
    @JsonProperty
    private String mx;
    @JsonProperty
    private int negativeExpiry;

    private Map<String, Name> monikers = new HashMap<String, Name>();

    @Override
    public void initialize() throws ConfigurationException {
        if (this.domains == null)
            throw new ConfigurationException("Monikers section missing");

        Map<String, DomainConfiguration> qualified = new HashMap<String, DomainConfiguration>();

        for(Map.Entry<String, DomainConfiguration> entry : this.domains.entrySet()) {
            String domainName = entry.getKey();
            if(!domainName.endsWith(".")) domainName = domainName + ".";
            Name name;
            try {
                name = new Name(domainName);
            } catch (TextParseException e) {
                throw new ConfigurationException("Can't parse domain " + domainName);
            }
            qualified.put(domainName, entry.getValue());
            this.monikers.put(entry.getValue().moniker, name);
        }

        this.domains.clear();
        this.domains.putAll(qualified);
    }

    @Override
    public Name qualifiedDomainName(String name, String moniker) throws TextParseException {
        return new Name(name, this.monikers.get(moniker));
    }

    @Override
    public String monikerFromName(Name domain) throws NoSuchDomainException {
        DomainConfiguration conf = this.domains.get(domain.toString());
        if (conf == null)
            throw new NoSuchDomainException("Unknown domain " + domain.toString());
        return conf.moniker;
    }

    @Override
    public int getTtl(String moniker) {
        return this.domains.get(this.monikers.get(moniker).toString()).ttl;
    }

    @Override
    public short getPort() {
        return this.port;
    }

    @Override
    public int getBackendTimeout() {
        return this.backendTimeout;
    }

    @Override
    public String getMX() {
        return this.mx;
    }

    @Override
    public int getNegativeExpiry() { return this.negativeExpiry; }
}
TOP

Related Classes of net.windwards.dnsfrontend.ConfigurationImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.