Package jfix.db4o.engine

Source Code of jfix.db4o.engine.PersistenceEngineXStream

/*
    Copyright (C) 2010 maik.jablonski@gmail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package jfix.db4o.engine;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.io.FileUtils;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
* This implemenation is ONLY intended for debug-purposes.
*
* @author mjablonski
*/
public class PersistenceEngineXStream implements PersistenceEngine {

  protected String directory;
  protected String filename;
  protected Set objects;
  protected Set objectsToSave;
  protected Set objectsToDelete;

  public void open(String database) {
    initDirectory(database);
    openEngine();
  }

  protected void initDirectory(String database) {
    if (database.contains(File.separator)) {
      directory = database.endsWith(File.separator) ? database : database + File.separator;
    } else {
      directory = System.getProperty("user.home") + File.separator
          + "xstream" + File.separator + database + File.separator;
    }
    filename = directory + "odb.xml";
    new File(directory).mkdirs();
  }

  protected void openEngine() {
    objects = new HashSet();
    objectsToSave = new HashSet();
    objectsToDelete = new HashSet();
  }

  public String getBlobDirectory() {
    return directory;
  }

  public Collection query() {
    try {
      if (new File(filename).exists()) {
        XStream xstream = new XStream(new DomDriver());
        xstream.setMode(XStream.ID_REFERENCES);
        FileReader reader = new FileReader(filename);
        objects = (Set) xstream.fromXML(reader);
        reader.close();
      }
      return objects;
    } catch (Exception e) {
      e.printStackTrace();
      throw new RuntimeException(e);
    }
  }

  public void save(Object object) {
    objectsToSave.add(object);
  }

  public void delete(Object object) {
    objectsToDelete.add(object);
  }

  public void commit() {
    objects.addAll(objectsToSave);
    objects.removeAll(objectsToDelete);
    objectsToSave.clear();
    objectsToDelete.clear();
  }

  public void rollback() {
    objectsToSave.clear();
    objectsToDelete.clear();
  }

  public void backup() {
    try {
      String backupFilename = filename
          + new SimpleDateFormat("-yyyyMMdd").format(new Date());
      FileUtils.copyFile(new File(filename), new File(backupFilename));
    } catch (IOException e) {
      e.printStackTrace();
      throw new RuntimeException(e);
    }
  }

  public void close() {
    try {
      XStream xstream = new XStream(new DomDriver());
      xstream.setMode(XStream.ID_REFERENCES);
      FileWriter writer = new FileWriter(filename);
      xstream.toXML(objects, writer);
      writer.close();
    } catch (Exception e) {
      e.printStackTrace();
      throw new RuntimeException(e);
    }
  }

  public String toString() {
    return filename;
  }
}
TOP

Related Classes of jfix.db4o.engine.PersistenceEngineXStream

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.