The Expeditor desktop client may prompt the user to login to the platform. This is sometimes referred to as platform SSO on the wiki. It may be required that custom application code only execute after the login process is complete. In such a scenario, how can code know when the login process is complete?
The ILoginContextEventListener type allows implementors to receive notification pertaining to the login process. Expeditor developers can register ILoginContextEventListener listeners to receive the logged in event and subsequently execute the necessary code.
Add the com.ibm.rcp.security.auth plugin as a dependency within your plugin. The following class demonstrates a listener that responds to login events by writing to the platform's logger.
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.security.auth.login.LoginException;
import org.eclipse.ui.PlatformUI;
import com.ibm.rcp.security.auth.events.ILoginContextEventListener;
import com.ibm.rcp.security.auth.events.LoginContextEvent;
import com.ibm.rcp.security.auth.login.CanceledLoginException;
public class PlatformLoginListener implements ILoginContextEventListener {
private static Logger logger = Logger.getLogger(PlatformLoginListener.class
.getName());
@Override
public void handleLogin(LoginContextEvent event) {
handleEvent(event, true);
}
@Override
public void handleLogout(LoginContextEvent event) {
handleEvent(event, false);
};
private void handleEvent(LoginContextEvent event, boolean login) {
String process = login ? "Login" : "Logout";
if (event.type == LoginContextEvent.MethodBegin) {
logger.finer(process + " starting");
} else if (event.type == LoginContextEvent.MethodEnd
&& event.hasException == false) {
logger.fine(process + " success");
} else if (event.type == LoginContextEvent.MethodEnd
&& event.hasException == true) {
// there was an exception
// was this canceled or another reason
boolean wasCanceled = false;
// if it was canceled, one of the nested exceptions will be a
// CanceledLoginException
Throwable cause = event.exception.getCause();
while (cause != null) {
if (cause instanceof CanceledLoginException) {
// found a CanceledLoginException, so login must have been
// canceled
wasCanceled = true;
logger.finer(process + " canceled");
// if cancelled login, close
if (login && !PlatformUI.getWorkbench().close()) {
System.err
.println("Logout cancelled; Could not close workbench");
System.exit(-1);
}
}
cause = cause.getCause();
}
if (!wasCanceled) {
// no cancellation, so another reason
LoginException e = event.exception;
logger.log(Level.SEVERE, process + " failed with exception", e);
}
}
}
}
After implementing your ILoginContextEventListener, register it with the platform. For example, the following code registers the listener within the plugin's Activator class.
Because Eclipse plugins are lazily loaded, the platform should automatically register the listener code as early as possible. This can be done using the com.ibm.rcp.lifecycle.application extension. Plugins that contribute to the extension are loaded when the platform starts.
The above extension starts the com.ibm.rcp.support.platform.login plugin that contains the above ILoginContextEventListener implementation when the platform loads. Since the ILoginContextEventListener is added within the plugin's start() method, the code is executed when the com.ibm.rcp.lifecycle.application.startBundles extension point is processed.