Java Reference
In-Depth Information
when developing, but by and large you prepare code for the module layer by adding
packaging metadata to your project's generated JAR files. For example, suppose you
want to share the following class.
Listing 1.2 Basic greeting implementation
package org.foo.hello;
public class Greeting {
final String m_name;
public Greeting(String name) {
m_name = name;
}
public void sayHello() {
System.out.println("Hello, " + m_name + "!");
}
}
During the build process, you compile the source code and put the generated class
file into a JAR file. To use the OSG i module layer, you must add some metadata into
your JAR file's META-INF/MANIFEST.MF file, such as the following:
Bundle-ManifestVersion: 2
Bundle-Name: Greeting API
Bundle-SymbolicName: org.foo.hello
Bundle-Version: 1.0
Export-Package: org.foo.hello;version="1.0"
The first line indicates the OSG i metadata syntax version. Next is the human-readable
name, which isn't strictly necessary. This is followed by the symbolic name and version
bundle identifier. The last line shares packages with other bundles.
In this example, the bulk of the metadata is related to bundle identification. The
important part is the Export-Package statement, because it extends the functionality
of a typical JAR file with the ability for you to explicitly declare which packages con-
tained in the JAR are visible to its users. In this example, only the contents of the
org.foo.hello package are externally visible; if the example included other pack-
ages, they wouldn't be externally visible. This means that when you run your applica-
tion, other modules won't be able to accidentally (or intentionally) depend on
packages your module doesn't explicitly expose.
To use this shared code in another module, you again add metadata. This time,
you use the Import-Package statement to explicitly declare which external packages
are required by the code contained in the client JAR . The following snippet illustrates:
Bundle-ManifestVersion: 2
Bundle-Name: Greeting Client
Bundle-SymbolicName: org.foo.hello.client
Bundle-Version: 1.0
Import-Package: org.foo.hello;version="[1.0,2.0)"
In this case, the last line specifies a dependency on an external package.
 
Search WWH ::




Custom Search