diff --git a/java/google/registry/dns/writer/BaseDnsWriter.java b/java/google/registry/dns/writer/BaseDnsWriter.java new file mode 100644 index 000000000..ee5b087d7 --- /dev/null +++ b/java/google/registry/dns/writer/BaseDnsWriter.java @@ -0,0 +1,36 @@ +// Copyright 2017 The Nomulus Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package google.registry.dns.writer; + +import static com.google.common.base.Preconditions.checkState; + +import java.util.concurrent.atomic.AtomicBoolean; + +/** + * A base implementation of {@link DnsWriter} that protects against multiple calls to commit(). + */ +public abstract class BaseDnsWriter implements DnsWriter { + + private final AtomicBoolean committedAlready = new AtomicBoolean(false); + + @Override + public final void commit() { + checkState(committedAlready.compareAndSet(false, true), "commit() has already been called"); + commitUnchecked(); + } + + /** Commits DNS updates. This can never be called more than once. */ + protected abstract void commitUnchecked(); +} diff --git a/java/google/registry/dns/writer/VoidDnsWriter.java b/java/google/registry/dns/writer/VoidDnsWriter.java index aaad34ea0..32a8d3b60 100644 --- a/java/google/registry/dns/writer/VoidDnsWriter.java +++ b/java/google/registry/dns/writer/VoidDnsWriter.java @@ -14,8 +14,6 @@ package google.registry.dns.writer; -import static com.google.common.base.Preconditions.checkState; - import com.google.common.base.Joiner; import java.util.HashSet; import java.util.Set; @@ -27,7 +25,7 @@ import javax.inject.Inject; * *

All this class does is write its displeasure to the logs. */ -public final class VoidDnsWriter implements DnsWriter { +public final class VoidDnsWriter extends BaseDnsWriter { /** * The name of the pricing engine, as used in {@code Registry.dnsWriter}. Remember to change @@ -37,8 +35,6 @@ public final class VoidDnsWriter implements DnsWriter { private static final Logger logger = Logger.getLogger(VoidDnsWriter.class.getName()); - private boolean committed = false; - private final Set names = new HashSet<>(); @Inject @@ -55,10 +51,7 @@ public final class VoidDnsWriter implements DnsWriter { } @Override - public void commit() { - checkState(!committed, "commit() has already been called"); - committed = true; - + protected void commitUnchecked() { logger.warning("Ignoring DNS zone updates! No DnsWriterFactory implementation specified!\n" + Joiner.on('\n').join(names)); } diff --git a/java/google/registry/dns/writer/clouddns/CloudDnsWriter.java b/java/google/registry/dns/writer/clouddns/CloudDnsWriter.java index e55db1753..766a5df30 100644 --- a/java/google/registry/dns/writer/clouddns/CloudDnsWriter.java +++ b/java/google/registry/dns/writer/clouddns/CloudDnsWriter.java @@ -15,7 +15,6 @@ package google.registry.dns.writer.clouddns; import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkState; import static google.registry.model.EppResourceUtils.loadByForeignKey; import com.google.api.client.googleapis.json.GoogleJsonError.ErrorInfo; @@ -33,6 +32,7 @@ import com.google.common.collect.ImmutableSet.Builder; import com.google.common.net.InternetDomainName; import com.google.common.util.concurrent.RateLimiter; import google.registry.config.RegistryConfig.Config; +import google.registry.dns.writer.BaseDnsWriter; import google.registry.dns.writer.DnsWriter; import google.registry.dns.writer.DnsWriterZone; import google.registry.model.domain.DomainResource; @@ -59,7 +59,7 @@ import org.joda.time.Duration; * * @see Google Cloud DNS Documentation */ -public class CloudDnsWriter implements DnsWriter { +public class CloudDnsWriter extends BaseDnsWriter { /** * The name of the pricing engine, as used in {@code Registry.dnsWriter}. Remember to change @@ -85,8 +85,6 @@ public class CloudDnsWriter implements DnsWriter { private final ImmutableMap.Builder> desiredRecordsBuilder = new ImmutableMap.Builder<>(); - private boolean committed = false; - @Inject CloudDnsWriter( Dns dnsConnection, @@ -273,15 +271,9 @@ public class CloudDnsWriter implements DnsWriter { * representation built via this writer. */ @Override - public void commit() { - checkState(!committed, "commit() has already been called"); - committed = true; - commit(desiredRecordsBuilder.build()); - } - - @VisibleForTesting - void commit(ImmutableMap> desiredRecords) { - retrier.callWithRetry(getMutateZoneCallback(desiredRecords), ZoneStateException.class); + protected void commitUnchecked() { + retrier.callWithRetry( + getMutateZoneCallback(desiredRecordsBuilder.build()), ZoneStateException.class); logger.info("Wrote to Cloud DNS"); } diff --git a/java/google/registry/dns/writer/dnsupdate/DnsUpdateWriter.java b/java/google/registry/dns/writer/dnsupdate/DnsUpdateWriter.java index 664b99feb..3dac52714 100644 --- a/java/google/registry/dns/writer/dnsupdate/DnsUpdateWriter.java +++ b/java/google/registry/dns/writer/dnsupdate/DnsUpdateWriter.java @@ -14,7 +14,6 @@ package google.registry.dns.writer.dnsupdate; -import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Verify.verify; import static com.google.common.collect.Sets.intersection; import static com.google.common.collect.Sets.union; @@ -26,7 +25,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.net.InternetDomainName; import google.registry.config.RegistryConfig.Config; -import google.registry.dns.writer.DnsWriter; +import google.registry.dns.writer.BaseDnsWriter; import google.registry.dns.writer.DnsWriterZone; import google.registry.model.domain.DomainResource; import google.registry.model.domain.secdns.DelegationSignerData; @@ -76,7 +75,7 @@ import org.xbill.DNS.Update; * message, as required by RFC 2136. Care must be taken to make sure the SOA serial number does not * go backwards if the entire TLD (zone) is "reset" to empty and republished. */ -public class DnsUpdateWriter implements DnsWriter { +public class DnsUpdateWriter extends BaseDnsWriter { /** * The name of the pricing engine, as used in {@code Registry.dnsWriter}. Remember to change @@ -92,12 +91,12 @@ public class DnsUpdateWriter implements DnsWriter { private final Update update; private final String zoneName; - private boolean committed = false; - /** * Class constructor. * - * @param dnsTimeToLive TTL used for any created resource records + * @param dnsDefaultATtl TTL used for any created resource records + * @param dnsDefaultNsTtl TTL used for any created nameserver records + * @param dnsDefaultDsTtl TTL used for any created DS records * @param transport the transport used to send/receive the UPDATE messages * @param clock a source of time */ @@ -168,10 +167,7 @@ public class DnsUpdateWriter implements DnsWriter { } @Override - public void commit() { - checkState(!committed, "commit() has already been called"); - committed = true; - + protected void commitUnchecked() { try { Message response = transport.send(update); verify(