Package org.freezedry.persistence

Source Code of org.freezedry.persistence.AbstractPersistence

/*
* Copyright 2012 Robert Philipp
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package org.freezedry.persistence;

import java.io.Reader;
import java.io.Writer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.freezedry.persistence.readers.JsonReader;
import org.freezedry.persistence.readers.PersistenceReader;
import org.freezedry.persistence.readers.XmlReader;
import org.freezedry.persistence.tree.InfoNode;
import org.freezedry.persistence.utils.ReflectionUtils;
import org.freezedry.persistence.writers.JsonWriter;
import org.freezedry.persistence.writers.PersistenceWriter;
import org.freezedry.persistence.writers.XmlWriter;

/**
* Abstract class implementing the {@link Persistence} interface, and managing the {@link PersistenceEngine}.<p>
*
* Implementing subclasses must implement two methods:
* <ul>
*   <ol>{@link #getPersistenceReader()}</ol>
*   <ol>{@link #getPersistenceWriter()}</ol>
* </ul>
* For example, the {@link XmlPersistence} class will return {@link XmlReader} and {@link XmlWriter}
* objects, respectively. And the {@link JsonPersistence} class will return {@link JsonReader} and {@link JsonWriter}
* objects, respectively.
* @author Robert Philipp
*/
public abstract class AbstractPersistence implements Persistence {

  private static final Logger LOGGER = LoggerFactory.getLogger( AbstractPersistence.class );
 
  private final PersistenceEngine engine;

  /**
   * Default constructor for persistence of objects
   */
  public AbstractPersistence()
  {
    engine = new PersistenceEngine();
  }
 
  /*
   * (non-Javadoc)
   * @see org.freezedry.persistence.Persistence#getPersistenceEngine()
   */
  @Override
  public PersistenceEngine getPersistenceEngine()
  {
    return engine;
  }
 
  /*
   * (non-Javadoc)
   * @see org.freezedry.persistence.Persistence#write(java.lang.Object, java.io.Writer)
   */
  @Override
  public void write( final Object object, final Writer writer )
  {
    // create the semantic model
    final InfoNode rootNode = getPersistenceEngine().createSemanticModel( object );
    if( LOGGER.isInfoEnabled() )
    {
      LOGGER.info( rootNode.simpleTreeToString() );
    }

    // write out XML
    getPersistenceWriter().write( rootNode, writer );
  }
 
  /**
   * @return the {@link PersistenceWriter} specific to the subclass implementation of the
   * {@link Persistence} interface
   */
  abstract protected PersistenceWriter getPersistenceWriter();

  /*
   * (non-Javadoc)
   * @see org.freezedry.persistence.Persistence#read(java.lang.Class, java.io.Reader)
   */
  @Override
  public < T > T read( final Class< ? extends T > clazz, final Reader reader )
  {
    final InfoNode rootNode = getPersistenceReader().read( clazz, reader );
    if( LOGGER.isInfoEnabled() )
    {
      LOGGER.info( rootNode.simpleTreeToString() );
    }
   
    // grab the deserialized object and attempt to cast it to the specified class.
    // if the cast fails, check to see if we need to convert between wrapped types and
    // their primitives
    final Object object = getPersistenceEngine().parseSemanticModel( clazz, rootNode );
    return ReflectionUtils.cast( clazz, object );
  }
 
  /**
   * @return the {@link PersistenceReader} specific to the subclass implementation of the
   * {@link Persistence} interface
   */
  abstract protected PersistenceReader getPersistenceReader();
}
TOP

Related Classes of org.freezedry.persistence.AbstractPersistence

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.