
{"id":25,"date":"2014-09-18T10:05:53","date_gmt":"2014-09-18T10:05:53","guid":{"rendered":"http:\/\/www.codergears.com\/Blog\/?p=25"},"modified":"2014-11-14T20:06:00","modified_gmt":"2014-11-14T20:06:00","slug":"the-osgi-puzzle","status":"publish","type":"post","link":"https:\/\/codergears.com\/Blog\/?p=25","title":{"rendered":"Go inside the Eclipse IDE implementation to understand how OSGi works"},"content":{"rendered":"<p><a href=\"http:\/\/www.osgi.org\/Main\/HomePage\">OSGi<\/a> became very popular today, thanks to its modularity approach and its capability to enforce logical boundaries between modules. \u00a0Many known applications chose it to enforce the modularity, we can enumerate eclipse, GlassFish and Oracle JDeveloper.<\/p>\n<p>To understand OSGi concepts we will try to\u00a0go inside eclipse IDE, which uses equinox as OSGi container.<!--more--><\/p>\n<p>Let\u2019s begin with the typical OSGi definition:<\/p>\n<p><i> OSGi reduces complexity by providing a modular architecture for today\u2019s large-scale distributed systems as well as small, embedded applications. Building systems from in-house and off-the-shelf modules significantly reduces complexity and thus development and maintenance expenses. The OSGi programming model realizes the promise of component-based systems.<br \/>\n<\/i><\/p>\n<h4><strong>The trivial part of OSGi is modularity, let\u2019s discover what\u2019s the OSGi modules?<\/strong><\/h4>\n<p>OSGI modules are called Bundles and every application therefore consist of at least one bundle.\u00a0These bundles are executed inside a container.<\/p>\n<p><em>Which contract must implements every module to be integrated into the container?<\/em><\/p>\n<p>Let\u2019s take as example the bundle &#8220;org.eclipse.equinox.jsp.jasper&#8221; from the Eclipse IDE and search for all its implemented interfaces, for that we can execute the following <a href=\"http:\/\/www.jarchitect.com\/cqlinq\">CQLinq<\/a> request:<\/p>\n<p><span style=\"color: #3366ff;\">from<\/span> t <span style=\"color: #3366ff;\">in<\/span> Types <span style=\"color: #3366ff;\">where<\/span> t.ParentProject.Name==\u201d<span style=\"color: #ff6666;\">org.eclipse.equinox.jsp.jasper_1.0.300.v20110502<\/span>\u201c<br \/>\n<span style=\"color: #3366ff;\">let<\/span> interfaces=t.InterfacesImplemented<br \/>\n<span style=\"color: #3366ff;\">from<\/span> interface\u00a0<span style=\"color: #3366ff;\">in<\/span> interfaces <span style=\"color: #3366ff;\">select<\/span> interface<\/p>\n<p><img decoding=\"async\" class=\"bordered\" src=\"..\/img\/osgi\/img1.png\" alt=\"\" \/><\/p>\n<p>This bundle implements The <strong>BundleActivator<\/strong> interface from the OSGi package, this interface contains two methods start and stop useful to customize the starting and stopping of the bundle.<\/p>\n<p>Another specificity of a bundle is its manifest file, here\u2019s a part from the &#8220;org.eclipse.equinox.jsp.jasper&#8221; manifest file:<\/p>\n<pre>Manifest-Version: 1.0\r\nBundle-Localization: plugin\r\nBundle-RequiredExecutionEnvironment: CDC-1.0\/Foundation-1.0,J2SE-1.3\r\nBundle-SymbolicName: org.eclipse.equinox.jsp.jasper\r\nEclipse-LazyStart: true\r\nEclipse-SourceReferences: scm:cvs:pserver:dev.eclipse.org:\/cvsroot\/rt:\r\n org.eclipse.equinox\/server-side\/bundles\/org.eclipse.equinox.jsp.jasper;tag=v20110502\r\nBundle-Activator: org.eclipse.equinox.internal.jsp.jasper.Activator\r\n<\/pre>\n<p>As we can observe this manifest contains some metadata needed by the container, like specifying the bundle activator class which implements the BundleActivator interface.<\/p>\n<p>Here\u2019s a simplified representation of a bundle:<\/p>\n<p><img decoding=\"async\" class=\"bordered\" src=\"..\/img\/osgi\/img2.png\" alt=\"\" \/><\/p>\n<p>To have an idea of all bundles used by eclipse, let\u2019s search for all classes implementing the BundleActivator interface.<\/p>\n<p><span style=\"color: #3366ff;\">from<\/span> t <span style=\"color: #3366ff;\">in<\/span> Types <span style=\"color: #3366ff;\">where<\/span> t.Implement (\u201c<span style=\"color: #ff6666;\">org.osgi.framework.BundleActivator<\/span>\u201c)<br \/>\n<span style=\"color: #3366ff;\">select<\/span> new { t, t.NbBCInstructions }<\/p>\n<p><img decoding=\"async\" class=\"bordered\" src=\"..\/img\/osgi\/img3.png\" alt=\"\" \/><\/p>\n<h4><strong>Who manages the bundle and invokes the BundleActivator methods?<\/strong><\/h4>\n<p>To discover that, let\u2019s search for methods invoking directly or indirectly the BundleActivator.start method<\/p>\n<p><span style=\"color: #3366ff;\">from<\/span> m <span style=\"color: #3366ff;\">in<\/span> Methods<br \/>\n<span style=\"color: #3366ff;\">let<\/span> depth0 = m.DepthOfIsUsing(\u201c<span style=\"color: #ff6666;\">org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleActivator)<\/span>\u201c)<br \/>\n<span style=\"color: #3366ff;\">where<\/span> depth0 &gt;= 0 orderby depth0<br \/>\n<span style=\"color: #3366ff;\">select<\/span> new { m, depth0 }<\/p>\n<p><img decoding=\"async\" class=\"bordered\" src=\"..\/img\/osgi\/img4.png\" alt=\"\" \/><\/p>\n<p>The bundle is activated by the OSGi framework when it\u2019s launched. The framework class is started by the equinox container. To understand better what happens when the container starts, here are some actions executed when the container is launched:<\/p>\n<p><img decoding=\"async\" class=\"bordered\" src=\"..\/img\/osgi\/img5.png\" alt=\"\" \/><\/p>\n<p>When the container is launched, it initialize the OSGi framework, which gets all installed bundles, and for each one it creates an instance of BundleHost class, and store them into a repository.<\/p>\n<p>The BundleHost class implements the Bundle interface, which contains methods like start, stop, uninstall and update, these methods are needed to manage the bundle life cycle.<\/p>\n<p>To resume,\u00a0the OSGi container is launched by the Equinoxlauncher, which initialize the framework. The framework class \u00a0load the bundles and activates them.<\/p>\n<h4><strong>After discovering some basic concepts about bundle and container, let\u2019s go deep inside bundles and discover how they work internally.<\/strong><\/h4>\n<p>Let\u2019s take as example the org.eclipse.equinox.http.servlet bundle and search for methods invoked by the start method of its Activator class.<\/p>\n<p><img decoding=\"async\" class=\"bordered\" src=\"..\/img\/osgi\/img6.png\" alt=\"\" \/><\/p>\n<p>This bundle creates a service and registers it into the container. A service in OSGi is defined by a standard Java class or interface. Typically a Java interface is used to define the service interface. The service is the preferred method bundles should use to communicate between each other.<\/p>\n<p>Here are some useful scenarios of using services:<\/p>\n<ul style=\"list-style-type: square;\">\n<li>Export functionality from a bundle to other bundles.<\/li>\n<li>Import functionality from other bundles.<\/li>\n<li>Register listeners for events from other bundles.<\/li>\n<\/ul>\n<p>To resume, each bundle can uses or declares some services, what enforce the component design approach, here\u2019s the new representation of the OSGi bundle.<\/p>\n<p><img decoding=\"async\" class=\"bordered\" src=\"..\/img\/osgi\/img7.png\" alt=\"\" \/><\/p>\n<h4><strong>If the bundle uses services to communicate with other bundles, how it communicates with other jars?<\/strong><\/h4>\n<p>If we develop a bundle and try to use a class from another jar, we can be surprised that it will not works as expected, the reason is that the ClassLoader is hooked by the OSGi container.<\/p>\n<p>Let\u2019s search which method invokes java.lang.Thread.setContextClassLoader to overload the standard class loader.<\/p>\n<p><span style=\"color: #3366ff;\">from<\/span> m <span style=\"color: #3366ff;\">in<\/span> Methods <span style=\"color: #3366ff;\">where<\/span> m.IsUsing (\u201c<span style=\"color: #ff6666;\">java.lang.Thread.setContextClassLoader(ClassLoader)<\/span>\u201c)<br \/>\n<span style=\"color: #3366ff;\">select<\/span> new { m, m.NbBCInstructions }<\/p>\n<p><img decoding=\"async\" class=\"bordered\" src=\"..\/img\/osgi\/img8.png\" alt=\"\" \/><\/p>\n<p>Many methods invoke it including the EquinoxLauncher. Every time the bundle try to create a class instance, the OSGi container will check if the code is permitted to did this action or not, and here come the role of imported and exported package in the manifest file.<\/p>\n<pre>Export-Package: org.eclipse.equinox.http.servlet;version=\u201d1.1.0\u2033\r\nImport-Package: javax.servlet;version=\u201d2.3\u2033,javax.servlet.http;version\r\n=\u201d2.3\u2033,org.osgi.framework;version=\u201d1.3.0\u2033,org.osgi.service.http;version=\u201d[1.2,1.3)\u201d\r\n<\/pre>\n<p>The bundle declares explicitly the exported and imported packages. Let\u2019s search for packages used by org.eclipse.equinox.http.servlet bundle and verify if it use only imported package from the manifest file.<\/p>\n<p><span style=\"color: #3366ff;\">from<\/span> n <span style=\"color: #3366ff;\">in<\/span> Packages <span style=\"color: #3366ff;\">where<\/span> n.IsUsedBy (\u201c<span style=\"color: #ff6666;\">org.eclipse.equinox.http.servlet_1.1.200.v20110502<\/span>\u201c)<br \/>\n<span style=\"color: #3366ff;\">select<\/span> new { n, n.NbBCInstructions }<\/p>\n<p><img decoding=\"async\" class=\"bordered\" src=\"..\/img\/osgi\/img9.png\" alt=\"\" \/><\/p>\n<p>As we can observe all packages used are specified in the manifest file, in the import package section.<\/p>\n<p>The exported package represents the package that can be used from other bundles. It\u2019s represented by the ExportedPackage interface, and we can search for the container classes using this interface.<\/p>\n<p><span style=\"color: #3366ff;\">from<\/span> t <span style=\"color: #3366ff;\">in<\/span> Types <span style=\"color: #3366ff;\">where<\/span> t.IsUsing (\u201c<span style=\"color: #ff6666;\">org.osgi.service.packageadmin.ExportedPackage<\/span>\u201c)<br \/>\n<span style=\"color: #3366ff;\">select<\/span> new { t, t.NbBCInstructions }<\/p>\n<p><img decoding=\"async\" class=\"bordered\" src=\"..\/img\/osgi\/img10.png\" alt=\"\" \/><\/p>\n<p>What interesting with this new capability, is that the bundle will have a well defined boundary. What it uses and what it exposes as services is very well specified. This capability to treat imported and exported packages is managed by the OSGi modules layer.<\/p>\n<p>We can enforce the check of using imported packages by using \u00a0tools, for example with CQLinq we can write some rules warning each time a project use packages other than specified ones, but it\u2019s better to have these checks in runtime\u00a0execution, so the developer can\u2019t break these rules.<\/p>\n<p>Let\u2019s come back to the OSGi container and discover which services it provides?<\/p>\n<h2>Container services<\/h2>\n<p>As we discovered before the container is launched by the EquinoxLauncher class and the framework class initialize and launch the bundles. to detect which services are provided. Let\u2019s search for all classes used in the framework initialization method.<\/p>\n<p><span style=\"color: #3366ff;\">from<\/span> t <span style=\"color: #3366ff;\">in<\/span> Types <span style=\"color: #3366ff;\">where<\/span> t.IsUsedBy (\u201c<span style=\"color: #ff6666;\">org.eclipse.osgi.framework.internal.core.Framework.initialize(FrameworkAdaptor)<\/span>\u201c)<br \/>\n<span style=\"color: #3366ff;\">select<\/span> new { t, t.NbBCInstructions }<\/p>\n<p><img decoding=\"async\" class=\"bordered\" src=\"..\/img\/osgi\/img11.png\" alt=\"\" \/><\/p>\n<p>Some classes are already discovered before like BundleRepository, BundleHost, PackageAdminImpl and ServiceRegistry.<\/p>\n<p>What about the other classes:<\/p>\n<ul style=\"list-style-type: square;\">\n<li>StartLevelManager:<br \/>\nEach OSGi Bundle is associated with a start level that enable the server to control the relative starting and stopping order of bundles. Only bundles that have a start level less or equal to the active start level of the server framework must be active. Usually, a bundle with a smaller start level tends to be started earlier.<\/li>\n<li>SecurityAdmin:<br \/>\nThe security layer handles the security aspects by limiting bundle functionality to pre-defined capabilities.<\/li>\n<li>EventManager:<br \/>\nThe Event Admin service provides a publish-subscribe model for handling events. It is realized according to the OSGi Event Admin Service Specification.<br \/>\nThe Event Admin service dispatches events between Event Publishers and Event Subscribers (Event Handlers) by interposing an event channel. Publishers post events to the channel and the event channel defines which handlers needs to be notified. Thus publishers and handlers have no direct knowledge of each other, which simplifies event management.<\/li>\n<\/ul>\n<h2>The OSGi whole picture<\/h2>\n<p>Let\u2019s assemble all puzzle pieces described before , and we will have the following OSGi picture:<\/p>\n<p><img decoding=\"async\" class=\"bordered\" src=\"..\/img\/osgi\/img12.png\" alt=\"\" \/><\/p>\n<p>This architecture has the following interesting benefits:<\/p>\n<ul style=\"list-style-type: square;\">\n<li>Simple,<\/li>\n<li>Reduced Complexity,<\/li>\n<li>Easy Deployment,<\/li>\n<li>Secure.<\/li>\n<\/ul>\n<p>What makes it very attractive and Worth a Detour, and you will not waste your time if you study it in depth.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>OSGi became very popular today, thanks to its modularity approach and its capability to enforce logical boundaries between modules. \u00a0Many known applications chose it to enforce the modularity, we can enumerate eclipse, GlassFish and Oracle JDeveloper. To understand OSGi concepts we will try to\u00a0go inside eclipse IDE, which uses equinox as OSGi container.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-25","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/25","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=25"}],"version-history":[{"count":20,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/25\/revisions"}],"predecessor-version":[{"id":745,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/25\/revisions\/745"}],"wp:attachment":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=25"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=25"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=25"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}