Search in sources :

Example 11 with TypeReference

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

the class SchematizerServiceTest method testTypeRules.

@Test
public void testTypeRules() {
    Schema s = schematizer.type("MyDTO", "/embedded", new TypeReference<MyEmbeddedDTO2<String>>() {
    }).type("MyDTO", "/embedded/value", String.class).schematize("MyDTO", new TypeReference<MyDTO3<MyEmbeddedDTO2<String>>>() {
    }).get("MyDTO");
    assertNotNull(s);
    Node embeddedNode = s.nodeAtPath("/embedded/value");
    assertTrue(!"ERROR".equals(embeddedNode.name()));
    Node parentNode = s.parentOf(embeddedNode);
    assertTrue(!"ERROR".equals(parentNode.name()));
    Node grandparentNode = s.parentOf(parentNode);
    assertTrue(!"ERROR".equals(grandparentNode.name()));
    assertEquals("/", grandparentNode.absolutePath());
}
Also used : Schema(org.apache.felix.schematizer.Schema) Node(org.apache.felix.schematizer.Node) TypeReference(org.osgi.util.converter.TypeReference) Test(org.junit.Test)

Example 12 with TypeReference

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

the class SchemaBasedConverter method convertCollectionItemToDTO.

@SuppressWarnings("unchecked")
private <U> U convertCollectionItemToDTO(Object obj, Class<U> targetCls, Schema schema, String path) {
    Node node = schema.nodeAtPath(path);
    if (node.typeReference().isPresent()) {
        TypeReference<U> tr = (TypeReference<U>) Util.typeReferenceOf(node.typeReference().get());
        return converter.convert(obj).to(tr);
    } else {
        Type type = node.type();
        type.toString();
        // obj = converter.convert(val).to(type);
        return null;
    }
}
Also used : Type(java.lang.reflect.Type) Node(org.apache.felix.schematizer.Node) TypeReference(org.osgi.util.converter.TypeReference)

Example 13 with TypeReference

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

the class SchematizerServiceTest method testSchematizeDTO.

@Test
public void testSchematizeDTO() {
    Schema s = schematizer.schematize("MyDTO", new TypeReference<MyDTO>() {
    }).get("MyDTO");
    assertNotNull(s);
    Node root = s.rootNode();
    assertNodeEquals("", "/", false, MyDTO.class, false, root);
    assertEquals(4, root.children().size());
    Node pingNode = root.children().get("/ping");
    assertNodeEquals("ping", "/ping", false, String.class, true, pingNode);
    Node pongNode = root.children().get("/pong");
    assertNodeEquals("pong", "/pong", false, Long.class, true, pongNode);
    Node countNode = root.children().get("/count");
    assertNodeEquals("count", "/count", false, MyDTO.Count.class, true, countNode);
    Node embeddedNode = root.children().get("/embedded");
    assertEquals(3, embeddedNode.children().size());
    assertNodeEquals("embedded", "/embedded", false, MyEmbeddedDTO.class, true, embeddedNode);
    Node marcoNode = embeddedNode.children().get("/marco");
    assertNodeEquals("marco", "/embedded/marco", false, String.class, true, marcoNode);
    Node poloNode = embeddedNode.children().get("/polo");
    assertNodeEquals("polo", "/embedded/polo", false, Long.class, true, poloNode);
    Node alphaNode = embeddedNode.children().get("/alpha");
    assertNodeEquals("alpha", "/embedded/alpha", false, MyEmbeddedDTO.Alpha.class, true, alphaNode);
    Node sRoot = s.nodeAtPath("/");
    assertNodeEquals("", "/", false, MyDTO.class, false, sRoot);
    Node sPingNode = s.nodeAtPath("/ping");
    assertNodeEquals("ping", "/ping", false, String.class, true, sPingNode);
    Node sPongNode = s.nodeAtPath("/pong");
    assertNodeEquals("pong", "/pong", false, Long.class, true, sPongNode);
    Node sCountNode = s.nodeAtPath("/count");
    assertNodeEquals("count", "/count", false, MyDTO.Count.class, true, sCountNode);
    Node sEmbeddedNode = s.nodeAtPath("/embedded");
    assertNodeEquals("embedded", "/embedded", false, MyEmbeddedDTO.class, true, sEmbeddedNode);
    Node sMarcoNode = s.nodeAtPath("/embedded/marco");
    assertNodeEquals("marco", "/embedded/marco", false, String.class, true, sMarcoNode);
    Node sPoloNode = s.nodeAtPath("/embedded/polo");
    assertNodeEquals("polo", "/embedded/polo", false, Long.class, true, sPoloNode);
    Node sAlphaNode = s.nodeAtPath("/embedded/alpha");
    assertNodeEquals("alpha", "/embedded/alpha", false, MyEmbeddedDTO.Alpha.class, true, sAlphaNode);
}
Also used : Schema(org.apache.felix.schematizer.Schema) Node(org.apache.felix.schematizer.Node) TypeReference(org.osgi.util.converter.TypeReference) Test(org.junit.Test)

Example 14 with TypeReference

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

the class SchematizerServiceTest method testGetParentNode.

@Test
public void testGetParentNode() {
    Schema s = schematizer.schematize("MyDTO", new TypeReference<MyDTO>() {
    }).get("MyDTO");
    assertNotNull(s);
    Node embeddedNode = s.nodeAtPath("/embedded/marco");
    assertTrue(!"ERROR".equals(embeddedNode.name()));
    Node parentNode = s.parentOf(embeddedNode);
    assertTrue(!"ERROR".equals(parentNode.name()));
    Node grandparentNode = s.parentOf(parentNode);
    assertTrue(!"ERROR".equals(grandparentNode.name()));
    assertEquals("/", grandparentNode.absolutePath());
}
Also used : Schema(org.apache.felix.schematizer.Schema) Node(org.apache.felix.schematizer.Node) TypeReference(org.osgi.util.converter.TypeReference) Test(org.junit.Test)

Example 15 with TypeReference

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

the class TypeConverter method convert.

/**
 * Convert a value to the given type
 * @param value The value
 * @param typeInfo Optional type info, might be {@code null}
 * @return The converted value or {@code null} if the conversion failed.
 * @throws IOException If an error happens
 */
public Object convert(final String pid, final Object value, final String typeInfo) throws IOException {
    if (typeInfo == null) {
        if (value instanceof String || value instanceof Boolean) {
            return value;
        } else if (value instanceof Long || value instanceof Double) {
            return value;
        } else if (value instanceof Integer) {
            return ((Integer) value).longValue();
        } else if (value instanceof Short) {
            return ((Short) value).longValue();
        } else if (value instanceof Byte) {
            return ((Byte) value).longValue();
        } else if (value instanceof Float) {
            return ((Float) value).doubleValue();
        }
        if (value instanceof List) {
            @SuppressWarnings("unchecked") final List<Object> list = (List<Object>) value;
            if (list.isEmpty()) {
                return new String[0];
            }
            final Object firstObject = list.get(0);
            boolean hasListOrMap = false;
            for (final Object v : list) {
                if (v instanceof List || v instanceof Map) {
                    hasListOrMap = true;
                    break;
                }
            }
            Object convertedValue = null;
            if (!hasListOrMap) {
                if (firstObject instanceof Boolean) {
                    convertedValue = getConverter().convert(list).defaultValue(null).to(Boolean[].class);
                } else if (firstObject instanceof Long || firstObject instanceof Integer || firstObject instanceof Byte || firstObject instanceof Short) {
                    convertedValue = getConverter().convert(list).defaultValue(null).to(Long[].class);
                } else if (firstObject instanceof Double || firstObject instanceof Float) {
                    convertedValue = getConverter().convert(list).defaultValue(null).to(Double[].class);
                }
            }
            if (convertedValue == null) {
                convertedValue = getConverter().convert(list).defaultValue(null).to(String[].class);
            }
            return convertedValue;
        }
        return null;
    }
    // binary
    if ("binary".equals(typeInfo)) {
        if (provider == null) {
            throw new IOException("Binary files only allowed within a bundle");
        }
        final String path = getConverter().convert(value).defaultValue(null).to(String.class);
        if (path == null) {
            throw new IOException("Invalid path for binary property: " + value);
        }
        final File filePath;
        try {
            filePath = BinUtil.extractFile(provider, pid, path);
        } catch (final IOException ioe) {
            throw new IOException("Unable to read " + path + " in bundle " + provider.getIdentifier() + " for pid " + pid + " and write to " + BinUtil.binDirectory + " : " + ioe.getMessage(), ioe);
        }
        if (filePath == null) {
            throw new IOException("Entry " + path + " not found in bundle " + provider.getIdentifier());
        }
        files.add(filePath);
        allFiles.add(filePath);
        return filePath.getAbsolutePath();
    } else if ("binary[]".equals(typeInfo)) {
        if (provider == null) {
            throw new IOException("Binary files only allowed within a bundle");
        }
        final String[] paths = getConverter().convert(value).defaultValue(null).to(String[].class);
        if (paths == null) {
            throw new IOException("Invalid paths for binary[] property: " + value);
        }
        final String[] filePaths = new String[paths.length];
        int i = 0;
        while (i < paths.length) {
            final File filePath;
            try {
                filePath = BinUtil.extractFile(provider, pid, paths[i]);
            } catch (final IOException ioe) {
                throw new IOException("Unable to read " + paths[i] + " in bundle " + provider.getIdentifier() + " for pid " + pid + " and write to " + BinUtil.binDirectory + " : " + ioe.getMessage(), ioe);
            }
            if (filePath == null) {
                throw new IOException("Entry " + paths[i] + " not found in bundle " + provider.getIdentifier());
            }
            files.add(filePath);
            allFiles.add(filePath);
            filePaths[i] = filePath.getAbsolutePath();
            i++;
        }
        return filePaths;
    }
    final Class<?> typeClass = TYPE_MAP.get(typeInfo);
    if (typeClass != null) {
        return getConverter().convert(value).defaultValue(null).to(typeClass);
    }
    // Collections of scalar types
    if ("Collection<String>".equals(typeInfo)) {
        return getConverter().convert(value).defaultValue(null).to(new TypeReference<ArrayList<String>>() {
        });
    } else if ("Collection<Integer>".equals(typeInfo)) {
        return getConverter().convert(value).defaultValue(null).to(new TypeReference<ArrayList<Integer>>() {
        });
    } else if ("Collection<Long>".equals(typeInfo)) {
        return getConverter().convert(value).defaultValue(null).to(new TypeReference<ArrayList<Long>>() {
        });
    } else if ("Collection<Float>".equals(typeInfo)) {
        return getConverter().convert(value).defaultValue(null).to(new TypeReference<ArrayList<Float>>() {
        });
    } else if ("Collection<Double>".equals(typeInfo)) {
        return getConverter().convert(value).defaultValue(null).to(new TypeReference<ArrayList<Double>>() {
        });
    } else if ("Collection<Byte>".equals(typeInfo)) {
        return getConverter().convert(value).defaultValue(null).to(new TypeReference<ArrayList<Byte>>() {
        });
    } else if ("Collection<Short>".equals(typeInfo)) {
        return getConverter().convert(value).defaultValue(null).to(new TypeReference<ArrayList<Short>>() {
        });
    } else if ("Collection<Character>".equals(typeInfo)) {
        return getConverter().convert(value).defaultValue(null).to(new TypeReference<ArrayList<Character>>() {
        });
    } else if ("Collection<Boolean>".equals(typeInfo)) {
        return getConverter().convert(value).defaultValue(null).to(new TypeReference<ArrayList<Boolean>>() {
        });
    } else if ("Collection".equals(typeInfo)) {
        if (value instanceof List) {
            @SuppressWarnings("unchecked") final List<Object> list = (List<Object>) value;
            if (list.isEmpty()) {
                return Collections.EMPTY_LIST;
            }
            final Object firstObject = list.get(0);
            boolean hasListOrMap = false;
            for (final Object v : list) {
                if (v instanceof List || v instanceof Map) {
                    hasListOrMap = true;
                    break;
                }
            }
            Object convertedValue = null;
            if (!hasListOrMap) {
                if (firstObject instanceof Boolean) {
                    convertedValue = getConverter().convert(list).defaultValue(null).to(new TypeReference<ArrayList<Boolean>>() {
                    });
                } else if (firstObject instanceof Long || firstObject instanceof Integer || firstObject instanceof Byte || firstObject instanceof Short) {
                    convertedValue = getConverter().convert(list).defaultValue(null).to(new TypeReference<ArrayList<Long>>() {
                    });
                } else if (firstObject instanceof Double || firstObject instanceof Float) {
                    convertedValue = getConverter().convert(list).defaultValue(null).to(new TypeReference<ArrayList<Double>>() {
                    });
                }
            }
            if (convertedValue == null) {
                convertedValue = getConverter().convert(list).defaultValue(null).to(new TypeReference<List<String>>() {
                });
            }
            return convertedValue;
        }
        return getConverter().convert(value).defaultValue(null).to(ArrayList.class);
    }
    // unknown type - ignore configuration
    throw new IOException("Invalid type information: " + typeInfo);
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) TypeReference(org.osgi.util.converter.TypeReference) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File)

Aggregations

TypeReference (org.osgi.util.converter.TypeReference)15 Test (org.junit.Test)13 Schema (org.apache.felix.schematizer.Schema)11 Node (org.apache.felix.schematizer.Node)6 ArrayList (java.util.ArrayList)2 List (java.util.List)2 SchematizerImpl (org.apache.felix.schematizer.impl.SchematizerImpl)2 Converter (org.osgi.util.converter.Converter)2 File (java.io.File)1 IOException (java.io.IOException)1 Type (java.lang.reflect.Type)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Ignore (org.junit.Ignore)1