The Adding an existing JXTA application to OSGI

The previous WIKI contribution demonstrated how you can set up JXTA for OSGI, and how to get the examples from Practical JXTA II up and running.

In this section we will show how to get an existing JXTA application to run in OSGI. This tutorial assumes that your workspace is set up according to the directions of the previous tutorial. It is also assumed that existing JAVA code needs to be ported in an OSGI bundle in the Eclipse workspace.

Setting up

This tutorial comprises of the following steps:

The first step consists of making a new bundle project, like the one we made previously. In essence this is a regular OSGI bundle, with a declarative service component defined in the OSGI-INF folder in a file called module.xml

Experienced OSGI bundle developers may wonder why the JAVA source code could not be simply added to the project, and started from the activator. Well, in essence this is exactly what is done, but the OSGI bundle accesses the JXTA Modules through the declarative service mechanism, and so the system needs to know when these modules are available.

Besides this, JXTA tends to take a lot of time to start up, so it would not be a good thing to do this in the start method of the bundle activator; there is a good chance that the bundle may time out. This is why JXTA is started in a separate thread.

project is created in the workspace, in which we next define a target definition

(new → target definition) Start with an empty definition and select a name of your choosing ,

Next add two update sites:

The Luna update site

Select the Eclipse Platform

http://download.eclipse.org/eclipse/updates/4.4

The JXSE-OSGI bundles:

Select the Eclipse Platform

https://chaupal.github.io/releases/2.8.0/milestone/20150116/alpha/jxse-osgi/site.xml

Select the bundles

(the latest releases can be found here: https://chaupal.github.io/releases.html)

Then set the target definition.

Creating an OSGI Bundle Project

Now we have the target set up, we can create a bundle project. This is done in the regular fashion. Let's call the project org.jxse.test. Next create a Declarative Service component in OSGI-INF/jxta.xml with the following code:

<?xml version="1.0" encoding="UTF-8"?>
<scr:component name="org.jxse.test" immediate="true"
       deactivate="deactivate" activate="activate"
       xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0">
<implementation class="org.jxse.test.Component"/> <reference name="org.jxse.test.ModuleBuilder" unbind="unregisterBuilder" policy="dynamic" interface="net.jxta.module.IModuleBuilder" cardinality="0..n" bind="registerBuilder"/> </scr:component>

Then include the following class:>

  package org.jxse.test;
import net.jxta.exception.ConfiguratorException; import net.osgi.jxse.AbstractJxseComponent; import Examples.A_JXTA_Connection_And_Local_Configuration._100_Starting_And_Stopping_JXTA_Example;
public class Component extends AbstractJxseComponent{
@Override protected void onRunJxse() { try _100_Starting_And_Stopping_JXTA_Example.main ( null ) } catch( ConfiguratorException e ){ e.printStackTrace(); } }

Next we have to create a launch configuration for an OSGI framework. Include the bundle we just made, and add the required bundles. Don't forget to add:

When you run the code you will find that you are running one of the examples from Jérôme Verstrynge's book, Practical JXTA II. These examples are shipped as one of the bundles in the set. If you simply want to run these, the following code will suffice as well:


package org.jxse.test;

import net.jxse.practical.jxta.AbstractPJ2Component;
public class Component extends AbstractPJ2Component{
public Component(){ super( Examples.A_100_Starting_And_Stopping_JXTA ); } }

A little background information

As was mentioned earlier, the JXSE-OSGI bundles form a wrapper around the JXSE code. The wrappers are mainly intended to break the dependencies with third party libraries, such as Jetty and Netty, so that the regular Eclipse replacements can be used instead. However, the JXSE code is also divided over two bundles, separating the code related to configuration (platform) from the core functionality. The complete set of bundles are the following:

net.jxse.osgi

Core JXSE


net.jxse.osgi.platform

configuration

e.g. NetworkManager, NetworkConfigurator

net.jxse.netty

Netty transport bundle


net.jxse.jetty

Jetty Servlet Http


net.jxse.derby

Derby database support


net.jxse.h2

H2 database support


org.spongycastle.fragment

Spongycastle support


net.jxse.pracxtical.jxta

Practical JXTA II

See: http://freecomputerbooks.com/Practical-JXTA-II.html

Licensing...

JXTA is a registered trademark of Sun Microsystems/Oracle.

JXSE is still governed by the JXTA license, which cannot be changed. New code by Chaupal that extends/modifies JXSE is governed by the Apache License, version 2.0, and must be committed to the JP2P GitHub repository.