Improve system execution
authorMathieu Baudier <mbaudier@argeo.org>
Fri, 2 Mar 2012 11:40:00 +0000 (11:40 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Fri, 2 Mar 2012 11:40:00 +0000 (11:40 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@5130 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

security/plugins/org.argeo.security.ui/META-INF/spring/monitor.xml
security/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/views/AdminLogView.java
security/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/views/LogContentProvider.java
security/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/views/LogView.java
security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/core/AbstractSystemExecution.java
security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/core/AuthenticatedApplicationContextInitialization.java
security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/core/KeyBasedSystemExecutionService.java
security/runtime/org.argeo.security.jackrabbit/src/main/java/org/argeo/security/jackrabbit/ArgeoLoginModule.java

index 93a370de4559c794daa53108b61329c05e269ddf..3c313de526907f89a9ddae042f232134d7d3a011 100644 (file)
@@ -15,6 +15,7 @@ log4j.rootLogger=WARN, console
 log4j.logger.org.argeo=DEBUG
 log4j.logger.org.argeo.jackrabbit.remote.ExtendedDispatcherServlet=WARN
 log4j.logger.org.argeo.server.webextender.TomcatDeployer=WARN
+log4j.logger.org.argeo.security.core=TRACE
 
 log4j.logger.org.apache.catalina=INFO
 log4j.logger.org.apache.coyote=INFO
index 63908cf8ab6a7b57fbdc34733b00939c48681686..d99a93ed6d924edba7f0ac4724fb5b1ba55a8ffa 100644 (file)
@@ -10,6 +10,8 @@ import org.eclipse.jface.viewers.TableViewer;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.graphics.Font;
 import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Table;
 import org.eclipse.ui.part.ViewPart;
 
 /**
@@ -23,22 +25,16 @@ public class AdminLogView extends ViewPart {
        private LogContentProvider logContentProvider;
        private SecureLogger argeoLogger;
 
-       private Font font;
-
        @Override
        public void createPartControl(Composite parent) {
                // FIXME doesn't return a monospace font in RAP
-               font = JFaceResources.getTextFont();
-//             if (font == JFaceResources.getDefaultFont()) {
-//                     Set<?> keySet = JFaceResources.getFontRegistry().getKeySet();
-//                     for (Object key : keySet) {
-//                             System.out.println(key);
-//                     }
-//             }
-
-               viewer = new TableViewer(parent, SWT.VIRTUAL | SWT.MULTI | SWT.H_SCROLL
+               Font font = JFaceResources.getTextFontDescriptor().setHeight(8)
+                               .createFont(Display.getCurrent());
+               Table table = new Table(parent, SWT.VIRTUAL | SWT.MULTI | SWT.H_SCROLL
                                | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
-               viewer.getTable().setFont(font);
+               table.setFont(font);
+
+               viewer = new TableViewer(table);
                viewer.setLabelProvider(new LabelProvider());
                logContentProvider = new LogContentProvider(viewer) {
 
index fb1ee13bb95574a4f1faa40519270d335e762dc1..c365f9e6cbf1bc28c794447a612f70bca597811c 100644 (file)
@@ -118,14 +118,12 @@ class LogContentProvider implements ILazyContentProvider, ArgeoLogListener {
        // }
 
        /** Scroll to the last line */
-       protected void scrollToLastLine() {
+       protected synchronized void scrollToLastLine() {
                // we try to show last line with two methods
                // viewer.reveal(lines.peekLast());
 
                Table table = viewer.getTable();
-               TableItem ti = table.getItem(lines.size() - 1);
-               if (ti == null)
-                       System.out.println("tableItem is null");
+               TableItem ti = table.getItem(table.getItemCount() - 1);
                table.showItem(ti);
        }
 
index a612bc9f71552d5a813690ba4e0fe129d294350c..56857d3b533bbb8e146637ec64d3a0b301db028a 100644 (file)
@@ -5,10 +5,14 @@ import java.util.ArrayList;
 import org.argeo.ArgeoLogListener;
 import org.argeo.ArgeoLogger;
 import org.argeo.security.ui.SecurityUiPlugin;
+import org.eclipse.jface.resource.JFaceResources;
 import org.eclipse.jface.viewers.LabelProvider;
 import org.eclipse.jface.viewers.TableViewer;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.graphics.Font;
 import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Table;
 import org.eclipse.ui.part.ViewPart;
 
 /**
@@ -25,8 +29,13 @@ public class LogView extends ViewPart {
 
        @Override
        public void createPartControl(Composite parent) {
-               viewer = new TableViewer(parent, SWT.VIRTUAL | SWT.MULTI | SWT.H_SCROLL
+               Font font = JFaceResources.getTextFontDescriptor().setHeight(8)
+                               .createFont(Display.getCurrent());
+               Table table = new Table(parent, SWT.VIRTUAL | SWT.MULTI | SWT.H_SCROLL
                                | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
+               table.setFont(font);
+
+               viewer = new TableViewer(table);
                viewer.setLabelProvider(new LabelProvider());
                logContentProvider = new LogContentProvider(viewer);
                viewer.setContentProvider(logContentProvider);
index b12629ab90ca64313ecb72317fae5408e5dd8296..c4a5fc6cba8113cfacc2fff9bb389fd868cec8a9 100644 (file)
@@ -1,12 +1,9 @@
 package org.argeo.security.core;
 
-import java.security.AccessController;
-
-import javax.security.auth.Subject;
-
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.ArgeoException;
+import org.argeo.security.SystemAuthentication;
 import org.springframework.security.Authentication;
 import org.springframework.security.AuthenticationManager;
 import org.springframework.security.context.SecurityContext;
@@ -17,7 +14,7 @@ public abstract class AbstractSystemExecution {
        static {
                // Forces Spring Security to use inheritable strategy
                // FIXME find a better place for forcing spring security mode
-               // doesn't work for the time besing
+               // doesn't work for the time being
 //             if (System.getProperty(SecurityContextHolder.SYSTEM_PROPERTY) == null)
 //                     SecurityContextHolder
 //                                     .setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL);
@@ -29,7 +26,7 @@ public abstract class AbstractSystemExecution {
        private String systemAuthenticationKey;
 
        /** Whether the current thread was authenticated by this component. */
-       private InheritableThreadLocal<Boolean> authenticatedBySelf = new InheritableThreadLocal<Boolean>() {
+       private ThreadLocal<Boolean> authenticatedBySelf = new ThreadLocal<Boolean>() {
                protected Boolean initialValue() {
                        return false;
                }
@@ -44,17 +41,19 @@ public abstract class AbstractSystemExecution {
                        return;
                SecurityContext securityContext = SecurityContextHolder.getContext();
                Authentication currentAuth = securityContext.getAuthentication();
-               if (currentAuth != null){
-                       throw new ArgeoException(
-                                       "System execution on an already authenticated thread: "
-                                                       + currentAuth + ", THREAD="
-                                                       + Thread.currentThread().getId());
+               if (currentAuth != null) {
+                       if (!(currentAuth instanceof SystemAuthentication))
+                               throw new ArgeoException(
+                                               "System execution on an already authenticated thread: "
+                                                               + currentAuth + ", THREAD="
+                                                               + Thread.currentThread().getId());
+                       return;
                }
-               Subject subject = Subject.getSubject(AccessController.getContext());
-               if (subject != null
-                               && !subject.getPrincipals(Authentication.class).isEmpty())
-                       throw new ArgeoException(
-                                       "There is already an authenticated subject: " + subject);
+               // Subject subject = Subject.getSubject(AccessController.getContext());
+               // if (subject != null
+               // && !subject.getPrincipals(Authentication.class).isEmpty())
+               // throw new ArgeoException(
+               // "There is already an authenticated subject: " + subject);
 
                String key = systemAuthenticationKey != null ? systemAuthenticationKey
                                : System.getProperty(
@@ -70,19 +69,17 @@ public abstract class AbstractSystemExecution {
                        log.trace("System authenticated");
        }
 
-       /** Removes the authentication from the calling thread. */
-       protected void deauthenticateAsSystem() {
-               // remove the authentication
-               SecurityContext securityContext = SecurityContextHolder.getContext();
-               if (securityContext.getAuthentication() != null) {
-                       securityContext.setAuthentication(null);
-                       authenticatedBySelf.set(false);
-                       if (log.isTraceEnabled()) {
-                               log.trace("System deauthenticated");
-                               // Thread.dumpStack();
-                       }
-               }
-       }
+       // /** Removes the authentication from the calling thread. */
+       // protected void deauthenticateAsSystem() {
+       // // remove the authentication
+       // // SecurityContext securityContext = SecurityContextHolder.getContext();
+       // // securityContext.setAuthentication(null);
+       // // authenticatedBySelf.set(false);
+       // if (log.isTraceEnabled()) {
+       // log.trace("System deauthenticated");
+       // // Thread.dumpStack();
+       // }
+       // }
 
        /**
         * Whether the current thread was authenticated by this component or a
index 59f6a517921e79c448e08ad342889e6c2fb5497e..f6573e806d5104329d221a94e60e6bbcd2dc7be6 100644 (file)
@@ -2,8 +2,6 @@ package org.argeo.security.core;
 
 import java.beans.PropertyDescriptor;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 import org.springframework.beans.BeansException;
 import org.springframework.beans.PropertyValues;
 import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
@@ -18,20 +16,17 @@ import org.springframework.context.event.ContextRefreshedEvent;
 public class AuthenticatedApplicationContextInitialization extends
                AbstractSystemExecution implements InstantiationAwareBeanPostProcessor,
                ApplicationListener {
-       private Log log = LogFactory
-                       .getLog(AuthenticatedApplicationContextInitialization.class);
+       // private Log log = LogFactory
+       // .getLog(AuthenticatedApplicationContextInitialization.class);
 
        @SuppressWarnings("rawtypes")
        public Object postProcessBeforeInstantiation(Class beanClass,
                        String beanName) throws BeansException {
-               // we authenticate when any beans is instantiated
+               // we authenticate when any bean is instantiated
                // we will deauthenticate only when the application context has been
                // refreshed in order to be able to deal with factory beans has well
                if (!isAuthenticatedBySelf()) {
                        authenticateAsSystem();
-                       if (log.isTraceEnabled())
-                               log.trace("Application context initialization authenticated for thread "
-                                               + Thread.currentThread().getName());
                }
                return null;
        }
@@ -67,10 +62,7 @@ public class AuthenticatedApplicationContextInitialization extends
                if (event instanceof ContextRefreshedEvent) {
                        // make sure that we have deauthenticated after the application
                        // context was initialized/refreshed
-                       deauthenticateAsSystem();
-                       if (log.isTraceEnabled())
-                               log.trace("Application context initialization deauthenticated for thread "
-                                               + Thread.currentThread().getName());
+                       // deauthenticateAsSystem();
                }
        }
 
index b5791c587f00ec2dd9ea021c4c06172cf3be7527..f41e5d99f6fdd41fd145f4cbb96b731388a7b69a 100644 (file)
@@ -41,7 +41,7 @@ public class KeyBasedSystemExecutionService extends AbstractSystemExecution
                                try {
                                        return runnable.call();
                                } finally {
-                                       deauthenticateAsSystem();
+//                                     deauthenticateAsSystem();
                                }
                        }
                };
index 3a18a38b8fb1b4030214779933582611dcfcdb9c..49bd304ed3564450adb53df65ae25b36cf3a8037 100644 (file)
@@ -26,6 +26,26 @@ import org.springframework.security.providers.anonymous.AnonymousAuthenticationT
 public class ArgeoLoginModule extends AbstractLoginModule {
        private String adminRole = "ROLE_ADMIN";
 
+       @Override
+       public boolean login() throws LoginException {
+               boolean loginOk = super.login();
+               if (!loginOk) {
+                       org.springframework.security.Authentication authen = (org.springframework.security.Authentication) SecurityContextHolder
+                                       .getContext().getAuthentication();
+               }
+               return loginOk;
+       }
+
+       @Override
+       public boolean commit() throws LoginException {
+               boolean commitOk = super.commit();
+               if (!commitOk) {
+                       org.springframework.security.Authentication authen = (org.springframework.security.Authentication) SecurityContextHolder
+                                       .getContext().getAuthentication();
+               }
+               return commitOk;
+       }
+
        /**
         * Returns the Spring {@link org.springframework.security.Authentication}
         * (which can be null)
@@ -39,9 +59,9 @@ public class ArgeoLoginModule extends AbstractLoginModule {
 
        protected Set<Principal> getPrincipals() {
                // clear already registered Jackrabbit principals
-               //clearPrincipals(AdminPrincipal.class);
-               //clearPrincipals(AnonymousPrincipal.class);
-               //clearPrincipals(GrantedAuthorityPrincipal.class);
+               // clearPrincipals(AdminPrincipal.class);
+               // clearPrincipals(AnonymousPrincipal.class);
+               // clearPrincipals(GrantedAuthorityPrincipal.class);
 
                return syncPrincipals();
        }
@@ -74,8 +94,8 @@ public class ArgeoLoginModule extends AbstractLoginModule {
                if (thisCredentials != null)
                        thisCredentials.clear();
                // override credentials since we did not used the one passed to us
-//             credentials = new SimpleCredentials(authen.getName(), authen
-//                             .getCredentials().toString().toCharArray());
+               // credentials = new SimpleCredentials(authen.getName(), authen
+               // .getCredentials().toString().toCharArray());
 
                return principals;
        }