KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > base > login > IpanemaLoginHandler


1 /*
2  * Created on Oct 4, 2004
3  * by Alexander Bieber
4  *
5  */

6 package com.nightlabs.ipanema.base.login;
7
8 import java.lang.reflect.InvocationTargetException JavaDoc;
9
10 import javax.security.auth.login.LoginException JavaDoc;
11 import javax.swing.SwingUtilities JavaDoc;
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 /**
20  * @see com.nightlabs.ipanema.base.login.ILoginHandler
21  *
22  * @author Alexander Bieber
23  */

24 public class IpanemaLoginHandler implements ILoginHandler {
25     public static final Logger LOGGER = Logger.getLogger(IpanemaLoginHandler.class);
26
27     /**
28      * Opens an instance of {@link LoginDialog}.
29      * The dialog sets the loginResult and loginContext values.
30      * A login verification is performed to be sure the user can
31      * be identified by the credentials he specified.
32      * @see com.nightlabs.ipanema.base.login.ILoginHandler#handleLogin(com.nightlabs.ipanema.base.login.IpanemaLoginContext)
33      * @see LoginDialog
34      */

35     public void handleLogin(IpanemaLoginContext loginContext, LoginConfigModule loginConfigModule, Login.AsyncLoginResult loginResult) throws LoginException JavaDoc {
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 JavaDoc {
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 does all the work
51
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 JavaDoc
59     {
60         final SplashLoginPanel loginPanel = new SplashLoginPanel(loginContext, loginConfigModule);
61         loginPanel.doLayout();
62         Object JavaDoc mutex = new Object JavaDoc();
63         try {
64             SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
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 JavaDoc e) {
74             throw new LoginException JavaDoc("Error in SplashLogin: "+e.getMessage());
75         } catch (InvocationTargetException JavaDoc e) {
76             throw new LoginException JavaDoc("Error in SplashLogin: "+e.getMessage());
77         }
78         boolean loggedIn = false;
79         int loginTries = 0;
80         // Wait for the login
81
while ((!loggedIn) && (loginTries < 3)) {
82             try {
83                 synchronized (SplashScreen.getMutex()) {
84                     SplashScreen.getMutex().wait();
85                 }
86             } catch (InterruptedException JavaDoc e) {
87                 throw new LoginException JavaDoc("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 JavaDoc(){
101                 public void run() {
102                     if ((!loginResult.isWasAuthenticationErr()) && (loginResult.isSuccess()))
103                         return;
104                     else {
105                         // login failed
106
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 JavaDoc message = loginResult.getMessage();
114                             if (loginResult.getException() != null) {
115                                 message += "\n"+loginResult.getException().getClass().getName()+": "+loginResult.getException().getLocalizedMessage();
116                                 Throwable JavaDoc 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