Coverage Report - org.acegisecurity.event.authentication.LoggerListener
 
Classes in this File Line Coverage Branch Coverage Complexity
LoggerListener
75% 
100% 
2.667
 
 1  
 /* Copyright 2004, 2005, 2006 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  
 import org.springframework.context.ApplicationEvent;
 21  
 import org.springframework.context.ApplicationListener;
 22  
 import org.springframework.util.ClassUtils;
 23  
 
 24  
 
 25  
 /**
 26  
  * Outputs authentication-related application events to Commons Logging.<P>All authentication events are logged at
 27  
  * the warning level.</p>
 28  
  *
 29  
  * @author Ben Alex
 30  
  * @version $Id: LoggerListener.java 1845 2007-05-23 06:31:32Z benalex $
 31  
  */
 32  20
 public class LoggerListener implements ApplicationListener {
 33  
     //~ Static fields/initializers =====================================================================================
 34  
 
 35  2
     private static final Log logger = LogFactory.getLog(LoggerListener.class);
 36  
     
 37  
     /** If set to true, {@link InteractiveAuthenticationSuccessEvent} will be logged (defaults to true) */
 38  20
     private boolean logInteractiveAuthenticationSuccessEvents = true;
 39  
     
 40  
     //~ Methods ========================================================================================================
 41  
 
 42  
     public void onApplicationEvent(ApplicationEvent event) {
 43  24
         if (event instanceof AbstractAuthenticationEvent) {
 44  1
             AbstractAuthenticationEvent authEvent = (AbstractAuthenticationEvent) event;
 45  
             
 46  1
             if (!logInteractiveAuthenticationSuccessEvents && authEvent instanceof InteractiveAuthenticationSuccessEvent) {
 47  0
                     return;
 48  
             }
 49  
 
 50  1
             if (logger.isWarnEnabled()) {
 51  1
                 String message = "Authentication event " + ClassUtils.getShortName(authEvent.getClass()) + ": "
 52  
                     + authEvent.getAuthentication().getName() + "; details: "
 53  
                     + authEvent.getAuthentication().getDetails();
 54  
 
 55  1
                 if (event instanceof AbstractAuthenticationFailureEvent) {
 56  1
                     message = message + "; exception: "
 57  
                         + ((AbstractAuthenticationFailureEvent) event).getException().getMessage();
 58  
                 }
 59  
 
 60  1
                 logger.warn(message);
 61  
             }
 62  
         }
 63  24
     }
 64  
 
 65  
         public boolean isLogInteractiveAuthenticationSuccessEvents() {
 66  0
                 return logInteractiveAuthenticationSuccessEvents;
 67  
         }
 68  
 
 69  
         public void setLogInteractiveAuthenticationSuccessEvents(
 70  
                         boolean logInteractiveAuthenticationSuccessEvents) {
 71  0
                 this.logInteractiveAuthenticationSuccessEvents = logInteractiveAuthenticationSuccessEvents;
 72  0
         }
 73  
 }