4. What we need is … Modular Systems
Dividing a complex software system into small
parts/modules.
Allows us to understand the system easily.
Allows us to develop part by part by different team.
Allows us to reuse already developed modules.
6. What are the limitations?
âž” Runtime vs Compile time difference.
â—Ź Flat, single classpath.
âž” No standard way of building a modular system.
â—Ź Class loader based techniques.
9. JAR (Java Archive)
âž” JAR is unit of deployment in Java.
âž” Typical Java application consists a set of JAR files.
âž” No runtime representation for a JAR.
âž” At runtime contents of all JAR files are treated as a
single, ordered and global list which is called the class
path
10. Problem with JAR
âž” Multiple versions of JAR files cannot be loaded
simultaneously
âž” A JAR cannot declare dependencies on other JARs.
âž” No mechanism for information hiding
âž” Hence, JARs cannot be considered as modules
11. Java for Modular System
âž” Can you update a part(can be a JAR file) of a
running Java application?
âž” Can you add new functionality to a new Java
application at runtime?
âž” If you need to add new functionality or update
existing functionality, JVM needed to be
restarted.
âž” Java lacks dynamism
12. What’s Next?
âž” How about an abstraction where you can work at
the package level as opposed to class/object level?
â—† Importing packages.
â—† Exporting packages.
â—† A way to have private packages.
âž” Can we achieve the limitations that we discussed
in Java from this model?
13. Next Level of Modularity
âž” Separate class loader per module.
â—† creating a class-space per module.
â—† delegate class loading to other modules when necessary.
âž” Solves the single class path problem.
âž” Now you have a class loader network formed from
classloader delegation.
âž” This network is formed with import/export
package restrictions.
âž” Now we have modularity at runtime as well as
compile time.
15. What is OSGi?
"The OSGi framework is a module system and
service platform for the Java programming
language that implements a complete and dynamic
component model, something that as of 2012 does
not exist in standalone Java/VM environments."
--Wikipedia
16. What is OSGi?
âž” Widely accepted specification which introduces a
standard way of building dynamic, modular
systems with Java.
âž” OSGi framework implementations.(Open source)
â—† Eclipse Equinox.
â—† Apache Felix.
â—† Knopflerfish.
18. OSGi Layered Model
âž” Bundles
â—† Bundles are the OSGi components made by the
developers.
âž” Services
â—† The services layer connects bundles in a dynamic
way by offering a publish-find-bind model for plain
old Java objects.
âž” Life-Cycle
â—† The API to install, start, stop, update, and uninstall
bundles.
19. OSGi Layered Model
âž” Modules
â—† The layer that defines how a bundle can import and
export code.
âž” Security
â—† The layer that handles the security aspects.
âž” Execution Environment
â—† Defines what methods and classes are available in
a specific platform.
20. Bundles
âž” Bundle is the unit of modularization in OSGi
âž” OSGi based application can be considered as a
collection of Bundle
âž” Bundles can share packages with other bundles
and hide packages from other bundles
âž” How does a bundle differs from a normal jar file ?
25. Dynamism
âž” The moment your instantiate a new object tight
coupling among modules occurs.
âž” What if I want to update the platform or part of
the platform during runtime.
26. OSGi Services
âž” In-VM SOA model.
âž” Introduces the OSGi service registry.
âž” A service is Java object published in the framework
service registry.
âž” Bundles can publish/lookup services using interface
names
27. Registering a Service
public class Activator implements BundleActivator {
public void start(BundleContext bc) {
Hashtable props = new Hashtable();
props.put("language", "en");
//Registering the HelloWorld service
bc.registerService(HelloService.class.getName(),
new HelloServiceImpl(), props);
}
}
28. Acquiring Services
public void start(BundleContext bc) {
//Get the service reference for HelloService
serviceRef = bc.getServiceReference(HelloService.class. getName());
//service reference can be null, if the service is not registered.
if(serviceRef != null) {
helloService = (HelloService)bc.getService(serviceRef);
} else {
System.err.println("service reference not found.");
}
}
30. Services are Dynamic
âž” A service can register/unregister itself at any time.
âž” Bundle can decide to withdraw its service from the
registry while other bundles are still using this
service.
âž” A Service may not be registered when the other
bundles trying to use it.
â—† this depends on the start order of bundles.
âž” Bundle developer should write code to handle this
dynamic behavior of services.