/*
 * The Gemma project
 * 
 * Copyright (c) 2012 University of British Columbia
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package ubic.gemma.persistence.retry;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryContext;
import org.springframework.retry.listener.RetryListenerSupport;
import org.springframework.stereotype.Component;

/**
 * Provide logging when an operation has failed and is being retried. This would not be needed if there was better
 * logging control over the default RetryContext.
 * 
 * @author paul
 * @version $Id$
 */
@Component
public class RetryLogger extends RetryListenerSupport {

    private static Log log = LogFactory.getLog( RetryLogger.class );

    @Override
    public <T> void close( RetryContext context, RetryCallback<T> callback, Throwable throwable ) {
        log( context );
        if ( throwable != null ) log.error( throwable, throwable );
        super.close( context, callback, throwable );
    }

    @Override
    public <T> void onError( RetryContext context, RetryCallback<T> callback, Throwable throwable ) {
        synchronized ( context ) {
            log( context );
            if ( throwable != null ) log.error( throwable, throwable );
        }
        super.onError( context, callback, throwable );
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.springframework.retry.listener.RetryListenerSupport#open(org.springframework.retry.RetryContext,
     * org.springframework.retry.RetryCallback)
     */
    @Override
    public <T> boolean open( RetryContext context, RetryCallback<T> callback ) {
        log( context );
        return super.open( context, callback );
    }

    private void log( RetryContext context ) {
        if ( context.getRetryCount() > 0 ) {
            log.warn( "Retry attempt: " + context.getRetryCount() + " [ Triggered by: "
                    + context.getLastThrowable().getMessage() + "]" );
        }
    }
}

@@ -682,16 +682,73 @@
 retry;%0A%0A
+import org.apache.commons.lang.exception.ExceptionUtils;%0A
 import o
@@ -778,16 +778,16 @@
 ng.Log;%0A
-
 import o
@@ -1519,65 +1519,8 @@
 text
- );%0A        if ( throwable != null ) log.error( throwable
 , th
@@ -1769,69 +1769,8 @@
 text
- );%0A            if ( throwable != null ) log.error( throwable
 , th
@@ -2172,16 +2172,22 @@
  context
+, null
  );%0A    
@@ -2271,32 +2271,53 @@
 yContext context
+, Throwable throwable
  ) %7B%0A        if 
@@ -2438,16 +2438,16 @@
 d by: %22%0A
-
         
@@ -2508,16 +2508,310 @@
 + %22%5D%22 );
+%0A%0A            if ( throwable != null ) %7B%0A                // only log it the first time.%0A                if ( context.getRetryCount() == 1 ) %7B%0A                    log.error( %22ERROR DETAILS (logged first time only): %22 + ExceptionUtils.getStackTrace( throwable ) );%0A                %7D%0A            %7D
 %0A       

make this quieter