Equinox framework for web development in osgi Environment

Source: Internet
Author: User

Level: elementary

Liu Qing (qlcdl@cn.ibm.com), software engineer, IBM China Software Development Center
Zhu Zhihui (zhuzhih@cn.ibm.com), software engineer, IBM China Software Development Center

July 23, 2009

Osgi, as a new de facto industrial standard, is booming in various fields, while web development technology has long been widely used as a mainstream technology in the software industry. How traditional Web developers combine web development and osgi technology to benefit from this is our focus. Through this tutorial, you can fully master the two methods of web development in the osgi environment using the Equinox framework.

Introduction to osgi and equinox frameworks

The essence of osgi is to switch Java object-oriented development to component-oriented and service-oriented development. The osgi framework provides a complete set of mechanisms for managing and controlling the lifecycle of components, services, and interactions between components and services within their lifecycles. Osgi provides common services such as logservice, configadmin, eventadmin, and HTTP Services).

The osgi framework was not known to developers until 2006. The initial osgi standards were mainly applied to j2se and j2se. The equinox project is the implementation of the osgi framework provided by the Eclipse open-source organization. The addition of equinox expands the application field of osgi standards. equinox not only provides bundle implementation for most osgi standard services, but also utilizes some of the characteristics of the eclipse environment, it provides many function extension services.

Equinox implements osgi applications in the j2se and j2se fields, and also promotes osgi applications in the J2EE field. Equinox provides a set of basic bundle so that web application projects using JSP, Servlet, struts, and other J2EE technologies can run in the equinoxosgi environment. Similarly, equinox can embed equinox osgi applications into existing Web servers (such as Tomcat and jetty) and application servers (such as WebSphere and WebLogic) through a bundle. This is the two ways we use equinox for web development.

This article focuses on the implementation of equinox In the J2EE field. We will introduce in detail how to use the Equinox framework to better combine traditional web development and osgi technologies.

 



Back to Top

Install and configure the development environment

To do well, you must first sharpen your tools. First, let's build our development environment. To reduce problems caused by Environment inconsistency, we recommend that you use the same software version as this article:

Download and install JDK (Sun JDK 5 is used in this article );

Download and decompress eclipse (the version used in this article is eclipse Ganymede J2EE Sr2 );

Download and install Tomcat (Tomcat 6.0.18 is used in this article );

The development environment is quite simple. After the above three steps, you only need to set up

Configure tomcat.

Figure 1. Configure tocmat

Next let's take a look at the first method of web development using the Equinox framework.

 



Back to Top

Place the HTTP server in the Equinox framework to develop Web Applications

This method is completely osgi. the HTTP server (jetty in this example) runs as a bundle throughout the Equinox framework. In this development mode, we need to first create an Eclipse plug-in Project: Open the Eclipse plug-in development view and create a new plug-in project.

Figure 2. Create a plug-in project in the plug-in Development View

We named the new project com. sample. Web. In the project property settings, we set the project running target to equinox.

Figure 3. Set Properties for a new project

In the subsequent attribute settings, do not create activator. The specific cause is described in the subsequent sections of the article.

Figure 4. Do not set activator for the plug-in

Follow the default settings in the remaining steps to create a project. This plug-in project is created.

Then we create webroot under the root directory of the project. The IMG and JSP folders are used to place common resource files (sample files) and JSP files respectively. Create a simple servlet in the Java source file directory: loginservlet. Java now, the project structure is as follows:

Figure 5. Project Structure

At this time, you will see a compilation error in the project. Don't worry. Let's take a look at the content in our servlet:

Listing 1: loginservlet. Java

PackageCom. sample. Web. servlet;ImportJava. Io. ioexception;ImportJava. Io. printwriter;ImportJavax. servlet. servletexception;ImportJavax. servlet. http. httpservlet;ImportJavax. servlet. http. httpservletrequest;ImportJavax. servlet. http. httpservletresponse ;/***@ AuthorLevin *@ Time2009-5-29 */Public ClassLoginservletExtendsHttpservlet {Public VoidDoget (httpservletrequest request, httpservletresponse response)ThrowsIoexception, servletexception {// web development scenario 1: Use the request object to obtain the client request data string username = request. getparameter ("username"); // web development scenario 2: Operation session request. getsession (). setattribute ("userfroma", username); // web development scenario 3: Use response to return response. setcharacterencoding ("GBK"); response. setcontenttype ("text/html"); printwriter out = response. getwriter (); out. println ("<HTML>"); out. println ("<body>"); out. println ("Public VoidDopost (httpservletrequest request, httpservletresponse response)ThrowsIoexception, servletexception {doget (request, response );}}

 

Although this example is simple, it covers common scenarios in traditional web development methods, such as request, response, and session. A compilation error occurs in the project because javax is used. the class in the servlet package, so we click the error message, eclipse will automatically help us modify the manifest of the project. mf file, add the required package to import-package. The following is the content of the index. jsp file in our project.

Listing 2: index. jsp file

<HTML> <HEAD> <TITLE>My App Home</TITLE> <META http-equiv=Content-Type content="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></HTML>

 

So far, we have prepared various preparations for the project, such as creating folders, creating JSP files, and Servlet files. However, at this time, the project still cannot parse the HTTP request and forward it to the corresponding resource file or JSP and servlet. To make the project have this capability, you can register the servlet with httpservice and use the extension points. In this example, a simple and widely used extension point method is used.

To configure the extension point, you can manually create a plugin under the root directory of the project. XML or open manifest. mf file, find the extension point setting area on its overview tab, and click extensions to configure the extension point.

Figure 6. Configure extension points

Plugin. the content of the XML file is shown in listing 3. We define two extension points, one for processing requests to the resource file sample file, etc, another request is used to process JSP and Servlet requests.

Listing 3: plugin. xml file

<?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></plugin>

 

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

Listing 4: manifest. MF File

Manifest-Version: 1.0Bundle-ManifestVersion: 2Bundle-Name: Web Plug-inBundle-SymbolicName: com.sample.web;singleton:=trueBundle-Version: 1.0.0Bundle-Vendor: SAMPLEBundle-RequiredExecutionEnvironment: J2SE-1.5Import-Package: javax.servlet,javax.servlet.httpRequire-Bundle: org.eclipse.equinox.jsp.jasper.registry, org.eclipse.equinox.http.registry

 

OK. Now all the settings and required files are ready. It seems that it is time to run. Don't worry. There is another configuration. Open the project running configuration window, select all target platforms first, and then click Add required bundles. In this way, eclipse will not start all bundle at runtime, but will only selectively start the bundle we configured in the configuration file.

Figure 7. Running Configuration window

When we enter the SS Command in the eclipse console, we can see that our com. sample. when the Web bundle status is active, we can start a browser to access our web application.

Figure 8. osgi Console

Readers can try to enter the following URL in your browser to see what is going on, and consider their causal relationship based on the configuration of the extension points in plugin. xml.

Http: // localhost/images/1.jpg

Http: // localhost/JSP/index. jsp

Http: // localhost/servlet/myfirstservlet? Username = Levin

If everything works properly, access JSP and you will see the running interface similar to figure 9.

Figure 9. jsp runtime Diagram



Back to Top

Place equinox in a servlet container to develop Web Applications

This development method is different from the previous one. This development method is to make the Equinox framework and the bundle of the project into a war file and deploy it into the servlet container. It is the same as the previous normal Web development, run as a common web application. In this way, a technology called servlet bridge is used. The HTTP request of the Web application is forwarded to the Web application by the servlet container, the equinox framework runs in the Web application, Processes requests to the application in a unified manner, and forwards the requests to the corresponding bundle.

Generally, you need to download the bridge. War file from the Equinox website, which is of the April 2007 version. We didn't let everyone choose to create activator when creating the project. The reason is that some classes referenced by activator do not exist in Bridge. War of this version. In addition: in our sample project, JSP is required, so several additional bundle is required. For your convenience, I made another bridge. War, which contains the required bundle. You can download some of the source code directly.

After the war file is downloaded, import it to our eclipse workspace.

Next, we will export the created sample project as a bundle: Open the build of our sample project. the proerties file contains the webroot folder, its subfolders, and plugin. select all XML files.

Figure 10. Modify build attributes

Right-click our sample project and export it as a deployable plug-ins and fragments

Figure 11. Export Project as bundle

Others can be set by default. At this time, you will see that eclipse will generate plugins/COM. sample. the web_1.0.0.jar file is placed in the plugins folder of the bridge project you just imported.

Figure 12. Workspace

In this way, we do not need to run our sample project, but run our bridge project, select the bridge project, right-click and choose run as> run on server to run the bridge project in the tomcat configuration. Since we have not set to enable automatic startup of our sample project by default, we need to start the bundle of our sample project here.

Figure 13. Start sample bundle

After the startup is complete, you can visit the following URL to see what is going on. Consider what is the difference between the same bundle and URL in this development mode and the first one?

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

Http: // localhost: 8080/bridge/JSP/index. jsp

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

If everything works properly, you will see a similar running effect when accessing the servlet. The default port of tocmat is 8080, and the default port of jetty in Figure 9 is 80. In addition, because all bundle is deployed in the bridge project, the access URL is changed to the base path of http: // localhost: 8080/bridge.

Figure 14. servlet Runtime

In the actual development process, if we need to export our project as a jar file to the bridge every time, the development efficiency is very low. The above steps are only applicable when the project is actually deployed on the server after the project development is complete. In the development process, we can take the following measures to simplify development:

Open the webcontent/Web-INF/Eclipse/configuration/config. ini file of the bridge project and add references to our sample project in the configuration of osgi. Bundles. For example:

Reference/: File/: D:/eclipse-jee-ganymede-SR2-win32/workspace/COM. sample. Web @ start

In this case, if we select the bridge project, right-click and choose run as> run on server to run the bridge project to the Tomcat we configured, the bridge will be based on config. the configuration in ini automatically starts our sample project as a bundle.

 



Back to Top

Data sharing of sessions between bundle

After learning, we can find that different Web applications can be deployed in an osgi environment and provide external web services, can the bundle of these web applications share data in the session? If we can share session data, what will it bring to our future development work? These are all very interesting topics. Interested readers can try some experiments after reading this article.

 



Back to Top

Integration with other J2EE frameworks

We know that many J2EE frameworks, such as struts, spring MVC, ECHO, and DWR, have similar running bases and are based on servlets. By configuring the servlet URL pattern, the HTTP request is forwarded to the servlet of the framework and then distributed. In our web project example just now, the support for common resource files, JSP, and Servlet makes it possible for us to integrate other J2EE frameworks in the project. If you are interested, you can try it yourself. This article will not go into details.

 



Back to Top

Conclusion

This article briefly introduces the Web Development Technology in the osgi environment. The combination of osgi and web is worth studying as a new development method. I believe that the example project in this article can help you quickly master this new development method and lay a good foundation for future study.

 



Back to Top

Installation requirements

All examples in this article are tested in Windows XP SP3. You need a machine that can run Windows XP smoothly. In addition, you need some tools to use the code in this tutorial. All these tools can be downloaded for free:

Jdk1.5 or later

Eclipse 3.4 or later

Tomcat 6.0 or later

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.