+/*
+ * Copyright (C) 2007-2012 Mathieu Baudier
+ *
+ * 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;
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<Object>() {
+ 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() {
if (log.isDebugEnabled())
log.debug("workbench stopped");
- // String username = CurrentUser.getUsername();
- // if (log.isDebugEnabled())
- // log.debug("workbench stopped, logged in as " + username);
-
}
}