Turn: Problems in osgi practice

Source: Internet
Author: User

After having a preliminary understanding of osgi, we are ready to write some code to run and try our best,
First, I downloaded a copy of bluedavy's osgi practice.
There is code that can be run directly in it. Double-click Run. BAT to run normally, which is cool!
Start to practice the user login authentication module in osgi practice, and repeat the code in a row. The first change is that the project references cannot be added to the build path, but changed to manifest. add import-package to MF
During the course of study, I still encountered many problems and recorded them to help me avoid detours due to the same problems as me.
I used eclipse3.4 jdk1.6.

1.The Org. Eclipse. Equinox. servlet. API package cannot be found during import-package.
If eclipse3.4 does not exist, you can directly import javax. servlet_2.4.0.v20080601_04.jar.
If javax. servlet is not added, installed uservalidatorwebbundle_1.0.0 will appear.
The following exception is thrown during forced start.
Org. osgi. Framework. bundleexception:
The bundle cocould not be resolved. Reason: Missing constraint:
Require-Bundle: javax. servlet; bundle-version = "2.4.0"

2.If equinox osgi declarative service is used, the eclipse-equinox-SDK-3.4.2.zip needs to be downloaded.
Because the declarative service implementation is not included in the default eclipse software package, you need to obtain it from the eclipse website separately. Copy the plugins and features in the downloaded package to eclipse and restart it.
Org. Eclipse. Equinox. ds_1.0.0.v20080427-0830.jar is the bundle used at runtime
Org. Eclipse. Equinox. DS uses org. Eclipse. Equinox. util_1.0.0.v20080414.jar.
Add and start in config. ini

3.Everything seems to have started properly, and the following exceptions still exist in the log:
Java. Lang. illegalstateexception:
Unable to acquire Application Service. Ensure that
Org. Eclipse. Core. runtime bundle is resolved and started (see
Config. INI ).
You have to start the bundle org. Eclipse. osgi. util_3.1.300.v20080303.jar.

4.If the http: // localhost/demo/page/login.htm page cannot be accessed. eclipse. equinox. the http_1.0.200.v20080421-2006.jar is not started, how can HTM access http: // localhost/demo/login not accessible possibly Org. eclipse. equinox. HTTP. servlet_1.0.100.v20080427-0830.jar not started

Summarize the bundle to be started in the user login verification module.
Id state bundle
0 active org. Eclipse. osgi_3.4.2.R34x_v20080826-1230
1 active configfilevalidatorbundle_1.0.0
2 active dbvalidatorbundle_1.0.0
4 active uservalidatorbundle_1.0.0
5 active ldapvalidatorbundle_1.0.0
9 Active uservalidatorwebbundle_1.0.0
10 active org. Eclipse. Equinox. util_1.0.0.v20080414
11. Active org. Eclipse. Equinox. ds_1.0.0.v20080427-0830
12 active javax. servlet_2.4.0.v2008060.004
13 active org. Eclipse. osgi. services_3.1.200.v20071203
14 active org. Eclipse. Equinox. http. servlet_1.0.100.v20080427-0830
16 active org. Eclipse. Equinox. http_1.0.200.v20080421-2006
17 active org. Eclipse. osgi. util_3.1.300.v20080303

When a bundle is missing, you can download it online and import it using plug-
INS and fragments can be referenced by importing bundle, so don't think of it as something advanced. Don't forget to use "add required
Bundles ", it helps us to add the bundles that will be used, which will help us a lot.

In the first example (classic in the source code), register the service by using code registration, that is, the activator in each bundle that implements the service interface registers its own service to the name of the specified service, for example

Java code
  1. Package org. riawork. Demo. User. validator;
  2. /*
  3. * Riawork.org
  4. *
  5. * Osgi opendoc demo
  6. */
  7. Import org. osgi. Framework. bundleactivator;
  8. Import org. osgi. Framework. bundlecontext;
  9. Import org. osgi. Framework. serviceregistration;
  10. Import org. riawork. Demo. Service. User. validator;
  11. Import org. riawork. Demo. Service. User. impl. configfilevalidatorimpl;
  12. /**
  13. * Desc: configfilebundle activator, which registers services in a traditional way.
  14. *
  15. * @ Author Jerry
  16. */
  17. Public class activator implements bundleactivator {
  18. // ---------------------------------------------- Instance variables
  19. Private serviceregistration servicereg = NULL;
  20. // ---------------------------------------------- Public Method
  21. /* (Non-javadoc)
  22. * @ See org. osgi. Framework. bundleactivator # Start (Org. osgi. Framework. bundlecontext)
  23. */
  24. // The bunble that implements validator will register its new implementation in activator to validator. class. in the getname () Name, validator. class. there are multiple services in the name of getname ().
  25. Public void start (bundlecontext context) throws exception {
  26. Servicereg = context. registerservice (validator. Class. getname (), new configfilevalidatorimpl (), null );
  27. }
  28. /* (Non-javadoc)
  29. * @ See org. osgi. Framework. bundleactivator # Stop (Org. osgi. Framework. bundlecontext)
  30. */
  31. Public void stop (bundlecontext context) throws exception {
  32. If (servicereg! = NULL)
  33. Servicereg. unregister ();
  34. }
  35. }
Package Org. riawork. demo. user. validator;/** riawork.org ** osgi opendoc demo */import Org. osgi. framework. bundleactivator; import Org. osgi. framework. bundlecontext; import Org. osgi. framework. serviceregistration; import Org. riawork. demo. service. user. validator; import Org. riawork. demo. service. user. impl. configfilevalidatorimpl;/*** Desc: configfilebundle activator, which registers services in the traditional way ** @ author Jerry */publi C Class activator implements bundleactivator {// specify instance variablesprivate serviceregistration servicereg = NULL; // specify public method/* (non-javadoc) * @ see Org. osgi. framework. bundleactivator # Start (Org. osgi. framework. bundlecontext) * // The bunble that implements validator will register its new implementation in the activator to validator. class. in the getname () Name, validator. Class. the service named getname () has multiple public void start (bundlecontext context) throws exception {servicereg = context. registerservice (validator. class. getname (), new configfilevalidatorimpl (), null);}/* (non-javadoc) * @ see Org. osgi. framework. bundleactivator # Stop (Org. osgi. framework. bundlecontext) */Public void stop (bundlecontext context) throws exception {If (servicereg! = NULL) servicereg. unregister ();}}

In the second example (Ds in the source code), the service in a bundle is published in the configuration file.
Configfilevalidatorbundle, dbvalidatorbundle, and ldapvalidatorbundle
In the bundle of the uservalidatorbundle interface, no activator is used to register their implementation services. Instead, the activator is added to the manifest. MF configuration file.
Service-component:
OSGI-INF/component. XML to publish the service that declares the OSGI-INF/component. xml configuration file under the project directory.


During packaging, it cannot be done according to the document. During packaging, the jar packages of org. Eclipse. osgi and run. Bat are in the same directory, and then created under them.
Configuration directory (which stores the config. ini file) and bundles directory (which stores the bundle that you package and the dependencies on which they run)
Bundle). Note that copying the config. ini content in the document to the config. ini file cannot run. It can contain Chinese characters.

Here I can run the following:

XML Code
  1. Osgi. noshutdown = true
  2. # The bundle running in the current system. You can specify the startup sequence of the bundle here.
  3. # Startlevel Service
  4. # Avoid unable to acquire Application Service. Ensure that the org. Eclipse. Core. runtime error
  5. Eclipse. ignoreapp = true
  6. # Osgi. bundles = reference \: file \: Bundles/configfilevalidatorbundle_1.0.0.jar @ start, reference \: file \: Bundles/dbvalidatorbundle_1.0.0.jar @ start, reference \: file \: Bundles/dependencies @ start, reference \: file \: Bundles/org. eclipse. equinox. http_1.0.301.r35x_v20090728.jar @ start, reference \: file \: Bundles/javax. servlet_2.5.0.v20080601_05.jar @ start, reference \: file \: Bundles/org. eclipse. osgi. services_3.2.0.v20090520-1800.jar @ start, reference \: file \: Bundles/uservalidatorbundle_1.0.0.jar @ start, reference \: file \: Bundles/uservalidatorwebbundle_1.0.0.jar @ start
  7. Osgi. bundles = plugins/configfilevalidatorbundle_1.0.0.jar @ start ,\
  8. Plugins/dbvalidatorbundle_1.0.0.jar @ start ,\
  9. Plugins/ldapvalidatorbundle_1.0.0.jar @ start ,\
  10. Plugins/org. Eclipse. Equinox. http_1.0.301.r35x_v20090728.jar @ start ,\
  11. Plugins/javax. servlet_2.5.0.v20080601_05.jar @ start ,\
  12. Plugins/org. Eclipse. osgi. services_3.2.0.v20090520-1800.jar @ start ,\
  13. Plugins/uservalidatorbundle_1.0.0.jar @ start ,\
  14. Plugins/uservalidatorwebbundle_1.0.0.jar @ start ,\
  15. Plugins/org. Eclipse. Equinox. ds_1.1.1.r35x_v20090806.jar @ start ,\
  16. Plugins/org. Eclipse. Equinox. util_1.0.100.v20090520-1800.jar @ start
  17. Osgi. Bundles. defaultstartlevel = 4
Osgi. noshutdown = true # The bundle running in the current system. You can specify the Starting sequence of the bundle here. It will be described in detail in the # startlevel service chapter # Avoid unable to acquire application service. ensure that the org. eclipse. core. runtime error eclipse. ignoreapp = true # osgi. bundles = reference \: file \: Bundles/configfilevalidatorbundle_1.0.0.jar @ start, reference \: file \: Bundles/dbvalidatorbundle_1.0.0.jar @ start, reference \: file \: Bundles/dependencies @ start, reference \: file \: Bundles/org. eclipse. equinox. http_1.0.301.r35x_v20090728.jar @ start, reference \: file \: Bundles/javax. servlet_2.5.0.v20080601_05.jar @ start, reference \: file \: Bundles/org. eclipse. osgi. services_3.2.0.v20090520-1800.jar @ start, reference \: file \: Bundles/uservalidatorbundle_1.0.0.jar @ start, reference \: file \: Bundles/UserValidatorWebBundle_1.0.0.jar@startosgi.bundles = plugins/start @ start, \ plugins/dbvalidatorbundle_1.0.0.jar @ start, \ plugins/ldapvalidatorbundle_1.0.0.jar @ start, \ plugins/org. eclipse. equinox. http_1.0.301.r35x_v20090728.jar @ start, \ plugins/javax. servlet_2.5.0.v20080601_05.jar @ start, \ plugins/org. eclipse. osgi. services_3.2.0.v20090520-1800.jar @ start, \ plugins/uservalidatorbundle_1.0.0.jar @ start, \ plugins/uservalidatorwebbundle_1.0.0.jar @ start, \ plugins/org. eclipse. equinox. ds_1.1.1.r35x_v20090806.jar @ start, \ plugins/org.eclipse.equinox.util_1.0.100.v20090520-1800.jar@startosgi.bundles.defaultStartLevel = 4

Do not forget to add the DS bundle here or use service-component: OSGI-INF/component. xml configuration does not work, and services cannot be published, because DS bundle scans and discovers corresponding services and adds them to management.

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.