001/*
002 * GWTEventService
003 * Copyright (c) 2011 and beyond, strawbill UG (haftungsbeschr?nkt)
004 *
005 * This is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU Lesser General Public License as
007 * published by the Free Software Foundation; either version 3 of
008 * the License, or (at your option) any later version.
009 * Other licensing for GWTEventService may also be possible on request.
010 * Please view the license.txt of the project for more information.
011 *
012 * This software 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
015 * Lesser 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 software; if not, write to the Free
019 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021 */
022package de.novanic.eventservice.service.connection.strategy.connector.streaming;
023
024import java.io.Serializable;
025
026import com.google.gwt.user.client.rpc.IsSerializable;
027import com.google.gwt.user.client.rpc.SerializationException;
028import com.google.gwt.user.server.rpc.SerializationPolicy;
029import com.google.gwt.user.server.rpc.impl.SerializabilityUtil;
030
031/**
032 * The {@link de.novanic.eventservice.service.connection.strategy.connector.streaming.EventSerializationPolicy} is required
033 * to serialize events (could implement IsSerializable or Serializable).
034 *
035 * @author sstrohschein
036 *         <br>Date: 16.03.2010
037 *         <br>Time: 23:14:40
038 */
039public class EventSerializationPolicy extends SerializationPolicy
040{
041    /**
042     * Returns true when the class is serializable (see {@link de.novanic.eventservice.service.connection.strategy.connector.streaming.EventSerializationPolicy#isValid(Class)}).
043     * @param aClass class to check
044     * @return true when the class is serializable (see {@link de.novanic.eventservice.service.connection.strategy.connector.streaming.EventSerializationPolicy#isValid(Class)}).
045     */
046        public boolean shouldDeserializeFields(Class<?> aClass) {
047                return isValid(aClass);
048        }
049
050    /**
051     * Returns true when the class is serializable (see {@link de.novanic.eventservice.service.connection.strategy.connector.streaming.EventSerializationPolicy#isValid(Class)}).
052     * @param aClass class to check
053     * @return true when the class is serializable (see {@link de.novanic.eventservice.service.connection.strategy.connector.streaming.EventSerializationPolicy#isValid(Class)}).
054     */
055        public boolean shouldSerializeFields(Class<?> aClass) {
056                return isValid(aClass);
057        }
058
059    /**
060     * There is no implementation of that method for {@link de.novanic.eventservice.service.connection.strategy.connector.streaming.EventSerializationPolicy}
061     * @param aClass class to check
062     * @throws SerializationException
063     */
064        public void validateDeserialize(Class<?> aClass) throws SerializationException {}
065
066    /**
067     * There is no implementation of that method for {@link de.novanic.eventservice.service.connection.strategy.connector.streaming.EventSerializationPolicy}
068     * @param aClass class to check
069     * @throws SerializationException
070     */
071        public void validateSerialize(Class<?> aClass) throws SerializationException {}
072
073    /**
074     * Checks if the class is serializable (when the class extends from IsSerializable or Serializable, has a custom field serializer,
075     * is a primitive or is an array which fulfills these conditions).
076     * @param aClass class to check
077     * @return true when the class is serializable
078     */
079        private boolean isValid(Class<?> aClass) {
080        if(aClass.isArray()) {
081                        return isValid(aClass.getComponentType());
082                }
083        return aClass.isPrimitive() ||
084                Serializable.class.isAssignableFrom(aClass) || IsSerializable.class.isAssignableFrom(aClass) ||
085                SerializabilityUtil.hasCustomFieldSerializer(aClass) != null;
086    }
087}