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:io.acme.solution.application.conf.CommandHandlerUtils.java

public static Map<String, CommandHandler> buildCommandHandlersRegistry(final String basePackage,
        final ApplicationContext context) {

    final Map<String, CommandHandler> registry = new HashMap<>();
    final ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(
            false);//ww  w.j  a v  a  2s.c  o m
    final AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory();
    scanner.addIncludeFilter(new AssignableTypeFilter(CommandHandler.class));

    CommandHandler currentHandler = null;

    for (BeanDefinition bean : scanner.findCandidateComponents(basePackage)) {
        currentHandler = (CommandHandler) beanFactory.createBean(
                ClassUtils.resolveClassName(bean.getBeanClassName(), context.getClassLoader()),
                AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
        registry.put(currentHandler.getInterest(), currentHandler);
    }

    return registry;
}

From source file:com.liferay.portal.spring.extender.internal.hibernate.configuration.ModuleHibernateConfiguration.java

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

    _classLoader = applicationContext.getClassLoader();
}

From source file:com.zuoxiaolong.niubi.job.spring.bean.SpringJobBeanFactory.java

/**
 * ?/*from  w  w w .  ja v  a  2s .c o  m*/
 *
 * @param applicationContext ApplicationContext
 */
public SpringJobBeanFactory(ApplicationContext applicationContext) {
    this.classLoader = applicationContext.getClassLoader();
    this.applicationContext = applicationContext;
}

From source file:com.zuoxiaolong.niubi.job.spring.node.SimpleSpringLocalJobNode.java

public SimpleSpringLocalJobNode(ApplicationContext applicationContext, String packagesToScan) {
    ClassHelper.overrideThreadContextClassLoader(applicationContext.getClassLoader());
    JobBeanFactory jobBeanFactory = new SpringJobBeanFactory(applicationContext);
    JobScanner jobScanner = JobScannerFactory.createClasspathJobScanner(ClassHelper.getDefaultClassLoader(),
            packagesToScan);//from   w  w  w.  j a  v a  2 s .  co  m
    schedulerManager = new DefaultAutomaticScheduleManager(jobBeanFactory, jobScanner.getJobDescriptorList());
}

From source file:com.agileapes.couteau.context.spring.event.impl.AbstractMappedEventsTranslationScheme.java

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    classLoader = applicationContext.getClassLoader();
}

From source file:com.haulmont.cuba.web.sys.WebJarResourceResolver.java

@EventListener
@Order(Events.HIGHEST_PLATFORM_PRECEDENCE + 200)
protected void init(@SuppressWarnings("unused") AppContextInitializedEvent event) {
    StopWatch stopWatch = new Slf4JStopWatch("WebJARs");
    try {/* w  ww.j  a v  a2  s  . c om*/
        ApplicationContext applicationContext = event.getApplicationContext();

        ClassLoader classLoader = applicationContext.getClassLoader();

        log.debug("Scanning WebJAR resources in {}", classLoader);

        scanResources(applicationContext);

        log.debug("Loaded {} WebJAR paths", mapping.size());
    } catch (IOException e) {
        throw new RuntimeException("Unable to load WebJAR resources");
    } finally {
        stopWatch.stop();
    }
}

From source file:org.solmix.runtime.support.spring.SpringContainer.java

/**
 * {@inheritDoc}/*from  w w  w . j  a  va  2  s .c om*/
 * 
 * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
 */
@SuppressWarnings("rawtypes")
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = (AbstractApplicationContext) applicationContext;
    ApplicationListener listener = new ApplicationListener() {

        @Override
        public void onApplicationEvent(ApplicationEvent event) {
            SpringContainer.this.onApplicationEvent(event);
        }
    };
    this.applicationContext.addApplicationListener(listener);
    ApplicationContext ac = applicationContext.getParent();
    while (ac != null) {
        if (ac instanceof AbstractApplicationContext) {
            ((AbstractApplicationContext) ac).addApplicationListener(listener);
        }
        ac = ac.getParent();
    }
    setExtension(applicationContext.getClassLoader(), ClassLoader.class);
    setExtension(new SpringConfigurer(applicationContext), BeanConfigurer.class);
    //
    setExtension(applicationContext, ApplicationContext.class);
    //        setBean(new SpringConfigureUnitManager(), ConfigureUnitManager.class);
    ResourceManager m = getExtension(ResourceManager.class);
    m.addResourceResolver(new SpringResourceResolver(applicationContext));
    //at last add the spring bean provider.
    ConfiguredBeanProvider provider = getExtension(ConfiguredBeanProvider.class);
    if (!(provider instanceof SpringBeanProvider)) {
        setExtension(new SpringBeanProvider(applicationContext, this), ConfiguredBeanProvider.class);
    }
    if (getStatus() != ContainerStatus.CREATED) {
        initialize();
    }
}

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

@Override
public void setApplicationContext(final ApplicationContext arg0) throws BeansException {
    // CHECKSTYLE:ON
    this.applicationContext = arg0;

    if (arg0.getClassLoader() instanceof ServiceClassLoader) {
        // running in GSC
        this.runningInGSC = true;
        final ServiceClassLoader scl = (ServiceClassLoader) arg0.getClassLoader();

        final URL url = scl.getSlashPath();
        logger.fine("The slashpath URL is: " + url);
        URI uri;//from   w w  w  .  jav a  2s. co  m
        try {
            uri = url.toURI();
        } catch (final URISyntaxException e) {
            throw new IllegalArgumentException(e);
        }
        logger.fine("The slashpath URI is: " + uri);
        this.puWorkDir = new File(uri);
        this.puExtDir = new File(this.puWorkDir, "ext");
        return;

    }

    final ResourceApplicationContext rac = (ResourceApplicationContext) arg0;

    try {
        this.runningInGSC = false;
        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();
                puExtDir = new File(puWorkDir, "ext");
                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");
    }

}

From source file:org.red5.server.plugin.PluginLauncher.java

public void afterPropertiesSet() throws Exception {

    ApplicationContext common = (ApplicationContext) applicationContext.getBean("red5.common");
    Server server = (Server) common.getBean("red5.server");

    //server should be up and running at this point so load any plug-ins now         

    //get the plugins dir
    File pluginsDir = new File(System.getProperty("red5.root"), "plugins");

    File[] plugins = pluginsDir.listFiles(new FilenameFilter() {
        public boolean accept(File dir, String name) {
            //lower the case
            String tmp = name.toLowerCase();
            //accept jars and zips
            return tmp.endsWith(".jar") || tmp.endsWith(".zip");
        }/*  w w  w.  j a v a  2s.  co m*/
    });

    if (plugins != null) {

        IRed5Plugin red5Plugin = null;

        log.debug("{} plugins to launch", plugins.length);
        for (File plugin : plugins) {
            JarFile jar = null;
            Manifest manifest = null;
            try {
                jar = new JarFile(plugin, false);
                manifest = jar.getManifest();
            } catch (Exception e1) {
                log.warn("Error loading plugin manifest: {}", plugin);
            } finally {
                jar.close();
            }
            if (manifest == null) {
                continue;
            }
            Attributes attributes = manifest.getMainAttributes();
            if (attributes == null) {
                continue;
            }
            String pluginMainClass = attributes.getValue("Red5-Plugin-Main-Class");
            if (pluginMainClass == null || pluginMainClass.length() <= 0) {
                continue;
            }
            // attempt to load the class; since it's in the plugins directory this should work
            ClassLoader loader = common.getClassLoader();
            Class<?> pluginClass;
            String pluginMainMethod = null;
            try {
                pluginClass = Class.forName(pluginMainClass, true, loader);
            } catch (ClassNotFoundException e) {
                continue;
            }
            try {
                //handle plug-ins without "main" methods
                pluginMainMethod = attributes.getValue("Red5-Plugin-Main-Method");
                if (pluginMainMethod == null || pluginMainMethod.length() <= 0) {
                    //just get an instance of the class
                    red5Plugin = (IRed5Plugin) pluginClass.newInstance();
                } else {
                    Method method = pluginClass.getMethod(pluginMainMethod, (Class<?>[]) null);
                    Object o = method.invoke(null, (Object[]) null);
                    if (o != null && o instanceof IRed5Plugin) {
                        red5Plugin = (IRed5Plugin) o;
                    }
                }
                //register and start
                if (red5Plugin != null) {
                    //set top-level context
                    red5Plugin.setApplicationContext(applicationContext);
                    //set server reference
                    red5Plugin.setServer(server);
                    //register the plug-in to make it available for lookups
                    PluginRegistry.register(red5Plugin);
                    //start the plugin
                    red5Plugin.doStart();
                }
                log.info("Loaded plugin: {}", pluginMainClass);
            } catch (Throwable t) {
                log.warn("Error loading plugin: {}; Method: {}", pluginMainClass, pluginMainMethod);
                log.error("", t);
            }
        }
    } else {
        log.info("Plugins directory cannot be accessed or doesnt exist");
    }

}