Pages

Java modularity and metadata

As a java developer one will encounter the spaghetti world of class path hell.Loads of libraries,jars,dependencies etc are used in large projects.Manageability is the major part of any application that is evolving through continued engineering process.When we access a class in a library we have to add the library class (which can be in jar file) to class path.This helps the compiler to use the class when its get compiled and the JVM to load the class when used at runtime. Just the basic stuff.java.lang.ClassLoader plays the main role in the life of class objects.we have jars/plugin mechanisms to do all the required functionality of so called modular.A jar can have dependencies to other jar files.We use lot of jar files usually the Apache commons library and all.Some time we wont be needing all the classes for the required functionality.Intelligent packaging is one of the main concern of any project while deployment.Less dependencies could overcome performance bottlenecks.Better versioning could help a better way of continued engineering and maintenance.The default class loader in java runtime is immutable.Any class that is loaded by the class loader is added to a name space which cannot be changed i.e you can add any class to this namespace but unable to unload them.That's what happening when the object is instantiated with new operator.So when we create a new ClassLoader instance it creates a new namespace.One of the solutions provided by the tech commumity is the modularization of JDK itself.This would allow applications to be installed with just those components of the JDK that they actually require.A specification JSR 277 was targeted to be delivered as a component of Java SE 7.0.The specification defines an architecture with first-class modularity, packaging and deployment support in the Java platform, including a distribution format, a versioning scheme, a repository infrastructure, and runtime support.They introduced the Java module format called JAM.But later on they decided to halt its development because of difficulty in integrating with JVM.They started a project Jigsaw under open jdk.The modular system can be independent or be implemented based on the language or compiler changes.Anyway, the concept of modules is interesting.According to JSR-277 spec
A Java module is a unit of encapsulation. It is generally written as a development module in a programming language in the Java platform, and is ultimately compiled into metadata and packaged together with classes and other resources as a deployment module.
These modules provides the metadata related to itself, like name,version,imported classes,dependencies etc.

But there is already an evolving system known as OSGI that intends to do the modular concept.In this bundles will be the modular jar files.You can install, uninstall, start and stop these bundles(without restarting the VM ).Also, it does offer services that can be dynamically discovered in JAR files at runtime.You can package OSGi with your application, or list it as a prerequisite, and then run on a wide range of Java platforms( design point of OSGi has always been to support a broad set of Java ME). .The concept of bundles is powered by custom class loaders which will provide encapsulation as well as runtime dynamics.
A bundle is a group of Java classes and additional resources equipped with a detailed manifest on all its contents, as well as additional services needed to give the included group of Java classes more sophisticated behaviors, to the extent of deeming the entire aggregate a component.
We know that a jar will have MANIFEST.MF file attached to its structure inside the META-INF directorythat contains information about its contents.By adding OSGi headers to the MANIFEST.MF we can make the jar a bundle.These headers can be

Bundle-Name: a name for this bundle
Bundle-SymbolicName: a unique identifier for a bundle (naming like java packages).
Bundle-Description: description of functionality.
Bundle-ManifestVersion: which OSGi specification to use for reading this bundle.
Bundle-Version: version number of the bundle.
Bundle-Activator: the class name to be invoked once a bundle is activated.
Export-Package: packages contained in a bundle available to the outside world.
Import-Package: packages required from the outside world

These standardized metadata is used by the framework to provide robust modularity while integrating to most of the existing applications.This metadata can act as a semantic definition to integrate pluggable modules to be integrated in large applications.I think modern enterprise application need this type of modularity to provide an easy integration with low cost of maintanence and development.The ISVs can provide an efficient solution by the light weight applications instead of heavy application suites having complex integration structure.It can be an asset to the low budget IT solutions.Also the standardization helps to coexist (and interoperable) with different solutions provided by open jdk community or spring community or any other vendors.

More header information in this OSGi core specification
Some osgi implementations equinox,apache felix
About jigsaw
OSGi elearning

No comments:

Post a Comment