Experience basic OSGi instances and go into another area of thought (RPM)----including packaging and publishing for executable files __ published

Source: Internet
Author: User
Tags file url ldap

Software modules more and more plug-in development, even the hardware is everywhere hot plug, software more when so. Remember that there is a JPF (Java Plugin Framework), but also to achieve dynamic plug-in, but if there is an industry-standard things generally better. So the turn to OSGi (Open Service Gateway Initiative) on the stage, OSGi has been out for several years, the application is also vigorous, such as Eclipse 3 began to no longer use the original plug-in system, completely with OSGi build. WebSphere 6.1 is also fully used in Osgi;jboss, WebLogic, Spring DM, and even in the control systems of BMW cars.

As mentioned earlier, you can use OSGI as your microkernel, and the benefits of microkernel can be as follows: A Linux server fails, the application is broken, some services can not access, etc., but as long as it is connected to the network, SSH is still alive, we have the means to go in to repair it, want to install, uninstall anything.

OSGI also allows you to dynamically add and subtract services, or dynamically load unload classes and other resources. The resources in OSGi are called Bundle, so if you are based on OSGi programs, you can find many useful Bundle online to plug into your software system directly. Bundle is actually a jar file, just in MANIFEST. MF has a special definition, each Bundle by the implementation of the Bundleactivator to control the Bundle lifecycle and release, listen to the framework of events, or to communicate with the framework.

OSGI is a specification, its implementation currently has Equinox, Knopflerfish, Oscar, Felix and so on, because every day I deal with Eclipse and the Equinox behind it, so here's the example program to use Equinox bar.

This example is based on the "OSGi combat" this PDF document is slightly modified, than the original example to be simpler, because as far as possible to avoid the introduction of other Bundle, not Web applications, but made a window program.

The same, but also a user login verification program, the program can dynamically switch the verification mode (database authentication or LDAP authentication), can also insert new authentication methods, such as file verification methods. Based on this, we need to establish four Bundle, respectively:

1. Uservalidatorbundle-Defines the Validator interface method, which is also used as an interaction with the user Bundle
2. Dbvalidatorbundle--The Validator method was implemented and validated by the database Bundle
3. Ldapvalidatorbundle--The Validator method is implemented, which is validated with Ldap Bundle
4. Filevalidatorbundle--The Validator method was implemented and verified by file Bundle

The Bundle that defines the interface needs to export the package that defines the interface, and the Bundle that implements the interface definition needs to import the previously exported interface package.

OSGI is a bit like the SPI (Service Provider Interface) pattern, with an interface definition at the top, and then several possible implementation providers that can choose which implementation to run, such as common-logging and log4j, JDK log, but Is that the SPI does not have the dynamics of OSGI. Similarly, a service interface needs to be defined in one of the Bundle of OSGI, and no interface specification can be used to guide how the method is invoked. The mapping to the SPI mode is that Uservalidatorbundle is the service-defined, and the other Bundle are service providers.

The Bundle of OSGI can be developed directly in Eclipse, and visual operations and debugging are supported. Because Eclipse itself is a Equinox, there is no need to download the Equinox SDK separately. As long as you have Eclipse on your computer, you'll be able to spy on Equinox.

In Eclipse, in the menu run->run configurations window, in the left OSGi Framework, right-click New, then deselect all, just select a few Bundle and click the Run button on the bottom right.

A osgi> prompt appears in Eclipse's Console window where you can enter various commands, such as the SS display all Bundle, as shown in the figure:


You can also install uninstall Bundle, enable stop Bundle, and so on. Of course, the actual OSGi program is not going to bring an OSGi console, the OSGi console can be done in your program, or as a true background management interface, users will not be aware of. The reason to put a osgi> out, is to develop, testing convenience.

What we're going to do now is like the Bundle that is displayed with SS commands under the Osgi> console. The Eclipse used in this example is a 3.5.2 version, and the other 3.x versions may be slightly different and should not be affected.


Officially started.

I. Establishment of PLUG-IN Project Project Uservalidatorbundle


Fill in the project name Uservalidatorbundle,this plug-in is targeted to run with: Specifies an OSGI framework standard, establishing a standard OSGi project.


Enter the relevant metadata information for the bundle, which will be reflected in the meta-inf/manifest. MF file.

Plug-in ID refers to the unique identification of bundle, in the actual project can adopt Java-like Package name organization strategy to ensure the uniqueness of identity;
Plug-in version refers to the versions of bundle;
Plug-in name refers to the more meaningful name of the bundle;
Plug-in provider refers to the provider of bundle;

Also create a key Activator class, consider the package name carefully. The figure shows that this is options (optional), but in our case, it is very heavy to start the interface or register the service.

If you don't need to <Next> Select a template, you can click the <Finish> button directly. Eclipse enters the Plug-in Development view and opens Editor with Plug-in Manifest Manifest. MF file. The Activator.java file that you specified earlier is also generated for you. The following figure:



Two. Provide user authentication interface package externally

1) Establish a Validator interface


2 Set the package of the external service to be exported

In MANIFEST. MF Editor, select the Runtime tag, click the Add button in exported Packages, and in the pop-up window, select the package com.unmi.login.activator where the Validator interface is located.


This step, in fact, is in MANIFEST. A row was added to MF

Export-package:com.unmi.login.service

Well, the job of creating uservalidatorbundle is over for the time being.

Three. Create several other Bundle

Create several other Bundle by creating a uservalidatorbundle similar method, and note the location of the Activator implementation class for each Bundle.

1) Import Service Interface Package

After creating the Dbvalidatorbundle, Ldapvalidatorbundle, and filevalidatorbundle, you need to import Uservalidatorbundle exported package for them to express that they are The provider of the Validator interface.

The method of operation is, individual MANIFEST. MF Editor in the Dependencies tab, imported Packages point Add button, pop-up window to select the Com.unmi.login.service package, in fact, is to specify a dependent package for programming.


This step will be in manefest. A row is generated in MF:

Import-package:com.unmi.login.service

2) to write implementation of Validator interface implementation class

Because it is a demo, the verification process is hard-coded, such as the implementation class of the Validator interface in Ldapvalidatorbundle Com.unmi.login.service.impl/ldapvalidatorimpl.java code is


The code for the Validator implementation class in several other Bundle is similar.

3 implementation of Bundle Bundleactivator interface

As mentioned earlier, the Bundleactivator implementation of Bundle is used to manage its own lifecycle and interact with the framework, so while in their respective bundleactivator implementations, when starting Bundle, register yourself so that the framework can find the Bundle and stop When Bundle, write yourself off from the frame to release the relevant resources. First, write the Bundleactivator implementations of those service providers, such as the Ldapvalidatorbundle class Com.unmi.login.activator.Activator.java code as follows:


Notice how the code registers its own instance with the Bundlecontext, and how to uninstall it to understand the meaning of the parameters of the Bundlecontext.registerservice () method.

Several other Bundle, such as the Bundleactivator implementation code for Filevalidatorbundle and Dbvalidatorbundle, are similar, please imitate it. But the implementation of Uservalidatorbundle's Activator class is not quite the same, because Uservalidatorbundle is a service definition, and it is also used as a portal to open the program interface, so its Activator implementation The start () method will be arranged to do something in Andover, but also to take out a separate explanation.

4) Compiling the Activator of Uservalidatorbundle


The Activator start () method pops up a login window, lets the user enter a username and password, and then logs in. Stopping the Bundle will shut down the landing window. Note that because other Bundle do not need to find instances of this Bundle in the context, there is no code to register itself with Bundlecontext in Start ().

Notice here that we run the bunble and will select the bunble to run, and the selected bunbles will be executed from the Start method of the subclass that inherits the Bundleactivator class, so we want to add what functionality is implemented when starting a bunble All we have to do is write to the Start method, where the validated form is put into the Start method, and the validation form pops up as soon as it runs.

5 The code that invokes the service

Complete the search for the available services in the framework, such as an instance of the Validator implementation class, and then invoke the validation method of the implementation class. The code is implemented in Loginwindow, in the Loginwindow.java code more, complete code can be consulted after the attachment, here only the key code, that is, the login button clicked after the things


So far, all the code has been written, and it's ready to run in Eclipse.

Four. Run Demo

Before presenting, a brief description of the behavior of the program is made. After running the program will jump out of a user Login window, enter the user name and password in the window, click the login button can call the actual validation implementation class to verify the user, while the window can see the landing is a success, or failure, and the reasons for the failure, Use a small letter next to the login button to indicate how the current is validated. As shown in figure:


The login button next to the letter identifies the current authentication method, which is the Bundle in the framework that provides the authentication service. N represents unknown, L represents Ldapvalidatorbundle,d on behalf of Dbvalidatorbundle,f Filevalidatorbundle.

1) Establish the running configuration

Eclipse's menu Run->run Configurations,osgi the Framework on the right, new one uservalidator run configuration, on the right bundles select the four Bundle we created, can be set separately Their Start level and auto-start, for example, where the Filevalidatorbundle Auto-start is set to false. In the lower right corner of the page, you can click Run directly.


After startup, the OSGi console appears, printing information about Ldapvalidatorbundle and Dbvalidatorbundle launches, and displays the landing window.




Now you can perform some operations on the Osgi> console to observe the running state of the program, such as entering the SS command first, seeing that the Bundle we need is loaded, and filevalidatorbundle is resolved rather than ACTIVE, That's because when we set up the run configuration, we set the Bundle Auto-start to False.

The basic operations under the Osgi> console are

SS shows all loaded Bundle, we can execute this instruction from time to to see what Bundle in
Stop <id> Stop Specifies the Bundle of the ID that triggers the Stop () method of the Activator implementation of the Bundle
Start <id> start the Bundle of the specified ID, triggering the Start () method of the Activator implementation of the Bundle
Install <url> Install Bundle, specify the jar file URL for Bundle
Uninstall <id> Uninstall the Bundle of the specified ID, and you must install it again before you can use

There are a lot of commands, not detailed here, at the osgi> prompt to randomly lose the instructions it does not know (such as DD),osgi> will be prompted to help.

2 Multi-Demo

(1) When the direct landing situation

Osgi> under the console to see what Bundle and state, and then enter the login window unmi/1234, landing to:

The login button shows L, and the console displays the "Log on using Ldap" description is Ldapvalidatorbundle verified.

(2) Stop ldapvalidatorbundle and see
Execute stop 1 under the Osgi> console and click the login button on the login window to see:


D is displayed next to the login button, and the "Log on using Data" description in the console is Dbvalidatorbundle verified.

(3) The Dbvalidatorbundle also stopped

Execute Stop 3 under the Osgi> console and click the login button on the login window to see:


Found that there is no active Validator to achieve Bundle, so can not log in.

(4) Set Filevalidatorbundle up

To execute start 4 under the Osgi> console, add a 5 to the password and click the login button on the login window to see:


No problem, you can use just enable Filevalidatorbundle to login verification.

You can also be the sleeping ldapvalidatorbundle or dbvalidatorbundle start up, or simply put a Bundle uninstall off, with SS can not see, to use words must install back.

Five. Publish an OSGI based system

All of the above are based on OSGI programs that run in the eclipse environment, so how do we run away from eclipse? We also do not see the so-called Bundle is a jar like a file, write a good program, the last thing is released.

1) Export Bundle project as JAR package

Eclipse menu File->exports, pop-up window select deployable Plug-ins and Fragments



Select the four Bundle, set the directory for the output file to E:workspaceosgidemo, and then click the Finish button



Upon completion, in the E:workspaceosgidemo folder, a plugins directory is generated, which is the few Bundle that have just been exported.


Dbvalidatorbundle_1.0.0.201003301134.jar
Filevalidatorbundle_1.0.0.201003301134.jar
Ldapvalidatorbundle_1.0.0.201003301134.jar
Uservalidatorbundle_1.0.0.201003301134.jar

2) into the required support package

Copy Org.eclipse.osgi_3.5.2.r35x_v20100126.jar to E:workspaceosgidemo directory from Eclipse's plugins directory.

3) Configure Config.ini

First you create the subdirectory configuration in the E:workspaceosgidemo directory, and then create the file Config.ini in it (note that the Osgi.bundles value is changed by the name of the bunble that we actually packed out). :

Osgi.noshutdown=true

#避免Unable to acquire application service. Ensure that the Org.eclipse.core.runtime error
Eclipse.ignoreapp=true

#因为使用了 Swing, no this property is reported Java.lang.noclassdeffounderror:javax/swing/jframe
org.osgi.framework.bootdelegation=*

#这里有意没有加载 filevalidatorbundle, install
osgi.bundles=plugins/dbvalidatorbundle_1.0.0.201003301134 later. Jar@start,
             plugins/ Ldapvalidatorbundle_1.0.0.201003301134.jar@start,
              Plugins/uservalidatorbundle_1.0.0.201003301134.jar@start

Osgi.bundles.defaultstartlevel=4


4 to establish a batch Run.bat

Create a batch file in E:workspaceosgidemo Run.bat, the content is (note here also is that your ORG.ECLIPSE.OSGI version is different from the following version, you need to change to your actual version):

@echo off
Java-jar org.eclipse.osgi_ The file directory structure in the 3.5.2.r35x_v20100126.jar-console

E:workspaceosgidemo is as follows:

E:workspaceosgidemo
│    Org.eclipse.osgi_3.5.2.r35x_v20100126.jar
│   run.bat

├─configuration
│        config.ini

└─plugins
            Dbvalidatorbundle_1.0.0.201003301134.jar
             Filevalidatorbundle_1.0.0.201003301134.jar
             Ldapvalidatorbundle_1.0.0.201003301134.jar
            Uservalidatorbundle_1.0.0.201003301134.jar

5) Running Run.bat

Double-click the Run.bat, the osgi> console and a landing window appear, but this is in a DOS window. You can not let the program appear in the DOS window, Org.eclipse.osgi_3.5.2.r35x_v20100126.jar is an executable jar, and then hit it normally will only display a landing window. You can also download a Equinox program Launcher to start the OSGI program. But we need to osgi> the console here, so we want to keep the DOS window, as shown below:


We did not load filevalidatorbundle when we first started, we executed the following command

Osgi>install reference:file:plugins/filevalidatorbundle_1.0.0.201003301134 #加载 FileValidatorBundle
Osgi>stop 1 2 #停掉 dbvalidatorbundle and Ldapvalidatorbundle
Osgi>start 4 #启动 Filevalidatorbundle

Then enter the username and password to try, from the console output and the landing button next to the F flag, is really using the newly installed Filevalidatorbundle.

With install <url> you can always install any Bundle jar packs anywhere. The

executes stop 1 2 to stop Dbvalidatorbundle and Ldapvalidatorbundle, and it is not unloaded from the JVM and can be seen in the resolved state with the SS command. At this point you can also try to remove those two files from the E:workspaceosgidemoplugins Dbvalidatorbundle_1.0.0.201003301134.jar and Ldapvalidatorbundle_ 1.0.0.201003301134.jar, the system will be prompted to use cannot delete. At this point, the uninstall command

Osgi>uninstall 1 2

To delete the two texts is successful, indicating that the JVM really unloaded the two packets. It can be seen how convenient it is for OSGI to install and uninstall Bundle dynamically through install and uninstall.

For example, to add a functional module to the original system, it can be used as long as it is dynamically installed into the frame. The original system has a Bug in the module, modify well, uninstall and then reload the OK, basically the system similar to this change does not need to stop service, restart the application.

or the previous sentence, the actual application is generally not the osgi> console, you want to do in the osgi> console to do those moving action. Because Equinox is a pure Java implementation, commands that can be executed under the Osgi> console can be controlled in your program, or in a way that is more user-friendly to the backend staff. The example above in

simply leads you to a basic impression of OSGi, and then truly comprehends the advanced ideas of OSGi so that you can be comfortable with your actual work. Of course, we can not in the project because of technology and technology, if it is a small project I would like to do not have to rub in the OSGI. Projects with a certain scale can consider using OSGI framework to stand at a very high level to do a micro-core, and to reasonable component planning, in order to flexible dynamic components of the plug-in.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.