package net.windwards.dnsfrontend.backend;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import net.windwards.dnsfrontend.api.Backend;
import net.windwards.dnsfrontend.api.BackendCommunicationException;
import net.windwards.dnsfrontend.api.Configuration;
import net.windwards.dnsfrontend.api.NoSuchDomainException;
import net.windwards.dnsfrontend.dialog.ProtocolCodingException;
import org.xbill.DNS.Record;
public class SimpleBackend implements Backend {
private final String prefix;
private final Record record;
private Ehcache cache;
private Configuration configuration;
public SimpleBackend(String prefix, Record record) {
this.prefix = prefix;
this.record = record;
}
@Override
public void setCache(Ehcache cache) {
this.cache = cache;
}
@Override
public void setConfiguration(Configuration configuration) {
this.configuration = configuration;
}
@Override public void start() { }
@Override public void stop() { }
@Override
public String makeKey(Record record) {
return this.prefix + ":" + record.getName();
}
@Override
public void notify(Record question) throws BackendCommunicationException, ProtocolCodingException, NoSuchDomainException {
Record adjusted = this.record.withName(question.getName());
Element element = new Element(this.makeKey(question), adjusted);
element.setTimeToLive((int) adjusted.getTTL());
this.cache.put(element);
}
}