{"id":13026,"date":"2017-02-18T19:18:00","date_gmt":"2017-02-18T18:18:00","guid":{"rendered":"https:\/\/touk.pl\/blog\/?guid=897e02beb7ca6e3cf6b1c33c45f617f5"},"modified":"2022-08-03T12:35:40","modified_gmt":"2022-08-03T10:35:40","slug":"osgi-blueprint-visualization","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2017\/02\/18\/osgi-blueprint-visualization\/","title":{"rendered":"OSGi Blueprint visualization"},"content":{"rendered":"<h2 id=\"what-is-blueprint\">What is blueprint?<\/h2>\n<p>Blueprint is a dependency injection framework for OSGi bundles. It could be written by hand or generated using <a href=\"http:\/\/aries.apache.org\/modules\/blueprint-maven-plugin.html\">Blueprint Maven Plugin<\/a>. Blueprint file is only an XML describing beans, services and references. Each OSGi bundle could have one or more blueprint files.<\/p>\n<p>Blueprint files represent architecture of our bundle. Let&#8217;s visualize it using groovy script and graphviz available in my <a href=\"https:\/\/github.com\/alien11689\/blueprint-vizualizer\">github repository<\/a> and analyze.<\/p>\n<h2 id=\"example-generation\">Example generation<\/h2>\n<p><strong>Pre: All you need is groovy and graphviz installed on your OS<\/strong><\/p>\n<p>I am working mostly with bundles with generated blueprint, so I will use blueprint file generated from Blueprint Maven Plugin tests as example. All examples are included in github repository.<\/p>\n<p>Generation could be invoked by running <code>run.sh<\/code> script with given destination file prefix (png extension will be added to it) and path to blueprint file:<\/p>\n<pre class=\"brush: bash\">mkdir -p target<\/pre>\n<p>.\/run.sh target\/fullBlueprint fullBlueprint.xml<\/p>\n<p>Visualization is available <a href=\"https:\/\/gist.github.com\/alien11689\/560e189859107159126e71d45065ec51\/raw\/d2a8b16e5741a1debf36168953f5608ec91a1656\/fullBlueprint.png\">here<\/a>.<\/p>\n<h2 id=\"separating-domains\">Separating domains<\/h2>\n<p>First if you look at the image, you see that some beans are grouped. You could easily extract such domains with tree roots: <code>beanWithConfigurationProperties<\/code> and <code>beanWithCallbackMethods<\/code> to separate blueprint files and bundles in future and generate images from them:<\/p>\n<pre class=\"brush: bash\">.\/run.sh target\/beanWithCallbackMethods example\/firstCut\/beanWithCallbackMethods.xml\r\n.\/run.sh target\/beanWithConfigurationProperties example\/firstCut\/beanWithConfigurationProperties.xml\r\n.\/run.sh target\/otherStuff example\/firstCut\/otherStuff.xml<\/pre>\n<p>Now we have three, a bit cleaner, images: <a href=\"https:\/\/gist.github.com\/alien11689\/560e189859107159126e71d45065ec51\/raw\/d2a8b16e5741a1debf36168953f5608ec91a1656\/beanWithConfigurationProperties.png\"><code>beanWithConfigurationProperties.png<\/code><\/a>, <a href=\"https:\/\/gist.github.com\/alien11689\/560e189859107159126e71d45065ec51\/raw\/d2a8b16e5741a1debf36168953f5608ec91a1656\/beanWithCallbackMethods.png\"><code>beanWithCallbackMethods.png<\/code><\/a> and <a href=\"https:\/\/gist.github.com\/alien11689\/560e189859107159126e71d45065ec51\/raw\/d2a8b16e5741a1debf36168953f5608ec91a1656\/otherStuff.png\"><code>otherStuff.png<\/code><\/a>.<\/p>\n<p>We also could generate image from more than one blueprint:<\/p>\n<pre class=\"brush: bash\">.\/run.sh target\/joinFirstCut example\/firstCut\/otherStuff.xml example\/firstCut\/beanWithConfigurationProperties.xml example\/firstCut\/beanWithCallbackMethods.xml<\/pre>\n<p>And the result is <a href=\"https:\/\/gist.github.com\/alien11689\/560e189859107159126e71d45065ec51\/raw\/d2a8b16e5741a1debf36168953f5608ec91a1656\/joinFirstCut.png\">here<\/a>. The image contains beans grouped by file, but if you do not like it, you could force generation without such separation using option <code>--no-group-by-file<\/code>:<\/p>\n<pre class=\"brush: bash\">.\/run.sh target\/joinFirstCutGrouped example\/firstCut\/otherStuff.xml example\/firstCut\/beanWithConfigurationProperties.xml example\/firstCut\/beanWithCallbackMethods.xml --no-group-by-file<\/pre>\n<p>It will generate <a href=\"https:\/\/gist.github.com\/alien11689\/560e189859107159126e71d45065ec51\/raw\/d2a8b16e5741a1debf36168953f5608ec91a1656\/joinFirstCutGrouped.png\">image with all beans from all files<\/a>.<\/p>\n<h2 id=\"exclusion\">Exclusion<\/h2>\n<p>Sometimes it is difficult to spot and extract other domains. It will be easier to do some experiments on blueprint. For example, bean <code>my1<\/code> is a dependency for too many other beans. You could consider converting <code>my1<\/code> bean to OSGi service and extracting it to another bundle.<\/p>\n<p>Let&#8217;s exclude <code>my1<\/code> bean from generation via <code>-e<\/code> option and see what happens:<\/p>\n<pre class=\"brush: bash\">.\/run.sh target\/otherStuffWithoutMy example\/firstCut\/otherStuff.xml -e my1<\/pre>\n<p>Result is available <a href=\"https:\/\/gist.github.com\/alien11689\/560e189859107159126e71d45065ec51\/raw\/d2a8b16e5741a1debf36168953f5608ec91a1656\/otherStuffWithoutMy.png\">here<\/a>. Now we see, that tree with root bean <code>myFactoryBeanAsService<\/code> could be separated and <code>my1<\/code> could be inject to it as osgi service in another bundle.<\/p>\n<p>You could exclude more than one bean adding <code>-e<\/code> switch for each of them, e. g. <code>-e my1 -e m2 -e myBean123<\/code>.<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>Blueprint is great for dependency injection for OSGi bundles, but it is easy to create quite big context containing many domains. It is much easier to recognize or search for such domains using blueprint visualizer script.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"prettyprint\"><code class=\"language-html\">YOUR CODE HRER<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"What is blueprint?Blueprint is a dependency injection framework for OSGi bundles. It could be written by hand or generated using Blueprint Maven Plugin. Blueprint file is only an XML describing beans, services and references. Each OSGi bundle could hav&#8230;\n","protected":false},"author":54,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[606,50,44,608],"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/13026"}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/54"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=13026"}],"version-history":[{"count":14,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/13026\/revisions"}],"predecessor-version":[{"id":14926,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/13026\/revisions\/14926"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=13026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=13026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=13026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}