Gangmax Blog

A Dip in OSGi

Here’s some thoughts of mine about OSGi after refering some articles(here and here).

  1. OSGi is a system which provides runtime module management for Java. It can install, start, stop, update and uninstall the Java module(s) at runtime, which is absent in the standard Java/VM environments.

  2. The values of OSGi:

    1. Runtime module management without rebooting the JVM, which is important in some scenarios;

    2. Explicit dependencies and explicit exposed services/interfaces for each OSGi bundle make the applications/systems rely on OSGi platform more modularized;

    3. Anything else?

  3. An OSGi bundle file is just an ordinary jar file with some specific items in the “manifest.mf” file, and it may implementing some specific OSGi interfaces, such as “org.osgi.framework.BundleActivator”.

  4. I’m wondering if there’s any alternative of OSGi in the other more dynamic languages, such as Ruby, Python. In another word, is it necessary?

The most popluar OSGi implementation is Felix, which is just 1.2MB. Here is the step-by-step examples of it. And this is the commands how I create one example and run it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 1. Create the directory structure.
cd ~/coderoot/eclipse/workspace42
mkdir -p example2b/src/tutorial/example2b/
mkdir -p example2b/bin
cd example2b

# 2. Copy the files content from: http://felix.apache.org/site/apache-felix-tutorial-example-2b.html
vi src/tutorial/example2b/Activator.java
vi manifest.mf

# 3. Compile the java file. Note: in the "-cp" section "~/java/...:~/gang/coderoot/..." causes error, full directory must be used to pass, it's weird.
javac -d ./bin/ -cp /home/user/java/felix-framework-4.0.3/bin/felix.jar:/home/user/coderoot/eclipse/workspace42/example2/example2.jar ./src/tutorial/example2b/*.java

# 4. Wrap everything into a jar as the OSGi file.
jar cvfm example2b.jar ./manifest.mf -C ./bin/ tutorial/example2b

# 5. Install the OSGi bundle in Felix console:
cd ~/java/felix-framework-4.0.3
java -jar ./bin/felix.jar
start file:/home/user/coderoot/eclipse/workspace42/example2b/example2b.jar

Comments