X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;ds=sidebyside;f=security%2Fplugins%2Forg.argeo.security.ui.rcp%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fsecurity%2Fui%2Frcp%2FAbstractSecureApplication.java;h=75184cbab6110c7848a040285dd78870bc00c0cf;hb=cd50e3711d3b86921f11d9e021fc6a43bef0d400;hp=2a9dd0c2506d7f01033d27e30469e5cb07a084cb;hpb=0aa1c816931dfce599fb6855eb216d743c1583b2;p=lgpl%2Fargeo-commons.git diff --git a/security/plugins/org.argeo.security.ui.rcp/src/main/java/org/argeo/security/ui/rcp/AbstractSecureApplication.java b/security/plugins/org.argeo.security.ui.rcp/src/main/java/org/argeo/security/ui/rcp/AbstractSecureApplication.java index 2a9dd0c25..75184cbab 100644 --- a/security/plugins/org.argeo.security.ui.rcp/src/main/java/org/argeo/security/ui/rcp/AbstractSecureApplication.java +++ b/security/plugins/org.argeo.security.ui.rcp/src/main/java/org/argeo/security/ui/rcp/AbstractSecureApplication.java @@ -1,3 +1,18 @@ +/* + * Copyright (C) 2007-2012 Argeo GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.argeo.security.ui.rcp; import java.security.PrivilegedAction; @@ -7,96 +22,122 @@ import javax.security.auth.login.LoginException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.argeo.eclipse.ui.dialogs.Error; -import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.Status; +import org.argeo.OperatingSystem; import org.eclipse.equinox.app.IApplication; import org.eclipse.equinox.app.IApplicationContext; -import org.eclipse.jface.dialogs.ErrorDialog; +import org.eclipse.equinox.security.auth.ILoginContext; import org.eclipse.swt.widgets.Display; import org.eclipse.ui.IWorkbench; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.application.WorkbenchAdvisor; /** - * Common base class for authenticated access to the Eclipse UI framework (RAP - * and RCP) + * RCP workbench initialization */ public abstract class AbstractSecureApplication implements IApplication { + final static String NODE_REPO_URI = "argeo.node.repo.uri"; + private static final Log log = LogFactory .getLog(AbstractSecureApplication.class); - protected abstract WorkbenchAdvisor createWorkbenchAdvisor(); + protected WorkbenchAdvisor createWorkbenchAdvisor(String username) { + return new SecureWorkbenchAdvisor(username); + } public Object start(IApplicationContext context) throws Exception { + // wait for the system to be initialized + // try { + // Thread.sleep(3000); + // } catch (Exception e2) { + // // silent + // } + + boolean remote = System.getProperty(NODE_REPO_URI) != null; + + // choose login context + final ILoginContext loginContext; + if (remote) { + loginContext = SecureApplicationActivator + .createLoginContext(SecureApplicationActivator.CONTEXT_REMOTE); + } else { + if (OperatingSystem.os == OperatingSystem.WINDOWS) + loginContext = SecureApplicationActivator + .createLoginContext(SecureApplicationActivator.CONTEXT_WINDOWS); + else + loginContext = SecureApplicationActivator + .createLoginContext(SecureApplicationActivator.CONTEXT_NIX); + } + + final Display display = PlatformUI.createDisplay(); - Integer returnCode = null; - Display display = PlatformUI.createDisplay(); + // login + Subject subject = null; try { - Subject subject = null; - Boolean retry = true; - while (retry) { - try { - SecureApplicationActivator.getLoginContext().login(); - subject = SecureApplicationActivator.getLoginContext() - .getSubject(); - retry = false; - } catch (LoginException e) { - Error.show("Cannot login", e); - retry = true; - } catch (Exception e) { - Error.show("Unexpected exception while trying to login", e); - retry = false; - } + loginContext.login(); + subject = loginContext.getSubject(); + } catch (LoginException e) { + log.error("Error when logging in.", e); + display.dispose(); + try { + Thread.sleep(2000); + } catch (InterruptedException e1) { + // silent } + return null; + } - if (subject == null) { - // IStatus status = new Status(IStatus.ERROR, - // "org.argeo.security.application", "Login is mandatory", - // loginException); - // ErrorDialog.openError(null, "Error", "Shutdown...", status); - // return status.getSeverity(); + // identify after successful login + if (log.isDebugEnabled()) + log.debug("subject=" + subject); + final String username = subject.getPrincipals().iterator().next() + .getName(); + if (log.isDebugEnabled()) + log.debug(username + " logged in"); +// display.disposeExec(new Runnable() { +// public void run() { +// log.debug("Display disposed"); +// logout(loginContext, username); +// } +// }); - // TODO: log as anonymous - } + try { + PrivilegedAction privilegedAction = new PrivilegedAction() { + public Object run() { + int result = PlatformUI.createAndRunWorkbench(display, + createWorkbenchAdvisor(username)); + return new Integer(result); + } + }; - if (subject != null) { - returnCode = (Integer) Subject.doAs(subject, - getRunAction(display)); - SecureApplicationActivator.getLoginContext().logout(); - return processReturnCode(returnCode); - } else { - return -1; - } + Integer returnCode = (Integer) Subject.doAs(subject, + privilegedAction); + logout(loginContext, username); + return processReturnCode(returnCode); } catch (Exception e) { - // e.printStackTrace(); - IStatus status = new Status(IStatus.ERROR, - "org.argeo.security.rcp", "Login failed", e); - ErrorDialog.openError(null, "Error", "Shutdown...", status); - return returnCode; + if (subject != null) + logout(loginContext, username); + log.error("Unexpected error", e); } finally { display.dispose(); } + return null; } protected Integer processReturnCode(Integer returnCode) { - return returnCode; - } - - @SuppressWarnings("rawtypes") - private PrivilegedAction getRunAction(final Display display) { - return new PrivilegedAction() { - - public Object run() { - int result = createAndRunWorkbench(display); - return new Integer(result); - } - }; + if (returnCode == PlatformUI.RETURN_RESTART) + return IApplication.EXIT_RESTART; + else + return IApplication.EXIT_OK; } - protected Integer createAndRunWorkbench(Display display) { - return PlatformUI.createAndRunWorkbench(display, - createWorkbenchAdvisor()); + static void logout(ILoginContext secureContext, String username) { + try { + secureContext.logout(); + log.info("Logged out " + (username != null ? username : "") + + " (THREAD=" + Thread.currentThread().getId() + ")"); + } catch (LoginException e) { + log.error("Erorr when logging out", e); + } } public void stop() { @@ -120,10 +161,6 @@ public abstract class AbstractSecureApplication implements IApplication { if (log.isDebugEnabled()) log.debug("workbench stopped"); - // String username = CurrentUser.getUsername(); - // if (log.isDebugEnabled()) - // log.debug("workbench stopped, logged in as " + username); - } }