    001// License: GPL. For details, see LICENSE file.
    002package org.openstreetmap.josm.plugins.streetside.io.export;
    003
    004import java.awt.image.BufferedImage;
    005import java.io.ByteArrayInputStream;
    006import java.io.IOException;
    007import java.util.concurrent.ArrayBlockingQueue;
    008
    009import javax.imageio.ImageIO;
    010
    011import org.apache.log4j.Logger;
    012import org.openstreetmap.josm.data.cache.CacheEntry;
    013import org.openstreetmap.josm.data.cache.CacheEntryAttributes;
    014import org.openstreetmap.josm.data.cache.ICachedLoaderListener;
    015import org.openstreetmap.josm.plugins.streetside.StreetsideAbstractImage;
    016import org.openstreetmap.josm.plugins.streetside.StreetsideImage;
    017import org.openstreetmap.josm.plugins.streetside.cache.CacheUtils;
    018import org.openstreetmap.josm.plugins.streetside.cache.StreetsideCache;
    019
    020/**
    021 * This is the thread that downloads one of the images that are going to be
    022 * exported and writes them in a {@link ArrayBlockingQueue}.
    023 *
    024 * @author nokutu
    025 * @see StreetsideExportManager
    026 * @see StreetsideExportWriterThread
    027 */
    028public class StreetsideExportDownloadThread extends Thread implements
    029    ICachedLoaderListener {
    030
    031  final static Logger logger = Logger.getLogger(StreetsideExportDownloadThread.class);
    032
    033  private final ArrayBlockingQueue<BufferedImage> queue;
    034  private final ArrayBlockingQueue<StreetsideAbstractImage> queueImages;
    035
    036  private final StreetsideImage image;
    037
    038  /**
    039   * Main constructor.
    040   *
    041   * @param image
    042   *          Image to be downloaded.
    043   * @param queue
    044   *          Queue of {@link BufferedImage} objects for the
    045   *          {@link StreetsideExportWriterThread}.
    046   * @param queueImages
    047   *          Queue of {@link StreetsideAbstractImage} objects for the
    048   *          {@link StreetsideExportWriterThread}.
    049   */
    050  public StreetsideExportDownloadThread(StreetsideImage image,
    051      ArrayBlockingQueue<BufferedImage> queue,
    052      ArrayBlockingQueue<StreetsideAbstractImage> queueImages) {
    053    this.queue = queue;
    054    this.image = image;
    055    this.queueImages = queueImages;
    056  }
    057
    058  @Override
    059  public void run() {
    060    //CacheUtils.submit(image.getId(), StreetsideCache.Type.FULL_IMAGE, this);
    061    CacheUtils.submit(image.getId(), StreetsideCache.Type.THUMBNAIL, this);
    062  }
    063
    064  @Override
    065  public synchronized void loadingFinished(CacheEntry data,
    066      CacheEntryAttributes attributes, LoadResult result) {
    067    try {
    068      synchronized (StreetsideExportDownloadThread.class) {
    069        queue
    070            .put(ImageIO.read(new ByteArrayInputStream(data.getContent())));
    071        queueImages.put(image);
    072      }
    073    } catch (InterruptedException | IOException e) {
    074      logger.error(e);
    075    }
    076  }
    077}