1 6 package com.nightlabs.ipanema.base.login; 7 8 import java.lang.reflect.InvocationTargetException ; 9 10 import javax.security.auth.login.LoginException ; 11 import javax.swing.SwingUtilities ; 12 13 import org.apache.log4j.Logger; 14 import org.eclipse.ui.PlatformUI; 15 16 import com.nightlabs.ipanema.base.IpanemaBasePlugin; 17 import com.nightlabs.rcp.splash.SplashScreen; 18 19 24 public class IpanemaLoginHandler implements ILoginHandler { 25 public static final Logger LOGGER = Logger.getLogger(IpanemaLoginHandler.class); 26 27 35 public void handleLogin(IpanemaLoginContext loginContext, LoginConfigModule loginConfigModule, Login.AsyncLoginResult loginResult) throws LoginException { 36 if (SplashScreen.isSplashVisible()) 37 handleSplashLogin(loginContext, loginConfigModule, loginResult); 38 else 39 handleSWTLogin(loginContext, loginConfigModule, loginResult); 40 } 41 42 protected void handleSWTLogin(IpanemaLoginContext loginContext, LoginConfigModule loginConfigModule, Login.AsyncLoginResult loginResult) throws LoginException { 43 LoginDialog loginDialog = new LoginDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()); 44 LoginDialog.registerSharedInstance(loginDialog); 45 try { 46 loginDialog.setLoginResult(loginResult); 47 loginDialog.setLoginModule(loginConfigModule); 48 loginDialog.setLoginContext(loginContext); 49 50 loginDialog.open(); 52 } 53 finally { 54 LoginDialog.deregisterSharedInstance(); 55 } 56 } 57 58 protected void handleSplashLogin(IpanemaLoginContext loginContext, LoginConfigModule loginConfigModule, final Login.AsyncLoginResult loginResult) throws LoginException 59 { 60 final SplashLoginPanel loginPanel = new SplashLoginPanel(loginContext, loginConfigModule); 61 loginPanel.doLayout(); 62 Object mutex = new Object (); 63 try { 64 SwingUtilities.invokeAndWait(new Runnable () { 65 public void run() { 66 SplashScreen.setSplashPanel(loginPanel); 67 SplashScreen.setProgressIndeterminite(false); 68 SplashScreen.setProgressMinMax(0,1); 69 SplashScreen.setProgressValue(0); 70 SplashScreen.setSplashMessage(""); 71 } 72 }); 73 } catch (InterruptedException e) { 74 throw new LoginException ("Error in SplashLogin: "+e.getMessage()); 75 } catch (InvocationTargetException e) { 76 throw new LoginException ("Error in SplashLogin: "+e.getMessage()); 77 } 78 boolean loggedIn = false; 79 int loginTries = 0; 80 while ((!loggedIn) && (loginTries < 3)) { 82 try { 83 synchronized (SplashScreen.getMutex()) { 84 SplashScreen.getMutex().wait(); 85 } 86 } catch (InterruptedException e) { 87 throw new LoginException ("Caught InterruptedException while waiting for login: "+e.getMessage()); 88 } 89 loginPanel.assignLoginValues(); 90 SplashScreen.setSplashMessage("Try to log in ..."); 91 Login.AsyncLoginResult testResult = Login.testLogin(loginContext); 92 testResult.copyValuesTo(loginResult); 93 loggedIn = testResult.isSuccess(); 94 if (loggedIn) { 95 SplashScreen.setSplashMessage("Login successful"); 96 break; 97 } 98 loginTries++; 99 100 SwingUtilities.invokeLater(new Runnable (){ 101 public void run() { 102 if ((!loginResult.isWasAuthenticationErr()) && (loginResult.isSuccess())) 103 return; 104 else { 105 if (loginResult.isWasAuthenticationErr()) { 107 loginPanel.setErrMessage(IpanemaBasePlugin.getResourceString("login.error.authenticationFailed")); 108 } 109 else if (loginResult.isWasCommunicationErr()) { 110 loginPanel.setErrMessage(IpanemaBasePlugin.getResourceString("login.error.communicatinError")+" "+loginResult.getException().getMessage()); 111 } 112 else { 113 String message = loginResult.getMessage(); 114 if (loginResult.getException() != null) { 115 message += "\n"+loginResult.getException().getClass().getName()+": "+loginResult.getException().getLocalizedMessage(); 116 Throwable cause = loginResult.getException().getCause(); 117 while ( cause != null ) { 118 message += "\n"+cause.getClass().getName()+": "+cause.getLocalizedMessage(); 119 cause = cause.getCause(); 120 } 121 loginResult.getException().printStackTrace(); 122 } 123 loginPanel.setErrMessage(message); 124 125 } 126 } 127 } 128 }); 129 } 130 SplashScreen.resetSplashPanel(); 131 } 132 133 } 134 | Popular Tags |