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;
}
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;
}
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;
}
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);
}
}
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);
}
}
}
Aggregations