Example usage for org.springframework.context.support GenericApplicationContext setClassLoader

List of usage examples for org.springframework.context.support GenericApplicationContext setClassLoader

Introduction

In this page you can find the example usage for org.springframework.context.support GenericApplicationContext setClassLoader.

Prototype

@Override
    public void setClassLoader(@Nullable ClassLoader classLoader) 

Source Link

Usage

From source file:org.jboss.fuse.wsdl2rest.util.SpringCamelContextFactory.java

private static List<SpringCamelContext> createCamelContextList(Resource resource, ClassLoader classLoader)
        throws Exception {

    if (classLoader == null)
        classLoader = SpringCamelContextFactory.class.getClassLoader();

    GenericApplicationContext appContext = new GenericApplicationContext();
    appContext.setClassLoader(classLoader);

    XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(appContext) {
        @Override//from w ww.ja  v a2s.  c om
        protected NamespaceHandlerResolver createDefaultNamespaceHandlerResolver() {
            NamespaceHandlerResolver defaultResolver = super.createDefaultNamespaceHandlerResolver();
            return new CamelNamespaceHandlerResolver(defaultResolver);
        }
    };
    xmlReader.loadBeanDefinitions(resource);

    SpringCamelContext.setNoStart(true);
    appContext.refresh();

    List<SpringCamelContext> result = new ArrayList<>();
    for (String name : appContext.getBeanNamesForType(SpringCamelContext.class)) {
        result.add(appContext.getBean(name, SpringCamelContext.class));
    }

    return Collections.unmodifiableList(result);
}

From source file:org.wildfly.extension.camel.SpringCamelContextFactory.java

private static CamelContext createSpringCamelContext(Resource resource, ClassLoader classLoader)
        throws Exception {
    GenericApplicationContext appContext = new GenericApplicationContext();
    appContext.setClassLoader(classLoader);
    XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(appContext) {
        @Override//from  ww  w.  j  av  a2  s. co  m
        protected NamespaceHandlerResolver createDefaultNamespaceHandlerResolver() {
            NamespaceHandlerResolver defaultResolver = super.createDefaultNamespaceHandlerResolver();
            return new CamelNamespaceHandlerResolver(defaultResolver);
        }
    };
    xmlReader.setEntityResolver(new EntityResolver() {
        @Override
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            InputStream inputStream = null;
            if (CAMEL_SPRING_SYSTEM_ID.equals(systemId)) {
                inputStream = SpringCamelContext.class.getResourceAsStream("/camel-spring.xsd");
            } else if (SPRING_BEANS_SYSTEM_ID.equals(systemId)) {
                inputStream = XmlBeanDefinitionReader.class.getResourceAsStream("spring-beans-3.1.xsd");
            }
            InputSource result = null;
            if (inputStream != null) {
                result = new InputSource();
                result.setSystemId(systemId);
                result.setByteStream(inputStream);
            }
            return result;
        }
    });
    xmlReader.loadBeanDefinitions(resource);
    appContext.refresh();
    return SpringCamelContext.springCamelContext(appContext);
}

From source file:org.wso2.carbon.springservices.GenericApplicationContextUtil.java

public static GenericApplicationContext getSpringApplicationContextClassPath(ClassLoader classLoader,
        String relPath) throws AxisFault {
    GenericApplicationContext appContext = new GenericApplicationContext();
    appContext.setClassLoader(classLoader);
    ClassLoader prevCl = Thread.currentThread().getContextClassLoader();
    try {//  w  ww  .  j a  v a 2  s.c  o  m
        Thread.currentThread().setContextClassLoader(classLoader);
        XmlBeanDefinitionReader xbdr = new XmlBeanDefinitionReader(appContext);
        xbdr.setValidating(false);
        InputStream in = classLoader.getResourceAsStream(relPath);
        if (in == null) {
            throw new AxisFault("Spring context cannot be located for AxisService");
        }
        xbdr.loadBeanDefinitions(new InputStreamResource(in));
        appContext.refresh();
    } catch (Exception e) {
        throw AxisFault.makeFault(e);
    } finally {
        // Restore
        Thread.currentThread().setContextClassLoader(prevCl);
    }
    return appContext;
}

From source file:org.wso2.carbon.springservices.GenericApplicationContextUtil.java

public static GenericApplicationContext getSpringApplicationContext(String applicationContxtFilePath,
        ClassLoader springBeansClassLoader) throws AxisFault {
    GenericApplicationContext appContext = new GenericApplicationContext();

    appContext.setClassLoader(springBeansClassLoader);

    ClassLoader prevCl = Thread.currentThread().getContextClassLoader();

    try {//from w w w . j  a  v  a2s.c  o  m
        // Save the class loader so that you can restore it later
        Thread.currentThread().setContextClassLoader(
                new MultiParentClassLoader(new URL[] {}, new ClassLoader[] { springBeansClassLoader, prevCl }));
        XmlBeanDefinitionReader xbdr = new XmlBeanDefinitionReader(appContext);
        xbdr.setValidating(false);
        xbdr.loadBeanDefinitions(
                new InputStreamResource(new FileInputStream(new File(applicationContxtFilePath))));
        appContext.refresh();
    } catch (FileNotFoundException e) {
        throw AxisFault.makeFault(e);
    } finally {
        // Restore
        Thread.currentThread().setContextClassLoader(prevCl);
    }

    return appContext;
}

From source file:com.mitre.cockpits.cmscockpit.CmsCockpitConfigurationTest.java

@BeforeClass
public static void testsSetup() {
    Registry.setCurrentTenantByID("junit");
    final GenericApplicationContext context = new GenericApplicationContext();
    context.setResourceLoader(new DefaultResourceLoader(Registry.class.getClassLoader()));
    context.setClassLoader(Registry.class.getClassLoader());
    context.getBeanFactory().setBeanClassLoader(Registry.class.getClassLoader());
    context.setParent(Registry.getGlobalApplicationContext());
    final XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(context);
    xmlReader.setBeanClassLoader(Registry.class.getClassLoader());
    xmlReader.setDocumentReaderClass(ScopeTenantIgnoreDocReader.class);
    xmlReader.loadBeanDefinitions(getSpringConfigurationLocations());
    context.refresh();// w w  w.j ava 2  s  . c  om
    applicationContext = context;
}

From source file:org.wso2.carbon.springservices.GenericApplicationContextUtil.java

/**
 * Method to get the spring application context for a spring service
 *
 * @param axisService - spring service/*from   w w  w. j  a v a 2  s  .c  o m*/
 * @param contextLocation - location of the application context file
 * @return - GenericApplicationContext for the given spring service
 * @throws AxisFault
 */

public static GenericApplicationContext getSpringApplicationContext(AxisService axisService,
        String contextLocation) throws AxisFault {

    Parameter appContextParameter = axisService.getParameter(SPRING_APPLICATION_CONTEXT);

    if (appContextParameter != null) {
        return (GenericApplicationContext) appContextParameter.getValue();

    } else {
        GenericApplicationContext appContext = new GenericApplicationContext();
        ClassLoader classLoader = axisService.getClassLoader();
        appContext.setClassLoader(classLoader);
        ClassLoader prevCl = Thread.currentThread().getContextClassLoader();
        try {
            Thread.currentThread().setContextClassLoader(classLoader);
            XmlBeanDefinitionReader xbdr = new XmlBeanDefinitionReader(appContext);
            xbdr.setValidating(false);
            InputStream in = classLoader.getResourceAsStream(contextLocation);
            if (in == null) {
                throw new AxisFault("Spring context cannot be located for AxisService");
            }
            xbdr.loadBeanDefinitions(new InputStreamResource(in));
            appContext.refresh();
            axisService.addParameter(new Parameter(SPRING_APPLICATION_CONTEXT, appContext));
        } catch (Exception e) {
            throw AxisFault.makeFault(e);
        } finally {
            // Restore
            Thread.currentThread().setContextClassLoader(prevCl);
        }
        return appContext;
    }
}

From source file:org.lexevs.tree.service.ApplicationContextFactory.java

/**
 * Instantiates a new application context factory.
 *///  ww  w .  jav a  2s .c o m
protected ApplicationContextFactory() {
    GenericApplicationContext ctx = new GenericApplicationContext();
    ctx.setClassLoader(MyClassLoader.instance());
    XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
    xmlReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
    InputStream stream = MyClassLoader.instance().getResourceAsStream("treeServiceContext.xml");
    xmlReader.loadBeanDefinitions(new InputStreamResource(stream));
    ctx.refresh();

    this.context = ctx;
}

From source file:org.jacpfx.vertx.spring.SpringVerticleFactory.java

private Verticle createSpringVerticle(final Class<?> currentVerticleClass, ClassLoader classLoader) {
    final SpringVerticle annotation = currentVerticleClass.getAnnotation(SpringVerticle.class);
    final Class<?> springConfigClass = annotation.springConfig();

    // Create the parent context  
    final GenericApplicationContext genericApplicationContext = new GenericApplicationContext();
    genericApplicationContext.setClassLoader(classLoader);
    if (parentContext != null) {
        genericApplicationContext.setParent(parentContext);
    }/*from  www  .  ja va  2 s .c  o  m*/
    genericApplicationContext.refresh();
    genericApplicationContext.start();

    // 1. Create a new context for each verticle and use the specified spring configuration class if possible
    AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
    annotationConfigApplicationContext.setParent(genericApplicationContext);
    annotationConfigApplicationContext.register(SpringContextConfiguration.class, springConfigClass);

    // 2. Register a bean definition for this verticle
    annotationConfigApplicationContext.registerBeanDefinition(currentVerticleClass.getSimpleName(),
            new VerticleBeanDefinition(currentVerticleClass));

    // 3. Add a bean factory post processor to avoid configuration issues
    annotationConfigApplicationContext
            .addBeanFactoryPostProcessor(new SpringSingleVerticleConfiguration(currentVerticleClass));
    annotationConfigApplicationContext.refresh();
    annotationConfigApplicationContext.start();
    annotationConfigApplicationContext.registerShutdownHook();

    // 5. Return the verticle by fetching the bean from the context
    return (Verticle) annotationConfigApplicationContext.getBeanFactory()
            .getBean(currentVerticleClass.getSimpleName());
}

From source file:de.hybris.platform.btgcockpit.service.BTGCockpitServiceTest.java

public void initApplicationContext() {
    final GenericApplicationContext context = new GenericApplicationContext();
    context.setResourceLoader(new DefaultResourceLoader(Registry.class.getClassLoader()));
    context.setClassLoader(Registry.class.getClassLoader());
    context.getBeanFactory().setBeanClassLoader(Registry.class.getClassLoader());
    context.setParent(Registry.getGlobalApplicationContext());
    final XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(context);
    xmlReader.setDocumentReaderClass(ScopeTenantIgnoreDocReader.class);
    xmlReader.setBeanClassLoader(Registry.class.getClassLoader());
    xmlReader.loadBeanDefinitions(getSpringConfigurationLocations());
    context.refresh();/*from   w  ww .  j  a v  a  2 s . c om*/
    final AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory();
    beanFactory.autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true);
    this.applicationContex = context;
}