Using osgi ds to implement configurable Web Applications

Source: Internet
Author: User

Eclipse's plug-in architecture allows us to flexibly define plug-ins and assemble them into pluggable software systems. osgi's declarative services (DS) there is a similar idea with eclipse extension points (see a detailed comparison Article). Naturally, if DS is applied to Web applicationsProgramWe will be able to build SOA Web applications by defining our own "extension points. "Configurable" in the question refers to the product/solution tailored to the customer by providing different packages to the customer based on the user's needs.

Now let's try to use ds to dynamically configure a menu in the Web application interface, and use osgi implementation or equinox. In eclipse, menu items are added by implementing extension points such as actionsets and editexceptions. There are no extension points in osgi. It doesn't matter. We can define them ourselves.

1. Use a bundle to define a Java interface file. Similar to the functions of eclipse extension points, these interfaces can be used as service access points. In this example, the bundle ID is net. bjzhanghao. osgi. Services, and the interface class is imenucontributor. The content is as follows:

Public   Interface Imenucontributor {

Public List < Menuitem > Getitems ();

}

The menuitem used above is a simple data structure defined by myself. It contains two string-type member variables named name and URL and the corresponding getter/setter method, which will not be shown here.

2. Use 0 .. n bundle to implement the interface defined above (currently called contributor) and declare it as a service. In this example, there are two such bundle IDs: net. bjzhanghao. osgi. menu. contributor and net. bjzhanghao. osgi. menu. contributor2, the implementation class is mymenucontributor, the former provides menu1 .. 3. The latter provides menu4 .. 5; The following is the declaration of the service, that is, the OSGI-INF/component in the project. XML file content:

<? XML version = "1.0" encoding = "UTF-8" ?>
< Component Name = "Services" >
< Implementation
Class = "Net. bjzhanghao. osgi. Menu. contributor. mymenucontributor" />  
< Service >
< Provide Interface = "Net. bjzhanghao. osgi. Services. imenucontributor" />
</ Service >
</ Component >

Mymenucontributor implements imenucontributor. You can implement it at will. In this example, it provides three menu items:

Public   Class Mymenucontributor Implements Imenucontributor {

Public List < Menuitem > Getitems (){
List < Menuitem > List =   New Arraylist < Menuitem > ();
List. Add ( New Menuitem ( " Menu1 " , Null ));
List. Add ( New Menuitem ( " Menu2 " , Null ));
List. Add ( New Menuitem ( " Menu3 " , Null ));
Return List;
}

}

3. Now, half of the SOA work is done, that is, the registration of services, and the rest is the other half, consuming these services. First, create a helper class in the Web application bundle to collect these services. The helper class function is equivalent to a registry. In this example, the bundle ID is net. bjzhanghao. osgi. example, helper class is menuhelper; below is the configuration of menuhelper, that is, OSGI-INF/component in the project. the content of the XML file. In this way, osgi registers the service implementing imenucontributor to menuhelper through the addmenucontributor method when starting bundle. Note that the cardinality value must be "0 .. N ", the policy value must be" dynamic ":

<? XML version = "1.0" encoding = "UTF-8" ?>
< Component Name = "Menuhelper" >
< Implementation
Class = "Net. bjzhanghao. osgi. example. menuhelper" />
< Reference Name = "Menuhelper"
Interface = "Net. bjzhanghao. osgi. Services. imenucontributor"
Cardinality = "0. N"
Policy = "Dynamic"
Bind = "Addmenucontributor"
Unbind = "Removemenucontributor"
/>
</ Component >

4. in JSP/servlet, use the helper class above to construct the interface (or business logic ). Because the interface we define is about the menu, the menu of the example application changes according to the bundle configuration (start/stop. The example is implemented in exampleservlet,CodeIt is as follows:

Protected   Void Doget (httpservletrequest req, httpservletresponse resp) Throws Servletexception, ioexception {
Resp. setcontenttype ( " Text/html " );
Resp. getwriter (). Write ( " Hello <br/> " );
Resp. getwriter (). Write ( " <Ul> " );
For (Imenucontributor menucontributor: menuhelper. getinstance (). getmenucontributors ()){
For (Menuitem mitem: menucontributor. getitems ()){
Resp. getwriter (). Write ( " <Li> <a href = \ "" + Mitem. geturl () + " \ " > "   + Mitem. getname () +   " </A> </LI> " );
}
}
Resp. getwriter (). Write ( " </Ul> " );
}

V. Execution method:

1. Import all projects in the code package in eclipse and start all the above projects and their dependent projects (add required bundles). For details, see:

2. Enter the address http: // localhost/exampleservlet in the browser. You can see five menu items. (In this simple example, order is not taken into account, so menu4 .. 5 may appear in the front. You can solve this problem by using a method similar to the position reserved for menu in eclipse ):

3. Return to eclipse and enter SS in the console to view the current bundle. You can see similar content:

4. Enter stop 35 (35 is the bundle Number of the contributor) in the console, and the console will prompt "calling removemenucontributor". Then, go to the browser to refresh the page and check that only menu4is left in the menu .. 5.

Code download

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.