Example usage for org.springframework.beans BeanInstantiationException getCause

List of usage examples for org.springframework.beans BeanInstantiationException getCause

Introduction

In this page you can find the example usage for org.springframework.beans BeanInstantiationException getCause.

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:com.cloudera.cli.validator.Main.java

/**
 * From the arguments, run the validation.
 *
 * @param args command line arguments/*from   w  ww  .  j a  v a  2  s .  c o  m*/
 * @throws IOException anything goes wrong with streams.
 * @return exit code
 */
public int run(String[] args) throws IOException {
    Writer writer = new OutputStreamWriter(outStream, Constants.CHARSET_UTF_8);
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    try {
        BeanDefinition cmdOptsbeanDefinition = BeanDefinitionBuilder
                .rootBeanDefinition(CommandLineOptions.class).addConstructorArgValue(appName)
                .addConstructorArgValue(args).getBeanDefinition();
        ctx.registerBeanDefinition(CommandLineOptions.BEAN_NAME, cmdOptsbeanDefinition);
        ctx.register(ApplicationConfiguration.class);
        ctx.refresh();
        CommandLineOptions cmdOptions = ctx.getBean(CommandLineOptions.BEAN_NAME, CommandLineOptions.class);
        CommandLineOptions.Mode mode = cmdOptions.getMode();
        if (mode == null) {
            throw new ParseException("No valid command line arguments");
        }
        ValidationRunner runner = ctx.getBean(mode.runnerName, ValidationRunner.class);
        boolean success = runner.run(cmdOptions.getCommandLineOptionActiveTarget(), writer);
        if (success) {
            writer.write("Validation succeeded.\n");
        }
        return success ? 0 : -1;
    } catch (BeanCreationException e) {
        String cause = e.getMessage();
        if (e.getCause() instanceof BeanInstantiationException) {
            BeanInstantiationException bie = (BeanInstantiationException) e.getCause();
            cause = bie.getMessage();
            if (bie.getCause() != null) {
                cause = bie.getCause().getMessage();
            }
        }
        IOUtils.write(cause + "\n", errStream);
        CommandLineOptions.printUsageMessage(appName, errStream);
        return -2;
    } catch (ParseException e) {
        LOG.debug("Exception", e);
        IOUtils.write(e.getMessage() + "\n", errStream);
        CommandLineOptions.printUsageMessage(appName, errStream);
        return -2;
    } finally {
        if (ctx != null) {
            ctx.close();
        }
        writer.close();
    }
}

From source file:org.springframework.test.context.support.AbstractTestContextBootstrapper.java

private List<TestExecutionListener> instantiateListeners(
        Collection<Class<? extends TestExecutionListener>> classes) {
    List<TestExecutionListener> listeners = new ArrayList<>(classes.size());
    for (Class<? extends TestExecutionListener> listenerClass : classes) {
        try {//from w  w  w.  j  a v a2  s.c o m
            listeners.add(BeanUtils.instantiateClass(listenerClass));
        } catch (BeanInstantiationException ex) {
            if (ex.getCause() instanceof NoClassDefFoundError) {
                // TestExecutionListener not applicable due to a missing dependency
                if (logger.isDebugEnabled()) {
                    logger.debug(String.format(
                            "Skipping candidate TestExecutionListener [%s] due to a missing dependency. "
                                    + "Specify custom listener classes or make the default listener classes "
                                    + "and their required dependencies available. Offending class: [%s]",
                            listenerClass.getName(), ex.getCause().getMessage()));
                }
            } else {
                throw ex;
            }
        }
    }
    return listeners;
}