/** * Creates a special OSGi namespace handler resolver that first searches the bundle class path falling back to the * namespace service published by Spring-DM. This allows embedded libraries that provide namespace handlers take * priority over namespace provided by other bundles. * * @param bundleContext the OSGi context of which the resolver should be aware of * @param filter OSGi service filter * @param bundleClassLoader classloader for creating the OSGi namespace resolver proxy * @return a OSGi aware namespace handler resolver */ private NamespaceHandlerResolver createNamespaceHandlerResolver(BundleContext bundleContext, String filter, ClassLoader bundleClassLoader) { Assert.notNull(bundleContext, "bundleContext is required"); // create local namespace resolver // we'll use the default resolver which uses the bundle local class-loader NamespaceHandlerResolver localNamespaceResolver = new DefaultNamespaceHandlerResolver(bundleClassLoader); // hook in OSGi namespace resolver NamespaceHandlerResolver osgiServiceNamespaceResolver = lookupNamespaceHandlerResolver(bundleContext, filter, localNamespaceResolver); DelegatedNamespaceHandlerResolver delegate = new DelegatedNamespaceHandlerResolver(); delegate.addNamespaceHandler(localNamespaceResolver, "LocalNamespaceResolver for bundle " + OsgiStringUtils.nullSafeNameAndSymName(bundleContext.getBundle())); delegate.addNamespaceHandler(osgiServiceNamespaceResolver, "OSGi Service resolver"); return delegate; }
/** * Creates a special OSGi namespace handler resolver that first searches the bundle class path falling back to the * namespace service published by Spring-DM. This allows embedded libraries that provide namespace handlers take * priority over namespace provided by other bundles. * * @param bundleContext the OSGi context of which the resolver should be aware of * @param filter * @param bundleClassLoader classloader for creating the OSGi namespace resolver proxy * @return a OSGi aware namespace handler resolver */ private NamespaceHandlerResolver createNamespaceHandlerResolver(BundleContext bundleContext, String filter, ClassLoader bundleClassLoader) { Assert.notNull(bundleContext, "bundleContext is required"); // create local namespace resolver // we'll use the default resolver which uses the bundle local class-loader NamespaceHandlerResolver localNamespaceResolver = new DefaultNamespaceHandlerResolver(bundleClassLoader); // hook in OSGi namespace resolver NamespaceHandlerResolver osgiServiceNamespaceResolver = lookupNamespaceHandlerResolver(bundleContext, filter, localNamespaceResolver); DelegatedNamespaceHandlerResolver delegate = new DelegatedNamespaceHandlerResolver(); delegate.addNamespaceHandler(localNamespaceResolver, "LocalNamespaceResolver for bundle " + OsgiStringUtils.nullSafeNameAndSymName(bundleContext.getBundle())); delegate.addNamespaceHandler(osgiServiceNamespaceResolver, "OSGi Service resolver"); return delegate; }
private Plugin(Bundle bundle) { this.bundle = bundle; ClassLoader loader = BundleDelegatingClassLoader.createBundleClassLoaderFor(bundle); entity = new DelegatingEntityResolver(loader); namespace = new DefaultNamespaceHandlerResolver(loader); }
protected void setUp() throws Exception { ServletConfig servletConfig = new FakeServletConfig("test", new FakeServletContext()); webappContext = new StaticWebApplicationContext(); webappContext.setServletConfig(servletConfig); NamespaceHandlerResolver resolver = new DefaultNamespaceHandlerResolver(getClass().getClassLoader()); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(webappContext); reader.setNamespaceHandlerResolver(resolver); reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD); reader.setEntityResolver(new DummySchemaResolver()); reader.loadBeanDefinitions(getResource()); container = new SpringContainer(); container.setBeanFactory(webappContext); container.addParameter("debug", "true"); ContainerUtil.setupDefaultContainer(container, servletConfig); StartupUtil.initWebContext(servletConfig, servletConfig.getServletContext(), null, container); StartupUtil.initServerContext(servletConfig, servletConfig.getServletContext(), container); // Configure default converters etc ContainerUtil.configureFromSystemDwrXml(container); ContainerUtil.configure(container, Collections.singletonList(webappContext.getBean(DwrNamespaceHandler.DEFAULT_SPRING_CONFIGURATOR_ID))); ContainerUtil.publishContainer(container, servletConfig); ContainerUtil.debugConfig(container); }
@Before public void setUp() throws Exception { NamespaceHandlerResolver resolver = new DefaultNamespaceHandlerResolver(CLASS.getClassLoader(), NS_PROPS); this.beanFactory = new GenericApplicationContext(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.beanFactory); reader.setNamespaceHandlerResolver(resolver); reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD); reader.setEntityResolver(new DummySchemaResolver()); reader.loadBeanDefinitions(getResource()); this.beanFactory.refresh(); }
@Test public void testResolvedMappedHandler() { DefaultNamespaceHandlerResolver resolver = new DefaultNamespaceHandlerResolver(getClass().getClassLoader()); NamespaceHandler handler = resolver.resolve("http://www.springframework.org/schema/util"); assertNotNull("Handler should not be null.", handler); assertEquals("Incorrect handler loaded", UtilNamespaceHandler.class, handler.getClass()); }
@Test public void testResolvedMappedHandlerWithNoArgCtor() { DefaultNamespaceHandlerResolver resolver = new DefaultNamespaceHandlerResolver(); NamespaceHandler handler = resolver.resolve("http://www.springframework.org/schema/util"); assertNotNull("Handler should not be null.", handler); assertEquals("Incorrect handler loaded", UtilNamespaceHandler.class, handler.getClass()); }
@Test public void testNonExistentHandlerClass() throws Exception { String mappingPath = "org/springframework/beans/factory/xml/support/nonExistent.properties"; try { new DefaultNamespaceHandlerResolver(getClass().getClassLoader(), mappingPath); // pass } catch (Throwable ex) { fail("Non-existent handler classes must be ignored: " + ex); } }
@Test public void testResolveInvalidHandler() throws Exception { String mappingPath = "org/springframework/beans/factory/xml/support/invalid.properties"; try { new DefaultNamespaceHandlerResolver(getClass().getClassLoader(), mappingPath); fail("Should not be able to map a class that doesn't implement NamespaceHandler"); } catch (Throwable expected) { } }
@Test public void testCtorWithNullClassLoaderArgument() throws Exception { // simply must not bail... new DefaultNamespaceHandlerResolver(null); }
@Test(expected=IllegalArgumentException.class) public void testCtorWithNullClassLoaderArgumentAndNullMappingLocationArgument() throws Exception { new DefaultNamespaceHandlerResolver(null, null); }
@Test public void testCtorWithNonExistentMappingLocationArgument() throws Exception { // simply must not bail; we don't want non-existent resources to result in an Exception new DefaultNamespaceHandlerResolver(null, "738trbc bobabloobop871"); }