MtasUpdateRequestProcessorResultReader.java.html 9.35 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="nc" id="L20">  private static final Log log = LogFactory</span>
<span class="nc" 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="nc" id="L48">      throws IOException {</span>
<span class="nc" id="L49">    file = null;</span>
<span class="nc" id="L50">    fileInputStream = null;</span>
<span class="nc" id="L51">    objectInputStream = null;</span>
<span class="nc" id="L52">    closed = false;</span>
<span class="nc" id="L53">    iterator = null;</span>
<span class="nc bnc" id="L54" title="All 2 branches missed.">    if (fileName != null) {</span>
<span class="nc" id="L55">      file = new File(fileName);</span>
<span class="nc" id="L56">      fileInputStream = new FileInputStream(file);</span>
<span class="nc" id="L57">      objectInputStream = new ObjectInputStream(fileInputStream);</span>
      try {
<span class="nc" id="L59">        Object o = objectInputStream.readObject();</span>
<span class="nc bnc" id="L60" title="All 2 branches missed.">        if (o instanceof String) {</span>
<span class="nc" id="L61">          storedStringValue = (String) o;</span>
        } else {
<span class="nc" id="L63">          throw new IOException(&quot;invalid tokenStream&quot;);</span>
        }
<span class="nc" id="L65">        iterator = new Iterator&lt;MtasUpdateRequestProcessorResultItem&gt;() {</span>
<span class="nc" id="L66">          MtasUpdateRequestProcessorResultItem next = null;</span>

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

          @Override
          public MtasUpdateRequestProcessorResultItem next() {
<span class="nc bnc" id="L84" title="All 2 branches missed.">            if (!closed) {</span>
              MtasUpdateRequestProcessorResultItem result;
<span class="nc bnc" id="L86" title="All 2 branches missed.">              if (next != null) {</span>
<span class="nc" id="L87">                result = next;</span>
<span class="nc" id="L88">                next = null;</span>
<span class="nc" 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="nc bnc" id="L106" title="All 2 branches missed.">            if (!closed) {</span>
              try {
<span class="nc" id="L108">                Object o = objectInputStream.readObject();</span>
<span class="nc bnc" id="L109" title="All 2 branches missed.">                if (o instanceof MtasUpdateRequestProcessorResultItem) {</span>
<span class="nc" id="L110">                  return (MtasUpdateRequestProcessorResultItem) o;</span>
                } else {
<span class="nc" id="L112">                  forceClose();</span>
<span class="nc" id="L113">                  return null;</span>
                }
<span class="nc" id="L115">              } catch (ClassNotFoundException | IOException e) {</span>
<span class="nc" id="L116">                log.debug(e.getClass().getSimpleName()</span>
                    + &quot; while retrieving data from &quot; + fileName, e);
<span class="nc" id="L118">                forceClose();</span>
<span class="nc" 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="nc" id="L136">      }</span>
    }

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

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

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

  /**
   * Gets the iterator.
   *
   * @return the iterator
   */
  public Iterator&lt;MtasUpdateRequestProcessorResultItem&gt; getIterator() {
<span class="nc" 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="nc bnc" id="L182" title="All 2 branches missed.">    if (file != null) {</span>
<span class="nc bnc" id="L183" title="All 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="nc" id="L186">      file = null;</span>
    }
    try {
<span class="nc" 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="nc" id="L192">    }</span>
<span class="nc" id="L193">    closed = true;</span>
<span class="nc" 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>