OSGi construction method using Equnix frame bridge approach

Source: Internet
Author: User
Tags ini tomcat
  
OSGi uses the Equnix Framework bridge approach (partly from the IBM Web site):

Http://www.cnblogs.com/lxhomj/archive/2012/04/19/2457911.html

Bridge Way Construction Method

A Install Eclipse,jdk,tomcat. This paper takes eclipse3.7.2,jdk1.7.0,tomcat-7.0.27 as an example

Lab environment: Win7 X64 Home Premium Edition

Two We need to configure our Tomcat in Eclipse preferences

Select runtime environment in the server

Note (If you don't have a server tag, make sure your ecplise has a Web dev plugin installed)


Point add


Click Next


Select a good tomcat directory and click Finish to complete the configuration.

Three Create a new Plug-project

We named the new Project Com.sample.web, and in the project properties setting, we set the project's run target to Equinox


In the next property setting, we do not choose to create the Activator, as shown in the following figure. Other options remain default

To this we have completed the creation of the entire project, after the creation of the project structure diagram


Four Now we add some real code to our project to facilitate testing

New Package Com.sample.web.servlet under SRC

Create a new folder Webroot in your project and create a new IMG and JSP folder under Webroot

And put in the relevant files

Last directory structure as shown in figure


and create a new file in the package Loginservlet.java (code below):

  Package  com.sample.web.servlet;  
 import java.io.ioexception;   import java.io.printwriter;   import javax.servlet.servletexception;   import javax.servlet.http.httpservlet;   import
javax.servlet.http.httpservletrequest;   import javax.servlet.http.httpservletresponse;       /**   * @authorLevin    * @time2009 -5-29   */   public class Loginservlet extends httpservlet{           public void doget (HttpServletRequest request, H Ttpservletresponse response)    throws IOException, servletexception   {   //web Development Scenario One: Use the Request object to get the client request data  string UserName = Request.getparameter ("UserName");   //web Development Scenario II: Operation
session   request.getsession (). SetAttribute ("Userfroma", UserName);   //web Development Scenario Three: using Response to return  response.setcharacterencoding ("GBK");   response.setcontenttype ("text/html");    PrintWriter out = Response.getwriter ();   out.println ("

The project will have compile errors because we are using the classes in the Javax.servlet package, so we click on the error message and Eclipse will automatically help us to modify the project's MANIFEST.MF file and add the required package to the import-package. Below is the content of the index.jsp file in our project

<HTML>  <HEAD>  <title>my App home</title>  <meta http-equiv= Content-typecontent= "text/html; Charset=iso-8859-1 ">  <%  String javaversion = System.getproperty (" java.version ");  %>  </HEAD>  <BODY>  <p>current JRE version: <font color= "#FF0000" ><%= javaversion%></font></p>  <p>  <a href= "/servlet/myfirstservlet?username=usera" >  Click to test seervlet  </a>  </BODY>  

 

the contents of the Plugin.xml file are as follows, where we define two extension points, one to handle requests for resource files such as picture files, and another to handle JSP and Servlet requests.

<?xml version= "1.0" encoding= "UTF-8"?> <?eclipse  version= "3.4"?>  <plugin>  < Extension  point= "org.eclipse.equinox.http.registry.resources" >  <resource  alias= "/images" Base-name= "/webroot/img" >  </resource>  </extension>  <extension  point= " Org.eclipse.equinox.http.registry.servlets ">  <servlet         alias="/servlet/myfirstservlet "         class= "Com.sample.web.servlet.LoginServlet"        load-on-startup= "true" >     </servlet>  < servlet  alias= "/jsp/*.jsp" class= "org.eclipse.equinox.jsp.jasper.registry.jspfactory:/webroot/jsp/" >  </servlet>  </extension>  

Through the configuration of the extension point, we also need to MANIFEST. The MF file is modified to introduce the required bundles and package. The final MANIFEST. MF file is as follows:

bundle-manifestversion
Bundle-name
Bundle-symbolicname
bundle-version
Bundle-vendor
bundle-requiredexecutionenvironment
Import-package
Require-bundle

Five

The last configuration. Open the project's Run Configuration window, first uncheck all target platform, then tick com.sample.web and then click the Add Required Bundles button. This way, Eclipse does not start all bundles at runtime, but only selectively launches the bundles we configure in the configuration file.


Click Run results as shown

Input SS

We can see that our bundle has been activated.

Now that our WEB application based on Equinox has been completed, the next step is to deploy the Equinox and bundle as a war into the servlet container, just like we used to do in normal web development, as a normal Web application. In this way, a technique called the servlet Bridge is used, and the user's HTTP request to the Web application is forwarded by the Servlet container to the Web application, and the Equinox framework runs in the Web application, uniformly processing the application's Request and forward the request to the corresponding bundle.

Six. In general, the first need to download the Bridge.war file to the Equinox site, this file is the April 2007 version. When we created the project earlier, we did not let anyone choose to create activator, because some of the classes referenced by Activator did not exist in this version of Bridge.war. Also: In our sample project, we need to support JSP, so we need a few additional bundles. For the convenience of everyone, I have also made a Bridge.war, which contains the necessary bundles. In SourceCode. In zip

We're going to import it into our Eclipse workspace,

Seven Next, we export the sample project we just created as a bundle: Open the Build.proerties file for our sample project, and select the Webroot folder and its subfolders and plugin.xml that we need.


It is then exported to deployable plug-ins and fragments

Other by default, you will see that Eclipse will generate the Plugins/com.sample.web_1.0.0.jar file in the directory you specified, and place the jar file under the Plugins folder of the bridge project you just imported

Eight Then put the jar into the bridge project just imported plugins under the specific directory structure as shown

Nine In this way, we do not need to run our sample project, but instead run our bridge project, select Bridge Project, right-->run as->run on server to run the bridge project to our configured TOMCA T to go in. Since we are not set up to let our sample project start automatically by default, everyone here needs to start the bundle of our sample project. Start

Ten Start bundle

Enter SS to view bundle IDs. Suppose id=5

Enter start 5; start the bundle

Now you can access our bundles by entering the URL.

Http://localhost:8080/bridge/servlet/myfirstservlet?userName=Levin

http://localhost:8080/bridge/jsp/index.jsp

Http://localhost:8080/bridge/images/1.jpg

PostScript: In the actual development process, if each time we need to export our project into a jar file to put in bridge, the development efficiency is very low. The above steps are only needed when the project has been developed and actually deployed to the server. In our development process, we can take the following approaches to simplify development:

Open the Webcontent/web-inf/eclipse/configuration/config.ini file for the bridge project and add a reference to our sample project in its osgi.bundles configuration, for example:

Reference\:file\:D:/eclipse-jee-ganymede-sr2-win32/workspace/com.sample.web@start

If we select Bridge project and right-->run as->run on server to run the bridge project into our configured Tomcat, bridge will automatically use our SA in accordance with the configuration in Config.ini Mple project also starts as a bundle.

The above content is excerpt from http://www.ibm.com/developerworks/cn/web/0907_osgiweb_liuqing/


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.