Search in sources :

Example 11 with Instantiator

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

the class DataTypeJUnitTest method getDataTypeShouldReturnUserDataSeriazliable2.

@Test
public void getDataTypeShouldReturnUserDataSeriazliable2() throws IOException {
    Instantiator mockInstantiator = PowerMockito.mock(Instantiator.class);
    doReturn(CustId.class).when(mockInstantiator).getInstantiatedClass();
    mockInstantiator.getInstantiatedClass();
    int someClassId = 1;
    PowerMockito.mockStatic(InternalInstantiator.class);
    when(InternalInstantiator.getClassId(mockInstantiator.getClass())).thenReturn(someClassId);
    when(InternalInstantiator.getInstantiator(someClassId)).thenReturn(mockInstantiator);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(baos);
    out.writeByte(DSCODE.USER_DATA_SERIALIZABLE_2);
    out.writeShort(someClassId);
    byte[] bytes = baos.toByteArray();
    String type = DataType.getDataType(bytes);
    assertThat(type).isEqualTo("org.apache.geode.Instantiator:org.apache.geode.internal.cache.execute.data.CustId");
}
Also used : DataOutputStream(java.io.DataOutputStream) InternalInstantiator(org.apache.geode.internal.InternalInstantiator) Instantiator(org.apache.geode.Instantiator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UnitTest(org.apache.geode.test.junit.categories.UnitTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 12 with Instantiator

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

the class PoolManagerImpl method allPoolsRegisterInstantiator.

/**
   * @param instantiator
   */
public static void allPoolsRegisterInstantiator(Instantiator instantiator) {
    Instantiator[] instantiators = new Instantiator[1];
    instantiators[0] = instantiator;
    for (Iterator<Pool> itr = PoolManager.getAll().values().iterator(); itr.hasNext(); ) {
        PoolImpl next = (PoolImpl) itr.next();
        try {
            EventID eventId = InternalInstantiator.generateEventId();
            if (eventId == null) {
            // cache must not exist, do nothing
            } else {
                RegisterInstantiatorsOp.execute(next, instantiators, InternalInstantiator.generateEventId());
            }
        } catch (RuntimeException e) {
            logger.warn(LocalizedMessage.create(LocalizedStrings.PoolmanagerImpl_ERROR_REGISTERING_INSTANTIATOR_ON_POOL), e);
        } finally {
            next.releaseThreadLocalConnection();
        }
    }
}
Also used : InternalInstantiator(org.apache.geode.internal.InternalInstantiator) Instantiator(org.apache.geode.Instantiator) Pool(org.apache.geode.cache.client.Pool) PoolImpl(org.apache.geode.cache.client.internal.PoolImpl)

Example 13 with Instantiator

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

the class InternalInstantiator method saveRegistrations.

/**
   * Persist this class's map to out
   */
public static void saveRegistrations(DataOutput out) throws IOException {
    for (Instantiator inst : InternalInstantiator.getInstantiators()) {
        out.writeInt(inst.getId());
        DataSerializer.writeClass(inst.getClass(), out);
        DataSerializer.writeClass(inst.getInstantiatedClass(), out);
    }
    // We know that Instantiator id's must not be 0 so write a zero
    // to mark then end of the instantiators.
    out.writeInt(0);
}
Also used : Instantiator(org.apache.geode.Instantiator)

Example 14 with Instantiator

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

the class InternalInstantiator method getInstantiator.

/**
   * Returns the class with the given id
   *
   * @see DataSerializer#readObject
   */
public static Instantiator getInstantiator(int classId) {
    final Integer idx = Integer.valueOf(classId);
    Marker marker;
    boolean retry;
    Object o = idsToInstantiators.get(idx);
    do {
        retry = false;
        if (o == null) {
            marker = new Marker();
            o = idsToInstantiators.putIfAbsent(idx, marker);
            retry = o != null;
        } else if (o instanceof Marker) {
            marker = (Marker) o;
        } else {
            return (Instantiator) o;
        }
    } while (retry);
    Instantiator instantiator = null;
    if (idsToHolders.get(classId) == null) {
        instantiator = marker.getInstantiator();
    }
    if (instantiator != null) {
        return instantiator;
    } else {
        InstantiatorAttributesHolder holder = idsToHolders.get(classId);
        if (holder != null) {
            try {
                Class instantiatorClass = InternalDataSerializer.getCachedClass(holder.getInstantiatorClassName());
                Class instantiatedClass = InternalDataSerializer.getCachedClass(holder.getInstantiatedClassName());
                // 46355: move getCachedClass out of sync
                synchronized (InternalInstantiator.class) {
                    register(instantiatorClass, instantiatedClass, holder.getId(), false, holder.getEventId(), holder.getContext());
                    classNamesToHolders.remove(holder.getInstantiatedClassName());
                    idsToHolders.remove(classId);
                    instantiator = (Instantiator) idsToInstantiators.get(classId);
                }
            } catch (ClassNotFoundException cnfe) {
                InternalCache cache = GemFireCacheImpl.getInstance();
                if (cache != null && cache.getLoggerI18n() != null && cache.getLoggerI18n().infoEnabled()) {
                    cache.getLoggerI18n().info(LocalizedStrings.InternalInstantiator_COULD_NOT_LOAD_INSTANTIATOR_CLASS_0, new Object[] { cnfe.getMessage() });
                }
            }
        }
        return instantiator;
    }
}
Also used : InternalCache(org.apache.geode.internal.cache.InternalCache) Instantiator(org.apache.geode.Instantiator)

Example 15 with Instantiator

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

the class InternalInstantiator method getClassId.

/**
   * Returns the class id for the given class.
   *
   * @return {@code 0} if the class has not be registered
   *
   * @see DataSerializer#writeObject(Object, DataOutput)
   */
public static int getClassId(Class c) {
    int result = 0;
    final Instantiator i = (Instantiator) dsMap.get(c.getName());
    if (i != null) {
        result = i.getId();
    } else {
        InstantiatorAttributesHolder iah = classNamesToHolders.get(c.getName());
        if (iah != null) {
            result = iah.getId();
        }
    }
    return result;
}
Also used : Instantiator(org.apache.geode.Instantiator)

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