[51cto] in "Hello, osgi
Previous Article in
, We will introduce
The configuration method of the osgi web application development tool Equinox. In this article, we will perform Hello World osgi
Web application development. The application in this exercise is an osgi suite that contains two resources. The first is helloworld.html, which is a static
HTML file; the second is helloworldservlet, which is an httpservlet. Note that the osgi container provides
Httpservice. Each suite that wants to process HTTP requests calls methods on the service to notify the osgi container of the URLs it can process. Set URL
Registered as an osgi suite can be processed in either of the following ways:
51cto editing recommendations:
Osgi introduction and Practice
Program Method: it is preferred to retrieve the service register httpservice from osgi, and then call the method on it to register the request URL as a suite for processing.
Declaration method: Define the request URL that the suite can process in the plugin. xml file.
We will explain these skills step by step, starting with the program registration method.
Program Registration Method
Follow these steps to register a URL as a plug-in.
What you should do first is to participate in a new osgi
Plug-in named com. javaworld. sample. osgi. Web. Programmatic. (For how to create osgi in eclipse
For more information about plug-ins, see section 1 of this series .)
Open manifest. MF of COM. javaworld. sample. osgi. Web. Programmatic.
File and modify it. Import javax. servlet, javax. servlet. HTTP, org. osgi. Service. HTTP
And the org. osgi. util. Tracker package. After the change, your manifest. MF should be similar to list 3.
List 3. manifest. MF file of the program plug-in
Manifest-version: 1.0
Bundle-manifestversion: 2
Bundle-Name: webapp plug-in
Bundle-symbolicname: COM. javaworld. sample. osgi. Web. Programmatic
Bundle-version: 1.0.0
Bundle-Activator: COM. javaworld. sample. osgi. Web. webapp. Activator
Bundle-vendor: javaworld
Bundle-localization: plugin
Import-package: javax. servlet; version = "2.4.0 ",
Javax. servlet. HTTP; version = "2.4.0 ",
Org. osgi. Framework; version = "1.3.0 ",
Org. osgi. Service. HTTP; version = "1.2.0 ",
Org. osgi. util. Tracker; version = "1.3.2"
As you can see, the value in the import-package header defines the list of packages to be imported.
Create a simple helloworld.html file in the root directory of the plug-in, as shown in list 4. This file is used to display the message "hello from"
Helloworld.html ".
List 4. helloworld.html
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = ISO-8859-1">
<Title> helloworld osgi Web </title>
</Head>
<Body>
<H3> hello from helloworld.html </Body>
</Html>
Next, create the helloworldservlet shown in List 5.
List 5. helloworldservlet
Package com. javaworld. sample. osgi. Web. webapp;
Import java. Io. ioexception;
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Public class helloworldservlet extends httpservlet {
Protected void doget (httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
Resp. setcontenttype ("text/html ");
Resp. getwriter (). println ("}
}
The helloworldservlet class extends httpservlet and overwrites its doget () method. New doget ()
The only operation of the method is to write "hello from helloworldservlet" in the output ".
Next, you need to go to com. javaworld. sample. osgi. Web. Programmatic
The same code is executed when the plug-in is started. Activator. Java will act as the activator of the plug-in suite, as shown in list 6.
LIST 6. activator. Java
Import org. osgi. Framework. bundleactivator;
Import org. osgi. Framework. bundlecontext;
Import org. osgi. util. tracker. servicetracker;
Public class activator implements bundleactivator {
Servicetracker httpservicetracker;
Public void start (bundlecontext context) throws exception {
System. Out. println ("Hello world !! ");
Httpservicetracker = new httpservicetracker (context );
Httpservicetracker. open ();
}
Public void stop (bundlecontext context) throws exception {
System. Out. println ("Goodbye World !! ");
Httpservicetracker. Close ();
Httpservicetracker = NULL;
}
}
The activator class extends bundleactivator and implements two methods:
Start (): Call the START () method when the osgi container starts the plug-in. In the START () httpservicetracker class
This is the servicetracker class that you use to track httpservice. Once you have httpservice
Class, you can call its open () method to start tracking httpservice.
Stop (): When the plug-in is disabled, the osgi container calls the stop () method. In the stop () method, you call httpservicetracker
The close () method of the object to terminate the tracking httpservice.
The last step is to create the httpservicetracker class, as shown in list 7.
List 7. httpservicetracker
Import org. osgi. Framework. bundlecontext;
Import org. osgi. Framework. servicereference;
Import org. osgi. Service. http. httpservice;
Import org. osgi. util. tracker. servicetracker;
Public class httpservicetracker extends servicetracker {
Public httpservicetracker (bundlecontext context ){
Super (context, httpservice. Class. getname (), null );
}
Public object addingservice (servicereference reference ){
Httpservice = (httpservice) Context. getservice (reference );
Try {
Httpservice. registerresources ("/helloworld.html", "/helloworld.html", null );
Httpservice. registerservlet ("/helloworld", new helloworldservlet (), null, null );
} Catch (exception e ){
E. printstacktrace ();
}
Return httpservice;
}
Public void removedservice (servicereference reference, Object Service ){
Httpservice = (httpservice) service;
Httpservice. unregister ("/helloworld.html ");
Httpservice. unregister ("/helloworld ");
Super. removedservice (reference, Service );
}
}
Httpservicetracker Introduction
Httpservice is an osgi service that allows Dynamic Registration of suites in the osgi environment and cancellation of registration of the URI of httpservice
Resources and servlets in The namespace -- in other words, map the request URI to a static html file or
Httpservlet. The httpservicetracker class is an object of the servicetracker type, which simplifies
Httpservice tracking. (For more information about osgi servicetracker, see "tracking service" in Section 1 of this series of articles ".)
In list 7, the httpservicetracker class overrides two methods: addingservice () and
Removedservice (). It is necessary to explain the two methods:
Addingservice ()
A callback method that is called once httpservice is available. In this method, call
Httpservice. registerresources ("/helloworld.html", "/helloworld.html ",
Null) to map the helloworld.html file to/helloworld.html. Then, whenever you requesthttp://localhost/helloworld.html
Httpservice will provide you with helloworld.html. Note that you do not need to map helloworld.html
/Helloworld.html URL; the file name does not need to match the address, and you can map it to a file similar to/test.html.
If you want to provide multiple HTML files in your plug-in, you need to create multiple directories. If you want a/html directory, you can call
Httpservice. registerresources ("/html", "/html", null) to register it. Then, if you want to access
The corresponding address of test.htm in the HTML folder ishttp://localhost/html/test.html
. Registerservlet ()
The method is used to map URLs to the httpservlet class. In this simple code
To call registerservlet ("/helloworld", new helloworldservlet (), null, null ),
/Helloworld URL is mapped to the helloworldservlet class. To pass the initialization parameters to your
Httpservlet, you can create a java. util. dictionary object and pass it as a third-party independent variable
Registerservlet ().
Removedservice ()
When you override the addingservice () method in your servicetracker to obtain a service
Removedservice () to cancel the service. In the removedservice () method, you call the unregister () method to cancel registration.
/Helloworld.html and/helloworld Uri. This will notify httpservice
: COM. javaworld. sample. osgi. Web. Programmatic no longer wants to provide request service for the specified URL. If you call
Unregister () method to cancel servlet registration. The destroy () method of the servlet will be called to clear itself.
Now, helloworld osgi web application is ready, and you can
Execute all your suites in the framework. You should be ablehttp://localhost/helloworld.html
Access
Helloworld.html andhttp://localhost/helloworld
Access helloworld
Servlet.
Declare Registration Method
You may have noticed that the request URL is registered as an osgi creation processor through a program, and the corresponding workflow is not small. And if you want to change
Helloworld.html URL (for example, from/helloworld.html to/hello.html), you will have to update
Httpservicetracker. Java, re-compile the code, and deploy it in the osgi container. Next, let's look at the Declaration method, which is a little simpler.
1. Create a new plug-in project, Com. javaworld. sample. osgi. Web. Declarative. Select osgi
The equinox framework serves as the target platform.
2. Edit the manfiest. MF file of COM. javaworld. sample. osgi. Web. declarative and import
Javax. servlet and javax. servlet. HTTP
Org. Eclipse. Equinox. http. Registry is set to the requested suite of the suite. After this modification is completed, your manifest. MF
The file will be similar to list 8.
List 8. manifest. MF file of the declaration method plug-in
Manifest-version: 1.0
Bundle-manifestversion: 2
Bundle-Name: declarative plug-in
Bundle-symbolicname: COM. javaworld. sample. osgi. Web. Declarative; singleton: = true
Bundle-version: 1.0.0
Bundle-vendor: javaworld
Bundle-localization: plugin
Import-package: javax. servlet; version = "2.4.0 ",
Javax. servlet. HTTP; version = "2.4.0"
Require-Bundle: org. Eclipse. Equinox. http. Registry
This require-bundle
The list header contains a list of the symbolic names of a suite. You need to search for the symbolic names after the import and search, and before searching for the suite path. However, for its request suite, only packages marked as exported through the request suite
Is visible.
3. Use helloworld.html and
Copy helloworldservlet. Java to com. javaworld. sample. osgi. Web. Declarative
Kit.
4. Finally, change the plug-in. xml of the COM. javaworld. sample. osgi. Web. Declarative suite.
File to register all requests for processing, as shown in List 9.
Listing 9. plugin. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<? Eclipse version = "3.0"?>
<Plugin>
<Extension-point id = "servlets" name = "httpservice servlets" schema = "schema/servlets. exsd"/>
<Extension-point id = "Resources" name = "httpservice resources" schema = "schema/resources. exsd"/>
<Extension-point id = "httpcontexts" name = "httpservice httpcontexts" schema = "schema/httpcontexts. exsd"/>
<Extension
Id = "helloservlet"
Point = "org. Eclipse. Equinox. http. Registry. servlets">
<Servlet alias = "/Decl/helloworld"
Class = "com. javaworld. sample. osgi. Web. webapp. helloworldservlet">
</Servlet>
</Extension>
<Extension id = "helloresource"
Point = "org. Eclipse. Equinox. http. Registry. Resources">
<Resource alias = "/Decl/helloworld.html"
Base-name = "/helloworld.html"/>
</Extension>
</Plugin>
Note that plugin. XML has two <extension> elements. First, it has the ID attribute and its value is
Helloservlet, indicating that the helloworldservlet class will be used to process/Decl/helloworld requests. By
Set the attribute value to org. Eclipse. Equinox. http. Registry. servlets. You can mark this as a servlet class. Second
<Extension> element with the ID attribute of the specified value helloresource, indicating the user request
/Decl/helloworld.html should return helloworld.html to the user.
Now, the helloworld osgi web application re-created using declarative method is ready, and you can
Execute all your suites in the osgi framework. You can usehttp://localhost/decl/helloworld.html
Access helloworld.html andhttp://localhost/decl/helloworld
Access
Helloworld servlet. In the next article and the last article in this series, we will introduce how to execute osgi containers outside eclipse. Stay tuned!