1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2002-2009 Oracle. All rights reserved. 5 * 6 * $Id$ 7 */ 8 9package com.sleepycat.persist.impl; 10 11import com.sleepycat.persist.model.EntityModel; 12import com.sleepycat.persist.evolve.Converter; 13import com.sleepycat.persist.raw.RawObject; 14 15/** 16 * Reader for invoking a class Converter mutation. 17 * 18 * @author Mark Hayes 19 */ 20public class ConverterReader implements Reader { 21 22 private static final long serialVersionUID = -305788321064984348L; 23 24 private Converter converter; 25 private transient Format oldFormat; 26 27 ConverterReader(Converter converter) { 28 this.converter = converter; 29 } 30 31 public void initializeReader(Catalog catalog, 32 EntityModel model, 33 int initVersion, 34 Format oldFormat) { 35 this.oldFormat = oldFormat; 36 } 37 38 public Object newInstance(EntityInput input, boolean rawAccess) { 39 /* Create the old format RawObject. */ 40 return oldFormat.newInstance(input, true); 41 } 42 43 public void readPriKey(Object o, EntityInput input, boolean rawAccess) { 44 /* Read the old format RawObject's primary key. */ 45 oldFormat.readPriKey(o, input, true); 46 } 47 48 public Object readObject(Object o, EntityInput input, boolean rawAccess) { 49 Catalog catalog = input.getCatalog(); 50 51 /* Read the old format RawObject and convert it. */ 52 boolean currentRawMode = input.setRawAccess(true); 53 try { 54 o = oldFormat.readObject(o, input, true); 55 } finally { 56 input.setRawAccess(currentRawMode); 57 } 58 o = converter.getConversion().convert(o); 59 60 /* Convert the current format RawObject to a live Object. */ 61 if (!rawAccess && o instanceof RawObject) { 62 o = catalog.convertRawObject((RawObject) o, null); 63 } 64 return o; 65 } 66} 67