0

It's about half a day I'm trying to find a way to migrate my implementations on SpringSecurity to the context of OSGi(equinox) bundles without switching to SpringDM.


Currently we have two projects:

1. I have an implementation of Spring Security based on some xml configuration files to handle authentication and authorization.
2. On the other hand, we have a huge OGSi bundled project structure with about 200 bundles which need to be integrated with a security bundle(the one described above)


As the first step to create mySpringBasedSecurityBundle I need to run this method after loading mySecurityBundle to access the security configuration xml-file located : com/myComp/backend/appsecurity/spring/resources/Spring-Context.xml which prepared me Spring-DataSource.xml and Spring-Security.xml as following:

    private void loadApplicationContext()
    {
        SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_GLOBAL);
        new ThreadLocal<Object>();
        setApplicationContext(new ClassPathXmlApplicationContext(SPRING_CONTEXT_ADDRESS));
    }       



But unfortunately this Exception occurred:

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [com/myComp/backend/appsecurity/spring/resources/Spring-Context.xml]; nested exception is java.io.FileNotFoundException: class path resource [com/myComp/backend/appsecurity/spring/resources/Spring-Context.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:126)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:92)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:467)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:397)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.myComp.backend.appsecurity.spring.appSecurityManager.loadApplicationContext(appSecurityManager.java:233)
at com.myComp.backend.appsecurity.spring.appSecurityManager.internalInitialize(appSecurityManager.java:106)
at com.myComp.BaseModuleManager.initialize(BaseModuleManager.java:511)
at com.myComp.BaseModuleManager.initialize(BaseModuleManager.java:1)
at com.myComp.backend.BaseBackendManager.initializeSubBackendManagers(BaseBackendManager.java:643)
at com.myComp.backend.BaseBackendManager.prepareSubBackendManagers(BaseBackendManager.java:885)
at com.myComp.backend.BackendManager.internalStart(BackendManager.java:127)
at com.myComp.BaseModuleManager.start(BaseModuleManager.java:574)
at com.myComp.BaseModuleManager.start(BaseModuleManager.java:1)
at com.myComp.application.BaseApplicationStub.startBackendManager(BaseApplicationStub.java:2407)
at com.myComp.Application.frameworkEvent(Application.java:72)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.dispatchEvent(BundleContextImpl.java:874)
at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340)Caused by: java.io.FileNotFoundException: class path resource [com/myComp/backend/appsecurity/spring/resources/Spring-Context.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
.. 26 more


as much as I search the web, the only recommendation for this issue ends to the application on SpringDM, but it is not acceptable for our ProjectManager to switch to SpringDM and to be honest I have no idea about SpringDM.

Would you please help me resolve this issue using Spring Core functionalities.


Thanks alot
Moein

Moein
  • 739
  • 2
  • 9
  • 17
  • Does your manager also object to Blueprint? – Jonathan W Jun 05 '12 at 20:16
  • Also, is the goal to apply security as an aspect to all of the existing bundles? If so, you might want to check out Equinox Aspects (this thread: http://stackoverflow.com/questions/3086011/best-solution-for-using-aop-with-osgi) – Jonathan W Jun 05 '12 at 20:30
  • @JonathanW Yes, we know Blueprint but we thought there is a predefined way of merging Spring and OSGi. Any way thank you for the recommended link, I will check it – Moein Jun 06 '12 at 04:57

1 Answers1

0

You don't actually need Spring DM. It simply provides a bridge between OSGi and Spring, with some niceties like loading all your context files properly in an OSGi environment. You can do this yourself as well, but you have to compensate for the classloading issues, which is the problem you are having.

Try this to fix your classloading issues.

ApplicationContext ctx = new ClassPathXmlApplicationContext(myCtxPath)
{
    protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader)
    {
        super.initBeanDefinitionReader(reader);
        reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
        reader.setBeanClassLoader(getClassLoader());
    }
}

BTW, the second line of your method serves no purpose.

Robin
  • 24,062
  • 5
  • 49
  • 58
  • Thanks Robin, You're right with the last comment, but about the first as I tried to do so, I got a same exception. I think my URL might be incorrect?! I've a resource folder under src and try to access the context file over there. whats your idea? – Moein Jun 06 '12 at 05:07
  • I've just tested my code using this code : File(SPRING_CONTEXT_ADDRESS).exists() which returns true and shows my file address is correct. :( – Moein Jun 06 '12 at 06:59
  • Now I'm trying to learn SpringDM and will appreciate any shortcuts – Moein Jun 09 '12 at 12:48