package org.oskari.print.loader;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;

import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;

/**
 * HystrixCommand that loads BufferedImage from URL
 * Retries up to 3 times
 */
public class CommandLoadImageFromURL extends CommandLoadImageBase {

    private static final Logger LOG = LogFactory.getLogger(CommandLoadImageFromURL.class);
    private static final int RETRY_COUNT = 3;
    
    private final String uri;

    protected CommandLoadImageFromURL(String commandName, String uri) {
        super(commandName);
        this.uri = uri;
    }

    @Override
    public BufferedImage run() throws Exception {
        LOG.info(uri);
        URL url = new URL(uri);
        for (int i = 0; i < RETRY_COUNT - 1; i++) {
            try {
                return ImageIO.read(url);
            } catch (IOException e) {
                LOG.warn(e);
                // Sleep for a tiny moment between retries
                Thread.sleep(50);
            }
        }
        return ImageIO.read(url);
    }

    @Override
    public BufferedImage getFallback() {
        return null;
    }

}

@@ -507,16 +507,76 @@
 NT = 3;%0A
+    private static final int SLEEP_BETWEEN_RETRIES_MS = 50;%0A
     %0A   
@@ -815,16 +815,39 @@
 OG.info(
+%22Loading image from:%22, 
 uri);%0A  
@@ -872,24 +872,58 @@
 w URL(uri);%0A
+        IOException toLog = null;%0A
         for 
@@ -1080,19 +1080,17 @@
     
-LOG.warn(e)
+toLog = e
 ;%0A  
@@ -1122,13 +1122,8 @@
 r a 
-tiny 
 mome
@@ -1170,18 +1170,40 @@
 d.sleep(
-50
+SLEEP_BETWEEN_RETRIES_MS
 );%0A     
@@ -1213,32 +1213,136 @@
     %7D%0A        %7D%0A
+        // Only log the last failed attempt%0A        LOG.warn(toLog, %22Failed to load image from:%22, uri);%0A
         return I

Update logging statements