基于Equinox构建OSGi项目

几种OSGi框架

Several independently implemented OSGi frameworks exist today, including four that are available as open source software.
  • Equinox  is the most widely deployed OSGi framework today owing to its use in the core runtime of Eclipse. It can also be found in many in-house custom applications as well as packaged products such as Lotus Notes and IBM WebSphere Application Server. Equinox implements Release 4.1 of the OSGi specifications and is licensed under the Eclipse Public License (EPL).
  • Knopflerfish  is a popular and mature implementation of both OSGi Release 3 and Release 4.1 specifications. It is developed and maintained by Makewave AB, a company based in Sweden, and is licensed under a BSD-style license. Makewave also offers Knopflerfish Pro, a commercially supported version of Knopflerfish.
  • Felix  is a community implementation of the OSGi Release 4.x under the Apache Group umbrella. It is designed particularly for compactness and  ease of embedding, and is the smallest (in terms of minimal JAR size) of the Release 4 implementations. It is licensed under the Apache License Version 2.0.
  • Concierge  is a very compact and highly optimized implementation of OSGi Release 3. This makes it particularly suited to resource-constrained plat-forms such as mobile phones. Concierge is licensed under a BSD-style license.
 

下载Equinox框架

下载解压后,设置环境变量EQUINOX_HOME。

设置Eclipse

设置User Libraries,将Equinox目录下的plugins/org.eclipse.-osgi_version.jar 文件加入到用户库中。
接下来创建一个Java项目,命名为 “OSGi Tutorial” ;选择依赖上述User Library。

运行Equinox

We’re now going to run Equinox using the Eclipse launcher. We could run Equinox from a shell or command prompt window, but if we run under Eclipse we can easily switch to debugging mode when things go wrong.
 

Run Configuration

From the main menu, select Run → Run Configurations (or Open Run Dia-log in older Eclipse versions). On the left hand side select “Java Application” and click the “New” button. Change the Name field at the top to “Equinox”. Ensure the Project field is set to the “OSGi Tutorial” project, and then click the Search button next to the Main class field. The only available “main” class should be the one called EclipseStarter, so choose that one.
 
Now flip to the Arguments tap of the Run Configurations dialog. In the Program Arguments field enter the following arguments exactly:
−console −configuration runtime
  • The first argument -console indicates that Equinox should create an interactive console for us to work with – if we forget this argument then it will start up and then immediately exit!
  • The second argument (consisting of two words, -configuration runtime) asks Equinox to create its “configuration area” in a local directory named runtime. The configuration area is where Equinox stores its temporary state information.

Run

Now click Run. Equinox will print the following:
osgi >
This is Equinox’s standard shell prompt and indicates it is waiting for input from us. We are now ready to run some commands. The most frequently used command is ss, which prints the list of currently installed bundles along with their state. Let’s try running ss now:
osgi > ss

Framework is launched .

id State Bundle
0 ACTIVE org . eclipse . osgi_3 . 5 . 0 . v20090311 − 1300
The only bundle here is Equinox itself, listed under the name of the Equinox JAR file. It is also called the “System Bundle”, and it always has the bundle ID of zero.

Run from command line

For reference, if we wanted to run Equinox from the command line then we could do so as follows, assuming we have a shell variable named EQUINOX_HOME set to the installation directory we created earlier:
java −jar $EQUINOX_HOME / plugins / org.eclipse.osgi_∗.jar −console −configuration runtime

Install bundle

For the moment, there is not much more we can do with Equinox until we install some more interesting bundles. However you may at this point wish to explore the commands available by typing help. When bored with this, don’t shutdown Equinox just yet; leave it running for now.
 
我们先创建一个bundle,名为helloworld.jar。BundleActivator如下:
package org.osgi.tutorial ;
import org.osgi.framework.BundleActivator ;
import org.osgi.framework.BundleContext ;
public class HelloWorldActivator implements BundleActivator {
    public void start ( BundleContext context ) throws Exception {
      System.out.println ( " Hello , World !" ) ;
  }
    public void stop ( BundleContext context ) throws Exception {
      System.out.println ( " Goodbye , World !" ) ;
  }
}
 
Now we can try installing this bundle into Equinox, which you should still have running from the previous section (if not, simply start it again). At the Equinox shell prompt, type the command:
install file : helloworld . jar
Equinox will respond with the bundle ID that it has assigned to the bundle:
Bundle id is 1
 
Let’s take a quick look at the bundle list by typing ss. It should look like this:
Framework is launched .
id State Bundle
0 ACTIVE org . eclipse . osgi_3 . 5 . 0 . v20090311 − 1300
1 INSTALLED helloworld_0 . 0 . 0
 

Start bundle

Now the moment of truth: start the new bundle by typing start 1. We should see:
osgi > start 1
Hello , World !
 
What does the bundle list look like now if we type ss? We’ll ignore the rest of the listing, as it hasn’t changed, and focus on our bundle:
1 ACTIVE helloworld_0 . 0 . 0
 

Stop bundle

The bundle is now in the “ACTIVE” state, which makes sense since we have explicitly started it. Now stop the bundle with stop 1:
osgi > stop 1
Goodbye , World !
 
This works as expected. What does the bundle list look like now?
1 RESOLVED helloworld_0 . 0 . 0
 
This is interesting. When we started the bundle it changed its state from Installed to Active, but when we stopped the bundle it didn’t go back to Installed: instead it went to a new state “RESOLVED”.
refer to: OSGi生命周期
 
 
 
 
 
 
 
 
 
 
原文地址:https://www.cnblogs.com/keanuyaoo/p/3356116.html