{"id":13264,"date":"2017-11-14T19:40:00","date_gmt":"2017-11-14T18:40:00","guid":{"rendered":"http:\/\/touk.pl\/blog\/?guid=828f7787ad0cd751cceb98c90b5fbc53"},"modified":"2022-08-02T14:33:30","modified_gmt":"2022-08-02T12:33:30","slug":"karaf-configuration-as-groovy-file","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2017\/11\/14\/karaf-configuration-as-groovy-file\/","title":{"rendered":"Karaf configuration as Groovy file"},"content":{"rendered":"<h2 id=\"introduction\">Introduction<\/h2>\n<p>By deafault, <a href=\"http:\/\/karaf.apache.org\/\">Apache Karaf<\/a> keeps configuration for bundles in the <code>etc<\/code> directory as flat properties files. We can override configuration for the storing mechanism by providing own implementation of the <code>org.apache.felix.cm.PersistenceManager<\/code> interface and use much more readable format for bundle properties, e. g. <a href=\"http:\/\/przybyszd.blogspot.com\/2015\/09\/easy-configuration-usege-with.html\">groovy config<\/a>.<\/p>\n<h2 id=\"turning-off-built-in-karaf-persistence\">Turning off built-in Karaf persistence<\/h2>\n<p>As we can read in <a href=\"https:\/\/github.com\/apache\/karaf\/blob\/master\/manual\/src\/main\/asciidoc\/user-guide\/configuration.adoc\">Karaf documentation<\/a>:<\/p>\n<blockquote><p>Apache Karaf persists configuration using own persistence manager in case of when available persistence managers do not support that.<\/p><\/blockquote>\n<p>We will use our custom implementation of persistence, so Karaf persistence is not needed. We can turn it off by setting variable <code>storage<\/code> to an empty value:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">$ cat etc\/org.apache.karaf.config.cfg\r\nstorage=<\/pre>\n<p>This option is available since version 4.1.3 when <a href=\"https:\/\/issues.apache.org\/jira\/browse\/KARAF-4803\">this issue<\/a> was resolved.<\/p>\n<h2 id=\"registering-custom-persistence-manager\">Registering custom Persistence Manager<\/h2>\n<p>First we have to create and register an OSGi service implementing <code>org.apache.felix.cm.PersistenceManager<\/code>. If we build and install the bundle with such service while Karaf is running (e.g. by putting jar in the <code>deploy<\/code> directory), then we should have at least two <code>PersistenceManager<\/code> services registered:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">karaf@root()&gt; ls org.apache.felix.cm.PersistenceManager\r\n[org.apache.felix.cm.PersistenceManager]\r\n----------------------------------------\r\n service.bundleid = 7\r\n service.description = Platform Filesystem Persistence Manager\r\n service.id = 14\r\n service.pid = org.apache.felix.cm.file.FilePersistenceManager\r\n service.ranking = -2147483648\r\n service.scope = singleton\r\n service.vendor = Apache Software Foundation\r\nProvided by :\r\n Apache Felix Configuration Admin Service (7)\r\nUsed by:\r\n Apache Felix Configuration Admin Service (7)\r\n\r\n[org.apache.felix.cm.PersistenceManager]\r\n----------------------------------------\r\n osgi.service.blueprint.compname = groovyConfigPersistenceManager\r\n service.bundleid = 56\r\n service.id = 117\r\n service.pid = com.github.alien11689.osgi.util.groovyconfig.impl.GroovyConfigPersistenceManager\r\n service.ranking = 100\r\n service.scope = bundle\r\nProvided by :\r\n groovy-config (56)\r\nUsed by:\r\n Apache Felix Configuration Admin Service (7)<\/pre>\n<p>Loaded configurations will be cached by configuration admin. We can use <code>org.apache.felix.cm.NotCachablePersistenceManager<\/code> interface if we want to implement custom caching strategy.<\/p>\n<h2 id=\"creating-a-new-properties-file\">Creating a new properties file<\/h2>\n<p>Let&#8217;s create a new properties file in groovy, e.g:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">$ cat etc\/com.github.alien11689.test1.groovy\r\na = '7'\r\nb {\r\n    c {\r\n        d = 1\r\n        e = 2\r\n    }\r\n    z = 9\r\n}\r\nx.y.z='test'<\/pre>\n<p>If we search for properties with pid <code>com.github.alien11689.test1<\/code>, Karaf will find these.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">karaf@root()&gt; config:list '(service.pid=com.github.alien11689.test1)'\r\n----------------------------------------------------------------\r\nPid:            com.github.alien11689.test1\r\nBundleLocation: null\r\nProperties:\r\n   a = 7\r\n   b.c.d = 1\r\n   b.c.e = 2\r\n   b.z = 9\r\n   service.pid = com.github.alien11689.test1\r\n   x.y.z = test<\/pre>\n<p>If we make any change to the file they won&#8217;t be mapped to properties, because there are no file watchers defined for it.<\/p>\n<p>We could manage such properties using Karaf commands instead.<\/p>\n<h2 id=\"managing-configuration-via-karaf-commands\">Managing configuration via Karaf commands<\/h2>\n<p>We can define a new pid using Karaf commands:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">karaf@root()&gt; config:property-set -p com.github.alien11689.test2 f.a 6\r\nkaraf@root()&gt; config:property-set -p com.github.alien11689.test2 f.b 'test'<\/pre>\n<p>Since our <code>PersistenceManager<\/code> has higher <code>service.ranking<\/code> (<code>100 &gt; -2147483648<\/code>), new pid will be stored as a groovy file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">$ cat etc\/com.github.alien11689.test2.groovy\r\nf {\r\n    b='test'\r\n    a='6'\r\n}<\/pre>\n<p>We can also change\/remove properties or remove the whole configuration pid using karaf commands and it will all be mapped to groovy configuration files.<\/p>\n<h2 id=\"sources\">Sources<\/h2>\n<p>Sources are available on <a href=\"https:\/\/github.com\/alien11689\/osgi-groovy-config\">github<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"IntroductionBy deafault, Apache Karaf keeps configuration for bundles in the etc directory as flat properties files. We can override configuration for the storing mechanism by providing own implementation of the org.apache.felix.cm.PersistenceManager i&#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":[50,44,629],"class_list":{"0":"post-13264","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-development-design","7":"tag-groovy","8":"tag-osgi","9":"tag-service"},"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/13264","targetHints":{"allow":["GET"]}}],"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=13264"}],"version-history":[{"count":6,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/13264\/revisions"}],"predecessor-version":[{"id":14856,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/13264\/revisions\/14856"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=13264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=13264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=13264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}