Use context class loader for DATA_ADMIN login
authorMathieu Baudier <mbaudier@argeo.org>
Thu, 1 Sep 2016 10:58:46 +0000 (10:58 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Thu, 1 Sep 2016 10:58:46 +0000 (10:58 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@9088 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

org.argeo.cms/src/org/argeo/security/core/AbstractSystemExecution.java

index 81eeadf21b146f42d628aeff841c64ba2128d639..c4beeed52444dcc5f40612d5ad7b1714c234da49 100644 (file)
@@ -25,8 +25,7 @@ import org.argeo.ArgeoException;
 
 /** Provides base method for executing code with system authorization. */
 public abstract class AbstractSystemExecution {
-       private final static Log log = LogFactory
-                       .getLog(AbstractSystemExecution.class);
+       private final static Log log = LogFactory.getLog(AbstractSystemExecution.class);
        private final Subject subject = new Subject();
 
        private final String loginModule = "SYSTEM";
@@ -36,22 +35,30 @@ public abstract class AbstractSystemExecution {
         * {@link AuthenticationManager}
         */
        protected void authenticateAsSystem() {
+               ClassLoader origClassLoader = Thread.currentThread().getContextClassLoader();
+               Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
                try {
                        LoginContext lc = new LoginContext(loginModule, subject);
                        lc.login();
                } catch (LoginException e) {
                        throw new ArgeoException("Cannot login as system", e);
+               } finally {
+                       Thread.currentThread().setContextClassLoader(origClassLoader);
                }
                if (log.isTraceEnabled())
                        log.trace("System authenticated");
        }
 
        protected void deauthenticateAsSystem() {
+               ClassLoader origClassLoader = Thread.currentThread().getContextClassLoader();
+               Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
                try {
                        LoginContext lc = new LoginContext(loginModule, subject);
                        lc.logout();
                } catch (LoginException e) {
                        throw new ArgeoException("Cannot logout as system", e);
+               } finally {
+                       Thread.currentThread().setContextClassLoader(origClassLoader);
                }
        }