Example usage for org.springframework.context ApplicationContext getClassLoader

List of usage examples for org.springframework.context ApplicationContext getClassLoader

Introduction

In this page you can find the example usage for org.springframework.context ApplicationContext getClassLoader.

Prototype

@Nullable
ClassLoader getClassLoader();

Source Link

Document

Expose the ClassLoader used by this ResourceLoader.

Usage

From source file:org.apache.camel.spring.SpringCamelContext.java

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = applicationContext;
    ClassLoader cl;//w  w w  .j  a  va 2 s  . c o m

    // set the application context classloader
    if (applicationContext != null && applicationContext.getClassLoader() != null) {
        cl = applicationContext.getClassLoader();
    } else {
        LOG.warn(
                "Cannot find the class loader from application context, using the thread context class loader instead");
        cl = Thread.currentThread().getContextClassLoader();
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Set the application context classloader to: " + cl);
    }
    this.setApplicationContextClassLoader(cl);

    if (applicationContext instanceof ConfigurableApplicationContext) {
        // only add if not already added
        if (hasComponent("spring-event") == null) {
            addComponent("spring-event", new EventComponent(applicationContext));
        }
    }
}

From source file:org.cloudifysource.usm.USMUtils.java

/*******
 * Returns true if current process is a GSC.
 *
 * @param ctx//from w  ww . j  av  a 2s . c o  m
 *            the spring application context.
 * @return .
 */
public static boolean isRunningInGSC(final ApplicationContext ctx) {
    return ctx.getClassLoader() instanceof ServiceClassLoader;
}

From source file:org.cloudifysource.usm.USMUtils.java

/*************
 * Return's the working directory of a PU.
 *
 * @param ctx// ww  w. j av  a  2  s  .c o m
 *            the spring application context.
 * @return the working directory.
 */
public static File getPUWorkDir(final ApplicationContext ctx) {
    File puWorkDir = null;

    if (isRunningInGSC(ctx)) {
        // running in GSC
        final ServiceClassLoader scl = (ServiceClassLoader) ctx.getClassLoader();

        final URL url = scl.getSlashPath();
        URI uri;
        try {
            uri = url.toURI();
        } catch (final URISyntaxException e) {
            throw new IllegalArgumentException("Failed to create URI from URL: " + url, e);
        }

        puWorkDir = new File(uri);

    } else {

        final ResourceApplicationContext rac = (ResourceApplicationContext) ctx;

        try {
            final Field resourcesField = rac.getClass().getDeclaredField("resources");
            final boolean accessibleBefore = resourcesField.isAccessible();

            resourcesField.setAccessible(true);
            final Resource[] resources = (Resource[]) resourcesField.get(rac);
            for (final Resource resource : resources) {
                // find META-INF/spring/pu.xml
                final File file = resource.getFile();
                if (file.getName().equals("pu.xml") && file.getParentFile().getName().equals("spring")
                        && file.getParentFile().getParentFile().getName().equals("META-INF")) {
                    puWorkDir = resource.getFile().getParentFile().getParentFile().getParentFile();
                    break;
                }

            }

            resourcesField.setAccessible(accessibleBefore);
        } catch (final Exception e) {
            throw new IllegalArgumentException("Could not find pu.xml in the ResourceApplicationContext", e);
        }
        if (puWorkDir == null) {
            throw new IllegalArgumentException("Could not find pu.xml in the ResourceApplicationContext");
        }
    }

    if (!puWorkDir.exists()) {
        throw new IllegalStateException("Could not find PU work dir at: " + puWorkDir);
    }

    final File puExtDir = new File(puWorkDir, "ext");
    if (!puExtDir.exists()) {
        throw new IllegalStateException("Could not find PU ext dir at: " + puExtDir);
    }

    return puWorkDir;
}