Java Code Examples for org.springframework.context.support.GenericApplicationContext#setClassLoader()

The following examples show how to use org.springframework.context.support.GenericApplicationContext#setClassLoader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SecureDeliveryServiceImpl.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Looks for the spring-context.xml file on the plugin JAR and loads the beans that implement the
 * SecureDeliveryModuleIfc interface 
 * 
 * @param secureDeliveryPlugin the path to the plugin JAR file
 */
private void handlePlugin( String secureDeliveryPlugin ) {

	try
	{
		File file = new File( secureDeliveryPlugin );
		if ( !file.exists() ) {
			log.warn( "Secure delivery plugin " + secureDeliveryPlugin + " not found" );
			return;
		}

		URL pluginUrl = new URL( "file:" + secureDeliveryPlugin );
		URLClassLoader classLoader = new URLClassLoader( new URL[] { pluginUrl },  this.getClass().getClassLoader() );
		GenericApplicationContext ctx = new GenericApplicationContext();
		ctx.setClassLoader( classLoader );
		Resource resource = ctx.getResource( "jar:file:" + secureDeliveryPlugin + "!/spring-context.xml" );

		XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
		xmlReader.loadBeanDefinitions( resource );
		ctx.refresh();
		
		String[] secureDeliveryModuleBeanNames = ctx.getBeanNamesForType( SecureDeliveryModuleIfc.class );
		if ( secureDeliveryModuleBeanNames.length == 0 )
			log.warn( "Secure delivery plugin doesn't define any beans of type SecureDeliveryModuleIfc" );
		for ( String name : secureDeliveryModuleBeanNames ) {
			
			SecureDeliveryModuleIfc secureDeliveryModuleBean = (SecureDeliveryModuleIfc) ctx.getBean( name );				
			log.info( "Loaded secure delivery module: " + secureDeliveryModuleBean + " (" + secureDeliveryModuleBean.getModuleName( Locale.getDefault() ) + ")"  );				
			if ( secureDeliveryModuleBean.initialize() ) {
			
				secureDeliveryModules.put( secureDeliveryModuleBean.getClass().getName(), secureDeliveryModuleBean );
			}
		}				
	}
	catch ( Exception e ) {
		log.error( "Unable to load secure delivery plugin " + secureDeliveryPlugin, e );
	}
}
 
Example 2
Source File: SecureDeliveryServiceImpl.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Looks for the spring-context.xml file on the plugin JAR and loads the beans that implement the
 * SecureDeliveryModuleIfc interface 
 * 
 * @param secureDeliveryPlugin the path to the plugin JAR file
 */
private void handlePlugin( String secureDeliveryPlugin ) {

	try
	{
		File file = new File( secureDeliveryPlugin );
		if ( !file.exists() ) {
			log.warn( "Secure delivery plugin " + secureDeliveryPlugin + " not found" );
			return;
		}

		URL pluginUrl = new URL( "file:" + secureDeliveryPlugin );
		URLClassLoader classLoader = new URLClassLoader( new URL[] { pluginUrl },  this.getClass().getClassLoader() );
		GenericApplicationContext ctx = new GenericApplicationContext();
		ctx.setClassLoader( classLoader );
		Resource resource = ctx.getResource( "jar:file:" + secureDeliveryPlugin + "!/spring-context.xml" );

		XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
		xmlReader.loadBeanDefinitions( resource );
		ctx.refresh();
		
		String[] secureDeliveryModuleBeanNames = ctx.getBeanNamesForType( SecureDeliveryModuleIfc.class );
		if ( secureDeliveryModuleBeanNames.length == 0 )
			log.warn( "Secure delivery plugin doesn't define any beans of type SecureDeliveryModuleIfc" );
		for ( String name : secureDeliveryModuleBeanNames ) {
			
			SecureDeliveryModuleIfc secureDeliveryModuleBean = (SecureDeliveryModuleIfc) ctx.getBean( name );				
			log.info( "Loaded secure delivery module: " + secureDeliveryModuleBean + " (" + secureDeliveryModuleBean.getModuleName( Locale.getDefault() ) + ")"  );				
			if ( secureDeliveryModuleBean.initialize() ) {
			
				secureDeliveryModules.put( secureDeliveryModuleBean.getClass().getName(), secureDeliveryModuleBean );
			}
		}				
	}
	catch ( Exception e ) {
		log.error( "Unable to load secure delivery plugin " + secureDeliveryPlugin, e );
	}
}
 
Example 3
Source File: SpringVerticleFactory.java    From spring-vertx-ext with Apache License 2.0 5 votes vote down vote up
private static GenericApplicationContext getGenericApplicationContext(ClassLoader classLoader) {
    final GenericApplicationContext genericApplicationContext = new GenericApplicationContext();
    genericApplicationContext.setClassLoader(classLoader);
    genericApplicationContext.refresh();
    genericApplicationContext.start();
    return genericApplicationContext;
}
 
Example 4
Source File: SpringCamelContextBootstrap.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
private void loadBeanDefinitions(Resource resource, ClassLoader classLoader) {
    applicationContext = new GenericApplicationContext();
    applicationContext.setClassLoader(classLoader);
    XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(applicationContext) {
        @Override
        protected NamespaceHandlerResolver createDefaultNamespaceHandlerResolver() {
            NamespaceHandlerResolver defaultResolver = super.createDefaultNamespaceHandlerResolver();
            return new SpringCamelContextBootstrap.CamelNamespaceHandlerResolver(defaultResolver);
        }
    };
    xmlReader.loadBeanDefinitions(resource);
}
 
Example 5
Source File: GroovyScriptFactoryTest.java    From engine with GNU General Public License v3.0 5 votes vote down vote up
private Map<String, Object> createGlobalVars(GroovyClassLoader classLoader) {
    GenericApplicationContext context = new GenericApplicationContext();
    context.setClassLoader(classLoader);

    XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(context);
    xmlReader.loadBeanDefinitions(new ClassPathResource("config/application-context.xml"));

    context.refresh();

    Map<String, Object> globalVars = new HashMap<>(1);
    globalVars.put("applicationContext", context);

    return globalVars;
}