In our previous tutorial installments, we looked at how bundles can be started and stopped, and how they can interact with the framework and each other's lifecycle. But what are bundles really for?
Bundles are modules. They allow us to split apart our monolithic projects into manageable pieces which can be loaded individually into an OSGi runtime. The problem is, whether we like it or not, modules nearly always have dependencies on other modules. In plain old Jar files, there was never a reliable way to specify the dependencies on other Jars (no, the Class-Path entry in the manifest was not a reliable way of doing this). Therefore you never really knew for sure if the code in a Jar would work, or would throw ClassNotFoundException s at runtime.
OSGi fixed this problem very elegantly. But it's better to show you than tell you... so let's hurry up and get to the code. Unfortunately up until now we have been using the default package, but this won't work any more; we will need to start working with proper packages. So lets start off with a very simple JavaBean-style class, which you should copy into the file osgitut/movies/Movie.java :
package osgitut.movies;
public class Movie {
private final String title;
private final String director;
public Movie(String title, String director) {
this.title = title; this.director = director;
}
public String getTitle() { return title; }
public String getDirector() { return director; }
}
Now we will create an interface in the same package. Create the file osgitut/movies/MovieFinder.java and copy in:
package osgitut.movies;
public interface MovieFinder {
Movie[] findAll();
}
Now lets get these two classes into a bundle. Yes, our bundle will be ridiculously small and almost useless, but that's okay for now. As before we need to create a manifest file, so open up MoviesInterface.mf and copy in the following:
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Movies Interface
Bundle-SymbolicName: MoviesInterface
Bundle-Version: 1.0.0
Export-Package: osgitut.movies;version="1.0.0"
There's a new line in here that we haven't seen before: Export-Package . This simply says that the package osgitut.movies is exported from the bundle. This may seem a little odd at first, because in plain old Java Jars, everything is exported. But haven't you ever wanted to put some code in a package which was only visible internally withing your Jar? Sure, you can make some classes private or protected, but then they're not visible to other packages within your Jar. So OSGi effectively has introduced a new code protection level: if a package in your bundle is not listed on the Export-Package header, then it is only accessible within your module.
You'll also notice that we attached a version number to the package. This is important as we will see later. It's not absolutely necessary to supply a version, by the way, but if you don't then OSGi will automatically assign the version "0.0.0" to your package. I think it's good practice to always add a version explicitly.
Now let's build this bundle:
> javac osgitut/movies/Movie.java osgitut/movies/MovieFinder.java
> jar -cfm MoviesInterface.jar MoviesInterface.mf osgitut/movies/*.class
We're not going to go right ahead and install that bundle into the runtime. First we're going to build another bundle that depends on it. We want to create a concrete implementation of the MovieFinder interface, so copy the following into osgitut/movies/impl/BasicMovieFinderImpl.java :
package osgitut.movies.impl;
import osgitut.movies.*;
public class BasicMovieFinderImpl implements MovieFinder {
private static final Movie[] MOVIES = new Movie[] {
new Movie("The Godfather", "Francis Ford Coppola"),
new Movie("Spirited Away", "Hayao Miyazaki")
};
public Movie[] findAll() { return MOVIES; }
}
Now we need a manifest file, so create BasicMovieFinder.mf :
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Basic Movie Finder
Bundle-SymbolicName: BasicMovieFinder
Bundle-Version: 1.0.0
Import-Package: osgitut.movies;version="[1.0.0,2.0.0)"
Notice that we are importing the package osgitut.movies which was exported by the other bundle. We have also this time added a version range on the import. The framework uses the range at runtime to match up the import with an appropriate export. OSGi uses a syntax for version ranges that will be familiar to most mathematicias: the square bracket means "inclusive" and the round bracket means "exclusive". Effectively we have specified the version "1.x".
Again, adding the version constraint on the import wasn't particularly necessary in this case, it's just a good habit to adopt.
Now lets compile and build our second bundle of the day as follows:
> javac -classpath MoviesInterface.jar osgitut/movies/impl/BasicMovieFinderImpl.java
> jar -cfm BasicMovieFinder.jar BasicMovieFinder.mf osgitut/movies/impl/*.class
Finally we're ready to try these bundles out in Equinox. I'm not going to give full instructions this time, as I think you should be getting the hang of it. Firstly install the BasicMovieFinder bundle, and run ss . You will find that the bundle is in INSTALLED state:
id State Bundle
0 ACTIVE org.eclipse.osgi_3.3.0.v20070208
4 INSTALLED BasicMovieFinder_1.0.0
(NB your bundle list may be starting to look a little different from mine, in particular the bundle ID will depend on how many times you installed and uninstalled the HelloWorld bundle from last time. You'll have to mentally translate the bundle IDs that follow).
INSTALLED just means that framework has got the bundle, but has not yet resolved its dependencies. One way to try to force Equinox to resolve our bundle is with the refresh command. So type refresh 4 and then ss and you should see this:
id State Bundle
0 ACTIVE org.eclipse.osgi_3.3.0.v20070208
4 INSTALLED BasicMovieFinder_1.0.0
The bundle still isn't resolved! Of course, we need to install the "interface" bundle which contains the Movie class and the MovieFinder interface. To confirm that this is the problem, type diag 4 to get diagnostic information:
file:BasicMovieFinder.jar [4]
Missing imported package osgitut.movies_[1.0.0,2.0.0).
Yes, that was the problem: we can't import the package osgitut.movies because no bundle is currently exporting it. So now install the MoviesInterface.jar bundle and run ss . The listing will look like this:
id State Bundle
0 ACTIVE org.eclipse.osgi_3.3.0.v20070208
4 INSTALLED BasicMovieFinder_1.0.0
5 INSTALLED MoviesInterface_1.0.0
The final step is to ask Equinox to try again to resolve the BasicMovieFinder bundle, by running refresh 4 . The output from ss will now show:
id State Bundle
0 ACTIVE org.eclipse.osgi_3.3.0.v20070208
4 RESOLVED BasicMovieFinder_1.0.0
5 RESOLVED MoviesInterface_1.0.0
The BasicMovieFinder bundle is now RESOLVED ! This is an essential step, because until the bundle is RESOLVED it cannot be started, and it cannot supply dependencies to any other bundle.
Note that usually it's not necessary to do manual resolution like this. Normally bundles are automatically resolved when they are needed -- for example, notice that the MoviesInterface bundle is now RESOLVED even though we didn't explicitly refresh it.
That's it for now. For more fun things you can do with the Equinox console, take a look at Chris Aniszczyk's excellent article on IBM developerWorks . Stay tuned for the next installment, where we start to delve into OSGi services.
分享到:
相关推荐
"Getting Started with OSGi 3 Dependencies between Bundles.doc"深入探讨了捆绑包间的依赖关系。在OSGi中,依赖管理是关键,因为它允许模块化系统中的组件动态地发现和使用彼此的服务。文档会解释如何声明和解决...
在标题“Getting Started with OSGi Part1”中,指明了这是一个关于OSGi入门的系列文章中的第一部分。描述部分虽然为“NULL”,但可以从给定的内容中提取出文章的重点信息。标签“源码工具”可能意味着在文章的系列...
OSGi(Open Service Gateway Initiative)是一个定义了Java应用程序如何组织和模块化以及如何动态发现、启动、停止、更新这些模块化组件的规范。Equinox是OSGi规范的一个实现,它是由Eclipse基金会开发的。本文将...
OSGi(Open Services Gateway initiative)是一种Java框架,它定义了服务加载和模块化应用的标准方式。OSGi技术广泛应用于企业级应用开发中,尤其是在Eclipse插件开发和Java EE应用服务器中。OSGi规范定义了如何在...
《OSGi初识系列教程——第三部分:模块间的依赖关系》 在OSGi(Open Service Gateway Initiative)框架中,理解并管理模块间的依赖关系是至关重要的。本篇教程将深入探讨这一主题,帮助开发者们更好地掌握OSGi环境...
本资源包括两部分:《深入理解OSGi:Equinox原理、应用与最佳实践》的源代码和equinox-SDK-3.8的源代码。 深入理解OSGi这本书提供了对OSGi,特别是Equinox实现的全面洞察。书中可能涵盖以下几个知识点: 1. **OSGi...
### 深入理解OSGi:Equinox原理、应用与最佳实践 #### OSGi概述 OSGi(Open Service Gateway Initiative)是一种模块化系统和服务组件模型,它为Java平台提供了一种动态部署、管理和更新应用程序和服务的方法。...
In a service oriented architecture, ...components (bundles) can be started and stopped any time. Other components often depend on these services and need to deal with changes in their availability.
《深入理解OSGi:Equinox原理、应用与最佳实践》自从1999年OSGi联盟成立以来,OSGi技术随着Java一起飞速发展,它已经成为一种被广泛认可的软件架构技术和方法,许多世界著名的IT企业都加入到OSGi的阵营之中,OSGi...
在《深入理解OSGi:Equinox原理、应用与最佳实践》这本书中,作者深入探讨了OSGi的核心概念、Equinox的工作原理以及如何在实际项目中应用OSGi。这本书的源码可能是为了辅助读者理解和实践书中所讲解的内容。 **OSGi...
本书《深入理解OSGi:Equinox原理、应用与最佳实践》深入剖析了OSGi技术的原理和应用,着重介绍了基于OSGi R5.0规范的内容,并结合了Equinox框架的实践经验,旨在帮助读者更好地理解和应用OSGi技术。 本书共分为四...
在深入理解OSGi:Equinox原理、应用与最佳实践中,我们可以学习到以下几个关键知识点: 1. **模块化编程**:OSGi的核心是模块化,它将应用程序划分为独立的单元,称为服务或bundle。每个bundle都有自己的类路径,...
《深入理解OSGi:Equinox原理、应用与最佳实践》这本书是关于OSGi技术的一部权威著作,其附赠光盘包含丰富的学习资源,旨在帮助读者深入掌握OSGi的精髓,特别是Equinox实现的细节。OSGi(Open Services Gateway ...
标题中的"SpringDM笔记28-Spring And OSGi:Layers of Integration"表明这是一篇关于Spring框架与OSGi(Open Service Gateway Initiative)集成的详细笔记。OSGi是一种模块化系统,它允许Java应用程序以模块化的方式...
总结来说,“spring-osgi-1.2.1-with-dependencies”是一个集成了Spring与OSGi的完整包,它提供了在OSGi环境中运行Spring应用所需的所有组件和服务。通过理解和掌握这个包,开发者可以更好地利用OSGi的模块化优势,...
spring-osgi-1.2.1-with-dependencies.zip spring-osgi-1.2.1-with-dependencies.zip spring-osgi-1.2.1-with-dependencies.zip
《Eclipse RCP与Spring OSGi:技术详解与最佳实践》由资源的Eclipse专家亲自执笔,并得到了Eclipse官方技术社区的强烈推荐,权威性毋庸置疑!内容全面,系统讲解了利用Eclipse RCP和Spring OSGi开发大规模Java应用的...
《Spring OSGi详解:基于1.2.0-rc1版本》 Spring OSGi是Spring框架与OSGi(开放服务网关倡议)技术的结合,它为开发者提供了在OSGi环境中使用Spring的能力。OSGi是一种Java模块化系统,旨在解决大型软件系统的复杂...
全面解读OSGi规范,深刻揭示OSGi原理,详细讲解OSGi服务,系统地介绍Equinox框架的用法,并通过源代码分析其工作机制,包含大量可操作性极强的解决方案和最佳实践。