Search in sources :

Example 31 with BinaryObjectException

use of org.apache.ignite.binary.BinaryObjectException in project ignite by apache.

the class BinaryMarshaller method unmarshal0.

/** {@inheritDoc} */
@Override
protected <T> T unmarshal0(InputStream in, @Nullable ClassLoader clsLdr) throws IgniteCheckedException {
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    // returns number of bytes remaining.
    try {
        byte[] arr = new byte[4096];
        int cnt;
        while ((cnt = in.read(arr)) != -1) buf.write(arr, 0, cnt);
        buf.flush();
        return impl.deserialize(buf.toByteArray(), clsLdr);
    } catch (IOException e) {
        throw new BinaryObjectException("Failed to unmarshal the object from InputStream", e);
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException)

Example 32 with BinaryObjectException

use of org.apache.ignite.binary.BinaryObjectException in project ignite by apache.

the class BinaryObjectExImpl method toString.

/** {@inheritDoc} */
@Override
public String toString() {
    try {
        BinaryReaderHandles ctx = new BinaryReaderHandles();
        ctx.put(start(), this);
        return toString(ctx, new IdentityHashMap<BinaryObject, Integer>());
    } catch (BinaryObjectException e) {
        throw new IgniteException("Failed to create string representation of binary object.", e);
    }
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) IgniteException(org.apache.ignite.IgniteException) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException)

Example 33 with BinaryObjectException

use of org.apache.ignite.binary.BinaryObjectException in project ignite by apache.

the class BinaryObjectBuilderImpl method checkMetadata.

/**
     * Checks metadata when a BinaryObject is being serialized.
     *
     * @param meta Current metadata.
     * @param fieldsMeta Map holding metadata information that has to be updated.
     * @param newVal Field value being serialized.
     * @param name Field name.
     * @param fieldId Field ID.
     */
private Map<String, BinaryFieldMetadata> checkMetadata(BinaryType meta, Map<String, BinaryFieldMetadata> fieldsMeta, Object newVal, String name, int fieldId) {
    String oldFldTypeName = meta == null ? null : meta.fieldTypeName(name);
    boolean nullFieldVal = false;
    int newFldTypeId;
    if (newVal instanceof BinaryValueWithType) {
        newFldTypeId = ((BinaryValueWithType) newVal).typeId();
        if (((BinaryValueWithType) newVal).value() == null)
            nullFieldVal = true;
    } else // Detect Enum and Enum array type.
    if (newVal instanceof BinaryEnumObjectImpl)
        newFldTypeId = GridBinaryMarshaller.ENUM;
    else if (newVal.getClass().isArray() && newVal.getClass().getComponentType() == BinaryObject.class) {
        BinaryObject[] arr = (BinaryObject[]) newVal;
        newFldTypeId = arr.length > 0 && arr[0] instanceof BinaryEnumObjectImpl ? GridBinaryMarshaller.ENUM_ARR : GridBinaryMarshaller.OBJ_ARR;
    } else
        newFldTypeId = BinaryUtils.typeByClass(newVal.getClass());
    if (oldFldTypeName == null) {
        // It's a new field, we have to add it to metadata.
        if (fieldsMeta == null) {
            if (BinaryUtils.FIELDS_SORTED_ORDER)
                fieldsMeta = new TreeMap<>();
            else
                fieldsMeta = new LinkedHashMap<>();
        }
        fieldsMeta.put(name, new BinaryFieldMetadata(newFldTypeId, fieldId));
    } else if (!nullFieldVal) {
        String newFldTypeName = BinaryUtils.fieldTypeName(newFldTypeId);
        if (!F.eq(newFldTypeName, oldFldTypeName)) {
            throw new BinaryObjectException("Wrong value has been set [" + "typeName=" + (typeName == null ? meta.typeName() : typeName) + ", fieldName=" + name + ", fieldType=" + oldFldTypeName + ", assignedValueType=" + newFldTypeName + ']');
        }
    }
    return fieldsMeta;
}
Also used : BinaryFieldMetadata(org.apache.ignite.internal.binary.BinaryFieldMetadata) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryEnumObjectImpl(org.apache.ignite.internal.binary.BinaryEnumObjectImpl) TreeMap(java.util.TreeMap) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException) LinkedHashMap(java.util.LinkedHashMap)

Example 34 with BinaryObjectException

use of org.apache.ignite.binary.BinaryObjectException in project ignite by apache.

the class BinaryContext method registerUserClassDescriptor.

/**
     * Creates and registers {@link BinaryClassDescriptor} for the given user {@code class}.
     *
     * @param desc Old descriptor that should be re-registered.
     * @return Class descriptor.
     */
private BinaryClassDescriptor registerUserClassDescriptor(BinaryClassDescriptor desc) {
    boolean registered;
    try {
        registered = marshCtx.registerClassName(JAVA_ID, desc.typeId(), desc.describedClass().getName());
    } catch (IgniteCheckedException e) {
        throw new BinaryObjectException("Failed to register class.", e);
    }
    if (registered) {
        BinarySerializer serializer = desc.initialSerializer();
        if (serializer == null)
            serializer = serializerForClass(desc.describedClass());
        desc = new BinaryClassDescriptor(this, desc.describedClass(), true, desc.typeId(), desc.typeName(), desc.affFieldKeyName(), desc.mapper(), serializer, true, true);
        descByCls.put(desc.describedClass(), desc);
    }
    return desc;
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException) BinarySerializer(org.apache.ignite.binary.BinarySerializer)

Example 35 with BinaryObjectException

use of org.apache.ignite.binary.BinaryObjectException in project ignite by apache.

the class BinaryContext method registerUserClassDescriptor.

/**
     * Creates and registers {@link BinaryClassDescriptor} for the given user {@code class}.
     *
     * @param cls Class.
     * @return Class descriptor.
     */
private BinaryClassDescriptor registerUserClassDescriptor(Class<?> cls, boolean deserialize) {
    boolean registered;
    final String clsName = cls.getName();
    BinaryInternalMapper mapper = userTypeMapper(clsName);
    final String typeName = mapper.typeName(clsName);
    final int typeId = mapper.typeId(clsName);
    try {
        registered = marshCtx.registerClassName(JAVA_ID, typeId, cls.getName());
    } catch (IgniteCheckedException e) {
        throw new BinaryObjectException("Failed to register class.", e);
    }
    BinarySerializer serializer = serializerForClass(cls);
    String affFieldName = affinityFieldName(cls);
    BinaryClassDescriptor desc = new BinaryClassDescriptor(this, cls, true, typeId, typeName, affFieldName, mapper, serializer, true, registered);
    if (!deserialize)
        metaHnd.addMeta(typeId, new BinaryMetadata(typeId, typeName, desc.fieldsMeta(), affFieldName, null, desc.isEnum(), cls.isEnum() ? enumMap(cls) : null).wrap(this));
    descByCls.put(cls, desc);
    typeId2Mapper.putIfAbsent(typeId, mapper);
    return desc;
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException) BinarySerializer(org.apache.ignite.binary.BinarySerializer)

Aggregations

BinaryObjectException (org.apache.ignite.binary.BinaryObjectException)42 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)9 BinaryObject (org.apache.ignite.binary.BinaryObject)8 Date (java.util.Date)4 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 TreeMap (java.util.TreeMap)4 BinarySerializer (org.apache.ignite.binary.BinarySerializer)4 BinaryType (org.apache.ignite.binary.BinaryType)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 BigInteger (java.math.BigInteger)2 Time (java.sql.Time)2 Timestamp (java.sql.Timestamp)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 BinaryBasicIdMapper (org.apache.ignite.binary.BinaryBasicIdMapper)2 BinaryInvalidTypeException (org.apache.ignite.binary.BinaryInvalidTypeException)2 BinaryObjectBuilder (org.apache.ignite.binary.BinaryObjectBuilder)2 BinaryTypeConfiguration (org.apache.ignite.binary.BinaryTypeConfiguration)2 BinaryEnumObjectImpl (org.apache.ignite.internal.binary.BinaryEnumObjectImpl)2