@Override
    public <T> T sudo(final SudoAction<T> c) throws LoginException, SudoExecutionException {
        final Subject s = createSubject(c);

        // save the current security context...
        final SecurityContext secCtx = SecurityContext.getCurrent();
        
        final LoginContext lc;

        if(c.getType() != LoginType.NO_LOGIN) {
            // this issues the real login and fills the subject with user roles...
            lc = new LoginContext(c.getRealm(), s);
            lc.login();
        } else {
            lc = null;
        }
        
        T result = null;

        try {
            // establish a new security context for the "sudo"...
            final SecurityContext newCtx = AccessController.doPrivileged(
                    new PrivilegedAction<SecurityContext>() {

                        @Override
                        public SecurityContext run() {
                            return new SecurityContext(s);
                        }
                    });
            SecurityContext.setCurrent(newCtx);
            if(log.isLoggable(Level.FINE)) {
                log.fine("[SUDO] New SecurityContext established");
            }

            // execute whatever you want...
            try {
                result = c.run();
            } catch(Exception x) {
                throw new SudoExecutionException(secCtx.getCallerPrincipal(), x);
            }
        } finally {
            if(lc != null) {
                try {
                    lc.logout();
                } catch(LoginException x) {
                    x.printStackTrace(System.err);
                }
            }

            // restore the original security context to not break anything...
            SecurityContext.setCurrent(secCtx);
            if(log.isLoggable(Level.FINE)) {
                log.fine("[SUDO] Original SecurityContext restored");
            }
        }
        
        return result;
    }