Search in sources :

Example 1 with SchemaAnnotationHandler

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");
    }
}
Also used : SchemaAnnotationHandler(com.linkedin.data.schema.annotation.SchemaAnnotationHandler) GnuParser(org.apache.commons.cli.GnuParser) ArrayList(java.util.ArrayList) SchemaAnnotationProcessor(com.linkedin.data.schema.annotation.SchemaAnnotationProcessor) DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Example 2 with SchemaAnnotationHandler

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;
}
Also used : Objects(java.util.Objects) SchemaAnnotationHandler(com.linkedin.data.schema.annotation.SchemaAnnotationHandler) URLClassLoader(java.net.URLClassLoader) List(java.util.List) Logger(org.slf4j.Logger) Paths(java.nio.file.Paths) URL(java.net.URL) StringTokenizer(java.util.StringTokenizer) LoggerFactory(org.slf4j.LoggerFactory) File(java.io.File) ArrayList(java.util.ArrayList) SchemaAnnotationHandler(com.linkedin.data.schema.annotation.SchemaAnnotationHandler) URLClassLoader(java.net.URLClassLoader) ArrayList(java.util.ArrayList) Objects(java.util.Objects) URLClassLoader(java.net.URLClassLoader) URL(java.net.URL)

Aggregations

SchemaAnnotationHandler (com.linkedin.data.schema.annotation.SchemaAnnotationHandler)2 ArrayList (java.util.ArrayList)2 DataSchema (com.linkedin.data.schema.DataSchema)1 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)1 SchemaAnnotationProcessor (com.linkedin.data.schema.annotation.SchemaAnnotationProcessor)1 File (java.io.File)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 Paths (java.nio.file.Paths)1 List (java.util.List)1 Objects (java.util.Objects)1 StringTokenizer (java.util.StringTokenizer)1 CommandLine (org.apache.commons.cli.CommandLine)1 CommandLineParser (org.apache.commons.cli.CommandLineParser)1 GnuParser (org.apache.commons.cli.GnuParser)1 ParseException (org.apache.commons.cli.ParseException)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1