Use an atomic reference instead of volatile.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 28 Sep 2014 18:21:57 +0000 (20:21 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 28 Sep 2014 18:39:33 +0000 (20:39 +0200)
src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java

index e49537b..1ddf0a9 100644 (file)
@@ -23,6 +23,7 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -78,7 +79,7 @@ public class FcpInterface {
        private final AtomicBoolean active = new AtomicBoolean();
 
        /** What function full access is required for. */
-       private volatile FullAccessRequired fullAccessRequired = FullAccessRequired.ALWAYS;
+       private final AtomicReference<FullAccessRequired> fullAccessRequired = new AtomicReference<FullAccessRequired>(FullAccessRequired.ALWAYS);
 
        /** All available FCP commands. */
        private final Map<String, AbstractSoneCommand> commands = Collections.synchronizedMap(new HashMap<String, AbstractSoneCommand>());
@@ -131,7 +132,7 @@ public class FcpInterface {
 
        @VisibleForTesting
        FullAccessRequired getFullAccessRequired() {
-               return fullAccessRequired;
+               return fullAccessRequired.get();
        }
 
        /**
@@ -141,7 +142,7 @@ public class FcpInterface {
         *            The action level for which full FCP access is required
         */
        public void setFullAccessRequired(FullAccessRequired fullAccessRequired) {
-               this.fullAccessRequired = checkNotNull(fullAccessRequired, "fullAccessRequired must not be null");
+               this.fullAccessRequired.set(checkNotNull(fullAccessRequired, "fullAccessRequired must not be null"));
        }
 
        //
@@ -172,7 +173,7 @@ public class FcpInterface {
                        return;
                }
                AbstractSoneCommand command = commands.get(parameters.get("Message"));
-               if ((accessType == FredPluginFCP.ACCESS_FCP_RESTRICTED) && (((fullAccessRequired == FullAccessRequired.WRITING) && command.requiresWriteAccess()) || (fullAccessRequired == FullAccessRequired.ALWAYS))) {
+               if ((accessType == FredPluginFCP.ACCESS_FCP_RESTRICTED) && (((fullAccessRequired.get() == FullAccessRequired.WRITING) && command.requiresWriteAccess()) || (fullAccessRequired.get() == FullAccessRequired.ALWAYS))) {
                        try {
                                sendReply(pluginReplySender, null, new ErrorResponse(401, "Not authorized"));
                        } catch (PluginNotFoundException pnfe1) {