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, download
Eclipse-equinox-SDK-3.4.2.zip
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 the 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. http_1.0.200.v20080421-2006.jar is not started, how HTM can access http: // localhost/demo/login
Could not access 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 there is a lack of bundle, you can just go online and import the bundle by importing plug-ins and fragments, so you can reference it, don't forget to use "add required bundles" at runtime. It will help us to add the bundles we will use, 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
- 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 a traditional way.
- *
- * @ Author Jerry
- */
- Public class activator implements bundleactivator {
- // ---------------------------------------------- Instance variables
- Private serviceregistration servicereg = NULL;
- // ---------------------------------------------- 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 activator to validator. class. in the getname () Name, validator. class. there are multiple services in the name of getname ().
- 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 ();
- }
- }
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, that is, in configfilevalidatorbundle, dbvalidatorbundle, ldapvalidatorbundle, the three bundle implementing the uservalidatorbundle interface, do not have activator to register their implementation services, but are in manifest. add service-component: OSGI-INF/component to the MF configuration file. XML to release the OSGI-INF/component declared under the project directory. xml configuration file service.
During packaging, it cannot be done according to the document. During packaging, org. eclipse. osgi jar package and run. bat is in a directory, and the configuration directory (storing config. INI files) and bundles directories (which store the bundle you have packaged and the bundle on which they run dependencies). Note that you can copy the config in the document. INI content to config. INI files cannot run. They can contain Chinese characters.
Here I can run the following:
XML Code
- Osgi. noshutdown = true
- # The bundle running in the current system. You can specify the startup sequence of the bundle here.
- # Startlevel Service
- # 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 @ start
- Osgi. bundles = plugins/configfilevalidatorbundle_1.0.0.jar @ 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 @ start
- 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.