Fix various RAP lifecycle issues
[lgpl/argeo-commons.git] / security / plugins / org.argeo.security.ui.rap / src / main / java / org / argeo / security / ui / rap / SecureActionBarAdvisor.java
index 98a9c64dba2d596f239496465901c986d0e50b61..88836f4669f29e205520e8bc62ba218bc0bc1813 100644 (file)
@@ -1,22 +1,37 @@
 package org.argeo.security.ui.rap;
 
+import java.security.Principal;
+
+import javax.security.auth.Subject;
+import javax.security.auth.login.LoginException;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.argeo.ArgeoException;
+import org.eclipse.jface.action.Action;
 import org.eclipse.jface.action.GroupMarker;
+import org.eclipse.jface.action.IAction;
 import org.eclipse.jface.action.ICoolBarManager;
 import org.eclipse.jface.action.IMenuManager;
 import org.eclipse.jface.action.IToolBarManager;
 import org.eclipse.jface.action.MenuManager;
 import org.eclipse.jface.action.Separator;
 import org.eclipse.jface.action.ToolBarManager;
+import org.eclipse.rwt.RWT;
 import org.eclipse.swt.SWT;
 import org.eclipse.ui.IWorkbenchActionConstants;
 import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.actions.ActionFactory;
 import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
 import org.eclipse.ui.application.ActionBarAdvisor;
 import org.eclipse.ui.application.IActionBarConfigurer;
 
 public class SecureActionBarAdvisor extends ActionBarAdvisor {
-       private IWorkbenchAction exitAction;
+       private final static Log log = LogFactory
+                       .getLog(SecureActionBarAdvisor.class);
+
+       private IAction logoutAction;
        private IWorkbenchAction openPerspectiveDialogAction;
        private IWorkbenchAction showViewMenuAction;
        private IWorkbenchAction preferences;
@@ -24,11 +39,8 @@ public class SecureActionBarAdvisor extends ActionBarAdvisor {
        private IWorkbenchAction saveAllAction;
        private IWorkbenchAction closeAllAction;
 
-       // private final Boolean isRcp;
-
        public SecureActionBarAdvisor(IActionBarConfigurer configurer, Boolean isRcp) {
                super(configurer);
-               // this.isRcp = isRcp;
        }
 
        protected void makeActions(IWorkbenchWindow window) {
@@ -40,10 +52,48 @@ public class SecureActionBarAdvisor extends ActionBarAdvisor {
                showViewMenuAction = ActionFactory.SHOW_VIEW_MENU.create(window);
                register(showViewMenuAction);
 
-               exitAction = ActionFactory.QUIT.create(window);
-               register(exitAction);
+               // logoutAction = ActionFactory.QUIT.create(window);
+
+               Subject subject = null;
+               try {
+                       subject = SecureRapActivator.getLoginContext().getSubject();
+               } catch (LoginException e1) {
+                       throw new ArgeoException("Cannot retrieve subject", e1);
+               }
+               final Principal principal = subject.getPrincipals().iterator().next();
+
+               logoutAction = new Action() {
+                       public String getId() {
+                               return SecureRapActivator.ID + ".logoutAction";
+                       }
+
+                       public String getText() {
+                               return "Logout " + principal.getName();
+                       }
+
+                       public void run() {
+                               try {
+                                       Subject subject = SecureRapActivator.getLoginContext()
+                                                       .getSubject();
+                                       String subjectStr = subject.toString();
+                                       SecureRapActivator.getLoginContext().logout();
+                                       log.info(subjectStr + " logged out");
+                               } catch (LoginException e) {
+                                       log.error("Error when logging out", e);
+                               }
+                               try {
+                                       RWT.getRequest().getSession().setMaxInactiveInterval(1);
+                                       PlatformUI.getWorkbench().close();
+                               } catch (Exception e) {
+                                       if (log.isTraceEnabled())
+                                               log.trace("Error when invalidating session", e);
+                               }
+                       }
+
+               };
+               register(logoutAction);
 
-               // Save semantiocs
+               // Save semantics
                saveAction = ActionFactory.SAVE.create(window);
                register(saveAction);
                saveAllAction = ActionFactory.SAVE_ALL.create(window);
@@ -73,7 +123,7 @@ public class SecureActionBarAdvisor extends ActionBarAdvisor {
                fileMenu.add(closeAllAction);
                fileMenu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
                fileMenu.add(new Separator());
-               fileMenu.add(exitAction);
+               fileMenu.add(logoutAction);
 
                // Edit
                editMenu.add(preferences);