How to Build an OSGi-Based Cytoscape 3 Plugin on Eclipse

Status

TableOfContents()

Introduction

This tutorial is a step-by-step instruction for creating Cytoscape 3 plugin based on OSGi framework. By following this tutorial, you can create a new plugin for Cytoscape 3.

Background

In an OSGi system, everything is a plugin. This means, Cytoscape 3 running on OSGi framework is a collection of plugins called bundle and developing a plugin for Cytoscape 3 is equivalent to developing OSGi bundles.

MLC: I'm finding the use of the term 'plugin' really confusing because there appears to be at least four different types of 'plugins'. There are the older (C2.6) Cytoscape plugins, Maven plugins, Eclipse plugins (that use OSGI framework?), and OSGi plugins. Several of these are being referenced in various ways in this document.

The main difference between Cytoscape 2.x plugin and 3.x plugin is that you do not have to implement CytoscapePlugin interface in Cytoscape 3. Instead, you need to build your plugin jar file with metadata for OSGi. However, this process will be handled by Eclipse and other tools and you do not have to create metadata manually.

Requirements

To understand each steps in this document, it is better to understand following before building your plugin:

Tools

The following tools should be installed on your machine before starting this tutorial:

Eclipse Plugins

Goal of This Tutorial

There are lots of ways to organize your plugin project directory, but in this tutorial, for simplicity, we will use the following directory structure:

Other directories will be generated automatically. Your workspace will look like the following:

In the example above, samplePluginA is the name of plugin project and plugin1 is the name of plugin bundle.

Prerequisite

Since this project depends on Cytoscape 3 core, you need to know how to build and run Cytoscape 3 on Eclipse. Please read [:Cytoscape 3.0/Eclipse/CoreDevelopment:this tutorial] first.

Procedure

Part 1: Create New Plugin Project

  1. Start Eclipse. For simplicity, I recommend to use fresh and empty workspace without any other projects.
  2. Select Run > Run Configurations...

  3. Right click Maven Build and create new configuration

  4. Press Variables... and select workspace_loc as the destination directory for the new plugin project. However, the location is up to you. You can select any directory. attachment:pluginProject1.png

  5. Press Select... and use the tree browser to expand pax and then choose create-project as the goal.

  6. Press Add and set artifactId and groupId. Usually, groupId is based on your organization's URL. This is because once you publish your plugin in the Maven repository, the string groupId+artifactId must be unique to locate your project attachment:pluginProject2.png

  7. Press Apply and Run.

  8. At this point, there is no new project in your workspace. This is normal since the process above only creates Maven project, not an Eclipse project.
  9. Open File > Import window and select Maven Projects attachment:pluginProject3.png

  10. Set parameters attachment:pluginProject4.png

Part 2: Create Plugin Bundle

Next, you need to create an actual OSGi bundle of your plugin. You can create multiple bundles for a plugin, but in this tutorial, your plugin consists of one bundle.

  1. Select Run > Run Configurations...

  2. Right click Maven Build and create new configuration

  3. Browse Workspace and select your project root directory

  4. Set the goal to create-bundle under the pax entry in the tree browser.

  5. Add two parameters for the new plugin. package will be the root package name of this bundle, and bundleName will be the name of your new plugin. Also, add junit parameter to automatically generate test case template. The value should be the version of JUnit you want to use. attachment:pluginProject5.png

  6. Press Apply and Run

  7. Next, you need to add OSGi project nature to your workspace. Select Run > Run Configurations...

  8. Right click Maven Build and create new configuration

  9. Browse Workspace and select your project root directory

  10. Goal should be pax:eclipse attachment:pluginProject6.png

  11. Press Apply and Run

  12. Import the generated bundle into your workspace. It is same as part 1. Now your workspace looks like the following: attachment:pluginProject7.png

    The samplebundle folder is the actual project you are going to write your plugin.

    MLC: I'm not seeing an analogous directory structure based on the values shown for part 2, #5 and #10. Wouldn't samplebundle be placed under the directory c3plugin, but instead, it is at the same level. It looks like this is because the base directory is set to ${workspace_loc:/c3plugin}.

    MLC: A sample workspace is shown, but this brings up the question of how we handle multiple plugins if the names generated are the same (e.g., compiled-bundle-settings, provision, shared-bundle-settings, wrapper-bundle-settings). If you have several plugins, how do you move to C3 and manage the names of all these generated Eclipse projects?

OSGi Project Directory Structure

In the samplebundle directory, you may see some new files you are not familier with.

MLC: Do we want to suggest how to place and name bundles and their associated plugins? As a newbie to C3, I'm a bit lost on how I should name and place my existing C2.6 plugins to make C3.0 plugins. For example, if I have a 'VCP' plugin that exists in C2.6 and is in the directory 'VistaClaraPlugin' and I want to migrate it to C3, what would I do? Is it better to place both the bundle and the plugin under a common superdirectory or separate them?. So, if I make a C3.0 version of this plugin, would I make a C3VistaClaraPlugin.

Part 3: Add Cytoscape Application Dependency

To run your plugin with Cytoscape 3 core distribution, you need to add all of its bundles to plugin pom.xml file.

Part 4: Adding Your Plugin to the Maven Repository

You now need to add your plugin to the Maven repository so it can be found and loaded when you run Cytoscape. To do this from Eclipse, you need to make another run configuration that installs the plugin:

  1. Select Run > Run Configurations...

  2. Right click Maven Build and create new configuration.

  3. Pick an approriate name for the configuration (e.g, Add Plugin to Repository), pick your Base directory to be the same as in Step 3

Part 5: Run Template Project with Cytoscape

  1. Create new Maven Run target. Base directory is your plugin root (see the screenshot below) attachment:pluginProject9.png

    MLC: The goal shown in the above screenshot is confusing because it doesn't follow the true form of the goals as is shown in previous screenshots. The real goal would be org.ops4j:maven-pax-plugin:1.3:provision.

  2. Apply and Run. You can see Cytoscape 3 and your new plugin are running together on the same OSGi framework. attachment:pluginProject10.png

    If you type services, you can see example OSGi service which is defined in the template code is running. attachment:pluginProject11.png

  3. Quit Cytoscape

Part 6: Edit Template Code

Now you are ready to write your own code. Let's add a very simple code to the template.

  1. Open samplebundle > src/main/java > org.cytoscape.sample.internal > ExampleActivator.java

  2. Edit the following method and save it

   1         public void start(BundleContext bc) throws Exception {
   2                 System.out.println("STARTING org.cytoscape.sample");
   3 
   4                 Dictionary props = new Properties();
   5                 // add specific service properties here...
   6 
   7                 System.out.println("REGISTER org.cytoscape.sample.ExampleService");
   8 
   9                 // Register our example service implementation in the OSGi service
  10                 // registry
  11                 bc.registerService(ExampleService.class.getName(),
  12                                 new ExampleServiceImpl(), props);
  13 
  14                 JOptionPane.showMessageDialog(null, "Hello Cytoscape 3!",
  15                                 "Plugin Initialized", JOptionPane.INFORMATION_MESSAGE);
  16         }
  17 
  1. Right-click the root dir of the project (in this example, it is c3plugin) and execute Run As > Maven install

Part 6: Debug your Plugin

  1. Add a brakepoint to your plugin code. As an example, add it in the new line you have just added to the template attachment:pluginProject12.png
  2. Create a new debug configuration. Select Run > Debug Configurations and create new OSGi configuration.

  3. In the Bundles tab, uncheck all bundles. Then select Felix 1.0.4 via PAX Runner for Framework attachment:pluginProject13.png

  4. In the Arguments tab, add following as VM arguments

    -Xbootclasspath/a:bundles/com.jgoodies.looks_2.1.2.jar:bundles/cytoscape-sun.jhall_1.0.0.jar
    attachment:pluginProject14.png
  5. In the PAX Cursor tab, press Add POM and select deploy-pom.xml under PLUGIN_PROJECT_ROOT/runner directory where PLUGIN_PROJECT_ROOT is your plugin project's root directory. You need to select Any File to make deploy-pom.xml be displayed.

  6. Set update to yes attachment:pluginProject15.png

  7. Apply and Debug. PAX Runner executes Cytoscape 3 and your plugin. It will stop at the break point. attachment:pluginProject16.png

  8. Press resume button and continue. Eventually, you will see the following dialog on the Cytoscape Desktop: attachment:pluginProject17.png

Use Spring Dynamic Modules for Your Plugin

If you want to use OSGi service mechanism, using Spring Framework is a good alternative to use OSGi-dependent API directly. In this section, you will learn how to create Spring-Powered bundles for your plugins.

Install Spring IDE

There is an Eclipse plugin for editing Spring configuration files. You can use this for your Spring-OSGi Cytoscape plugin project.

Note: although this program is called Spring IDE, it is not an independent application. It is just a regular Eclipse plugin.

You can follow this instruction to install it:

http://springide.org/project/wiki/SpringideInstall

Create Spring-Powered Bundle

There are only two differences between OSGi-dependent plugins and Spring-DM plugins:

  1. Add one more parameter when you create bundle. When you create a plugin bundle, add spring as an extra parameter. Then PAX Construct automatically generates template bundle project ready for writing Spring-DM code. attachment:spring1.png attachment:spring12.png

  2. Set PAX Cursor profile to spring. All you have to do is just check the spring checkbox on the Pax Cursor tab. attachment:spring2.png

That's it. While your plugin is running with PAX Cursor, type ps in the console. You can see Spring-DM infrastructure bundles are running with Cytoscape 3.

More advanced tutorial including how to write Spring-DM setting files will be available soon.

Funding for Cytoscape is provided by a federal grant from the U.S. National Institute of General Medical Sciences (NIGMS) of the Na tional Institutes of Health (NIH) under award number GM070743-01. Corporate funding is provided through a contract from Unilever PLC.

MoinMoin Appliance - Powered by TurnKey Linux