use of com.linkedin.data.schema.annotation.SchemaAnnotationHandler in project rest.li by linkedin.
the class SchemaAnnotationValidatorCmdLineApp method main.
public static void main(String[] args) throws IOException {
String resolverPath = null;
String handlerJarPaths = null;
String handlerClassNames = null;
String inputDir = null;
try {
final CommandLineParser parser = new GnuParser();
CommandLine cl = parser.parse(_options, args);
if (cl.hasOption('h')) {
help();
System.exit(0);
}
String[] cliArgs = cl.getArgs();
if (cliArgs.length != 1) {
_log.error("Wrong argument given");
help();
System.exit(1);
}
resolverPath = RestLiToolsUtils.readArgFromFileIfNeeded(cl.getOptionValue('r'));
;
handlerJarPaths = cl.getOptionValue('j');
handlerClassNames = cl.getOptionValue('c');
inputDir = cliArgs[0];
} catch (ParseException e) {
_log.error("Invalid arguments: " + e.getMessage());
help();
System.exit(1);
}
List<SchemaAnnotationHandler> handlers = null;
try {
handlers = ClassJarPathUtil.getAnnotationHandlers(handlerJarPaths, handlerClassNames);
} catch (IllegalStateException e) {
_log.error(e.getMessage());
throw new IllegalStateException("ValidateSchemaAnnotation task failed");
}
boolean hasError = false;
List<String> schemaWithFailures = new ArrayList<>();
List<DataSchema> namedDataSchema = parseSchemas(resolverPath, inputDir);
for (DataSchema dataSchema : namedDataSchema) {
SchemaAnnotationProcessor.SchemaAnnotationProcessResult result = SchemaAnnotationProcessor.process(handlers, dataSchema, new SchemaAnnotationProcessor.AnnotationProcessOption());
// If any of the nameDataSchema failed to be processed, log error and throw exception
if (result.hasError()) {
String schemaName = ((NamedDataSchema) dataSchema).getFullName();
_log.error("Annotation processing for data schema [{}] failed, detailed error: \n", schemaName);
_log.error(result.getErrorMsgs());
schemaWithFailures.add(schemaName);
hasError = true;
} else {
_log.info("Successfully resolved and validated data schema [{}]", ((NamedDataSchema) dataSchema).getFullName());
}
}
if (hasError) {
_log.error("ValidateSchemaAnnotation task failed due to failure in following schemas [{}]", schemaWithFailures);
// Throw exception at the end if any of
throw new IllegalStateException("ValidateSchemaAnnotation task failed");
}
}
use of com.linkedin.data.schema.annotation.SchemaAnnotationHandler in project rest.li by linkedin.
the class ClassJarPathUtil method getAnnotationHandlers.
/**
* A helper method which is used to get the SchemaAnnotationHandler classes based on the given handlerJarPaths and class names
* @param handlerJarPaths
* @param classNames
* @return a list of SchemaAnnotationHandler classes. List<SchemaAnnotationHandler>
* @throws IllegalStateException if it could not instantiate the given class.
*/
public static List<SchemaAnnotationHandler> getAnnotationHandlers(String handlerJarPaths, String classNames) throws IllegalStateException {
List<SchemaAnnotationHandler> handlers = new ArrayList<>();
ClassLoader classLoader = new URLClassLoader(parsePaths(handlerJarPaths).stream().map(str -> {
try {
return Paths.get(str).toUri().toURL();
} catch (Exception e) {
_logger.error("Parsing class jar path URL {} parsing failed", str, e);
}
return null;
}).filter(Objects::nonNull).toArray(URL[]::new));
for (String className : parsePaths(classNames)) {
try {
Class<?> handlerClass = Class.forName(className, false, classLoader);
SchemaAnnotationHandler handler = (SchemaAnnotationHandler) handlerClass.newInstance();
handlers.add(handler);
} catch (Exception e) {
throw new IllegalStateException("Error instantiating class: " + className + e.getMessage(), e);
}
}
return handlers;
}
Aggregations