package yi.util.io.output;

import java.util.*;
import java.io.*;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.mapping.Mapping;
import yi.util.log.*;
import yi.util.config.*;

public class DOU_Ser extends DOU implements Configurable{

  private static DOU_Ser m_singleton = null;

  public int LOG_LEVEL = 1;

  protected DOU_Ser() {
    Configuration.configParameters(this);
    FILE_POSTFIX = ".ser";
  }

  public static DOU getSingleton() {
    if (m_singleton == null) m_singleton = new DOU_Ser();
    return m_singleton;
  }

  public String getOutputFileName(String sourceName, String producerName, String dataName) {
    return getInstanceName(sourceName + CONNECT_CHAR + producerName +
      CONNECT_CHAR + dataName, false) + FILE_POSTFIX;
  }

  public void output(String sourceName, String producerName, String dataName, Object obj) {
    String name = getInstanceName(sourceName +
                                  ((sourceName.length() > 0)? Character.toString(CONNECT_CHAR) : "") +
                                  producerName +
                                  ((producerName.length() > 0)? Character.toString(CONNECT_CHAR) : "") +
                                  dataName) + FILE_POSTFIX;

    try {
      LogHandler.log(this, LOG_LEVEL, 3, "Serialize object to: " + name);
      ObjectOutputStream out = new ObjectOutputStream(
        new BufferedOutputStream(new FileOutputStream(name)));
      out.writeObject(obj);
      out.close();
    }
    catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static void output(String file_name, Object obj) throws IOException{
    ObjectOutputStream out = new ObjectOutputStream(
      new BufferedOutputStream(new FileOutputStream(file_name)));
    out.writeObject(obj);
    out.close();
  }

  public String[] getParameterNames() {
    return new String[] {
      "LOG_LEVEL"
    };
  }
}