MtasUpdateRequestProcessorResultReader.java

  1. package mtas.solr.update.processor;

  2. import java.io.Closeable;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.ObjectInputStream;
  7. import java.util.Iterator;
  8. import java.util.NoSuchElementException;

  9. import org.apache.commons.logging.Log;
  10. import org.apache.commons.logging.LogFactory;

  11. /**
  12.  * The Class MtasUpdateRequestProcessorResultReader.
  13.  */
  14. public class MtasUpdateRequestProcessorResultReader implements Closeable {

  15.   /** The Constant log. */
  16.   private static final Log log = LogFactory
  17.       .getLog(MtasUpdateRequestProcessorResultReader.class);

  18.   /** The stored string value. */
  19.   private String storedStringValue;

  20.   /** The file input stream. */
  21.   private FileInputStream fileInputStream;

  22.   /** The object input stream. */
  23.   private ObjectInputStream objectInputStream;

  24.   /** The file. */
  25.   private File file;

  26.   /** The iterator. */
  27.   private Iterator<MtasUpdateRequestProcessorResultItem> iterator;

  28.   /** The closed. */
  29.   private boolean closed;

  30.   /**
  31.    * Instantiates a new mtas update request processor result reader.
  32.    *
  33.    * @param fileName the file name
  34.    * @throws IOException Signals that an I/O exception has occurred.
  35.    */
  36.   public MtasUpdateRequestProcessorResultReader(String fileName)
  37.       throws IOException {
  38.     file = null;
  39.     fileInputStream = null;
  40.     objectInputStream = null;
  41.     closed = false;
  42.     iterator = null;
  43.     if (fileName != null) {
  44.       file = new File(fileName);
  45.       fileInputStream = new FileInputStream(file);
  46.       objectInputStream = new ObjectInputStream(fileInputStream);
  47.       try {
  48.         Object o = objectInputStream.readObject();
  49.         if (o instanceof String) {
  50.           storedStringValue = (String) o;
  51.         } else {
  52.           throw new IOException("invalid tokenStream");
  53.         }
  54.         iterator = new Iterator<MtasUpdateRequestProcessorResultItem>() {
  55.           MtasUpdateRequestProcessorResultItem next = null;

  56.           @Override
  57.           public boolean hasNext() {
  58.             if (!closed) {
  59.               if (next != null) {
  60.                 return true;
  61.               } else {
  62.                 next = getNext();
  63.                 return next != null;
  64.               }
  65.             } else {
  66.               return false;
  67.             }
  68.           }

  69.           @Override
  70.           public MtasUpdateRequestProcessorResultItem next() {
  71.             if (!closed) {
  72.               MtasUpdateRequestProcessorResultItem result;
  73.               if (next != null) {
  74.                 result = next;
  75.                 next = null;
  76.                 return result;
  77.               } else {
  78.                 next = getNext();
  79.                 if (next != null) {
  80.                   result = next;
  81.                   next = null;
  82.                   return result;
  83.                 } else {
  84.                   throw new NoSuchElementException();
  85.                 }
  86.               }
  87.             } else {
  88.               throw new NoSuchElementException();
  89.             }
  90.           }

  91.           private MtasUpdateRequestProcessorResultItem getNext() {
  92.             if (!closed) {
  93.               try {
  94.                 Object o = objectInputStream.readObject();
  95.                 if (o instanceof MtasUpdateRequestProcessorResultItem) {
  96.                   return (MtasUpdateRequestProcessorResultItem) o;
  97.                 } else {
  98.                   forceClose();
  99.                   return null;
  100.                 }
  101.               } catch (ClassNotFoundException | IOException e) {
  102.                 log.debug(e.getClass().getSimpleName()
  103.                     + " while retrieving data from " + fileName, e);
  104.                 forceClose();
  105.                 return null;
  106.               }
  107.             } else {
  108.               return null;
  109.             }
  110.           }
  111.         };
  112.       } catch (IOException e) {
  113.         log.error(e.getClass().getSimpleName() + " while processing " + fileName
  114.             + " (" + e.getMessage() + ")", e);
  115.         forceClose();
  116.         throw new IOException(e.getMessage());
  117.       } catch (ClassNotFoundException e) {
  118.         log.error(e.getClass().getSimpleName() + " while processing " + fileName
  119.             + " (" + e.getMessage() + ")", e);
  120.         forceClose();
  121.         throw new IOException("invalid tokenStream");
  122.       }
  123.     }

  124.   }

  125.   /**
  126.    * Gets the stored string value.
  127.    *
  128.    * @return the stored string value
  129.    */
  130.   public String getStoredStringValue() {
  131.     return storedStringValue;
  132.   }

  133.   /**
  134.    * Gets the stored bin value.
  135.    *
  136.    * @return the stored bin value
  137.    */
  138.   public byte[] getStoredBinValue() {
  139.     return new byte[0];
  140.   }

  141.   /**
  142.    * Gets the iterator.
  143.    *
  144.    * @return the iterator
  145.    */
  146.   public Iterator<MtasUpdateRequestProcessorResultItem> getIterator() {
  147.     return iterator;
  148.   }

  149.   /*
  150.    * (non-Javadoc)
  151.    *
  152.    * @see java.io.Closeable#close()
  153.    */
  154.   @Override
  155.   public void close() throws IOException {
  156.     forceClose();
  157.   }

  158.   /**
  159.    * Force close.
  160.    */
  161.   private void forceClose() {
  162.     if (file != null) {
  163.       if (file.exists() && file.canWrite() && !file.delete()) {
  164.         log.debug("couldn't delete " + file.getName());
  165.       }
  166.       file = null;
  167.     }
  168.     try {
  169.       objectInputStream.close();
  170.     } catch (IOException e) {
  171.       log.debug(e);
  172.     }
  173.     closed = true;
  174.   }

  175. }