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-

Advertisements

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-

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-

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

# The following property makes specified packages from the class path
# available to all bundles. You should avoid using this property.
org.osgi.framework.bootdelegation=sun.*,com.sun.*

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 – 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-