Blog

Sie befinden sich hier: Blog

Felix-Fileinstaller zum Deployment von nativen OSGi Bundles in Confluence

Montag, 12. April 2010 @ 09:09 geschrieben von Arno Schumacher

Dieser Blog beschäftigt sich damit, wie man ein beliebiges OSGi Bundles einfach in Confluence deployen kann, in dem man das OSGi Bundle in ein Deployment-Verzeichnis kopiert. Im Prinzip verwendet man hier den gleichen Trick wie bei der OSGi-Console, den ich in meinem vorherigen Blog beschrieben habe.

Im Manifest wird ein Bundle-Activator spezifiziert Manifest-Version: 1.0 Bundle-Version: 1.0.0 Bundle-Name: avono Confluence OSGi Deployer Bundle-ManifestVersion: 2 Bundle-SymbolicName: com.avono.confluence.osgi.deployer Import-Package: org.springframework.stereotype, org.apache.log4j, org.osgi.framework, com.atlassian.config;resolution:=optional Bundle-Activator: com.avono.confluence.osgi.deployer.internal.Activator

Der Bundle-Activator installiert/deinstalliert dann wieder OSGi-Bundles programmatisch. Für den Deployer werden folgende Bundles installiert:

  • org.apache.felix.configadmin-1.2.4
  • org.apache.felix.fileinstall-2.0.4

Für beide Bundles erzeugt der Activator noch die benötigten Dateistrukturen im Confluence Home-Verzeichnis. avono-osgi |-- cm | `-- dir | `-- fileinstall |-- dir `-- tmpdir

Im Verzeichnis avono-osgi/fileinstall/dir können jetzt beliebige OSGi Bundles abgelegt werden (und wieder entfernt werden). Das OSGi Bundle org.apache.felix.fileinstall-2.0.4 sorgt dafür, dass diese installiert (bzw. deinstalliert) werden. Etwa die beiden Bundles:

  • org.apache.felix.shell-1.4.1
  • org.apache.felix.shell.remote-1.0.4
welche wir schon von der OSGi-Console kennen.

Hier noch der Source Code des Activators: public class Activator implements BundleActivator { private static final String AVONO_OSGI = "/avono-osgi"; private static final String AVONO_OSGI_CM = "/avono-osgi/cm"; private static final String AVONO_OSGI_CM_DIR = "/avono-osgi/cm/dir"; private static final String AVONO_OSGI_FILEINSTALL = "/avono-osgi/fileinstall"; private static final String AVONO_OSGI_FILEINSTALL_DIR = "/avono-osgi/fileinstall/dir"; private static final String AVONO_OSGI_FILEINSTALL_TMPDIR = "/avono-osgi/fileinstall/tmpdir"; private static Logger LOG = Logger .getLogger(Activator.class); private List<Long> ids = new ArrayList<Long>(); @Override public void start(BundleContext context) throws Exception { try { LOG.info("Retrieving application configuration ... "); ServiceReference serviceReference = context .getServiceReference(ApplicationConfiguration.class .getName()); ApplicationConfiguration applicationConfiguration = (ApplicationConfiguration) context .getService(serviceReference); String applicationHome = applicationConfiguration .getApplicationHome(); context.ungetService(serviceReference); LOG.info("Creating installer directories ... "); final String[] dirs = new String[] { AVONO_OSGI, AVONO_OSGI_FILEINSTALL, AVONO_OSGI_FILEINSTALL_TMPDIR, AVONO_OSGI_FILEINSTALL_DIR, AVONO_OSGI_CM, AVONO_OSGI_CM_DIR }; final Map<String, String> constructedDirs = new HashMap<String, String>(); for (String dir : dirs) { File directory = new File(applicationHome + dir); if (!directory.exists()) { directory.mkdir(); } constructedDirs.put(dir, directory .getAbsolutePath()); } System.setProperty("felix.fileinstall.dir", constructedDirs .get(AVONO_OSGI_FILEINSTALL_DIR)); System .setProperty( "felix.fileinstall.tmpdir", constructedDirs .get(AVONO_OSGI_FILEINSTALL_TMPDIR)); System.setProperty("felix.cm.dir", constructedDirs.get(AVONO_OSGI_CM_DIR)); LOG.info("Installing bundles ... "); List<Bundle> bs = Arrays .asList( install(context, "org.apache.felix.configadmin-1.2.4"), install(context, "org.apache.felix.fileinstall-2.0.4")); LOG.info("Starting bundles ..."); for (Bundle b : bs) { LOG.info("Starting bundle " + b.getSymbolicName()); b.start(); ids.add(b.getBundleId()); } LOG.info("OK"); } catch (Exception e) { LOG.error(e); throw e; } } private Bundle install(BundleContext context, String bundleJar) throws Exception { final ClassLoader classLoader = this .getClass().getClassLoader(); return context.installBundle(bundleJar, classLoader .getResourceAsStream("/bundles/" + bundleJar + ".jar")); } @Override public void stop(BundleContext context) throws Exception { Collections.reverse(ids); try { LOG.info("Stopping bundles"); for (Long id : ids) { Bundle b = context.getBundle(id); if (b != null) { LOG.info("Stopping bundle " + b.getSymbolicName()); b.stop(); } } LOG.info("Uninstalling bundles"); for (Long id : ids) { Bundle b = context.getBundle(id); if (b != null) { LOG.info("Uninstalling bundle " + b.getSymbolicName()); b.uninstall(); } } LOG.info("OK"); } catch (Exception e) { LOG.error(e); throw e; } finally { ids.clear(); } } }

Links:

  1. OSGi Console für Confluence: http://www.avono.de/blog/-/asset_publisher/Zp9s/blog/id/20905
  2. Confluence Plugin Framework: http://confluence.atlassian.com/display/PLUGINFRAMEWORK/Plugin+Framework+Developer+Documentation
  3. OSGi: http://en.wikipedia.org/wiki/OSGi
  4. Apache Felix OSGi Container: http://felix.apache.org
  5. Spring Dynamic Modules: http://www.springsource.org/osgi

zur Übersicht

Willkommen auf dem avono Blog

Hier auf dem avono Blog finden Sie in regelmäßigen Abständen sowohl technische Neuigkeiten aus unserer Partnerproduktwelt als auch nützliche Entwicklertipps.
Und jetzt kommt der obligatorische Disclaimer: Die Ausführungen der Blogeinträge spiegeln nicht die Meinung der avono AG sondern nur die Sicht der einzelnen Autoren wider.

Kategorien


Suche