/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2014, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.wildfly.clustering.server.dispatcher;

import static java.security.AccessController.doPrivileged;

import java.security.PrivilegedAction;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;

import org.jboss.threads.JBossThreadFactory;
import org.jgroups.Address;
import org.jgroups.UnreachableException;
import org.wildfly.clustering.dispatcher.Command;
import org.wildfly.clustering.dispatcher.CommandDispatcher;
import org.wildfly.clustering.dispatcher.CommandResponse;
import org.wildfly.clustering.group.Node;
import org.wildfly.security.manager.WildFlySecurityManager;

/**
 * Non-clustered {@link CommandDispatcher} implementation
 * @author Paul Ferraro
 * @param <C> command context
 */
public class LocalCommandDispatcher<C> implements CommandDispatcher<C> {

    final C context;
    private final Node node;
    private final ExecutorService executor;

    public LocalCommandDispatcher(Node node, C context) {
        this(node, context, Executors.newCachedThreadPool(createThreadFactory()));
    }

    private static ThreadFactory createThreadFactory() {
        return doPrivileged(new PrivilegedAction<ThreadFactory>() {
            @Override
            public ThreadFactory run() {
                return new JBossThreadFactory(new ThreadGroup(LocalCommandDispatcher.class.getSimpleName()), Boolean.FALSE, null, "%G - %t", null, null);
            }
        });
    }

    public LocalCommandDispatcher(Node node, C context, ExecutorService executor) {
        this.node = node;
        this.context = context;
        this.executor = executor;
    }

    @Override
    public <R> CommandResponse<R> executeOnNode(Command<R, ? super C> command, Node node) {
        if (!this.node.equals(node)) {
            throw new UnreachableException((Address) null);
        }
        try {
            return new SimpleCommandResponse<>(command.execute(this.context));
        } catch (Throwable e) {
            return new SimpleCommandResponse<>(e);
        }
    }

    @Override
    public <R> Map<Node, CommandResponse<R>> executeOnCluster(Command<R, ? super C> command, Node... excludedNodes) {
        Map<Node, CommandResponse<R>> results = new HashMap<>();
        if ((excludedNodes == null) || (excludedNodes.length == 0) || !Arrays.asList(excludedNodes).contains(this.node)) {
            results.put(this.node, this.executeOnNode(command, this.node));
        }
        return results;
    }

    @Override
    public <R> Future<R> submitOnNode(final Command<R, ? super C> command, Node node) {
        Callable<R> task = new Callable<R>() {
            @Override
            public R call() throws Exception {
                return command.execute(LocalCommandDispatcher.this.context);
            }
        };
        return this.executor.submit(task);
    }

    @Override
    public <R> Map<Node, Future<R>> submitOnCluster(Command<R, ? super C> command, Node... excludedNodes) {
        Map<Node, Future<R>> results = new HashMap<>();
        if ((excludedNodes == null) || (excludedNodes.length == 0) || !Arrays.asList(excludedNodes).contains(this.node)) {
            results.put(this.node, this.submitOnNode(command, this.node));
        }
        return results;
    }

    @Override
    public void close() {
        WildFlySecurityManager.doUnchecked((PrivilegedAction<Void>) () -> {
            this.executor.shutdown();
            return null;
        });
    }
}

@@ -1071,68 +1071,8 @@
 r;%0A%0A
-import static java.security.AccessController.doPrivileged;%0A%0A
 impo
@@ -2272,32 +2272,8 @@
     
-return doPrivileged(new 
 Priv
@@ -2303,98 +2303,23 @@
 ory%3E
-() %7B%0A            @Override%0A            public ThreadFactory run() %7B%0A                return
+ action = () -%3E
  new
@@ -2454,31 +2454,64 @@
         
-    %7D%0A        %7D
+return WildFlySecurityManager.doUnchecked(action
 );%0A    %7D
@@ -4379,44 +4379,8 @@
     
-WildFlySecurityManager.doUnchecked((
 Priv
@@ -4397,17 +4397,25 @@
 on%3CVoid%3E
-)
+ action =
  () -%3E %7B
@@ -4478,16 +4478,16 @@
 n null;%0A
-
         
@@ -4487,16 +4487,67 @@
        %7D
+;%0A        WildFlySecurityManager.doUnchecked(action
 );%0A    %7D

Replace AccessController usage with WildFlySecurityManager.