001 /**
002 * GRANITE DATA SERVICES
003 * Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004 *
005 * This file is part of Granite Data Services.
006 *
007 * Granite Data Services is free software; you can redistribute it and/or modify
008 * it under the terms of the GNU Library General Public License as published by
009 * the Free Software Foundation; either version 2 of the License, or (at your
010 * option) any later version.
011 *
012 * Granite Data Services is distributed in the hope that it will be useful, but
013 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015 * for more details.
016 *
017 * You should have received a copy of the GNU Library General Public License
018 * along with this library; if not, see <http://www.gnu.org/licenses/>.
019 */
020 package org.granite.client.messaging.jmf.ext;
021
022 import java.io.IOException;
023 import java.lang.reflect.InvocationTargetException;
024 import java.util.ArrayList;
025 import java.util.List;
026
027 import org.granite.client.persistence.Entity;
028 import org.granite.client.persistence.Persistence;
029 import org.granite.client.platform.Platform;
030 import org.granite.messaging.jmf.ExtendedObjectInput;
031 import org.granite.messaging.jmf.ExtendedObjectOutput;
032 import org.granite.messaging.jmf.codec.ExtendedObjectCodec;
033 import org.granite.messaging.reflect.Property;
034
035 /**
036 * @author Franck WOLFF
037 */
038 public class ClientEntityCodec implements ExtendedObjectCodec {
039
040 public boolean canEncode(ExtendedObjectOutput out, Object v) {
041 return v.getClass().isAnnotationPresent(Entity.class);
042 }
043
044 public String getEncodedClassName(ExtendedObjectOutput out, Object v) {
045 return out.getAlias(v.getClass().getName());
046 }
047
048 public void encode(ExtendedObjectOutput out, Object v) throws IOException, IllegalAccessException, InvocationTargetException {
049
050 Persistence persistence = Platform.persistence();
051
052 boolean initialized = persistence.isInitialized(v);
053
054 out.writeBoolean(initialized);
055 out.writeUTF(persistence.getDetachedState(v));
056
057 if (!initialized)
058 out.writeObject(persistence.getId(v));
059 else {
060 List<Property> properties = new ArrayList<Property>(out.getReflection().findSerializableProperties(v.getClass()));
061 properties.remove(persistence.getInitializedProperty(v.getClass()));
062 properties.remove(persistence.getDetachedStateProperty(v.getClass()));
063
064 for (Property property : properties)
065 out.getAndWriteProperty(v, property);
066 }
067 }
068
069 public boolean canDecode(ExtendedObjectInput in, String className) throws ClassNotFoundException {
070 String alias = in.getAlias(className);
071 Class<?> cls = in.getReflection().loadClass(alias);
072 return cls.isAnnotationPresent(Entity.class);
073 }
074
075 public String getDecodedClassName(ExtendedObjectInput in, String className) {
076 return in.getAlias(className);
077 }
078
079 public Object newInstance(ExtendedObjectInput in, String className)
080 throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException,
081 InvocationTargetException, SecurityException, NoSuchMethodException, IOException {
082
083 Class<?> cls = in.getReflection().loadClass(className);
084 return in.getReflection().newInstance(cls);
085 }
086
087 public void decode(ExtendedObjectInput in, Object v) throws IOException, ClassNotFoundException, IllegalAccessException, InvocationTargetException {
088
089 Persistence persistence = Platform.persistence();
090
091 boolean initialized = in.readBoolean();
092 String detachedState = in.readUTF();
093
094 persistence.setInitialized(v, initialized);
095 persistence.setDetachedState(v, detachedState);
096
097 if (!initialized)
098 persistence.setId(v, in.readObject());
099 else {
100 List<Property> properties = new ArrayList<Property>(in.getReflection().findSerializableProperties(v.getClass()));
101 properties.remove(persistence.getInitializedProperty(v.getClass()));
102 properties.remove(persistence.getDetachedStateProperty(v.getClass()));
103
104 for (Property property : properties)
105 in.readAndSetProperty(v, property);
106 }
107 }
108 }