package com.onaro.sanscreen.client.error; import java.util.HashSet; import java.util.ResourceBundle; import java.util.Set; import javax.ejb.EJBAccessException; import javax.security.auth.login.FailedLoginException; import javax.swing.JOptionPane; import javax.swing.SwingUtilities; import com.onaro.commons.lang.OnaroExceptionUtils; import com.onaro.sanscreen.client.FrameDirector; import com.onaro.sanscreen.client.MainDirector; import com.onaro.sanscreen.client.Messages; import com.onaro.sanscreen.client.login.LoginHandler; /** * Handles failure to access unauthorized services in the server. When such an It provide the user with the options to * login as another user, to ignore the error (and try again later) or to exit the application. */ public class UnauthorizedHandler extends MessageWindowHandler { //possible known exception causes static final Set> knownSecurityExceptions = new HashSet>(); static { knownSecurityExceptions.add(SecurityException.class); knownSecurityExceptions.add(FailedLoginException.class); knownSecurityExceptions.add(EJBAccessException.class); } static final class LoginAction extends MessageWindowHandler.Action { private static final long serialVersionUID = 1L; public LoginAction() { putValue(Action.NAME, Messages.INSTANCE.getLoginActionText()); } @Override public boolean doAction(String shortDescription, Throwable exception) { boolean loggedIn = LoginHandler.showDialog(); if (!loggedIn) return true; //todo: move this functionality somewhere else final FrameDirector frameDirector = MainDirector.getFrameDirector(); if (frameDirector != null) { SwingUtilities.invokeLater(new Runnable() { public void run() { frameDirector.showCurrentView(); } }); } return true; } } /** * Gets the messages and titles from the resources. Set the options to "exit", "ignore" or "login". * * @param bundle * locale dependent resources */ public UnauthorizedHandler(ResourceBundle bundle) { super(bundle.getString("unauthorized.relogin.title"), bundle.getString("unauthorized.relogin.message"), //$NON-NLS-1$ //$NON-NLS-2$ JOptionPane.WARNING_MESSAGE); addActionIgnore(); addAction(new LoginAction()); } /** * Handle the error if the exception is any kind of SecurityException. When checking for a match, * the chain of causes is also searched. * */ @Override public boolean handle(String shortDescription, Throwable exception) { for (Class knownSecurityException : knownSecurityExceptions) { if (OnaroExceptionUtils.isCause(exception, knownSecurityException)) { return super.handle(shortDescription, exception); } } return false; } @Override protected boolean isLogOnly() { return false; } }