0

While migrating existing spring project to osgi karaf, we are facing the problem while import resource from the dependent bundle.Eg.
Bundle A has the appcontxt-A.xml and Bundle B which has appcontext-B.xml.

Here I am referring appcontxt-A.xml in Bundle B as (<import resource="classpath:appContext-A.xml" />) for which I am getting Caused by: java.io.FileNotFoundException: class path resource [appContext-A.xml] cannot be opened because it does not exist.

How I can achieve the above defined scenario.Thanks in advance.

Root
  • 2,269
  • 5
  • 29
  • 58
PSTADU
  • 31
  • 5

3 Answers3

1

It is not entirely clear to me how exactly you attempt to access the appContext-A.xml resource since you have not included any code samples, however keep in mind that in OSGi bundles A and B have different classloaders and therefore you will not be able to get a resource of bundle A directly from bundle B. What you can do is get a reference to bundle A through the BundleContext and get the resource you need from there. Something like the following:

bundleContext.getBundle(bundleA).getResource(resource)
Christina
  • 3,562
  • 3
  • 22
  • 32
  • Thanks for the valuable inputs.Adding the code snippet below . i have the activator class in Bundle B which creates the applicationContext using ClassPathXmlApplicationContext as below ApplicationContext applicationContext = new ClassPathXmlApplicationContext("appContext-B.xml"); inside appContext-B.xml i have import resoures of Bundle A as – PSTADU Apr 23 '15 at 04:48
1

After long research resolved the file doesnt exist issue was resolved by (adding * after classpath) . However while accessing "context:annotation-config" there is another issue Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/context], Any help to resolve this issue?

PSTADU
  • 31
  • 5
0

With the further added information it's clear, you'll need to make sure that bundle A does actually export the resource, and be does actually import it. Additionally you'll need to make sure you retrieve that resource as Christina said.

If you are using a Spring based approach, make sure you have Spring-DM also available.

Achim Nierbeck
  • 5,265
  • 2
  • 14
  • 22
  • Is it really required to have Spring-DM? Actually we are migrating the existing spring modules to OSGI.Hence we are trying with the spring version(3.1.4_RELEASE) used in our project. Also i have tried exporting and importing the packages but it doesn't work. – PSTADU Apr 23 '15 at 09:03
  • If you want to stick to Spring, you'll need to add spring-dm, otherwise Spring will not work within the OSGi context. But might just be better to move to blueprint – Achim Nierbeck Apr 23 '15 at 09:05
  • we have lot of modules which is completely running and dependent on spring framework.Does it mean first migrate those code from spring implementation to spring-dm? Or using the blueprint? – PSTADU Apr 23 '15 at 09:25
  • spring-dm just adds the posibility to reference osgi services as spring-beans and it'll help to get the spring-contexts straightened. So no need to migrate to spring-dm, just add it to the container. If you want to migrate to a more service-oriented approach move to blueprint! – Achim Nierbeck Apr 23 '15 at 09:45
  • got it. first i will install the spring-dm in osgi container(karaf). still i am having confusion related to the export and import resources. in my pom.xml Bundle A a.b.c.appContext-A.xml src/main/resources. however when i try to access the same appcontxt-B.xml in Bundle B i am getting file doesnt exist exception. i am still not clear on the Christina point. – PSTADU Apr 23 '15 at 10:04