package yi.util.io.input;

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 DIU_Ser extends DIU implements Configurable{
  public String FILE_POSTFIX = ".ser";

  private static DIU_Ser m_singleton = null;

  public int LOG_LEVEL = 1;

  protected DIU_Ser() {
    Configuration.configParameters(this);
  }

  public static DIU getSingleton() {
    if (m_singleton == null) m_singleton = new DIU_Ser();
    return m_singleton;
  }

  public Object input(String sourceName, String producerName, String dataName, Class c) {
    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, "Read Serialized object from: " + name);
      ObjectInputStream in = new ObjectInputStream(
        new BufferedInputStream(new FileInputStream(name)));
      Object obj = in.readObject();
      in.close();
      return obj;
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
    return null;
  }

  public static Object input(String file_name) throws IOException, ClassNotFoundException{
    ObjectInputStream in = new ObjectInputStream(
      new BufferedInputStream(new FileInputStream(file_name)));
    Object obj = in.readObject();
    in.close();
    return obj;
  }

  public String[] getParameterNames() {
    return new String[] {
      "LOG_LEVEL"
    };
  }
  public static void resetIndex() {
    getSingleton().m_inputInstances.clear();
  }
}