Search in sources :

Example 16 with DataSerializer

use of org.apache.geode.DataSerializer in project geode by apache.

the class InternalDataSerializer method register.

public static DataSerializer register(Class c, boolean distribute, EventID eventId, ClientProxyMembershipID context) {
    DataSerializer s = newInstance(c);
    // This method is only called when server connection and CacheClientUpdaterThread
    s.setEventId(eventId);
    s.setContext(context);
    return _register(s, distribute);
}
Also used : DataSerializer(org.apache.geode.DataSerializer)

Example 17 with DataSerializer

use of org.apache.geode.DataSerializer in project geode by apache.

the class InternalDataSerializer method saveRegistrations.

/**
   * Persist this class's map to out TODO: saveRegistrations is unused
   */
public static void saveRegistrations(DataOutput out) throws IOException {
    for (Object v : idsToSerializers.values()) {
        if (v instanceof InitMarker) {
            v = ((Marker) v).getSerializer();
        }
        if (v instanceof DataSerializer) {
            DataSerializer ds = (DataSerializer) v;
            // since 5.7 an int instead of a byte
            out.writeInt(ds.getId());
            DataSerializer.writeClass(ds.getClass(), out);
        }
    }
    if (!dsClassesToHolders.isEmpty()) {
        Iterator<Entry<String, SerializerAttributesHolder>> iterator = dsClassesToHolders.entrySet().iterator();
        Class dsClass = null;
        while (iterator.hasNext()) {
            try {
                dsClass = getCachedClass(iterator.next().getKey());
            } catch (ClassNotFoundException ignored) {
                logger.info(LogMarker.SERIALIZER, LocalizedMessage.create(LocalizedStrings.InternalDataSerializer_COULD_NOT_LOAD_DATASERIALIZER_CLASS_0, dsClass));
                continue;
            }
            DataSerializer ds = register(dsClass, false);
            iterator.remove();
            idsToHolders.remove(ds.getId());
            for (Class clazz : ds.getSupportedClasses()) {
                supportedClassesToHolders.remove(clazz.getName());
            }
            // since 5.7 an int instead of a byte
            out.writeInt(ds.getId());
            DataSerializer.writeClass(ds.getClass(), out);
        }
    }
    // We know that DataSerializer's id must be > 0 so write a zero
    // to mark the end of the ds list.
    // since 5.7 an int instead of a byte
    out.writeInt(0);
}
Also used : Entry(java.util.Map.Entry) ObjectStreamClass(java.io.ObjectStreamClass) DataSerializer(org.apache.geode.DataSerializer)

Example 18 with DataSerializer

use of org.apache.geode.DataSerializer in project geode by apache.

the class InternalDataSerializer method unregister.

/**
   * Unregisters a {@code Serializer} that was previously registered with the data serialization
   * framework.
   */
public static void unregister(int id) {
    final Integer idx = id;
    Object o = idsToSerializers.remove(idx);
    if (o != null) {
        if (o instanceof InitMarker) {
            o = ((Marker) o).getSerializer();
        }
    }
    if (o instanceof DataSerializer) {
        DataSerializer s = (DataSerializer) o;
        Class[] classes = s.getSupportedClasses();
        for (Class aClass : classes) {
            classesToSerializers.remove(aClass.getName(), s);
            supportedClassesToHolders.remove(aClass.getName());
        }
        dsClassesToHolders.remove(s.getClass().getName());
        idsToHolders.remove(idx);
    }
}
Also used : BigInteger(java.math.BigInteger) ObjectStreamClass(java.io.ObjectStreamClass) DataSerializer(org.apache.geode.DataSerializer)

Example 19 with DataSerializer

use of org.apache.geode.DataSerializer in project geode by apache.

the class DataSerializableJUnitTest method testUDDS4.

/**
   * Make sure a user defined ds with an id of42 bytes works.
   */
@Test
public void testUDDS4() throws Exception {
    DataSerializer ds2 = DataSerializer.register(Class_testSupportedClasses4.class);
    int id2 = ds2.getId();
    try {
        Object o = new NonDataSerializable(new Random());
        DataSerializer.writeObject(o, getDataOutput());
        assertTrue(Class_testSupportedClasses4.wasInvoked);
        assertTrue(Class_testSupportedClasses4.toDataInvoked);
        assertFalse(Class_testSupportedClasses4.fromDataInvoked);
        Object o2 = DataSerializer.readObject(getDataInput());
        assertTrue(Class_testSupportedClasses4.fromDataInvoked);
        assertEquals(o, o2);
    } finally {
        InternalDataSerializer.unregister(id2);
    }
}
Also used : DataSerializer(org.apache.geode.DataSerializer) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Aggregations

DataSerializer (org.apache.geode.DataSerializer)19 ObjectStreamClass (java.io.ObjectStreamClass)7 UnitTest (org.apache.geode.test.junit.categories.UnitTest)5 Test (org.junit.Test)5 InternalDataSerializer (org.apache.geode.internal.InternalDataSerializer)4 BigInteger (java.math.BigInteger)3 ArrayList (java.util.ArrayList)3 Entry (java.util.Map.Entry)2 Instantiator (org.apache.geode.Instantiator)2 StringId (org.apache.geode.i18n.StringId)2 EventID (org.apache.geode.internal.cache.EventID)2 LogMarker (org.apache.geode.internal.logging.log4j.LogMarker)2 IOException (java.io.IOException)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CancelException (org.apache.geode.CancelException)1 CanonicalInstantiator (org.apache.geode.CanonicalInstantiator)1