com.anrisoftware.sscontrol.dns.service.DnsServiceImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sscontrol-dns Show documentation
Show all versions of sscontrol-dns Show documentation
Setups the DNS service for the server.
The newest version!
/*
* Copyright 2012-2015 Erwin Müller
*
* This file is part of sscontrol-dns.
*
* sscontrol-dns is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* sscontrol-dns is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with sscontrol-dns. If not, see .
*/
package com.anrisoftware.sscontrol.dns.service;
import static com.anrisoftware.sscontrol.dns.service.DnsServiceFactory.SERVICE_NAME;
import static com.anrisoftware.sscontrol.dns.service.DnsServiceStatement.ACLS_KEY;
import static com.anrisoftware.sscontrol.dns.service.DnsServiceStatement.ADDRESSES_KEY;
import static com.anrisoftware.sscontrol.dns.service.DnsServiceStatement.ADDRESS_KEY;
import static com.anrisoftware.sscontrol.dns.service.DnsServiceStatement.ALIAS_KEY;
import static com.anrisoftware.sscontrol.dns.service.DnsServiceStatement.DURATION_KEY;
import static com.anrisoftware.sscontrol.dns.service.DnsServiceStatement.GENERATE_KEY;
import static com.anrisoftware.sscontrol.dns.service.DnsServiceStatement.NAME_KEY;
import static com.anrisoftware.sscontrol.dns.service.DnsServiceStatement.ROOT_KEY;
import static com.anrisoftware.sscontrol.dns.service.DnsServiceStatement.SERIAL_KEY;
import static com.anrisoftware.sscontrol.dns.service.DnsServiceStatement.SERVERS_KEY;
import static com.anrisoftware.sscontrol.dns.service.DnsServiceStatement.SERVER_KEY;
import static com.anrisoftware.sscontrol.dns.service.DnsServiceStatement.TTL_KEY;
import static com.anrisoftware.sscontrol.dns.service.DnsServiceStatement.UPSTREAM_KEY;
import static java.lang.String.format;
import static java.util.Collections.unmodifiableList;
import groovy.lang.Script;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.inject.Inject;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.joda.time.DateTime;
import com.anrisoftware.sscontrol.core.api.Service;
import com.anrisoftware.sscontrol.core.api.ServiceException;
import com.anrisoftware.sscontrol.core.api.ServiceScriptFactory;
import com.anrisoftware.sscontrol.core.bindings.BindingAddress;
import com.anrisoftware.sscontrol.core.bindings.BindingAddressesStatementsTable;
import com.anrisoftware.sscontrol.core.bindings.BindingAddressesStatementsTableFactory;
import com.anrisoftware.sscontrol.core.groovy.StatementsException;
import com.anrisoftware.sscontrol.core.groovy.StatementsMap;
import com.anrisoftware.sscontrol.core.groovy.StatementsMapFactory;
import com.anrisoftware.sscontrol.core.groovy.StatementsTable;
import com.anrisoftware.sscontrol.core.groovy.StatementsTableFactory;
import com.anrisoftware.sscontrol.core.list.StringToListFactory;
import com.anrisoftware.sscontrol.core.service.AbstractService;
import com.anrisoftware.sscontrol.dns.zone.DnsZone;
import com.anrisoftware.sscontrol.dns.zone.DnsZoneFactory;
import com.anrisoftware.sscontrol.dns.zone.Record;
import com.anrisoftware.sscontrol.dns.zone.ZoneRecord;
/**
* DNS service.
*
* @author Erwin Mueller, [email protected]
* @since 1.0
*/
@SuppressWarnings("serial")
class DnsServiceImpl extends AbstractService implements DnsService {
private final List zones;
@Inject
private DnsServiceImplLogger log;
@Inject
private StringToListFactory toListFactory;
@Inject
private DnsZoneFactory zoneFactory;
private StatementsMap statementsMap;
private StatementsTable statementsTable;
private BindingAddressesStatementsTable bindingAddressesStatementsTable;
DnsServiceImpl() {
this.zones = new ArrayList();
}
@Inject
public void setStatementsMapFactory(StatementsMapFactory factory) {
StatementsMap map = factory.create(this, SERVICE_NAME);
this.statementsMap = map;
map.addAllowed(SERIAL_KEY, SERVERS_KEY, ACLS_KEY);
map.setAllowValue(true, SERIAL_KEY);
map.setAllowMultiValue(true, ACLS_KEY);
map.addAllowedKeys(SERIAL_KEY, GENERATE_KEY);
map.addAllowedKeys(SERVERS_KEY, UPSTREAM_KEY, ROOT_KEY);
map.putValue(SERIAL_KEY.toString(), 0);
map.putMapValue(SERIAL_KEY.toString(), GENERATE_KEY.toString(), true);
}
@Inject
public void setStatementsTableFactory(StatementsTableFactory factory) {
StatementsTable table = factory.create(this, SERVICE_NAME);
this.statementsTable = table;
table.addAllowed(SERVER_KEY, ALIAS_KEY);
table.addAllowedKeys(SERVER_KEY, ADDRESS_KEY);
table.addAllowedKeys(ALIAS_KEY, ADDRESS_KEY, ADDRESSES_KEY);
}
@Inject
public final void setBindingAddressesStatementsTable(
BindingAddressesStatementsTableFactory factory) {
BindingAddressesStatementsTable table;
table = factory.create(this, SERVICE_NAME);
table.setRequirePort(false);
this.bindingAddressesStatementsTable = table;
}
@Override
protected Script getScript(String profileName) throws ServiceException {
ServiceScriptFactory scriptFactory = findScriptFactory(SERVICE_NAME);
return (Script) scriptFactory.getScript();
}
/**
* Because we load the script from a script service the dependencies are
* already injected.
*/
@Override
protected void injectScript(Script script) {
}
/**
* Returns the DNS service name.
*/
@Override
public String getName() {
return SERVICE_NAME;
}
/**
* Entry point for the DNS service script.
*
* @param statements
* the DNS service statements.
*
* @return this {@link Service}.
*/
public Service dns(Object statements) {
return this;
}
@Override
public int getSerialNumber() {
int serial = statementsMap.value(SERIAL_KEY);
return isSerialGenerate() ? generateSerial(serial) : serial;
}
@Override
public boolean isSerialGenerate() {
return statementsMap.mapValue(SERIAL_KEY, GENERATE_KEY);
}
@Override
public Map> getBindingAddresses() {
return bindingAddressesStatementsTable.getBindingAddresses();
}
@Override
public List getUpstreamServers() {
return statementsMap.mapValueAsStringList(SERVERS_KEY, UPSTREAM_KEY);
}
@Override
public List getRootServers() {
return statementsMap.mapValueAsStringList(SERVERS_KEY, ROOT_KEY);
}
@Override
public Map getServers() {
return statementsTable.tableKeys(SERVER_KEY, ADDRESS_KEY);
}
@Override
public Map> getAliases() {
Map> amap = new HashMap>();
List alist;
Map> address;
address = statementsTable.tableKeysAsList(ALIAS_KEY, ADDRESS_KEY);
Map> addresses;
addresses = statementsTable.tableKeysAsList(ALIAS_KEY, ADDRESSES_KEY);
if (address != null) {
for (Entry> entry : address.entrySet()) {
alist = amap.get(entry.getKey());
if (alist == null) {
alist = new ArrayList();
amap.put(entry.getKey(), alist);
}
alist.addAll(entry.getValue());
}
}
if (addresses != null) {
for (Entry> entry : addresses.entrySet()) {
alist = amap.get(entry.getKey());
if (alist == null) {
alist = new ArrayList();
amap.put(entry.getKey(), alist);
}
alist.addAll(entry.getValue());
}
}
return amap.size() == 0 ? null : amap;
}
@Override
public List getAcls() {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy