Search in sources :

Example 6 with DataSchemaLocation

use of com.linkedin.data.schema.DataSchemaLocation in project rest.li by linkedin.

the class AbstractDataSchemaResolver method locateDataSchema.

/**
   * Locate a {@link NamedDataSchema} with the specified name.
   *
   * @param name of schema to locate.
   * @param errorMessageBuilder to append error messages to.
   * @return the NamedDataSchema if it can be located, else return null.
   */
protected NamedDataSchema locateDataSchema(String name, StringBuilder errorMessageBuilder) {
    NamedDataSchema schema = null;
    Iterator<DataSchemaLocation> locations = possibleLocations(name);
    while (locations.hasNext()) {
        DataSchemaLocation location = locations.next();
        if (location == null || isBadLocation(location)) {
            continue;
        }
        InputStream inputStream = null;
        try {
            inputStream = locationToInputStream(location, errorMessageBuilder);
            if (inputStream == null) {
                addBadLocation(location);
            } else {
                schema = parse(inputStream, location, name, errorMessageBuilder);
                if (schema != null) {
                    break;
                }
            }
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException exc) {
                }
            }
        }
    }
    return schema;
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) FilterInputStream(java.io.FilterInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) DataSchemaLocation(com.linkedin.data.schema.DataSchemaLocation)

Example 7 with DataSchemaLocation

use of com.linkedin.data.schema.DataSchemaLocation in project rest.li by linkedin.

the class ClassNameDataSchemaResolver method locateDataSchema.

@Override
protected NamedDataSchema locateDataSchema(String className, StringBuilder errorMessageBuilder) {
    final DataSchemaLocation location = new ClassNameDataSchemaLocation(className);
    if (isBadLocation(location)) {
        return null;
    }
    final Class<?> clazz;
    try {
        clazz = _classLoader.loadClass(className);
    } catch (ClassNotFoundException e) {
        addBadLocation(location);
        errorMessageBuilder.append(String.format("Unable to locate DataSchema: class \"%s\" not found", className));
        return null;
    }
    final DataSchema schema = DataTemplateUtil.getSchema(clazz);
    if (schema instanceof NamedDataSchema) {
        return (NamedDataSchema) schema;
    }
    if (DataTemplate.class.isAssignableFrom(clazz)) {
        @SuppressWarnings("unchecked") final Class<? extends DataTemplate<?>> clazzWithTyperef = (Class<? extends DataTemplate<?>>) clazz;
        final TyperefInfo typerefInfo = DataTemplateUtil.getTyperefInfo(clazzWithTyperef);
        if (typerefInfo != null) {
            return typerefInfo.getSchema();
        }
    }
    addBadLocation(location);
    errorMessageBuilder.append(String.format("Unable to locate DataSchema: class \"%s\" is not a NamedDataSchema", className));
    return null;
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) DataTemplate(com.linkedin.data.template.DataTemplate) DataSchemaLocation(com.linkedin.data.schema.DataSchemaLocation) TyperefInfo(com.linkedin.data.template.TyperefInfo)

Example 8 with DataSchemaLocation

use of com.linkedin.data.schema.DataSchemaLocation in project rest.li by linkedin.

the class ClasspathResourceDataSchemaResolver method locateDataSchema.

@Override
protected NamedDataSchema locateDataSchema(String schemaName, StringBuilder errorMessageBuilder) {
    NamedDataSchema schema = null;
    final String schemaResourcePath = getDataSchemaResourcePath(schemaName);
    try (InputStream stream = _classLoader.getResourceAsStream(schemaResourcePath)) {
        if (stream == null) {
            errorMessageBuilder.append(String.format("Unable to find data schema file \"%s\" in classpath.", schemaResourcePath));
        } else {
            DataSchemaLocation location = new FileDataSchemaLocation(new File(schemaResourcePath));
            schema = parse(stream, location, schemaName, errorMessageBuilder);
        }
    } catch (IOException e) {
        errorMessageBuilder.append(String.format("Failed to read/close data schema file \"%s\" in classpath: \"%s\"", schemaResourcePath, e.getMessage()));
    }
    return schema;
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) InputStream(java.io.InputStream) IOException(java.io.IOException) File(java.io.File) DataSchemaLocation(com.linkedin.data.schema.DataSchemaLocation)

Example 9 with DataSchemaLocation

use of com.linkedin.data.schema.DataSchemaLocation in project rest.li by linkedin.

the class FileFormatDataSchemaParser method parseJarEntry.

private void parseJarEntry(JarFile schemaJarFile, JarEntry jarEntry, DataSchemaParser.ParseResult result) throws IOException {
    final DataSchemaLocation location = getSchemaLocation(schemaJarFile, jarEntry.getName());
    if (_schemaResolver.locationResolved(location)) {
        return;
    }
    final InputStream jarStream = schemaJarFile.getInputStream(jarEntry);
    final List<DataSchema> schemas = parseSchemaStream(jarStream, location, result);
    for (DataSchema schema : schemas) {
        if (schema instanceof NamedDataSchema) {
            validateSchemaWithPath(location.toString(), (NamedDataSchema) schema);
        }
        result.getSchemaAndLocations().put(schema, location);
    }
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) InJarFileDataSchemaLocation(com.linkedin.data.schema.resolver.InJarFileDataSchemaLocation) DataSchemaLocation(com.linkedin.data.schema.DataSchemaLocation) FileDataSchemaLocation(com.linkedin.data.schema.resolver.FileDataSchemaLocation)

Example 10 with DataSchemaLocation

use of com.linkedin.data.schema.DataSchemaLocation in project rest.li by linkedin.

the class TestDataSchemaResolver method lookup.

public void lookup(DataSchemaResolver resolver, String[][] lookups, char separator, boolean debug) {
    PegasusSchemaParser parser = new SchemaParser(resolver);
    for (String[] entry : lookups) {
        String name = entry[0];
        String expectFound = entry[1];
        String expected = entry[2];
        DataSchema schema = parser.lookupName(name);
        if (debug) {
            out.println("----" + name + "-----");
        }
        String errorMessage = parser.errorMessage();
        if (debug && errorMessage.isEmpty() == false) {
            out.println(errorMessage);
        }
        if (expectFound == ERROR) {
            assertTrue(parser.hasError());
            assertTrue(expected == null || errorMessage.contains(expected));
        } else if (expectFound == FOUND) {
            assertTrue(schema != null);
            String schemaText = schema.toString();
            if (debug) {
                out.println(schemaText);
            }
            assertFalse(parser.hasError());
            assertTrue(schema instanceof NamedDataSchema);
            NamedDataSchema namedSchema = (NamedDataSchema) schema;
            assertEquals(namedSchema.getFullName(), name);
            assertTrue(schemaText.contains(expected));
            assertTrue(resolver.bindings().containsKey(name));
            assertSame(resolver.bindings().get(name), namedSchema);
            String location = entry[3];
            DataSchemaLocation namedSchemalocation = resolver.nameToDataSchemaLocations().get(name);
            String locationNorm;
            if (namedSchemalocation.toString().contains(".jar")) {
                locationNorm = location.replace(separator, '/');
            } else {
                locationNorm = location.replace('/', separator);
            }
            assertNotNull(namedSchemalocation);
            assertEquals(namedSchemalocation.toString().indexOf(locationNorm), namedSchemalocation.toString().length() - locationNorm.length());
            assertTrue(resolver.locationResolved(namedSchemalocation));
        } else if (expectFound == NOT_FOUND) {
            assertTrue(schema == null);
            assertFalse(parser.hasError());
            assertTrue(expected == null || errorMessage.contains(expected));
            assertFalse(resolver.bindings().containsKey(name));
        } else {
            assertTrue(false);
        }
    }
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) SchemaParser(com.linkedin.data.schema.SchemaParser) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) DataSchemaLocation(com.linkedin.data.schema.DataSchemaLocation)

Aggregations

DataSchemaLocation (com.linkedin.data.schema.DataSchemaLocation)10 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)9 DataSchema (com.linkedin.data.schema.DataSchema)6 FileDataSchemaLocation (com.linkedin.data.schema.resolver.FileDataSchemaLocation)5 File (java.io.File)5 InputStream (java.io.InputStream)4 Map (java.util.Map)4 InJarFileDataSchemaLocation (com.linkedin.data.schema.resolver.InJarFileDataSchemaLocation)3 IOException (java.io.IOException)3 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)2 FileInputStream (java.io.FileInputStream)2 DataSchemaResolver (com.linkedin.data.schema.DataSchemaResolver)1 PegasusSchemaParser (com.linkedin.data.schema.PegasusSchemaParser)1 SchemaParser (com.linkedin.data.schema.SchemaParser)1 DataTemplate (com.linkedin.data.template.DataTemplate)1 TyperefInfo (com.linkedin.data.template.TyperefInfo)1 ClassTemplateSpec (com.linkedin.pegasus.generator.spec.ClassTemplateSpec)1 FileUtil (com.linkedin.util.FileUtil)1 FileCodeWriter (com.sun.codemodel.writer.FileCodeWriter)1 FilterInputStream (java.io.FilterInputStream)1