Using NETBEANS6 to develop OSGi applications (3)--Integrating knopflerfish! [88250 Original]

Source: Internet
Author: User

Reprint please retain the author information:

Author: 88250

blog:http:/blog.csdn.net/dl88250

MSN & Gmail & qq:dl88250@gmail.com


SummaryLast time, we wrote two bundles, one is the service provider, one is the customer who uses the service, the operation is very good:-)
This time, let's briefly analyze the design of the KF (Knopflerfish) framework and learn the design of the application framework. Finally, the question of controlling the KF framework and letting OSGi serve our applications is discussed today in conjunction with the end of the previous article.

on the design of Knopflerfish frameAt the beginning, we'll look at the design of the KF framework.
MainAfter reading some of the code for the KF framework, Starting with the main program of the KF Framework (Org.knopflerfish.framework.Main) we can see that KF, in addition to implementing the OSGI specification, implemented its own framework (org.knopflerfish.framework.Framework), mainly is to start the class around it: Org.knopflerfish.framework.Main has done a series of foreshadowing: The version of the OSGi specification implemented (my version is OSGi 4.0.6), the storage bundle file location, the operating system version, and so on.
This main class, mainly to do is the command line parameters (startup parameters) processing, because the KF framework can be started with the Xargs file (all parameters to write a file), so the processing is more cumbersome. The Handleargs method below main shows the basic operations of all bundles (start, stop, install, etc.) are called Org.knopflerfish.framework.Framework This class is implemented, this class is the basic implementation of the KF framework, has been very useful encapsulation of the function.

should we use the framework class directly? We should directly use the framework classes that KF implements, but the entire framework is very cumbersome to start. The amount of code from main and its associated utils can be seen. A series of matting is needed before the entire framework is started.
Here, combined with my own project, because time is tight, rewrite these possible time is not allowed, so I chose to rewrite Main, the main class as its own application framework for the basic packaging of KF. The practice is to delegate some methods of the framework class, exposing the methods outside of main.

In short, it is simpler to complete one of your application frameworks based on Knopflerfish:-)

let's do a little bit of practice. Ready toDitto:-)

Start: 1. Create the projectOpen NB6 to create a common Java application Engineering--myosgiframework:



Note that version file, which refers to the versions of OSGi implemented by KF, has a version class in the OSGi framework implementation that is used for versioning management. To create a version file under our src.

2. Rewrite Knopflerfish's main classAdd the basic operations to the bundle to the main class, and achieve the purpose of encapsulating the KF framework. Here are some examples of how to add:
/**
* Get the bundle context used by the system bundle.
* @return System <code>BundleContext</code>
*/
public static Bundlecontext Getbundlecontext () {
return Framework.getsystembundlecontext ();
}

/**
* Start a bundle.
* @param ID ID of bundle to start
*/
public static void Startbundle (long id) {
try {
Framework.startbundle (ID);
catch (Bundleexception ex) {
Logger.getlogger (Main. class. GetName ()). log (Level.severe, NULL, ex);
}
}

/**
* Stop a bundle.
* @param ID ID of bundle to stop
*/
public static void Stopbundle (long id) {
try {
Framework.stopbundle (ID);
catch (Bundleexception ex) {
Logger.getlogger (Main. class. GetName ()). log (Level.severe, NULL, ex);
}
}
Here, the work done is "pure" commissioned.

Write our test class for main:
/*
* @ (#) Maintest.java
*
* This are free software; You can redistribute it and/or modify
* It under the terms of the GNU general public License as published by
* the free Software Foundation; Either version 3 of the License, or
* (at your option) any later version.
*
* This are distributed in the hope that it'll be useful,
* but without any WARRANTY; Without even the implied warranty of
* merchantability or FITNESS for A particular purpose. The
* GNU Library general public License for more details.
*
* You should have received a copy of the GNU general public License
* Along with this program; If not, write to the free Software
* Foundation, Inc., Temple Place-suite, Boston, MA 02111-1307, USA.
*/
Package myosgiframework;

Import Org.knopflerfish.framework.Main;
Import Org.osgi.framework.Bundle;
Import Org.osgi.framework.BundleContext;

/**
* A test case of <code>org.knopflerfish.framework.main</code>
* <p>
* Base on the knopflerfish framework, modify some source in <code>main</code>
* Annotate modifications with <code> @since </code> Mark,
* Mark as <tt> @since 4.0.6</tt>
* </p>
* @author 88250
* @version 1.0.0.0, Feb 15, 2008
*/
public class Maintest {

/**
* Program entry point.
* @param args should be <code>null</code>
*/
public static void Main (string[] args) {
Main.main (args);
Main.startbundle (1);
Displaybundlesstatus ();
Main.stopbundle (1);
Displaybundlesstatus ();
Main.shutdown (0);
}

private static void Displaybundlesstatus () {
Bundlecontext BC = Main.getbundlecontext ();

Bundle[] B = bc.getbundles ();
for (int i = 0; i < b.length;
i + +) {
Bundle Bundle = B[i];
System.out.println ("ID: #" + Bundle.getbundleid () +
"Location:" + bundle.getlocation () +
"Status:" + bundle.getstate ());
}
}
}

3. TestRemember the last time we had that bundle?
For it to start in our framework, write a startup parameter file Secondosgi.xargs:
-istart/home/daniel/work/sources/java/secondosgi/dist/secondosgi.jar

Then, modify the startup parameters:

Test output:

summing up this time, we simply analyzed the start of the KF framework and some of the designs. and modified the KF framework's startup class, as our own one of the underlying application framework encapsulation. Next time, we'll practice OSGi with a specific project (using OSGi as the basis for the implementation of the entire project, supporting the plugin's Thesaurus--stoneagedict).

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.