/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2010, 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.jboss.as.jpa.validator;

import java.security.AccessController;
import java.security.PrivilegedAction;

/**
 * Package privileged actions
 *
 * @author Scott.Stark@jboss.org
 * @version $Id $
 */
// TODO: Security action classes must not be public
public class SecurityActions {
    private interface TCLAction {
        class UTIL {
            static TCLAction getTCLAction() {
                return System.getSecurityManager() == null ? NON_PRIVILEGED : PRIVILEGED;
            }

            static ClassLoader getContextClassLoader() {
                return getTCLAction().getContextClassLoader();
            }

            static ClassLoader getContextClassLoader(Thread thread) {
                return getTCLAction().getContextClassLoader(thread);
            }

            static void setContextClassLoader(ClassLoader cl) {
                getTCLAction().setContextClassLoader(cl);
            }

            static void setContextClassLoader(Thread thread, ClassLoader cl) {
                getTCLAction().setContextClassLoader(thread, cl);
            }
        }

        TCLAction NON_PRIVILEGED = new TCLAction() {
            public ClassLoader getContextClassLoader() {
                return Thread.currentThread().getContextClassLoader();
            }

            public ClassLoader getContextClassLoader(Thread thread) {
                return thread.getContextClassLoader();
            }

            public void setContextClassLoader(ClassLoader cl) {
                Thread.currentThread().setContextClassLoader(cl);
            }

            public void setContextClassLoader(Thread thread, ClassLoader cl) {
                thread.setContextClassLoader(cl);
            }
        };

        TCLAction PRIVILEGED = new TCLAction() {
            private final PrivilegedAction<ClassLoader> getTCLPrivilegedAction = new PrivilegedAction<ClassLoader>() {
                public ClassLoader run() {
                    return Thread.currentThread().getContextClassLoader();
                }
            };

            public ClassLoader getContextClassLoader() {
                return AccessController.doPrivileged(getTCLPrivilegedAction);
            }

            public ClassLoader getContextClassLoader(final Thread thread) {
                return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
                    public ClassLoader run() {
                        return thread.getContextClassLoader();
                    }
                });
            }

            public void setContextClassLoader(final ClassLoader cl) {
                AccessController.doPrivileged(
                    new PrivilegedAction<ClassLoader>() {
                        public ClassLoader run() {
                            Thread.currentThread().setContextClassLoader(cl);
                            return null;
                        }
                    }
                );
            }

            public void setContextClassLoader(final Thread thread, final ClassLoader cl) {
                AccessController.doPrivileged(
                    new PrivilegedAction<ClassLoader>() {
                        public ClassLoader run() {
                            thread.setContextClassLoader(cl);
                            return null;
                        }
                    }
                );
            }
        };

        ClassLoader getContextClassLoader();

        ClassLoader getContextClassLoader(Thread thread);

        void setContextClassLoader(ClassLoader cl);

        void setContextClassLoader(Thread thread, ClassLoader cl);
    }

    protected static ClassLoader getContextClassLoader() {
        return TCLAction.UTIL.getContextClassLoader();
    }

    protected static void setContextClassLoader(ClassLoader loader) {
        TCLAction.UTIL.setContextClassLoader(loader);
    }
}

@@ -1137,3776 +1137,1254 @@
 n;%0A%0A
-/**%0A * Package privileged actions%0A *%0A * @author Scott.Stark@jboss.org%0A * @version $Id $%0A */%0A// TODO: Security action classes must not be public%0Apublic class SecurityActions %7B%0A    private interface TCLAction %7B%0A        class UTIL %7B%0A            static TCLAction getTCLAction() %7B%0A                return System.getSecurityManager() == null ? NON_PRIVILEGED : PRIVILEGED;%0A            %7D%0A%0A            static ClassLoader getContextClassLoader() %7B%0A                return getTCLAction().getContextClassLoader();%0A            %7D%0A%0A            static ClassLoader getContextClassLoader(Thread thread) %7B%0A                return getTCLAction().getContextClassLoader(thread);%0A            %7D%0A%0A            static void setContextClassLoader(ClassLoader cl) %7B%0A                getTCLAction().setContextClassLoader(cl);%0A            %7D%0A%0A            static void setContextClassLoader(Thread thread, ClassLoader cl) %7B%0A                getTCLAction().setContextClassLoader(thread, cl);%0A            %7D%0A        %7D%0A%0A        TCLAction NON_PRIVILEGED = new TCLAction() %7B%0A            public ClassLoader getContextClassLoader() %7B%0A                return Thread.currentThread().getContextClassLoader();%0A            %7D%0A%0A            public ClassLoader getContextClassLoader(Thread thread) %7B%0A                return thread.getContextClassLoader();%0A            %7D%0A%0A            public void setContextClassLoader(ClassLoader cl) %7B%0A                Thread.currentThread().setContextClassLoader(cl);%0A            %7D%0A%0A            public void setContextClassLoader(Thread thread, ClassLoader cl) %7B%0A                thread.setContextClassLoader(cl);%0A            %7D%0A        %7D;%0A%0A        TCLAction PRIVILEGED = new TCLAction() %7B%0A            private final PrivilegedAction%3CClassLoader%3E getTCLPrivilegedAction = new PrivilegedAction%3CClassLoader%3E() %7B%0A                public ClassLoader run() %7B%0A                    return Thread.currentThread().getContextClassLoader();%0A                %7D%0A            %7D;%0A%0A            public ClassLoader getContextClassLoader() %7B%0A                return AccessController.doPrivileged(getTCLPrivilegedAction);%0A            %7D%0A%0A            public ClassLoader getContextClassLoader(final Thread thread) %7B%0A                return AccessController.doPrivileged(new PrivilegedAction%3CClassLoader%3E() %7B%0A                    public ClassLoader run() %7B%0A                        return thread.getContextClassLoader();%0A                    %7D%0A                %7D);%0A            %7D%0A%0A            public void setContextClassLoader(final ClassLoader cl) %7B%0A                AccessController.doPrivileged(%0A                    new PrivilegedAction%3CClassLoader%3E() %7B%0A                        public ClassLoader run() %7B%0A                            Thread.currentThread().setContextClassLoader(cl);%0A                            return null;%0A                        %7D%0A                    %7D%0A                );%0A            %7D%0A%0A            public void setContextClassLoader(final Thread thread, final ClassLoader cl) %7B%0A                AccessController.doPrivileged(%0A                    new PrivilegedAction%3CClassLoader%3E() %7B%0A                        public ClassLoader run() %7B%0A                            thread.setContextClassLoader(cl);%0A                            return null;%0A                        %7D%0A                    %7D%0A                );%0A            %7D%0A        %7D;%0A%0A        ClassLoader getContextClassLoader();%0A%0A        ClassLoader getContextClassLoader(Thread thread);%0A%0A        void setContextClassLoader(ClassLoader cl);%0A%0A        void setContextClassLoader(Thread thread, ClassLoader cl);%0A    %7D%0A%0A    protected static ClassLoader getContextClassLoader() %7B%0A        return TCLAction.UTIL.getContextClassLoader();%0A    %7D%0A%0A    protected static void setContextClassLoader(ClassLoader loader) %7B%0A        TCLAction.UTIL.setContextClassLoader(loader);%0A    %7D
+final class SecurityActions %7B%0A%0A    private SecurityActions() %7B%0A        // forbidden inheritance%0A    %7D%0A%0A    /**%0A     * Gets context classloader.%0A     *%0A     * @return the current context classloader%0A     */%0A    static ClassLoader getContextClassLoader() %7B%0A        if (System.getSecurityManager() == null) %7B%0A            return Thread.currentThread().getContextClassLoader();%0A        %7D else %7B%0A            return AccessController.doPrivileged(new PrivilegedAction%3CClassLoader%3E() %7B%0A                public ClassLoader run() %7B%0A                    return Thread.currentThread().getContextClassLoader();%0A                %7D%0A            %7D);%0A        %7D%0A    %7D%0A%0A    /**%0A     * Sets context classloader.%0A     *%0A     * @param classLoader%0A     *            the classloader%0A     */%0A    static void setContextClassLoader(final ClassLoader classLoader) %7B%0A        if (System.getSecurityManager() == null) %7B%0A            Thread.currentThread().setContextClassLoader(classLoader);%0A        %7D else %7B%0A            AccessController.doPrivileged(new PrivilegedAction%3CObject%3E() %7B%0A                public Object run() %7B%0A                    Thread.currentThread().setContextClassLoader(classLoader);%0A                    return null;%0A                %7D%0A            %7D);%0A        %7D%0A    %7D%0A
 %0A%7D%0A

Fix SecurityActions issues