public Registration( SerializerRegistration reg ) { this.id = reg.getId(); this.className = reg.getType().getName(); if( reg.getSerializer().getClass() != FieldSerializer.class ) { this.serializerClassName = reg.getSerializer().getClass().getName(); } }
/** * Registers the specified class. The failOnMiss flag controls whether or * not this method returns null for failed registration or throws an exception. */ @SuppressWarnings("unchecked") public static SerializerRegistration registerClass(Class cls, boolean failOnMiss) { if (cls.isAnnotationPresent(Serializable.class)) { Serializable serializable = (Serializable)cls.getAnnotation(Serializable.class); Class serializerClass = serializable.serializer(); short classId = serializable.id(); if (classId == 0) classId = nextId(); Serializer serializer = getSerializer(serializerClass, false); if (serializer == null) serializer = fieldSerializer; SerializerRegistration existingReg = getExactSerializerRegistration(cls); if (existingReg != null) classId = existingReg.getId(); SerializerRegistration reg = new SerializerRegistration(serializer, cls, classId); return registerClassForId( classId, cls, serializer ); } if (failOnMiss) { throw new IllegalArgumentException( "Class is not marked @Serializable:" + cls ); } return null; }
/** * Write the class and object. * * @param buffer The buffer to write to. * @param object The object to write. * @throws IOException If serializing fails. */ public static void writeClassAndObject(ByteBuffer buffer, Object object) throws IOException { if (object == null) { buffer.putShort((short)-1); return; } SerializerRegistration reg = writeClass(buffer, object.getClass()); // If the caller (or us) has registered a generic base class (like Enum) // that is meant to steer automatic resolution for things like FieldSerializer // that have final classes in fields... then there are cases where the exact // type isn't known by the outer class. (Think of a message object // that has an Object field but tries to send an Enum subclass in it.) // In that case, the SerializerRegistration object we get back isn't // really going to be capable of recreating the object on the other // end because it won't know what class to use. This only comes up // in writeclassAndObejct() because we just wrote an ID to a more generic // class than will be readable on the other end. The check is simple, though. if( reg.getType() != object.getClass() ) { throw new IllegalArgumentException("Class has not been registered:" + object.getClass() + " but resolved to generic serializer for:" + reg.getType()); } reg.getSerializer().writeObject(buffer, object); }
private void writeType(ByteBuffer buffer, Class<?> clazz) throws IOException{ if (clazz == void.class){ buffer.putShort((short)0); } else { SerializerRegistration reg = Serializer.getSerializerRegistration(clazz); if (reg == null){ logger.log(Level.WARNING, "Unknown class: {0}", clazz); throw new IOException(); // prevents message from being serialized } buffer.putShort(reg.getId()); } }
public <T> T readObject( ByteBuffer data, Class<T> c ) throws IOException { // Use serializer's support for class reading/writing SerializerRegistration reg = readClass(data); if( reg == null ) { throw new SerializerException("Class not found for buffer data."); } if( reg.getId() == -1 ) { return null; } return c.cast(reg.getType()); }
private Class<?> readType(ByteBuffer buffer) throws IOException{ SerializerRegistration reg = Serializer.readClass(buffer); if (reg == null){ // either "void" or unknown val short id = buffer.getShort(buffer.position()-2); if (id == 0){ return void.class; } else{ logger.log(Level.WARNING, "Undefined class ID: {0}", id); throw new IOException(); // prevents message from being serialized } } return reg.getType(); }
public static Serializer getExactSerializer(Class cls) { return classRegistrations.get(cls).getSerializer(); }
/** * Directly registers a class for a specific ID. Generally, use the regular * registerClass() method. This method is intended for framework code that might * be maintaining specific ID maps across client and server. */ public static SerializerRegistration registerClassForId( short id, Class cls, Serializer serializer ) { if( locked ) { throw new RuntimeException("Serializer registry locked trying to register class:" + cls); } SerializerRegistration reg = new SerializerRegistration(serializer, cls, id); idRegistrations.put(id, reg); classRegistrations.put(cls, reg); log.log( Level.FINE, "Registered class[" + id + "]:{0} to:" + serializer, cls ); serializer.initialize(cls); // Add the class after so that dependency order is preserved if the // serializer registers its own classes. registrations.add(reg); return reg; }
/** * Read the class and the object. * * @param buffer Buffer to read from. * @return The Object that was read. * @throws IOException If serialization failed. */ @SuppressWarnings("unchecked") public static Object readClassAndObject(ByteBuffer buffer) throws IOException { SerializerRegistration reg = readClass(buffer); if (reg == NULL_CLASS) return null; if (reg == null) throw new SerializerException( "Class not found for buffer data." ); return reg.getSerializer().readObject(buffer, reg.getType()); }
/** * Write a class and return its SerializerRegistration. * * @param buffer The buffer to write the given class to. * @param type The class to write. * @return The SerializerRegistration that's registered to the class. */ public static SerializerRegistration writeClass(ByteBuffer buffer, Class type) throws IOException { SerializerRegistration reg = getSerializerRegistration(type); if (reg == null) { throw new SerializerException( "Class not registered:" + type ); } if( log.isLoggable(Level.FINER) ) { log.log(Level.FINER, "writing class:{0} with ID:{1}", new Object[]{type, reg.getId()}); } buffer.putShort(reg.getId()); return reg; }
public static void compile() { // Let's just see what they are here List<Registration> list = new ArrayList<Registration>(); for( SerializerRegistration reg : Serializer.getSerializerRegistrations() ) { Class type = reg.getType(); if( ignore.contains(type) ) continue; if( type.isPrimitive() ) continue; list.add(new Registration(reg)); } if( log.isLoggable(Level.FINE) ) { log.log( Level.FINE, "Number of registered classes:{0}", list.size()); for( Registration reg : list ) { log.log( Level.FINE, " {0}", reg); } } compiled = list.toArray(new Registration[list.size()]); INSTANCE = new SerializerRegistrationsMessage(compiled); Serializer.setReadOnly(true); }
public static Serializer getSerializer(Class cls, boolean failOnMiss) { return getSerializerRegistration(cls, failOnMiss).getSerializer(); }
/** * Directly registers a class for a specific ID. Generally, use the regular * registerClass() method. This method is intended for framework code that might * be maintaining specific ID maps across client and server. */ public static SerializerRegistration registerClassForId( short id, Class cls, Serializer serializer ) { if( locked ) { throw new RuntimeException("Serializer registry locked trying to register class:" + cls); } SerializerRegistration reg = new SerializerRegistration(serializer, cls, id); idRegistrations.put(id, reg); classRegistrations.put(cls, reg); log.log( Level.FINE, "Registered class[" + id + "]:{0} to:" + serializer, cls ); serializer.initialize(cls); // Add the class after so that dependency order is preserved if the // serializer registers its own classes. registrations.add(reg); return reg; }
@SuppressWarnings("unchecked") public <T> T readObject(ByteBuffer data, Class<T> c) throws IOException { int length = data.getInt(); Collection collection; try { collection = (Collection)c.newInstance(); } catch (Exception e) { log.log(Level.FINE, "[Serializer][???] Could not determine collection type. Using ArrayList."); collection = new ArrayList(length); } if (length == 0) return (T)collection; if (data.get() == (byte)1) { SerializerRegistration reg = Serializer.readClass(data); Class clazz = reg.getType(); Serializer serializer = reg.getSerializer(); for (int i = 0; i != length; ++i) { collection.add(serializer.readObject(data, clazz)); } } else { for (int i = 0; i != length; ++i) { collection.add(Serializer.readClassAndObject(data)); } } return (T)collection; }
public Registration( SerializerRegistration reg ) { this.id = reg.getId(); this.className = reg.getType().getName(); if( reg.getSerializer().getClass() != FieldSerializer.class ) { this.serializerClassName = reg.getSerializer().getClass().getName(); } }
public static SerializerRegistration registerClass(Class cls, Serializer serializer) { SerializerRegistration existingReg = getExactSerializerRegistration(cls); short id; if (existingReg != null) { id = existingReg.getId(); } else { id = --nextId; } SerializerRegistration reg = new SerializerRegistration(serializer, cls, id); idRegistrations.put(id, reg); classRegistrations.put(cls, reg); log.log( Level.INFO, "Registered class[" + id + "]:{0} to:" + serializer, cls ); serializer.initialize(cls); return reg; }
public static SerializerRegistration registerClass(Class cls, Serializer serializer) { SerializerRegistration existingReg = getExactSerializerRegistration(cls); short id; if (existingReg != null) { id = existingReg.getId(); } else { id = nextId(); } return registerClassForId( id, cls, serializer ); }
private Class<?> readType(ByteBuffer buffer) throws IOException{ SerializerRegistration reg = Serializer.readClass(buffer); if (reg == null){ // either "void" or unknown val short id = buffer.getShort(buffer.position()-2); if (id == 0){ return void.class; } else{ logger.log(Level.WARNING, "Undefined class ID: {0}", id); throw new IOException(); // prevents message from being serialized } } return reg.getType(); }
public static Serializer getExactSerializer(Class cls) { return classRegistrations.get(cls).getSerializer(); }
if (!uniqueKeys){ SerializerRegistration reg = Serializer.readClass(data); keyClazz = reg.getType(); keySerial = reg.getSerializer(); valClazz = reg.getType(); valSerial = reg.getSerializer();