Search in sources :

Example 1 with ConversionException

use of org.osgi.util.converter.ConversionException in project felix by apache.

the class YamlDeserializingImpl method from.

@Override
public T from(InputStream in, Charset charset) {
    try {
        byte[] bytes = Util.readStream(in);
        String s = new String(bytes, charset);
        return from(s);
    } catch (IOException e) {
        throw new ConversionException("Error reading inputstream", e);
    }
}
Also used : ConversionException(org.osgi.util.converter.ConversionException) IOException(java.io.IOException)

Example 2 with ConversionException

use of org.osgi.util.converter.ConversionException in project felix by apache.

the class SchemaBasedConverter method convertToDTO.

@SuppressWarnings({ "rawtypes", "unchecked" })
private <U extends DTO> U convertToDTO(Class<U> targetCls, Map<?, ?> m, Schema schema, String contextPath) {
    try {
        U dto;
        try {
            dto = targetCls.newInstance();
        } catch (Throwable t) {
            throw new ConversionException("Cannot create instance of DTO " + targetCls + ". Bad constructor?", t);
        }
        for (Map.Entry entry : m.entrySet()) {
            try {
                Field f = targetCls.getField(entry.getKey().toString());
                Object val = entry.getValue();
                if (val == null)
                    continue;
                String path = contextPath + f.getName();
                Node node = schema.nodeAtPath(path);
                Object obj;
                if (node.typeReference().isPresent()) {
                    TypeReference<?> tr = Util.typeReferenceOf(node.typeReference().get());
                    if (node.isCollection())
                        if (!Collection.class.isAssignableFrom(val.getClass()))
                            // TODO: PANIC! Something is wrong... what should we do??
                            obj = null;
                        else
                            obj = convertToCollection((Class) Util.rawClassOf(tr), (Class) node.collectionType(), (Collection) val, schema, path);
                    else
                        obj = convertToDTO((Class<? extends DTO>) rawClassOf(tr), (Map<?, ?>) val, schema, path + "/");
                } else {
                    if (node.isCollection()) {
                        Collection c = instantiateCollection(node.collectionType());
                        Type type = node.type();
                        for (Object o : (Collection) val) {
                            if (o == null)
                                c.add(null);
                            else if (asDTO(rawClassOf(type)))
                                c.add(convertToDTO((Class) Util.rawClassOf(type), (Map) o, schema, path + "/"));
                            else
                                c.add(converter.convert(o).to(type));
                        }
                        obj = c;
                    } else {
                        Class<?> rawClass = rawClassOf(node.type());
                        if (asDTO(rawClass))
                            obj = convertToDTO((Class<? extends DTO>) rawClass, (Map<?, ?>) val, schema, path + "/");
                        else
                            obj = converter.convert(val).to(node.type());
                    }
                }
                f.set(dto, obj);
            } catch (NoSuchFieldException e) {
            }
        }
        return dto;
    } catch (Exception e) {
        throw new ConversionException("Cannot create DTO " + targetCls, e);
    }
}
Also used : ConversionException(org.osgi.util.converter.ConversionException) Node(org.apache.felix.schematizer.Node) ConversionException(org.osgi.util.converter.ConversionException) Field(java.lang.reflect.Field) Type(java.lang.reflect.Type) Collection(java.util.Collection) Map(java.util.Map)

Example 3 with ConversionException

use of org.osgi.util.converter.ConversionException in project felix by apache.

the class SchemaBasedConverter method convertToCollection.

@SuppressWarnings({ "rawtypes", "unchecked" })
private <U, V extends Collection<U>> V convertToCollection(Class<U> targetCls, Class<V> collectionClass, Collection sourceCollection, Schema schema, String path) {
    try {
        V targetCollection = instantiateCollection(collectionClass);
        sourceCollection.stream().map(obj -> convertCollectionItemToObject(obj, targetCls, schema, path)).forEach(u -> targetCollection.add((U) u));
        return targetCollection;
    } catch (Exception e) {
        throw new ConversionException("Cannot create DTO " + targetCls, e);
    }
}
Also used : Util.asDTO(org.apache.felix.schematizer.impl.Util.asDTO) Converter(org.osgi.util.converter.Converter) ConverterFunction(org.osgi.util.converter.ConverterFunction) TargetRule(org.osgi.util.converter.TargetRule) Collection(java.util.Collection) TypeReference(org.osgi.util.converter.TypeReference) Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) Schema(org.apache.felix.schematizer.Schema) ConversionException(org.osgi.util.converter.ConversionException) List(java.util.List) Node(org.apache.felix.schematizer.Node) DTO(org.osgi.dto.DTO) Type(java.lang.reflect.Type) Map(java.util.Map) Converters(org.osgi.util.converter.Converters) Util.rawClassOf(org.apache.felix.schematizer.impl.Util.rawClassOf) ConversionException(org.osgi.util.converter.ConversionException) ConversionException(org.osgi.util.converter.ConversionException)

Example 4 with ConversionException

use of org.osgi.util.converter.ConversionException in project felix by apache.

the class JsonDeserializingImpl method from.

@Override
public T from(InputStream in, Charset charset) {
    try {
        byte[] bytes = Util.readStream(in);
        String s = new String(bytes, charset);
        return from(s);
    } catch (IOException e) {
        throw new ConversionException("Error reading inputstream", e);
    }
}
Also used : ConversionException(org.osgi.util.converter.ConversionException) IOException(java.io.IOException)

Aggregations

ConversionException (org.osgi.util.converter.ConversionException)4 IOException (java.io.IOException)2 Field (java.lang.reflect.Field)2 Type (java.lang.reflect.Type)2 Collection (java.util.Collection)2 Map (java.util.Map)2 Node (org.apache.felix.schematizer.Node)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Schema (org.apache.felix.schematizer.Schema)1 Util.asDTO (org.apache.felix.schematizer.impl.Util.asDTO)1 Util.rawClassOf (org.apache.felix.schematizer.impl.Util.rawClassOf)1 DTO (org.osgi.dto.DTO)1 Converter (org.osgi.util.converter.Converter)1 ConverterFunction (org.osgi.util.converter.ConverterFunction)1 Converters (org.osgi.util.converter.Converters)1 TargetRule (org.osgi.util.converter.TargetRule)1 TypeReference (org.osgi.util.converter.TypeReference)1