Reference: http://www.ibm.com/developerworks/cn/opensource/os-ecl-osgibdev/
Open Services Gateway Initiative (osgi) defines a standard and service-oriented computing environment for network services, an open, service-oriented, and easy-to-deploy programming model is provided for users. This programming model allows users to bind custom interface specifications to specific services in the osgi runtime environment, in the process of service-oriented enterprise application of component SOA, osgi technology is playing an increasingly important role. This article introduces the concept and architecture of osgi and uses eclipse 3.2 to develop a service application bundle Based on osgi specifications. By studying this article, you can learn how to develop and deploy bundle applications based on osgi specifications.
Osgi specifications
The osgi Alliance was established in 1999 and is a non-profit institution designed to establish an open service specification. The osgi Specification defines a standard, component-oriented computing environment for network services. Its initial goal is to provide a general software running platform for various embedded devices, the middleware platform that shields device operating systems from hardware differences. Through this platform, you can perform component lifecycle management for applications provided by different software vendors (called bundle in osgi, for example, application components can be installed, upgraded, or removed from the running without interrupting devices. application components can dynamically discover and use other libraries or applications. Because osgi technology has the advantages of modular service components and dynamic loading applications, it is becoming more and more attention in the field, such as embedded equipment manufacturing, automotive manufacturing, and enterprise applications. Currently, the latest osgi Service Specification released by the osgi alliance is 4.0. For more information, see references.
Osgi Architecture
The osgi architecture is a plug-in-type software structure, including an osgi framework and a series of plug-ins. In osgi, plug-ins are called bundle. Among them, osgi framework specifications are the core part of osgi specifications, it provides a general, secure, and manageable Java framework that supports the deployment and expansion of bundle service applications. You can use import package and require-bundle to share Java classes. On the osgi service platform, you can develop bundle to provide the required functions. These bundle can be dynamically loaded and detached, you can also remotely download and upgrade the SDK as needed. Osgi architecture Diagram 1:
Figure 1 osgi Architecture
Where:
Execution Environment:
Bundle applications rely on the running Java execution environment, such as J2SE-1.4, CDC-1.0, etc. are available execution environment.
Modules:
The Module layer defines the load policy of the bundle application. The osgi framework is a robust and strictly defined class loading model. In most Java applications, there is usually only one separate classpath, which contains all Java class files and resource files. osgi is based on the Java technology, for each bundle application that implements the bundleactivator interface, generate a separate classloader for it, making the bundle application more modular.
Life cycle:
The lifecycle layer can dynamically install, start, stop, upgrade, and uninstall bundle. Based on the Module layer, this layer provides a set of APIs to control the runtime operations of bundle applications.
Service Registry and services:
The osgi service layer defines a dynamic collaboration model integrated in the lifecycle layer. It is a service model for publishing, dynamic searching, and binding. A service is usually a Java object that implements a specific service interface and is bound to the osgi runtime environment through service registration. Bundle applications can register and publish services, dynamically bind services, and receive event messages when the service registration status changes.
Security:
Osgi security management is based on the Java2 security system and runs through all layers of the osgi platform. It can manage and control bundle applications deployed in the osgi runtime environment in detail.
Status of bundle Lifecycle
In a dynamically scalable osgi environment, the osgi framework manages the installation and update of bundle and the dependencies between bundle and services. A bundle may be in the following six States, as shown in Figure 2:
Figure 2 bundle status chart
Installed: the installation is complete and local resources are loaded successfully.
Resolved: The dependency is satisfied. This status means that the bundle is either ready to run or stopped.
Starting: the bundle is being started. The start () method of bundleactivator has been called but no response has been returned.
Stopping: the bundle is being stopped. The stop () method of bundleactivator has been called but no response has been returned.
Active: the bundle is successfully started and running.
Uninstalled: the bundle is uninstalled and cannot enter another state.
The bundle interface defines the getstate () method to return the status of the bundle.
Osgi standard service
On the osgi platform, the osgi Alliance defines many services. A service is defined by a Java interface. Bundle can implement this interface and register the service to the service registry. You can find the desired service from the registry, you can also respond to changes in the status of a specific service, such as service registration and cancellation. The following describes some main services of osgi Release 4. The osgi framework provides the permission management service, package management service, and initial loading system service. These services are part of the osgi framework and manage the operations of the osgi framework.
Permission Admin Service: permission management indicates whether the bundle permits other bundle codes. The current or other bundle permissions can be operated through this service. Once the permissions are set, they will take effect immediately. Package Admin Service: the bundle can share the Java classes and resources in the package. Updating bundle may require the osgi framework to re-parse the dependencies between bundle, this service provides the shared status information in the osgi service platform.
Start level service: Start level refers to a series of bundle that must be run or initialized before a specific bundle starts. The Start lever service can set the initial start level of the current osgi service framework, and can specify and query the start level of a specific bundle.
Use eclipse to develop bundle applications
The equinox framework is an implementation framework of eclipse Based on osgi Release 4. It implements the core framework of osgi specifications and the implementation of many standard framework services. For more information about the Equinox project, see references. In this article, we use the eclipse 3.2 platform to develop two osgi-based bundle applications. The first bundle application declares, implements, and registers a name query service, used to determine whether the given name is in the defined query list. The second bundle application queries and references the name query service registered by the first bundle application, if the user name is included in the query list, the correct information is returned. Finally, the Equinox osgi framework deployed by the developed bundle application is included, you can enter commands in the osgi control command line to query detailed information about the framework and bundle application. You can obtain the source code of the bundle application in this document from references.
(1) create a plug-in project. In the eclipse 3.2 development environment, choose File> New> project... from the menu bar ..., open the New Project Wizard and you will see the plug-in Project Creation wizard, which creates a plug-in project. The bundle application named example implements and registers a name query service to implement the bundleactivator interface. Select the Equinox framework as the osgi Service Platform for running the bundle application.
Figure 3 plug-in Project Wizard
(2) It usually takes two steps to implement the osgi service. First, define the interface of the provided service and then implement this service interface. In this example, we create a name query service to check whether the given name is valid. First, define the name query interface nameservice. java. The source code of this interface is as follows:
Nameservice Interface source code
package example.service; /** * A simple service interface that defines a name service. * A name service simply verifies the existence of a Name. **/ public interface NameService { /** * Check for the existence of a Name. * @param name the Name to be checked. * @return true if the Name is in the list, * false otherwise. **/ public boolean checkName(String name); }
|
This service interface is simple and contains only one method to be implemented. To separate service interfaces from Service implementations and facilitate other bundle to reference the service, we usually need to put the service interfaces in a separate package. In this example, the nameservice is stored. the Java package of the Java interface is example. service. Next, you need to implement the nameservice interface and register the service. In this example, we use an internal class to implement this interface. below is part of the source code of the bundle application.
Sample bundle source code
public void start(BundleContext context) throws Exception { Properties props = new Properties(); props.put("ClassRoom", "ClassOne"); context.registerService(NameService.class.getName(), new NameImpl(), props); } private static class NameImpl implements NameService { // The set of names contained in the arrays. String[] m_name = { "Marry", "John", "David", "Rachel", "Ross" }; /** * Implements NameService.checkName(). Determines if the passed name is * contained in the Array. * * @param name * the name to be checked. * @return true if the name is in the Array, false otherwise. */ public boolean checkName(String name) { // This is very inefficient for (int i = 0; i < m_name.length; i++) { if (m_name[i].equals(name)) { return true; } } return false; } }
|
In the START () method, use bundlecontext to register a name query service and set relevant properties for the Service for service query. When implementing the name query service, we define a static array to store valid name information.
(3) define the bundle description file manifest. MF. The manifest. MF file of the bundle application example is as follows:
Manifest. MF File Information
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: Example Bundle Bundle-SymbolicName: example Bundle-Version: 1.0.0 Bundle-Activator: example.osgi.Activator Bundle-Localization: plugin Import-Package: org.osgi.framework;version="1.3.0" Export-Package: example.service
|
The bundle-activator attribute specifies the class for implementing the bundleactivator interface, which is used to start and stop the bundle application. The export-package attribute specifies the shared package output by this bundle. This attribute allows other bundle applications to reference our defined service interfaces.
(4) create a bundle application named exampleclient. The application searches for and references the name query service registered by the example bundle application on the osgi platform, then, read the name entered by the user from the standard input to determine whether the entered name is valid. Some source code of the exampleclient application is as follows. You can obtain the complete source code from references.
Exampleclient bundle part of source code
public void start(BundleContext context) throws Exception { ServiceReference[] refs = context.getServiceReferences( NameService.class.getName(), "(ClassRoom=*)"); if (refs != null) { try { System.out.println("Enter a blank line to exit."); BufferedReader in = new BufferedReader(new InputStreamReader( System.in)); String name = ""; // Loop endlessly. while (true) { // Ask the user to enter a name. System.out.print("Enter a Name: "); name = in.readLine(); // If the user entered a blank line, then // exit the loop. if (name.length() == 0) { break; } // First, get a name service and then check // if the name is correct. NameService nameservice = (NameService) context .getService(refs[0]); if (nameservice.checkName(name)) { System.out.println("The Name is Correct."); } else { System.out.println("The Name is Incorrect."); } // Unget the name service. context.ungetService(refs[0]); } } catch (IOException ex) { } } else { System.out.println("Couldn't find any name service..."); } }
|
Deployment and operation of bundle
On the eclipse platform, select file --> export... and export the developed bundle applications example and exampleclient to jar files to deploy them on the osgi service platform. Select the bundle application to be run, right-click it, and choose run as --> equinox framework from the context menu to start the osgi service platform. In the Equinox STARTUP configuration console, you can set the default start level for the bundle application and whether the bundle application needs to be automatically started. In this example, to illustrate how to install and start the bundle application, we only set the example bundle application to Automatic startup, and the exampleclient bundle application needs to be installed and started using commands.
After the osgi equinox framework is started, enter the SS Command in the osgi console to view the information and status of the bundle application installed on the osgi service platform. 4. You can see that there are two bundle in the active status on the osgi service platform. bundle_3.2.0.v20060328 is the system bundle of the osgi framework, while example_1.0.0 is the bundle application for registering the name query service, and 1.0.0 is the version number of the bundle application.
Figure 4 bundle Information Query
Use the install command on the osgi console to install the exampleclient bundle application. Use the SS Command to view the information and status of the installed bundle application. 5:
Figure 5 install bundle
Use the start command in the osgi console to install the exampleclient bundle application. You can enter a name and use the name query service to determine whether the entered name is valid, run the SS Command to view information about the started bundle application and its status. 6:
Figure 6 start bundle
In the osgi console, you can use the Stop command to stop the specified bundle application. The close command is used to stop and exit the osgi console. For more information about the commands on the osgi equinox framework console, see references.
Summary
The osgi service framework provides an open, service-oriented, and easy-to-deploy programming model. osgi technology is playing an increasingly important role in service-oriented enterprise applications of components. Currently, the eclipse 3.2 architecture is implemented with reference to osgi. The equinox framework is an implementation framework of the eclipse organization based on osgi Release 4. It implements the core framework of osgi specifications and many standard framework services. In this article, we developed two bundle applications using the eclipse platform, and declared, implemented, registered, and referenced a simple service in the bundle application. Finally, deploy the bundle application to the Equinox osgi service framework. Through this article, you can learn how to develop and deploy bundle applications based on osgi specifications.
Download
Name |
Size |
Download Method |
Example.zip |
8 KB |
HTTP |