搜索

此内容没有您所选择的语言版本。

2.3. Library Bundle

download PDF

Overview

This section explains how to set up a Maven project for a typical library bundle.
The time-util bundle exemplifies a library bundle, where the main purpose of a library is to make interfaces and classes available to other bundles. Hence, the library should export all of its own packages and associate a version number with the exported packages.

Directory structure

Assuming it is built as a Maven project, the time-util bundle has the following directory structure:
time-util/
  |
  \--src/
     |
     \--main/
     |  |
     |  \--java/
     |     |
     |     \--org/fusesource/example/time/
     |        |
     |        \-TimeUtil.java
     |        |
     |        \-Clock.java
     |   
     \--test/
The Java source code is located under the src/main/java sub-directory. The org.fusesource.example.time package is public and all of its classes and interfaces can be exported from the bundle.
There are no blueprint resources associated with this bundle.

Sample implementation

The time-util bundle is essentially a wrapper around some of the standard time utilities in Java. It provides a Clock class, which returns the local time in a particular time zone when you invoke the Clock.getLocalTime() method. The Clock class is defined as follows:
// Java
package org.fusesource.example.time;

import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;

public class Clock {
    ...    
    public Clock(TimeUtil.TimeZone tz) {
        ...
    }
    
    public String getLocalTime() {
        return jcal.getTime().toString();
    }
}
The TimeUtil class is a factory that is used to create Clock instances for particular time zones. Two time zones are supported: TimeZone.BOSTON and TimeZone.PARIS. The TimeUtil class is defined as follows:
// Java
package org.fusesource.example.time;

public class TimeUtil {
  public enum TimeZone {
    BOSTON,
    PARIS
  }

  public static Clock createClock(TimeZone tz) {
      return new Clock(tz);
  }
}

Import and export rules

The following import and export rules apply to the time-util bundle:
  • Exporting own packages—the org.fusesource.example.time package is public, and must be exported.
  • Importing own packages—none of the bundle's own packages should be imported, which is the usual case for a library bundle.
  • Importing dependent packages—any external package dependencies must be imported. In this particular example, however, there are none (the time-util bundle depends only on classes from the JVM).

Maven bundle plug-in settings

The Maven bundle plug-in is configured to export the library package, org.fusesource.example.time (coded as ${project.groupId}.time). The Export-Package instruction also contains entries to block the export of any packages containing .impl or .internal. In this case, the bundle plug-in instructions are as follows:
<instructions>
  <Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
  <Import-Package>*</Import-Package>
  <Export-Package>
    !${project.groupId}*.impl*,
    !${project.groupId}*.internal*,
    ${project.groupId}.time*;version=${project.version}
  </Export-Package>
</instructions>

Generated MANIFEST.MF file

When you build the bundle using Maven, the Maven bundle plug-in automatically generates the following MANIFEST.MF file:
Manifest-Version: 1.0
Bundle-Name: time-util
Built-By: JBLOGGS
Build-Jdk: 1.5.0_08
Created-By: Apache Maven Bundle Plugin
Bundle-ManifestVersion: 2
Bundle-SymbolicName: org.fusesource.example.time-util
Tool: Bnd-1.15.0
Bnd-LastModified: 1297357076457
Export-Package: org.fusesource.example.time;version="1.0"
Bundle-Version: 1.0.0
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.