View Javadoc

1   /* Copyright 2004, 2005 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.acegisecurity.event.authentication;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  
21  import org.springframework.context.ApplicationEvent;
22  import org.springframework.context.ApplicationListener;
23  
24  import org.springframework.util.ClassUtils;
25  
26  
27  /***
28   * Outputs authentication-related application events to Commons Logging.
29   * 
30   * <P>
31   * All authentication events are logged at the warning level.
32   * </p>
33   *
34   * @author Ben Alex
35   * @version $Id: LoggerListener.java,v 1.3 2005/12/02 12:14:38 benalex Exp $
36   */
37  public class LoggerListener implements ApplicationListener {
38      //~ Static fields/initializers =============================================
39  
40      private static final Log logger = LogFactory.getLog(LoggerListener.class);
41  
42      //~ Methods ================================================================
43  
44      public void onApplicationEvent(ApplicationEvent event) {
45          if (event instanceof AbstractAuthenticationEvent) {
46              AbstractAuthenticationEvent authEvent = (AbstractAuthenticationEvent) event;
47  
48              if (logger.isWarnEnabled()) {
49                  String message = "Authentication event "
50                      + ClassUtils.getShortName(authEvent.getClass()) + ": "
51                      + authEvent.getAuthentication().getName() + "; details: "
52                      + authEvent.getAuthentication().getDetails();
53  
54                  if (event instanceof AbstractAuthenticationFailureEvent) {
55                      message = message + "; exception: "
56                          + ((AbstractAuthenticationFailureEvent) event).getException()
57                             .getMessage();
58                  }
59  
60                  logger.warn(message);
61              }
62          }
63      }
64  }