OSGi issues – Converting Big Jar (100+ Dependency jars) to Bundle

Problem Statement: There is a project with around 100+ third party jar files. How can we convert this spring project X jar to OSGi X Bundle? It is painful process.

We have two approaches.
1. Convert all third party jars to bundles. (few are available on public repositories, few we need to package manually. This is painful process)
2. Make one big X Bundle by embedding all jars in its lib folder. Make is work. Slowly push out jars as bundles. This is also painful process

Failed to achieve success in both ways. Spring DM is giving problems with XML parsers in between and lot of other issues.

Final Solution: Prepare Big X Bundle by embedding all jars. Dont depend on Spring DM to expose services. We need to do on our own using API.

This code is working

private static void loadBeanFactory(BundleContext context) {
		try {
			OsgiBundleXmlApplicationContext applicationContext = new OsgiBundleXmlApplicationContext(
					new String[] { "/META-INF/abcd-context.xml" });
			applicationContext.setPublishContextAsService(false);
			applicationContext.setBundleContext(context);
			applicationContext.refresh();

			SuperMessage dataProcessor = (SuperMessage) applicationContext.getBean("messageBean");
			System.out.println("message from bean:==>"
					+ dataProcessor.getMessage());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

Quick Check: Call printClassPath from activator and see what is there in classpath.

public static void printClassPath() {
		System.out.println("------------ printClassPath Begin ------------");
		// Get the System Classloader
		ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader();

		// Get the URLs
		URL[] urls = ((URLClassLoader) sysClassLoader).getURLs();

		for (int i = 0; i " + urls[i].getFile());
		}
		System.out.println("------------ printClassPath End ------------");
	}

The following code never works in OSGi. Because OSGi wont expose jar, file resources. We never get those files on classpath.

private void loadBeanFactory1() {
		try {
			XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource(
					"Test-context.xml"));
			SuperMessage dataProcessor = (SuperMessage) factory
					.getBean("messageBean");
			System.out.println("message from bean:==>"
					+ dataProcessor.getMessage());
		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}

	private void loadBeanFactory2() {
		try {
			String[] configFiles = new String[] { "META-INF/Test-context.xml" };
			ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext(
					configFiles);

			SuperMessage dataProcessor = (SuperMessage) factory
					.getBean("messageBean");
			System.out.println("message from bean:==>"
					+ dataProcessor.getMessage());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private static void loadBeanFactory3() {
		try {
			GenericApplicationContext ctx = new GenericApplicationContext();
			ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(
					ctx);
			scanner
					.scan(new String[] { "org.apache.felix.example.servicebased.circle.DefaultMessage" });
			ctx.refresh();

			SuperMessage dataProcessor = (SuperMessage) ctx
					.getBean("messageBean");
			System.out.println("message from bean:==>"
					+ dataProcessor.getMessage());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private static void loadBeanFactory4() {
		try {
			GenericApplicationContext ctx = new GenericApplicationContext();

			XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
			xmlReader.loadBeanDefinitions(new ClassPathResource(
					"META-INF/Test-context.xml"));
			// PropertiesBeanDefinitionReader propReader = new
			// PropertiesBeanDefinitionReader(ctx);
			// propReader.loadBeanDefinitions(new
			// ClassPathResource("otherBeans.properties"));
			ctx.refresh();

			SuperMessage dataProcessor = (SuperMessage) ctx
					.getBean("messageBean");
			System.out.println("message from bean:==>"
					+ dataProcessor.getMessage());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

-o-
Please share your ideas.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s