001/** 002 * GRANITE DATA SERVICES 003 * Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S. 004 * 005 * This file is part of the Granite Data Services Platform. 006 * 007 * Granite Data Services is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * Granite Data Services is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 015 * General Public License for more details. 016 * 017 * You should have received a copy of the GNU Lesser General Public 018 * License along with this library; if not, write to the Free Software 019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 020 * USA, or see <http://www.gnu.org/licenses/>. 021 */ 022package org.granite.hibernate.jmf; 023 024import java.io.IOException; 025import java.util.Collection; 026import java.util.Map; 027import java.util.SortedMap; 028import java.util.SortedSet; 029 030import org.granite.messaging.jmf.ExtendedObjectInput; 031import org.granite.messaging.jmf.ExtendedObjectOutput; 032import org.granite.messaging.jmf.JMFConstants; 033import org.granite.messaging.jmf.codec.ExtendedObjectCodec; 034import org.granite.messaging.jmf.persistence.JMFPersistentCollectionSnapshot; 035import org.granite.messaging.persistence.PersistentCollectionSnapshot; 036import org.hibernate.collection.PersistentCollection; 037 038/** 039 * @author Franck WOLFF 040 */ 041public abstract class AbstractPersistentCollectionCodec<H extends PersistentCollection> implements ExtendedObjectCodec { 042 043 protected final Class<H> hibernateCollectionClass; 044 protected final String clientCollectionClassName; 045 046 public AbstractPersistentCollectionCodec(Class<H> hibernateCollectionClass) { 047 this.hibernateCollectionClass = hibernateCollectionClass; 048 this.clientCollectionClassName = JMFConstants.CLIENT_PERSISTENCE_COLLECTION_PACKAGE + "." + hibernateCollectionClass.getSimpleName(); 049 } 050 051 public boolean canEncode(ExtendedObjectOutput out, Object v) { 052 return v.getClass() == hibernateCollectionClass; 053 } 054 055 public String getEncodedClassName(ExtendedObjectOutput out, Object v) { 056 return clientCollectionClassName; 057 } 058 059 public void encode(ExtendedObjectOutput out, Object v) throws IOException, IllegalAccessException { 060 JMFPersistentCollectionSnapshot snapshot = null; 061 062 PersistentCollection collection = (PersistentCollection)v; 063 if (!collection.wasInitialized()) 064 snapshot = new JMFPersistentCollectionSnapshot(collection instanceof SortedSet || collection instanceof SortedMap, null); 065 else if (collection instanceof Map) 066 snapshot = new JMFPersistentCollectionSnapshot(true, null, collection.isDirty(), (Map<?, ?>)collection); 067 else 068 snapshot = new JMFPersistentCollectionSnapshot(true, null, collection.isDirty(), (Collection<?>)collection); 069 070 snapshot.writeExternal(out); 071 } 072 073 public boolean canDecode(ExtendedObjectInput in, String className) { 074 return clientCollectionClassName.equals(className); 075 } 076 077 public String getDecodedClassName(ExtendedObjectInput in, String className) { 078 return hibernateCollectionClass.getName(); 079 } 080 081 @SuppressWarnings("unchecked") 082 public void decode(ExtendedObjectInput in, Object v) throws IOException, ClassNotFoundException, IllegalAccessException { 083 PersistentCollection collection = (PersistentCollection)v; 084 if (collection.wasInitialized()) { 085 boolean sorted = (collection instanceof SortedSet || collection instanceof SortedMap); 086 PersistentCollectionSnapshot snapshot = new JMFPersistentCollectionSnapshot(sorted, null); 087 snapshot.readCoreData(in); 088 089 if (collection instanceof Map) 090 ((Map<Object, Object>)collection).putAll(snapshot.getElementsAsMap()); 091 else 092 ((Collection<Object>)collection).addAll(snapshot.getElementsAsCollection()); 093 094 if (snapshot.isDirty()) 095 collection.dirty(); 096 else 097 collection.clearDirty(); 098 } 099 } 100}