Search in sources :

Example 11 with DataSerializer

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

the class PoolManagerImpl method allPoolsRegisterDataSerializers.

public static void allPoolsRegisterDataSerializers(DataSerializer dataSerializer) {
    DataSerializer[] dataSerializers = new DataSerializer[1];
    dataSerializers[0] = dataSerializer;
    for (Iterator<Pool> itr = PoolManager.getAll().values().iterator(); itr.hasNext(); ) {
        PoolImpl next = (PoolImpl) itr.next();
        try {
            EventID eventId = (EventID) dataSerializer.getEventId();
            if (eventId == null) {
                eventId = InternalDataSerializer.generateEventId();
            }
            if (eventId == null) {
            // cache must not exist, do nothing
            } else {
                RegisterDataSerializersOp.execute(next, dataSerializers, eventId);
            }
        } catch (RuntimeException e) {
            logger.warn(LocalizedMessage.create(LocalizedStrings.PoolmanagerImpl_ERROR_REGISTERING_INSTANTIATOR_ON_POOL), e);
        } finally {
            next.releaseThreadLocalConnection();
        }
    }
}
Also used : Pool(org.apache.geode.cache.client.Pool) PoolImpl(org.apache.geode.cache.client.internal.PoolImpl) DataSerializer(org.apache.geode.DataSerializer) InternalDataSerializer(org.apache.geode.internal.InternalDataSerializer)

Example 12 with DataSerializer

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

the class InternalDataSerializer method getSerializer.

/**
   * Returns the {@code DataSerializer} with the given id.
   */
public static DataSerializer getSerializer(int id) {
    final Integer idx = id;
    final GetMarker marker = new GetMarker();
    DataSerializer result = null;
    boolean timedOut = false;
    SerializerAttributesHolder sah = idsToHolders.get(idx);
    while (result == null && !timedOut && sah == null) {
        Object o = idsToSerializers.putIfAbsent(idx, marker);
        if (o == null) {
            result = marker.getSerializer();
            if (result == null) {
                // timed out
                timedOut = true;
                idsToSerializers.remove(idx, marker);
            }
        } else if (o instanceof Marker) {
            result = ((Marker) o).getSerializer();
        } else {
            result = (DataSerializer) o;
        }
    }
    if (result == null) {
        if (sah != null) {
            Class dsClass = null;
            try {
                dsClass = getCachedClass(sah.getClassName());
                DataSerializer ds = register(dsClass, false);
                dsClassesToHolders.remove(sah.getClassName());
                idsToHolders.remove(id);
                for (Class clazz : ds.getSupportedClasses()) {
                    supportedClassesToHolders.remove(clazz.getName());
                }
                return ds;
            } catch (ClassNotFoundException ignored) {
                logger.info(LogMarker.SERIALIZER, LocalizedMessage.create(LocalizedStrings.InternalDataSerializer_COULD_NOT_LOAD_DATASERIALIZER_CLASS_0, dsClass));
            }
        }
    }
    return result;
}
Also used : BigInteger(java.math.BigInteger) ObjectStreamClass(java.io.ObjectStreamClass) LogMarker(org.apache.geode.internal.logging.log4j.LogMarker) DataSerializer(org.apache.geode.DataSerializer)

Example 13 with DataSerializer

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

the class InternalDataSerializer method getSerializer.

/**
   * Returns the {@code DataSerializer} for the given class. If no class has been registered,
   * {@code null} is returned. Remember that it is okay to return {@code null} in this case. This
   * method is invoked when writing an object. If a serializer isn't available, then its the user's
   * fault.
   */
private static DataSerializer getSerializer(Class c) {
    DataSerializer ds = classesToSerializers.get(c.getName());
    if (ds == null) {
        SerializerAttributesHolder sah = supportedClassesToHolders.get(c.getName());
        if (sah != null) {
            Class dsClass = null;
            try {
                dsClass = getCachedClass(sah.getClassName());
                DataSerializer serializer = register(dsClass, false);
                dsClassesToHolders.remove(dsClass.getName());
                idsToHolders.remove(serializer.getId());
                for (Class clazz : serializer.getSupportedClasses()) {
                    supportedClassesToHolders.remove(clazz.getName());
                }
                return serializer;
            } catch (ClassNotFoundException ignored) {
                logger.info(LogMarker.SERIALIZER, LocalizedMessage.create(LocalizedStrings.InternalDataSerializer_COULD_NOT_LOAD_DATASERIALIZER_CLASS_0, dsClass));
            }
        }
    }
    return ds;
}
Also used : ObjectStreamClass(java.io.ObjectStreamClass) DataSerializer(org.apache.geode.DataSerializer)

Example 14 with DataSerializer

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

the class InternalDataSerializer method getSerializers.

/**
   * Returns all of the currently registered serializers
   */
public static DataSerializer[] getSerializers() {
    final int size = idsToSerializers.size();
    Collection coll = new ArrayList(size);
    for (Object v : idsToSerializers.values()) {
        if (v instanceof InitMarker) {
            v = ((Marker) v).getSerializer();
        }
        if (v instanceof DataSerializer) {
            coll.add(v);
        }
    }
    Iterator<Entry<String, SerializerAttributesHolder>> iterator = dsClassesToHolders.entrySet().iterator();
    while (iterator.hasNext()) {
        Entry<String, SerializerAttributesHolder> entry = iterator.next();
        String name = entry.getKey();
        SerializerAttributesHolder holder = entry.getValue();
        try {
            Class cl = getCachedClass(name);
            DataSerializer ds;
            if (holder.getEventId() != null) {
                ds = register(cl, false, holder.getEventId(), holder.getProxyId());
            } else {
                ds = register(cl, false);
            }
            coll.add(ds);
            iterator.remove();
            idsToHolders.remove(ds.getId());
            for (Class clazz : ds.getSupportedClasses()) {
                supportedClassesToHolders.remove(clazz.getName());
            }
        } catch (ClassNotFoundException ignored) {
            logger.info(LogMarker.SERIALIZER, LocalizedMessage.create(LocalizedStrings.InternalDataSerializer_COULD_NOT_LOAD_DATASERIALIZER_CLASS_0, name));
        }
    }
    return (DataSerializer[]) coll.toArray(new DataSerializer[coll.size()]);
}
Also used : ArrayList(java.util.ArrayList) Entry(java.util.Map.Entry) Collection(java.util.Collection) ObjectStreamClass(java.io.ObjectStreamClass) DataSerializer(org.apache.geode.DataSerializer)

Example 15 with DataSerializer

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

the class InternalDataSerializer method newInstance.

/**
   * Instantiates an instance of {@code DataSerializer}
   *
   * @throws IllegalArgumentException If the class can't be instantiated
   *
   * @see DataSerializer#register(Class)
   */
private static DataSerializer newInstance(Class c) {
    if (!DataSerializer.class.isAssignableFrom(c)) {
        throw new IllegalArgumentException(LocalizedStrings.DataSerializer_0_DOES_NOT_EXTEND_DATASERIALIZER.toLocalizedString(c.getName()));
    }
    Constructor init;
    try {
        init = c.getDeclaredConstructor(new Class[0]);
    } catch (NoSuchMethodException ignored) {
        StringId s = LocalizedStrings.DataSerializer_CLASS_0_DOES_NOT_HAVE_A_ZEROARGUMENT_CONSTRUCTOR;
        Object[] args = new Object[] { c.getName() };
        if (c.getDeclaringClass() != null) {
            s = LocalizedStrings.DataSerializer_CLASS_0_DOES_NOT_HAVE_A_ZEROARGUMENT_CONSTRUCTOR_IT_IS_AN_INNER_CLASS_OF_1_SHOULD_IT_BE_A_STATIC_INNER_CLASS;
            args = new Object[] { c.getName(), c.getDeclaringClass() };
        }
        throw new IllegalArgumentException(s.toLocalizedString(args));
    }
    DataSerializer s;
    try {
        init.setAccessible(true);
        s = (DataSerializer) init.newInstance(new Object[0]);
    } catch (IllegalAccessException ignored) {
        throw new IllegalArgumentException(LocalizedStrings.DataSerializer_COULD_NOT_INSTANTIATE_AN_INSTANCE_OF_0.toLocalizedString(c.getName()));
    } catch (InstantiationException ex) {
        throw new IllegalArgumentException(LocalizedStrings.DataSerializer_COULD_NOT_INSTANTIATE_AN_INSTANCE_OF_0.toLocalizedString(c.getName()), ex);
    } catch (InvocationTargetException ex) {
        throw new IllegalArgumentException(LocalizedStrings.DataSerializer_WHILE_INSTANTIATING_AN_INSTANCE_OF_0.toLocalizedString(c.getName()), ex);
    }
    return s;
}
Also used : StringId(org.apache.geode.i18n.StringId) Constructor(java.lang.reflect.Constructor) ObjectStreamClass(java.io.ObjectStreamClass) InvocationTargetException(java.lang.reflect.InvocationTargetException) DataSerializer(org.apache.geode.DataSerializer)

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