MtasUpdateRequestProcessorResultReader.java.html 9.38 KB
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>MtasUpdateRequestProcessorResultReader.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">MTAS</a> &gt; <a href="index.source.html" class="el_package">mtas.solr.update.processor</a> &gt; <span class="el_source">MtasUpdateRequestProcessorResultReader.java</span></div><h1>MtasUpdateRequestProcessorResultReader.java</h1><pre class="source lang-java linenums">package mtas.solr.update.processor;

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Iterator;
import java.util.NoSuchElementException;

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

/**
 * The Class MtasUpdateRequestProcessorResultReader.
 */
public class MtasUpdateRequestProcessorResultReader implements Closeable {

  /** The Constant log. */
<span class="fc" id="L20">  private static final Log log = LogFactory</span>
<span class="fc" id="L21">      .getLog(MtasUpdateRequestProcessorResultReader.class);</span>

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

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

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

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

  /** The iterator. */
  private Iterator&lt;MtasUpdateRequestProcessorResultItem&gt; iterator;

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

  /**
   * Instantiates a new mtas update request processor result reader.
   *
   * @param fileName the file name
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public MtasUpdateRequestProcessorResultReader(String fileName)
<span class="fc" id="L48">      throws IOException {</span>
<span class="fc" id="L49">    file = null;</span>
<span class="fc" id="L50">    fileInputStream = null;</span>
<span class="fc" id="L51">    objectInputStream = null;</span>
<span class="fc" id="L52">    closed = false;</span>
<span class="fc" id="L53">    iterator = null;</span>
<span class="pc bpc" id="L54" title="1 of 2 branches missed.">    if (fileName != null) {</span>
<span class="fc" id="L55">      file = new File(fileName);</span>
<span class="fc" id="L56">      fileInputStream = new FileInputStream(file);</span>
<span class="fc" id="L57">      objectInputStream = new ObjectInputStream(fileInputStream);</span>
      try {
<span class="fc" id="L59">        Object o = objectInputStream.readObject();</span>
<span class="pc bpc" id="L60" title="1 of 2 branches missed.">        if (o instanceof String) {</span>
<span class="fc" id="L61">          storedStringValue = (String) o;</span>
        } else {
<span class="nc" id="L63">          throw new IOException(&quot;invalid tokenStream&quot;);</span>
        }
<span class="fc" id="L65">        iterator = new Iterator&lt;MtasUpdateRequestProcessorResultItem&gt;() {</span>
<span class="fc" id="L66">          MtasUpdateRequestProcessorResultItem next = null;</span>

          @Override
          public boolean hasNext() {
<span class="pc bpc" id="L70" title="1 of 2 branches missed.">            if (!closed) {</span>
<span class="fc bfc" id="L71" title="All 2 branches covered.">              if (next != null) {</span>
<span class="fc" id="L72">                return true;</span>
              } else {
<span class="fc" id="L74">                next = getNext();                </span>
<span class="fc bfc" id="L75" title="All 2 branches covered.">                return next != null;</span>
              }
            } else {
<span class="nc" id="L78">              return false;</span>
            }
          }

          @Override
          public MtasUpdateRequestProcessorResultItem next() {
<span class="pc bpc" id="L84" title="1 of 2 branches missed.">            if (!closed) {</span>
              MtasUpdateRequestProcessorResultItem result;
<span class="pc bpc" id="L86" title="1 of 2 branches missed.">              if (next != null) {</span>
<span class="fc" id="L87">                result = next;</span>
<span class="fc" id="L88">                next = null;</span>
<span class="fc" id="L89">                return result;</span>
              } else {
<span class="nc" id="L91">                next = getNext();</span>
<span class="nc bnc" id="L92" title="All 2 branches missed.">                if (next != null) {</span>
<span class="nc" id="L93">                  result = next;</span>
<span class="nc" id="L94">                  next = null;</span>
<span class="nc" id="L95">                  return result;</span>
                } else {
<span class="nc" id="L97">                  throw new NoSuchElementException();</span>
                }
              }
            } else {
<span class="nc" id="L101">              throw new NoSuchElementException();</span>
            }
          }

          private MtasUpdateRequestProcessorResultItem getNext() {
<span class="pc bpc" id="L106" title="1 of 2 branches missed.">            if (!closed) {</span>
              try {                
<span class="fc" id="L108">                Object o = objectInputStream.readObject();</span>
<span class="pc bpc" id="L109" title="1 of 2 branches missed.">                if (o instanceof MtasUpdateRequestProcessorResultItem) {</span>
<span class="fc" id="L110">                  return (MtasUpdateRequestProcessorResultItem) o;</span>
                } else {
<span class="nc" id="L112">                  forceClose();</span>
<span class="nc" id="L113">                  return null;</span>
                }
<span class="fc" id="L115">              } catch (ClassNotFoundException | IOException e) {</span>
<span class="fc" id="L116">                log.debug(e.getClass().getSimpleName()</span>
                    + &quot; while retrieving data from &quot; + fileName, e);
<span class="fc" id="L118">                forceClose();</span>
<span class="fc" id="L119">                return null;</span>
              }
            } else {
<span class="nc" id="L122">              return null;</span>
            }
          }
        };
<span class="nc" id="L126">      } catch (IOException e) {</span>
<span class="nc" id="L127">        log.error(e.getClass().getSimpleName() + &quot; while processing &quot; + fileName</span>
<span class="nc" id="L128">            + &quot; (&quot; + e.getMessage() + &quot;)&quot;, e);</span>
<span class="nc" id="L129">        forceClose();</span>
<span class="nc" id="L130">        throw new IOException(e.getMessage());</span>
<span class="nc" id="L131">      } catch (ClassNotFoundException e) {</span>
<span class="nc" id="L132">        log.error(e.getClass().getSimpleName() + &quot; while processing &quot; + fileName</span>
<span class="nc" id="L133">            + &quot; (&quot; + e.getMessage() + &quot;)&quot;, e);</span>
<span class="nc" id="L134">        forceClose();</span>
<span class="nc" id="L135">        throw new IOException(&quot;invalid tokenStream&quot;);</span>
<span class="fc" id="L136">      }</span>
    }

<span class="fc" id="L139">  }</span>

  /**
   * Gets the stored string value.
   *
   * @return the stored string value
   */
  public String getStoredStringValue() {
<span class="fc" id="L147">    return storedStringValue;</span>
  }

  /**
   * Gets the stored bin value.
   *
   * @return the stored bin value
   */
  public byte[] getStoredBinValue() {    
<span class="fc" id="L156">    return null;</span>
  }

  /**
   * Gets the iterator.
   *
   * @return the iterator
   */
  public Iterator&lt;MtasUpdateRequestProcessorResultItem&gt; getIterator() {
<span class="fc" id="L165">    return iterator;</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see java.io.Closeable#close()
   */
  @Override
  public void close() throws IOException {
<span class="nc" id="L175">    forceClose();</span>
<span class="nc" id="L176">  }</span>

  /**
   * Force close.
   */
  private void forceClose() {
<span class="pc bpc" id="L182" title="1 of 2 branches missed.">    if (file != null) {</span>
<span class="pc bpc" id="L183" title="3 of 6 branches missed.">      if (file.exists() &amp;&amp; file.canWrite() &amp;&amp; !file.delete()) {</span>
<span class="nc" id="L184">        log.debug(&quot;couldn't delete &quot; + file.getName());</span>
      }
<span class="fc" id="L186">      file = null;</span>
    }
    try {
<span class="fc" id="L189">      objectInputStream.close();</span>
<span class="nc" id="L190">    } catch (IOException e) {</span>
<span class="nc" id="L191">      log.debug(e);</span>
<span class="fc" id="L192">    }</span>
<span class="fc" id="L193">    closed = true;</span>
<span class="fc" id="L194">  }</span>

}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.7.9.201702052155</span></div></body></html>