Search in sources :

Example 31 with NamedDataSchema

use of com.linkedin.data.schema.NamedDataSchema 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 32 with NamedDataSchema

use of com.linkedin.data.schema.NamedDataSchema 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 33 with NamedDataSchema

use of com.linkedin.data.schema.NamedDataSchema 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 34 with NamedDataSchema

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

the class PdlSchemaParser method parseNamedType.

private NamedDataSchema parseNamedType(NamedTypeDeclarationContext namedType) throws ParseException {
    NamedDataSchema schema;
    if (namedType.recordDeclaration() != null) {
        schema = parseRecord(namedType, namedType.recordDeclaration());
    } else if (namedType.typerefDeclaration() != null) {
        schema = parseTyperef(namedType, namedType.typerefDeclaration());
    } else if (namedType.fixedDeclaration() != null) {
        schema = parseFixed(namedType, namedType.fixedDeclaration());
    } else if (namedType.enumDeclaration() != null) {
        schema = parseEnum(namedType, namedType.enumDeclaration());
    } else {
        throw new ParseException(namedType, "Unrecognized named type parse node: " + namedType.getText());
    }
    schema.setPackage(getCurrentPackage());
    return schema;
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema)

Example 35 with NamedDataSchema

use of com.linkedin.data.schema.NamedDataSchema 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)

Aggregations

NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)46 DataSchema (com.linkedin.data.schema.DataSchema)25 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)16 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)12 MapDataSchema (com.linkedin.data.schema.MapDataSchema)11 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)11 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)11 DataMap (com.linkedin.data.DataMap)8 IOException (java.io.IOException)8 DataSchemaLocation (com.linkedin.data.schema.DataSchemaLocation)7 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)7 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)6 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)6 SchemaParser (com.linkedin.data.schema.SchemaParser)6 ComplexDataSchema (com.linkedin.data.schema.ComplexDataSchema)5 File (java.io.File)5 Test (org.testng.annotations.Test)5 CustomInfoSpec (com.linkedin.pegasus.generator.spec.CustomInfoSpec)4 RestLiInternalException (com.linkedin.restli.internal.server.RestLiInternalException)4 FileInputStream (java.io.FileInputStream)4