Search in sources :

Example 16 with Instantiator

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

the class InternalInstantiator method register.

/**
   * Creates a new {@code Instantiator} with the given class and id and
   * {@linkplain #register(Instantiator, boolean) registers} it with the data serialization
   * framework.
   *
   * This method is only called when server connection and CacheClientUpdaterThread
   *
   * @throws IllegalArgumentException The instantiator cannot be created
   * @throws IllegalStateException The instantiator cannot be registered
   */
public static void register(Class instantiatorClass, Class instantiatedClass, int id, boolean distribute, EventID eventId, ClientProxyMembershipID context) {
    Instantiator inst = newInstance(instantiatorClass, instantiatedClass, id);
    // This method is only called when server connection and CacheClientUpdaterThread
    inst.setEventId(eventId);
    inst.setContext(context);
    _register(inst, distribute);
}
Also used : Instantiator(org.apache.geode.Instantiator)

Example 17 with Instantiator

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

the class InternalInstantiator method newInstance.

/**
   * Reflectively instantiates an instance of {@code Instantiator}.
   *
   * @param instantiatorClass The implementation of {@code Instantiator} to instantiate
   * @param instantiatedClass The implementation of {@code DataSerialization} that will be produced
   *        by the {@code Instantiator}
   *
   * @throws IllegalArgumentException If the class can't be instantiated
   */
protected static Instantiator newInstance(Class instantiatorClass, Class instantiatedClass, int id) {
    if (!Instantiator.class.isAssignableFrom(instantiatorClass)) {
        throw new IllegalArgumentException(LocalizedStrings.InternalInstantiator_0_DOES_NOT_EXTEND_INSTANTIATOR.toLocalizedString(instantiatorClass.getName()));
    }
    Constructor init;
    boolean intConstructor = false;
    Class[] types;
    try {
        types = new Class[] { Class.class, int.class };
        init = instantiatorClass.getDeclaredConstructor(types);
        intConstructor = true;
    } catch (NoSuchMethodException ex) {
        // for backwards compat check for (Class, byte)
        try {
            types = new Class[] { Class.class, byte.class };
            init = instantiatorClass.getDeclaredConstructor(types);
        } catch (NoSuchMethodException ex2) {
            StringId msg = LocalizedStrings.InternalInstantiator_CLASS_0_DOES_NOT_HAVE_A_TWOARGUMENT_CLASS_INT_CONSTRUCTOR;
            Object[] msgArgs = new Object[] { instantiatorClass.getName() };
            if (instantiatorClass.getDeclaringClass() != null) {
                msg = LocalizedStrings.InternalInstantiator_CLASS_0_DOES_NOT_HAVE_A_TWOARGUMENT_CLASS_INT_CONSTRUCTOR_IT_IS_AN_INNER_CLASS_OF_1_SHOULD_IT_BE_A_STATIC_INNER_CLASS;
                msgArgs = new Object[] { instantiatorClass.getName(), instantiatorClass.getDeclaringClass() };
            }
            throw new IllegalArgumentException(msg.toLocalizedString(msgArgs));
        }
    }
    Instantiator s;
    try {
        init.setAccessible(true);
        Object[] args = new Object[] { instantiatedClass, intConstructor ? (Object) Integer.valueOf(id) : (Object) Byte.valueOf((byte) id) };
        s = (Instantiator) init.newInstance(args);
    } catch (IllegalAccessException ex) {
        throw new IllegalArgumentException(LocalizedStrings.InternalInstantiator_COULD_NOT_ACCESS_ZEROARGUMENT_CONSTRUCTOR_OF_0.toLocalizedString(instantiatorClass.getName()));
    } catch (InstantiationException ex) {
        RuntimeException ex2 = new IllegalArgumentException(LocalizedStrings.InternalInstantiator_COULD_NOT_INSTANTIATE_AN_INSTANCE_OF_0.toLocalizedString(instantiatorClass.getName()));
        ex2.initCause(ex);
        throw ex2;
    } catch (InvocationTargetException ex) {
        RuntimeException ex2 = new IllegalArgumentException(LocalizedStrings.InternalInstantiator_WHILE_INSTANTIATING_AN_INSTANCE_OF_0.toLocalizedString(instantiatorClass.getName()));
        ex2.initCause(ex);
        throw ex2;
    }
    return s;
}
Also used : Constructor(java.lang.reflect.Constructor) Instantiator(org.apache.geode.Instantiator) InvocationTargetException(java.lang.reflect.InvocationTargetException) StringId(org.apache.geode.i18n.StringId)

Example 18 with Instantiator

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

the class InternalInstantiator method register.

/**
   * Creates a new {@code Instantiator} with the given class and id and
   * {@linkplain #register(Instantiator, boolean) registers} it with the data serialization
   * framework.
   *
   * @throws IllegalArgumentException The instantiator cannot be created
   * @throws IllegalStateException The instantiator cannot be registered
   */
public static void register(Class instantiatorClass, Class instantiatedClass, int id, boolean distribute) {
    if (checkForThread()) {
        Instantiator inst = newInstance(instantiatorClass, instantiatedClass, id);
        _register(inst, distribute);
    }
}
Also used : Instantiator(org.apache.geode.Instantiator)

Example 19 with Instantiator

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

the class InternalInstantiator method _register.

/**
   * Actually registers an {@code Instantiator} with the data serialization framework.
   *
   * @param instantiator
   * @param distribute
   * @throws IllegalArgumentException If the instantiator has an id of zero
   * @throws IllegalStateException The instantiator cannot be registered
   */
private static void _register(Instantiator instantiator, boolean distribute) {
    if (instantiator == null) {
        throw new NullPointerException(LocalizedStrings.InternalInstantiator_CANNOT_REGISTER_A_NULL_INSTANTIATOR.toLocalizedString());
    }
    final int classId = instantiator.getId();
    if (classId == 0) {
        throw new IllegalArgumentException(LocalizedStrings.Instantiator_INSTANTIATOR_ID_CANNOT_BE_ZERO.toLocalizedString());
    }
    Class c = instantiator.getInstantiatedClass();
    final String cName = c.getName();
    {
        int oldId = getClassId(c);
        if (oldId != 0 && oldId != classId) {
            throw new IllegalStateException(LocalizedStrings.InternalInstantiator_CLASS_0_IS_ALREADY_REGISTERED_WITH_ID_1_SO_IT_CANNOT_BE_REGISTERED_WTH_ID_2.toLocalizedString(new Object[] { c.getName(), Integer.valueOf(oldId), Integer.valueOf(classId) }));
        }
    }
    final Integer idx = Integer.valueOf(classId);
    synchronized (InternalInstantiator.class) {
        boolean retry;
        do {
            retry = false;
            Object oldInst = idsToInstantiators.putIfAbsent(idx, instantiator);
            if (oldInst != null) {
                if (oldInst instanceof Marker) {
                    retry = !idsToInstantiators.replace(idx, oldInst, instantiator);
                    if (!retry) {
                        dsMap.put(cName, instantiator);
                        ((Marker) oldInst).setInstantiator(instantiator);
                    }
                } else {
                    Class oldClass = ((Instantiator) oldInst).getInstantiatedClass();
                    if (!oldClass.getName().equals(cName)) {
                        throw new IllegalStateException(LocalizedStrings.InternalInstantiator_CLASS_ID_0_IS_ALREADY_REGISTERED_FOR_CLASS_1_SO_IT_COULD_NOT_BE_REGISTED_FOR_CLASS_2.toLocalizedString(new Object[] { Integer.valueOf(classId), oldClass.getName(), cName }));
                    } else {
                        // it was already registered
                        return;
                    }
                }
            } else {
                dsMap.put(cName, instantiator);
            }
        } while (retry);
        // if instantiator is getting registered for first time
        // its EventID will be null, so generate a new event id
        // the the distributed system is connected
        InternalCache cache = GemFireCacheImpl.getInstance();
        if (cache != null && instantiator.getEventId() == null) {
            instantiator.setEventId(new EventID(cache.getDistributedSystem()));
        }
        logger.info(LocalizedMessage.create(LocalizedStrings.InternalInstantiator_REGISTERED, new Object[] { Integer.valueOf(classId), c.getName() }));
    }
    if (distribute) {
        // originated in this VM
        // send a message to other peers telling them about a newly-registered
        // instantiator, it also send event id of the originator along with the
        // instantiator
        sendRegistrationMessage(instantiator);
        // send it to cache servers if it is a client
        sendRegistrationMessageToServers(instantiator);
    }
    // send it to all cache clients irelevent of distribute
    // bridge servers send it all the clients irelevent of
    // originator VM
    sendRegistrationMessageToClients(instantiator);
    InternalDataSerializer.fireNewInstantiator(instantiator);
}
Also used : InternalCache(org.apache.geode.internal.cache.InternalCache) Instantiator(org.apache.geode.Instantiator) EventID(org.apache.geode.internal.cache.EventID)

Example 20 with Instantiator

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

the class InternalDataSerializer method readUserDataSerializable.

private static Object readUserDataSerializable(final DataInput in, int classId) throws IOException {
    Instantiator instantiator = InternalInstantiator.getInstantiator(classId);
    if (instantiator == null) {
        logger.error(LogMarker.SERIALIZER, LocalizedMessage.create(LocalizedStrings.DataSerializer_NO_INSTANTIATOR_HAS_BEEN_REGISTERED_FOR_CLASS_WITH_ID_0, classId));
        throw new IOException(LocalizedStrings.DataSerializer_NO_INSTANTIATOR_HAS_BEEN_REGISTERED_FOR_CLASS_WITH_ID_0.toLocalizedString(classId));
    } else {
        try {
            DataSerializable ds;
            if (instantiator instanceof CanonicalInstantiator) {
                CanonicalInstantiator ci = (CanonicalInstantiator) instantiator;
                ds = ci.newInstance(in);
            } else {
                ds = instantiator.newInstance();
            }
            ds.fromData(in);
            return ds;
        } catch (Exception ex) {
            throw new SerializationException(LocalizedStrings.DataSerializer_COULD_NOT_DESERIALIZE_AN_INSTANCE_OF_0.toLocalizedString(instantiator.getInstantiatedClass().getName()), ex);
        }
    }
}
Also used : CanonicalInstantiator(org.apache.geode.CanonicalInstantiator) SerializationException(org.apache.geode.SerializationException) CanonicalInstantiator(org.apache.geode.CanonicalInstantiator) Instantiator(org.apache.geode.Instantiator) IOException(java.io.IOException) GemFireIOException(org.apache.geode.GemFireIOException) DataSerializable(org.apache.geode.DataSerializable) InvocationTargetException(java.lang.reflect.InvocationTargetException) NonPortableClassException(org.apache.geode.pdx.NonPortableClassException) IOException(java.io.IOException) CancelException(org.apache.geode.CancelException) EOFException(java.io.EOFException) UTFDataFormatException(java.io.UTFDataFormatException) GemFireIOException(org.apache.geode.GemFireIOException) SerializationException(org.apache.geode.SerializationException) CacheClosedException(org.apache.geode.cache.CacheClosedException) NotSerializableException(java.io.NotSerializableException) ToDataException(org.apache.geode.ToDataException)

Aggregations

Instantiator (org.apache.geode.Instantiator)24 UnitTest (org.apache.geode.test.junit.categories.UnitTest)9 Test (org.junit.Test)9 CanonicalInstantiator (org.apache.geode.CanonicalInstantiator)7 InternalInstantiator (org.apache.geode.internal.InternalInstantiator)7 DataSerializable (org.apache.geode.DataSerializable)5 InternalCache (org.apache.geode.internal.cache.InternalCache)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 DataOutputStream (java.io.DataOutputStream)3 IOException (java.io.IOException)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 DataSerializer (org.apache.geode.DataSerializer)2 CacheClosedException (org.apache.geode.cache.CacheClosedException)2 DataInput (java.io.DataInput)1 EOFException (java.io.EOFException)1 NotSerializableException (java.io.NotSerializableException)1 UTFDataFormatException (java.io.UTFDataFormatException)1