159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/*
259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Copyright (c) 2009-2010 jMonkeyEngine
359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * All rights reserved.
459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Redistribution and use in source and binary forms, with or without
659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * modification, are permitted provided that the following conditions are
759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * met:
859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions of source code must retain the above copyright
1059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   notice, this list of conditions and the following disclaimer.
1159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
1259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions in binary form must reproduce the above copyright
1359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   notice, this list of conditions and the following disclaimer in the
1459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   documentation and/or other materials provided with the distribution.
1559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
1659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
1759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   may be used to endorse or promote products derived from this software
1859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   without specific prior written permission.
1959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
2059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
2459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
2759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
2859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */
3259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
3359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapackage com.jme3.network.serializing;
3459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
3559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.Vector3f;
3659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.network.message.ChannelInfoMessage;
3759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.network.message.ClientRegistrationMessage;
3859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.network.message.DisconnectMessage;
3959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.network.message.GZIPCompressedMessage;
4059b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.network.message.ZIPCompressedMessage;
4159b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.network.serializing.serializers.*;
4259b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.beans.beancontext.BeanContextServicesSupport;
4359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.beans.beancontext.BeanContextSupport;
4459b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.io.File;
4559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.io.IOException;
4659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.net.URL;
4759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.nio.ByteBuffer;
4859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.*;
4959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.jar.Attributes;
5059b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.logging.Level;
5159b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.logging.Logger;
5259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
5359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/**
5459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * The main serializer class, which will serialize objects such that
5559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *  they can be sent across the network. Serializing classes should extend
5659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *  this to provide their own serialization.
5759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
5859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @author Lars Wesselius
5959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */
6059b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapublic abstract class Serializer {
6159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    protected static final Logger log = Logger.getLogger(Serializer.class.getName());
6259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
6359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private static final SerializerRegistration NULL_CLASS = new SerializerRegistration( null, Void.class, (short)-1 );
6459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
6559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private static final Map<Short, SerializerRegistration> idRegistrations         = new HashMap<Short, SerializerRegistration>();
6659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private static final Map<Class, SerializerRegistration> classRegistrations      = new HashMap<Class, SerializerRegistration>();
6759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
6859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private static final Serializer                         fieldSerializer         = new FieldSerializer();
6959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private static final Serializer                         serializableSerializer  = new SerializableSerializer();
7059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private static final Serializer                         arraySerializer         = new ArraySerializer();
7159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
7259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private static short                                    nextId                  = -1;
7359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
7459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private static boolean strictRegistration = true;
7559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
7659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /****************************************************************
7759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     ****************************************************************
7859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     ****************************************************************
7959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
8059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        READ THIS BEFORE CHANGING ANYTHING BELOW
8159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
8259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        If a registration is moved or removed before the
8359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        ClientRegistrationMessage then it screws up the application's
8459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        ability to gracefully warn users about bad versions.
8559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
8659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        There really needs to be a version rolled into the protocol
8759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        and I intend to do that very soon.  In the mean time, don't
8859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        edit the static registrations without decrementing nextId
8959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        appropriately.
9059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
9159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Yes, that's how fragile this is.  Live and learn.
9259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
9359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     ****************************************************************
9459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     ****************************************************************
9559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     ****************************************************************/
9659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
9759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
9859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    // Registers the classes we already have serializers for.
9959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    static {
10059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(boolean.class,   new BooleanSerializer());
10159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(byte.class,      new ByteSerializer());
10259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(char.class,      new CharSerializer());
10359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(short.class,     new ShortSerializer());
10459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(int.class,       new IntSerializer());
10559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(long.class,      new LongSerializer());
10659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(float.class,     new FloatSerializer());
10759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(double.class,    new DoubleSerializer());
10859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
10959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(Boolean.class,   new BooleanSerializer());
11059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(Byte.class,      new ByteSerializer());
11159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(Character.class, new CharSerializer());
11259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(Short.class,     new ShortSerializer());
11359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(Integer.class,   new IntSerializer());
11459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(Long.class,      new LongSerializer());
11559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(Float.class,     new FloatSerializer());
11659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(Double.class,    new DoubleSerializer());
11759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(String.class,    new StringSerializer());
11859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
11959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(Vector3f.class,  new Vector3Serializer());
12059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
12159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(Date.class,      new DateSerializer());
12259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
12359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        // all the Collection classes go here
12459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(AbstractCollection.class,         new CollectionSerializer());
12559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(AbstractList.class,               new CollectionSerializer());
12659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(AbstractSet.class,                new CollectionSerializer());
12759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(ArrayList.class,                  new CollectionSerializer());
12859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(BeanContextServicesSupport.class, new CollectionSerializer());
12959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(BeanContextSupport.class,         new CollectionSerializer());
13059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(HashSet.class,                    new CollectionSerializer());
13159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(LinkedHashSet.class,              new CollectionSerializer());
13259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(LinkedList.class,                 new CollectionSerializer());
13359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(TreeSet.class,                    new CollectionSerializer());
13459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(Vector.class,                     new CollectionSerializer());
13559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
13659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        // All the Map classes go here
13759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(AbstractMap.class,                new MapSerializer());
13859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(Attributes.class,                 new MapSerializer());
13959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(HashMap.class,                    new MapSerializer());
14059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(Hashtable.class,                  new MapSerializer());
14159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(IdentityHashMap.class,            new MapSerializer());
14259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(TreeMap.class,                    new MapSerializer());
14359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(WeakHashMap.class,                new MapSerializer());
14459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
14559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(Enum.class,      new EnumSerializer());
14659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(GZIPCompressedMessage.class, new GZIPSerializer());
14759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(ZIPCompressedMessage.class, new ZIPSerializer());
14859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
14959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(DisconnectMessage.class);
15059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(ClientRegistrationMessage.class);
15159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        registerClass(ChannelInfoMessage.class);
15259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
15359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
15459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
15559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     *  When set to true, classes that do not have intrinsic IDs in their
15659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     *  @Serializable will not be auto-registered during write.  Defaults
15759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     *  to true since this is almost never desired behavior with the way
15859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     *  this code works.  Set to false to get the old permissive behavior.
15959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
16059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public static void setStrictRegistration( boolean b ) {
16159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        strictRegistration = b;
16259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
16359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
16459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public static SerializerRegistration registerClass(Class cls) {
16559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return registerClass(cls, true);
16659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
16759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
16859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public static void registerClasses(Class... classes) {
16959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        for( Class c : classes ) {
17059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            registerClass(c);
17159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
17259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
17359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
17459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
17559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     *  Registers the specified class. The failOnMiss flag controls whether or
17659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     *  not this method returns null for failed registration or throws an exception.
17759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
17859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    @SuppressWarnings("unchecked")
17959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public static SerializerRegistration registerClass(Class cls, boolean failOnMiss) {
18059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (cls.isAnnotationPresent(Serializable.class)) {
18159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            Serializable serializable = (Serializable)cls.getAnnotation(Serializable.class);
18259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
18359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            Class serializerClass = serializable.serializer();
18459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            short classId = serializable.id();
18559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            if (classId == 0) classId = --nextId;
18659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
18759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            Serializer serializer = getSerializer(serializerClass, false);
18859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
18959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            if (serializer == null) serializer = fieldSerializer;
19059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
19159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            SerializerRegistration existingReg = getExactSerializerRegistration(cls);
19259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
19359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            if (existingReg != null) classId = existingReg.getId();
19459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            SerializerRegistration reg = new SerializerRegistration(serializer, cls, classId);
19559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
19659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            idRegistrations.put(classId, reg);
19759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            classRegistrations.put(cls, reg);
19859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
19959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            serializer.initialize(cls);
20059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
20159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            log.log( Level.INFO, "Registered class[" + classId + "]:{0}.", cls );
20259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            return reg;
20359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
20459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (failOnMiss) {
20559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            throw new IllegalArgumentException( "Class is not marked @Serializable:" + cls );
20659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
20759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return null;
20859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
20959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
21059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
21159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     *  @deprecated This cannot be implemented in a reasonable way that works in
21259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     *              all deployment methods.
21359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
21459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    @Deprecated
21559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public static SerializerRegistration[] registerPackage(String pkgName) {
21659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        try {
21759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
21859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            String path = pkgName.replace('.', '/');
21959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            Enumeration<URL> resources = classLoader.getResources(path);
22059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            List<File> dirs = new ArrayList<File>();
22159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            while (resources.hasMoreElements()) {
22259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                URL resource = resources.nextElement();
22359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                dirs.add(new File(resource.getFile()));
22459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            }
22559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            ArrayList<Class> classes = new ArrayList<Class>();
22659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            for (File directory : dirs) {
22759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                classes.addAll(findClasses(directory, pkgName));
22859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            }
22959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
23059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            SerializerRegistration[] registeredClasses = new SerializerRegistration[classes.size()];
23159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            for (int i = 0; i != classes.size(); ++i) {
23259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                Class clz = classes.get(i);
23359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                registeredClasses[i] = registerClass(clz, false);
23459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            }
23559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            return registeredClasses;
23659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        } catch (Exception e) {
23759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            e.printStackTrace();
23859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
23959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return new SerializerRegistration[0];
24059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
24159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
24259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private static List<Class> findClasses(File dir, String pkgName) throws ClassNotFoundException {
24359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        List<Class> classes = new ArrayList<Class>();
24459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (!dir.exists()) {
24559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            return classes;
24659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
24759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        File[] files = dir.listFiles();
24859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        for (File file : files) {
24959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            if (file.isDirectory()) {
25059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                assert !file.getName().contains(".");
25159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                classes.addAll(findClasses(file, pkgName + "." + file.getName()));
25259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            } else if (file.getName().endsWith(".class")) {
25359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                classes.add(Class.forName(pkgName + '.' + file.getName().substring(0, file.getName().length() - 6)));
25459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            }
25559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
25659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return classes;
25759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
25859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
25959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public static SerializerRegistration registerClass(Class cls, Serializer serializer) {
26059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        SerializerRegistration existingReg = getExactSerializerRegistration(cls);
26159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
26259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        short id;
26359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (existingReg != null) {
26459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            id = existingReg.getId();
26559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        } else {
26659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            id = --nextId;
26759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
26859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        SerializerRegistration reg = new SerializerRegistration(serializer, cls, id);
26959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
27059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        idRegistrations.put(id, reg);
27159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        classRegistrations.put(cls, reg);
27259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
27359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        log.log( Level.INFO, "Registered class[" + id + "]:{0} to:" + serializer, cls );
27459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
27559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        serializer.initialize(cls);
27659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
27759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return reg;
27859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
27959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
28059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public static Serializer getExactSerializer(Class cls) {
28159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return classRegistrations.get(cls).getSerializer();
28259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
28359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
28459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public static Serializer getSerializer(Class cls) {
28559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return getSerializer(cls, true);
28659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
28759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
28859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public static Serializer getSerializer(Class cls, boolean failOnMiss) {
28959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return getSerializerRegistration(cls, failOnMiss).getSerializer();
29059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
29159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
29259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public static SerializerRegistration getExactSerializerRegistration(Class cls) {
29359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return classRegistrations.get(cls);
29459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
29559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
29659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public static SerializerRegistration getSerializerRegistration(Class cls) {
29759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return getSerializerRegistration(cls, strictRegistration);
29859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
29959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
30059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    @SuppressWarnings("unchecked")
30159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public static SerializerRegistration getSerializerRegistration(Class cls, boolean failOnMiss) {
30259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        SerializerRegistration reg = classRegistrations.get(cls);
30359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
30459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (reg != null) return reg;
30559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
30659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        for (Map.Entry<Class, SerializerRegistration> entry : classRegistrations.entrySet()) {
30759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            if (entry.getKey().isAssignableFrom(Serializable.class)) continue;
30859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            if (entry.getKey().isAssignableFrom(cls)) return entry.getValue();
30959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
31059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
31159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (cls.isArray()) return registerClass(cls, arraySerializer);
31259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
31359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (Serializable.class.isAssignableFrom(cls)) {
31459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            return getExactSerializerRegistration(java.io.Serializable.class);
31559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
31659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
31759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        // See if the class could be safely auto-registered
31859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (cls.isAnnotationPresent(Serializable.class)) {
31959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            Serializable serializable = (Serializable)cls.getAnnotation(Serializable.class);
32059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            short classId = serializable.id();
32159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            if( classId != 0 ) {
32259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                // No reason to fail because the ID is fixed
32359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                failOnMiss = false;
32459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            }
32559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
32659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
32759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if( failOnMiss ) {
32859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            throw new IllegalArgumentException( "Class has not been registered:" + cls );
32959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
33059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return registerClass(cls, fieldSerializer);
33159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
33259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
33359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
33459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    ///////////////////////////////////////////////////////////////////////////////////
33559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
33659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
33759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
33859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * Read the class from given buffer and return its SerializerRegistration.
33959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     *
34059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param buffer The buffer to read from.
34159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @return The SerializerRegistration, or null if non-existent.
34259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
34359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public static SerializerRegistration readClass(ByteBuffer buffer) {
34459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        short classID = buffer.getShort();
34559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (classID == -1) return NULL_CLASS;
34659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return idRegistrations.get(classID);
34759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
34859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
34959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
35059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * Read the class and the object.
35159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     *
35259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param buffer Buffer to read from.
35359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @return The Object that was read.
35459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @throws IOException If serialization failed.
35559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
35659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    @SuppressWarnings("unchecked")
35759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public static Object readClassAndObject(ByteBuffer buffer) throws IOException {
35859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        SerializerRegistration reg = readClass(buffer);
35959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (reg == NULL_CLASS) return null;
36059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (reg == null) throw new SerializerException( "Class not found for buffer data." );
36159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return reg.getSerializer().readObject(buffer, reg.getType());
36259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
36359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
36459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
36559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * Write a class and return its SerializerRegistration.
36659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     *
36759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param buffer The buffer to write the given class to.
36859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param type The class to write.
36959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @return The SerializerRegistration that's registered to the class.
37059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
37159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public static SerializerRegistration writeClass(ByteBuffer buffer, Class type) throws IOException {
37259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        SerializerRegistration reg = getSerializerRegistration(type);
37359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (reg == null) {
37459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            throw new SerializerException( "Class not registered:" + type );
37559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
37659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        buffer.putShort(reg.getId());
37759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return reg;
37859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
37959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
38059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
38159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * Write the class and object.
38259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     *
38359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param buffer The buffer to write to.
38459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param object The object to write.
38559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @throws IOException If serializing fails.
38659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
38759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public static void writeClassAndObject(ByteBuffer buffer, Object object) throws IOException {
38859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (object == null) {
38959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            buffer.putShort((short)-1);
39059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            return;
39159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
39259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        SerializerRegistration reg = writeClass(buffer, object.getClass());
39359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        reg.getSerializer().writeObject(buffer, object);
39459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
39559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
39659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
39759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * Read an object from the buffer, effectively deserializing it.
39859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     *
39959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param data The buffer to read from.
40059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param c The class of the object.
40159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @return The object read.
40259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @throws IOException If deserializing fails.
40359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
40459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public abstract <T> T readObject(ByteBuffer data, Class<T> c) throws IOException;
40559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
40659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
40759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * Write an object to the buffer, effectively serializing it.
40859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     *
40959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param buffer The buffer to write to.
41059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param object The object to serialize.
41159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @throws IOException If serializing fails.
41259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
41359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public abstract void writeObject(ByteBuffer buffer, Object object) throws IOException;
41459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
41559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
41659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * Registration for when a serializer may need to cache something.
41759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     *
41859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * Override to use.
41959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     *
42059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param clazz The class that has been registered to the serializer.
42159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
42259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void initialize(Class clazz) { }
42359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta}
424