Java Services

Hmm….thought of developing project which should have service as back end, Apache fuse ESB and Client program.

Service must run under Java Service Wrapper and OSGi
Java Service Wrapper ==> http://www.tanukisoftware.com
Pax Runner with Profiles ==> http://www.jroller.com/habuma/entry/pax_runner_profiles_and_distributed

Here is the list of Arch Types to generate project http://docs.codehaus.org/display/MAVENUSER/Archetypes+List

Rest we need to prepare maven archtype, which should give basic code.
The given code should have following
1. Service side code.
2. Fuse ESB Service assembly
3. Client Code

-o-

OSGi Activator showing wrong status

Problem Statement: OSGi Activator showing wrong status

Solution 1:
Make sure that we are throwing exception, in case of failure in start up.
Make sure that we are not starting new threads and exiting start method, without waiting and verifying its status.


public class MyProjectActivator implements BundleActivator {

public void start(BundleContext context) throws java.lang.Exception
{
// Make sure that all threads are completed.
// Catch Exception 
// Log Exception
// Throw exception.
}

public void stop(BundleContext context) throws java.lang.Exception
{
// Catch Exception 
// Log Exception
// Throw exception.
}

}

-o-

#osgi-bundles, #osgi-wrong-bundle-status

Equinox – bootdelegation Issue with Pax runner

Problem: When running Equinox with Pax runner, Boot delegation is not working.
org.osgi.framework.bootdelegation=com.sun.*
This is not resolving Class com.sun.org.apache.xerces.internal.dom.ElementNSImpl

Solution: Need to investigate

http://issues.ops4j.org/browse/PAXRUNNER-123
Setting system property org.osgi.framework.bootdelegation does not work on Equinox

http://lists.ibiblio.org/pipermail/xom-interest/2009-June/003912.html
-o-

#bootdelegation, #equinox

OSGi Class not found exception sun.*

Class not found exception sun.*
We used to get this exception in OSGi deployments.

In Felix, we need to use this line.

felix/conf/config.properties

  1. The following property makes specified packages from the class path
  2. available to all bundles. You should avoid using this property.

org.osgi.framework.bootdelegation=sun.*,com.sun.*

#osgi

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.

OSGi Bundle Repositories

Here are the OSGi Bundle repositories for reference:

http://www.springsource.com/repository/app/ (Best)

http://repo1.maven.org/maven2/

https://repository.apache.org/index.html#welcome

http://www.mvnbrowser.com/index.html

http://mvnrepository.com/

http://www.jarvana.com/jarvana/

-o-

#osgi-repositories

OSGi – Felix / Spring DM / Logging / Environment Variables

>pax-run “–platform=felix” “–log=DEBUG” “–profiles=spring.dm”
Using standard felix is painful. We need to identify and install all spring dm files.
This way we need not to do anything. Pax runner starts felix with log level debug and installs all spring dm files.

-o-

How to set environment variables in OSGi?

http://felix.apache.org/site/apache-felix-configuration-admin-service.html
https://opensource.luminis.net/wiki/display/SITE/OSGi+Configuration+Admin+command+line+client

-o-

#pax-runner, #spring-dynamic-modules

OSGi – is Fun to play, difficult to work on big projects

Today I am frustrated with OSGi Technology….Somebody got great idea….they released half cooked tools and software into market. Somebody likes the idea and simply say go and use it…without doing proper analysis. OSGi is good for project that is having very few jars (bundles) and they should available on spring source. Otherwise life is screwed up in stitching bundles and respective versions. Soon it will become great technology…it becomes like butterfly….for now it is caterpillar …looks ugly and difficult to appreciate…

If you have 100+ jars to be bundles, then you can realize the pain behind it.

TestNG for OSGi Bundles

Problem: We need to test bundles deployed in OSGi.

Solution: Bundle test classes as xyz_tests.jar file. Convert this into bundle and deploy along with main bundle code.
In test bundle activator, programmatic way call TestNG as shown in http://testng.org/doc/documentation-main.html

TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] { Run2.class });
testng.addListener(tla);
testng.run();

TestNG opens in its own JVM. Due to this it is not recognizing test classes. We can build OSGi classpath with the help of bundle context. But we dont have option to pass classpath to TestNG programitically. Even though we can pass, it uses jar files in path, But not the bundles.

Conclusion: We can’t use TestNG inside OSGi. Please let me know your experience.

-o-

#osgi, #testng, #testng-in-osgi

org.osgi.framework.BundleException: Cannot start a fragment bundle

Exception: While trying to start com.springsource.org.apache.xerces-2.8.1.jar bundle I got the following exception.

org.osgi.framework.BundleException: Cannot start a fragment bundle
at org.knopflerfish.framework.BundleImpl.start(BundleImpl.java:300)

Solution: We can’t start Fragment Bundle. As long as it is in resolved state, it is good. The following links can give more information about it.
http://www.osgi.org/javadoc/r4v41/org/osgi/framework/Bundle.html
http://blog.linkedin.com/2008/07/10/osgi-at-linkedin-integrating-spring-dm-part-2/
http://osgi.mjahn.net/2008/03/13/half-bundle-half-jar-%E2%80%93-the-nature-of-fragment-a-blessing-or-a-curse/

-o-

#osgi-fragment