MtasUpdateRequestProcessorResultWriter.java

  1. package mtas.solr.update.processor;

  2. import java.io.Closeable;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.ObjectOutputStream;

  7. import org.apache.commons.logging.Log;
  8. import org.apache.commons.logging.LogFactory;
  9. import org.apache.lucene.util.BytesRef;

  10. /**
  11.  * The Class MtasUpdateRequestProcessorResultWriter.
  12.  */
  13. public class MtasUpdateRequestProcessorResultWriter implements Closeable {

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

  17.   /** The object output stream. */
  18.   private ObjectOutputStream objectOutputStream;

  19.   /** The file output stream. */
  20.   private FileOutputStream fileOutputStream;

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

  23.   /** The token number. */
  24.   private int tokenNumber;

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

  27.   /**
  28.    * Instantiates a new mtas update request processor result writer.
  29.    *
  30.    * @param value the value
  31.    */
  32.   public MtasUpdateRequestProcessorResultWriter(String value) {
  33.     closed = false;
  34.     tokenNumber = 0;
  35.     file = null;
  36.     fileOutputStream = null;
  37.     objectOutputStream = null;
  38.     try {
  39.       file = File.createTempFile("MtasUpdateRequestProcessorResult", ".data");
  40.       fileOutputStream = new FileOutputStream(file);
  41.       objectOutputStream = new ObjectOutputStream(fileOutputStream);
  42.       objectOutputStream.writeObject(value);
  43.     } catch (IOException e) {
  44.       forceCloseAndDelete();
  45.       log.debug(e);
  46.     }
  47.   }

  48.   /**
  49.    * Adds the item.
  50.    *
  51.    * @param term the term
  52.    * @param offsetStart the offset start
  53.    * @param offsetEnd the offset end
  54.    * @param posIncr the pos incr
  55.    * @param payload the payload
  56.    * @param flags the flags
  57.    */
  58.   public void addItem(String term, Integer offsetStart, Integer offsetEnd,
  59.       Integer posIncr, BytesRef payload, Integer flags) {
  60.     if (!closed) {
  61.       tokenNumber++;
  62.       MtasUpdateRequestProcessorResultItem item = new MtasUpdateRequestProcessorResultItem(
  63.           term, offsetStart, offsetEnd, posIncr, payload, flags);
  64.       try {
  65.         objectOutputStream.writeObject(item);
  66.         objectOutputStream.reset();
  67.         objectOutputStream.flush();
  68.       } catch (IOException e) {
  69.         forceCloseAndDelete();
  70.         log.debug(e);
  71.       }
  72.     }
  73.   }

  74.   /**
  75.    * Gets the token number.
  76.    *
  77.    * @return the token number
  78.    */
  79.   public int getTokenNumber() {
  80.     return tokenNumber;
  81.   }

  82.   /**
  83.    * Gets the file name.
  84.    *
  85.    * @return the file name
  86.    * @throws IOException Signals that an I/O exception has occurred.
  87.    */
  88.   public String getFileName() throws IOException {
  89.     if (file != null) {
  90.       return file.getAbsolutePath();
  91.     } else {
  92.       throw new IOException("no file");
  93.     }
  94.   }

  95.   /*
  96.    * (non-Javadoc)
  97.    *
  98.    * @see java.io.Closeable#close()
  99.    */
  100.   @Override
  101.   public void close() throws IOException {
  102.     if (!closed) {
  103.       objectOutputStream.close();
  104.       fileOutputStream.close();
  105.       closed = true;
  106.     }
  107.   }

  108.   /**
  109.    * Force close and delete.
  110.    */
  111.   public void forceCloseAndDelete() {
  112.     try {
  113.       if (objectOutputStream != null) {
  114.         objectOutputStream.close();
  115.         objectOutputStream = null;
  116.       }
  117.       if (fileOutputStream != null) {
  118.         fileOutputStream.close();
  119.         fileOutputStream = null;
  120.       }
  121.     } catch (IOException e) {
  122.       log.debug(e);
  123.     }
  124.     closed = true;
  125.     tokenNumber = 0;
  126.     if (file != null) {
  127.       if (file.exists() && file.canWrite() && !file.delete()) {
  128.         log.debug("couldn't delete " + file.getName());
  129.       }
  130.       file = null;
  131.     }
  132.   }

  133. }