{"id":68,"date":"2010-05-11T21:00:42","date_gmt":"2010-05-11T19:00:42","guid":{"rendered":"http:\/\/www.pmannel.de\/wordpress\/?p=68"},"modified":"2024-01-10T18:05:46","modified_gmt":"2024-01-10T17:05:46","slug":"osgi-mit-felix","status":"publish","type":"post","link":"https:\/\/www.pmannel.de\/wordpress\/?p=68","title":{"rendered":"OSGI mit Felix"},"content":{"rendered":"<p>Hier ein kleines Beispiel zu OSGI mit Apache Felix<\/p>\n<p>Man ben\u00f6tigt:<\/p>\n<p><a href=\"http:\/\/felix.apache.org\/site\/downloads.cgi\">Apache Felix<\/a><br \/>\n<a href=\"http:\/\/www.aqute.biz\/Code\/Download\">BND-Tool<\/a><br \/>\noder, ganz neu, f\u00fcr noch komfortableres Arbeiten das BND-Tool als Eclipse-Plugin<\/p>\n<p><a href=\"http:\/\/njbartlett.name\/blog\/2010\/05\/10\/announcing-bndtools-simple-powerful-osgi-tools-for-eclipse\/\">bndtools<\/a><br \/>\n<a href=\"http:\/\/www.eclipse.org\/downloads\/\">Eclipse<\/a><\/p>\n<p><strong>Einstellungen f\u00fcr Eclipse:<\/strong><br \/>\nMan legt eine neue User Library mit Namen Felix an: <em>Window-&gt;Preferences-&gt;Java-&gt;Build path-&gt;User Libraries-&gt;New<\/em><\/p>\n<p>Die felix.jar wird mit der neuen User Library verlinkt: <em>Window-&gt;Preferences-&gt;Java-&gt;Build path-&gt;User Libraries-&gt;Add JARs<\/em><br \/>\n<a href=\"http:\/\/www.pmannel.de\/wordpress\/wp-content\/uploads\/userlib.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-379\" title=\"userlib\" src=\"http:\/\/www.pmannel.de\/wordpress\/wp-content\/uploads\/userlib-300x189.jpg\" alt=\"\" width=\"300\" height=\"189\" \/><\/a><\/p>\n<p>Anschlie\u00dfend legt man ein neues Java-Projekt in Eclipse an und f\u00fcgen die neue User Library hinzu.<br \/>\nDanach kopiert man die beiden Ordner <strong>bundle <\/strong>und <strong>conf <\/strong>direkt in das Projektverzeichnis des erstellten Projekts.<\/p>\n<p><a href=\"http:\/\/www.pmannel.de\/wordpress\/wp-content\/uploads\/dir.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-378\" title=\"dir\" src=\"http:\/\/www.pmannel.de\/wordpress\/wp-content\/uploads\/dir.jpg\" alt=\"\" width=\"217\" height=\"137\" \/><\/a><\/p>\n<p><strong>Felix starten:<\/strong><br \/>\nMan richtet unter Eclipse eine neue Laufzeitkonfiguration f\u00fcr Felixein: <em>Run-&gt;Run Configurations-&gt;Java Applaction-&gt;New<\/em><br \/>\n<a href=\"http:\/\/www.pmannel.de\/wordpress\/wp-content\/uploads\/console.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-377\" title=\"console\" src=\"http:\/\/www.pmannel.de\/wordpress\/wp-content\/uploads\/console-300x120.jpg\" alt=\"\" width=\"300\" height=\"120\" \/><\/a><\/p>\n<p>In der Console kann man sich jetzt mit <em>\u201cps\u201d<\/em> die installierten Bundles oder mit <em>\u201chelp\u201d<\/em> die verf\u00fcgbaren Kommandos ansehen.<\/p>\n<p><strong>BND-Tool:<\/strong><br \/>\nDie Jar des BND-Tools wird in das Projektverzeichnis kopiert und au\u00dferdem in das Dropin-Verzeichnis von Eclipse. Danach muss man Eclipse neu starten.<\/p>\n<p><strong>Das eigentliche Programm:<\/strong><\/p>\n<p>Man erzeugt ein Service-Interface sowie die Implementierung dazu:<\/p>\n<pre class=\"brush:java\">package de.pmannel.osgi;\r\n\r\npublic interface HelloService\r\n{\r\n\tvoid sayHello();\r\n}<\/pre>\n<pre class=\"brush:java\">package de.pmannel.osgi;\r\n\r\npublic class HelloServiceImpl implements HelloService\r\n{\r\n\r\n\t@Override\r\n\tpublic void sayHello()\r\n\t{\r\n\t\tSystem.out.println(\"Hello Felix! Greetings from Service\");\r\n\r\n\t}\r\n}<\/pre>\n<p>Au\u00dferdem erzeugt man einen Activator, der beim Starten\/Stoppen des Bundles ausgef\u00fchrt wird.<\/p>\n<pre class=\"brush:java\">package de.pmannel.osgi;\r\n\r\nimport java.util.Dictionary;\r\nimport java.util.Hashtable;\r\n\r\nimport org.osgi.framework.BundleActivator;\r\nimport org.osgi.framework.BundleContext;\r\nimport org.osgi.util.tracker.ServiceTracker;\r\n\r\npublic class HelloFelixActivator implements BundleActivator\r\n{\r\n\tprivate HelloService helloService;\r\n\tprivate ServiceTracker serviceTracker;\r\n\r\n\t@Override\r\n\tpublic void start(BundleContext context) throws Exception\r\n\t{\r\n\t\tDictionary props = new Hashtable();\r\n\t\tprops.put(\"objectName\", \"de.pmannel.osgi<:type=HelloService\");\r\n\t\tcontext.registerService(HelloService.class.getName(), new HelloServiceImpl(), props);\r\n\t\tserviceTracker = new ServiceTracker(context, HelloService.class.getName(), null);\r\n\t\tserviceTracker.open();\r\n\t\thelloService = (HelloService)serviceTracker.getService();\r\n\r\n\t\thelloService.sayHello();\r\n\t}\r\n\r\n\t@Override\r\n\tpublic void stop(BundleContext context) throws Exception\r\n\t{\r\n\t\tserviceTracker.close();\r\n\t\tserviceTracker = null;\r\n\t\thelloService = null;\r\n\t\tSystem.out.println(\"Goodby Felix..\");\r\n\r\n\t}\r\n}<\/pre>\n<p>Nun erzeugt man im Projektverzeichnis eine .bnd-Datei, in der man bestimmte Parameter f\u00fcr die sp\u00e4tere Manifest-datei des OSGI-Bundles setzt.<\/p>\n<pre># felix.bnd\r\nPrivate-Package: de.pmannel.osgi\r\nBundle-Activator: de.pmannel.osgi.HelloFelixActivator\r\nBundle-SymbolicName: HelloFelix\r\nBundle-Version: 1.1.0<\/pre>\n<p>Mit einem Rechtsklick auf der Datei -&gt;Make Bundle erzeugt man automatisch die Manifest-Datei.<\/p>\n<pre>Manifest-Version: 1.0\r\nPrivate-Package: de.pmannel.osgi\r\nBundle-Version: 1.1.0\r\nTool: Bnd-0.0.384\r\nBnd-LastModified: 1273493680531\r\nBundle-Name: HelloFelix\r\nBundle-ManifestVersion: 2\r\nCreated-By: 1.6.0_07 (Sun Microsystems Inc.)\r\nBundle-Activator: de.pmannel.osgi.HelloFelixActivator\r\nImport-Package: org.osgi.framework,org.osgi.util.tracker\r\nBundle-SymbolicName: HelloFelix<\/pre>\n<p>Das Bundle wird in Felix \u00fcber die Console installiert:<\/p>\n<pre class=\"brush:java\">install file:HelloFelix.jar<\/pre>\n<p>Anschlie\u00dfend wird das Bundle gestartet, indem man sich \u00fcber \u201cps\u201d die ID des Bundles holt:<\/p>\n<pre>start 9<\/pre>\n<p><a href=\"http:\/\/www.pmannel.de\/wordpress\/wp-content\/uploads\/cons.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-381\" title=\"cons\" src=\"http:\/\/www.pmannel.de\/wordpress\/wp-content\/uploads\/cons-300x101.jpg\" alt=\"\" width=\"300\" height=\"101\" \/><\/a><\/p>\n<p>Das war\u2019s!<\/p>\n<p>Man kann jetzt auch im laufenden Betrieb zb.: die <em>sayhello()<\/em>-Methode \u00e4ndern und dann mit<\/p>\n<pre>update 9<\/pre>\n<p>eine neue Ausgabe erzeugen\u2026.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hier ein kleines Beispiel zu OSGI mit Apache Felix Man ben\u00f6tigt: Apache Felix BND-Tool oder, ganz neu, f\u00fcr noch komfortableres Arbeiten das BND-Tool als Eclipse-Plugin bndtools Eclipse Einstellungen f\u00fcr Eclipse: Man legt eine neue User Library mit Namen Felix an: Window-&gt;Preferences-&gt;Java-&gt;Build path-&gt;User Libraries-&gt;New Die felix.jar wird mit der neuen User Library verlinkt: Window-&gt;Preferences-&gt;Java-&gt;Build path-&gt;User Libraries-&gt;Add&#8230; <\/p>\n<div class=\"read-more\"><a href=\"https:\/\/www.pmannel.de\/wordpress\/?p=68\">Weiterlesen<\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76,3,8],"tags":[14,73],"class_list":["post-68","post","type-post","status-publish","format-standard","hentry","category-adobe-aem","category-java","category-osgi","tag-apache-felix","tag-osgi"],"_links":{"self":[{"href":"https:\/\/www.pmannel.de\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/68","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pmannel.de\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pmannel.de\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pmannel.de\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pmannel.de\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=68"}],"version-history":[{"count":9,"href":"https:\/\/www.pmannel.de\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/68\/revisions"}],"predecessor-version":[{"id":661,"href":"https:\/\/www.pmannel.de\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/68\/revisions\/661"}],"wp:attachment":[{"href":"https:\/\/www.pmannel.de\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=68"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pmannel.de\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=68"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pmannel.de\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}