Example usage for org.springframework.context ApplicationContext publishEvent

List of usage examples for org.springframework.context ApplicationContext publishEvent

Introduction

In this page you can find the example usage for org.springframework.context ApplicationContext publishEvent.

Prototype

default void publishEvent(ApplicationEvent event) 

Source Link

Document

Notify all matching listeners registered with this application of an application event.

Usage

From source file:org.springframework.richclient.security.support.DefaultApplicationSecurityManager.java

/**
 * Process a login attempt and fire all related events. If the authentication fails,
 * then a {@link AuthenticationFailedEvent} is published and the exception is
 * rethrown. If the authentication succeeds, then an {@link AuthenticationEvent} is
 * published, followed by a {@link LoginEvent}.
 * /*from   w w w  .  ja va2 s  .c o m*/
 * @param authentication token to use for the login attempt
 * @return Authentication token resulting from a successful call to
 *         {@link AuthenticationManager#authenticate(org.springframework.security.Authentication)}.
 * @see org.springframework.richclient.security.ApplicationSecurityManager#doLogin(org.springframework.security.Authentication)
 * @throws SpringSecurityException If the authentication attempt fails
 */
public Authentication doLogin(Authentication authentication) {
    final ApplicationContext appCtx = Application.instance().getApplicationContext();

    Authentication result = null;

    try {
        result = getAuthenticationManager().authenticate(authentication);
    } catch (SpringSecurityException e) {
        logger.info("authentication failed: " + e.getMessage());

        // Fire application event to advise of failed login
        appCtx.publishEvent(new AuthenticationFailedEvent(authentication, e));

        // rethrow the exception
        throw e;
    }

    // Handle success or failure of the authentication attempt
    if (logger.isDebugEnabled()) {
        logger.debug("successful login - update context holder and fire event");
    }

    // Commit the successful Authentication object to the secure ContextHolder
    SecurityContextHolder.getContext().setAuthentication(result);
    setAuthentication(result);

    // Fire application events to advise of new login
    appCtx.publishEvent(new AuthenticationEvent(result));
    appCtx.publishEvent(new LoginEvent(result));

    return result;
}

From source file:org.springframework.richclient.security.support.DefaultApplicationSecurityManager.java

/**
 * Perform a logout. Set the current authentication token to null (in both the
 * per-thread security context and the global context), then publish an
 * {@link AuthenticationEvent} followed by a {@link LogoutEvent}.
 * @return Authentication token that was in place prior to the logout.
 * @see org.springframework.richclient.security.ApplicationSecurityManager#doLogout()
 *//*  w ww  .  j  a  va  2 s  . c om*/
public Authentication doLogout() {
    Authentication existing = getAuthentication();

    // Make the Authentication object null if a SecureContext exists
    SecurityContextHolder.getContext().setAuthentication(null);
    setAuthentication(null);

    // Fire application event to advise of logout
    ApplicationContext appCtx = Application.instance().getApplicationContext();
    appCtx.publishEvent(new AuthenticationEvent(null));
    appCtx.publishEvent(new LogoutEvent(existing));

    return existing;
}