7/15

# 2 Writing HK2 Components

The Hundred-Kilobyte Kernel (HK2) is the lightweight and extensible kernel of GlassFish Server. To interact with GlassFish Server, add-on components plug in to this kernel. In the HK2 component model, the functions of an add-on component are declared through a contract-service implementation paradigm. An HK2 contract identifies and describes the building blocks or the extension points of an application. An HK2 service implements an HK2 contract.

The following topics are addressed here:

- HK2 Component Model

- Services in the HK2 Component Model

- HK2 Runtime

- Inversion of Control

- Identifying a Class as an Add-On Component

- Using the Apache Maven Build System to Develop HK2 Components

## HK2 Component Model

The Hundred-Kilobyte Kernel (HK2) provides a module system and component model for building complex software systems. HK2 forms the core of GlassFish Server's architecture.

The module system is responsible for instantiating classes that constitute the application functionality. The HK2 runtime complements the module system by creating objects. It configures such objects by:

- Injecting other objects that are needed by a newly instantiated object

- Injecting configuration information needed for that object

- Making newly created objects available, so that they can then be injected to other objects that need it

## Services in the HK2 Component Model

An HK2 service identifies the building blocks or the extension points of an application. A service is a plain-old Java object (POJO) with the following characteristics:

- The object implements an interface.

- The object is declared in a JAR file with the `META-INF/services` file.

To clearly separate the contract interface and its implementation, the HK2 runtime requires the following information:

- Which interfaces are contracts

- Which implementations of such interfaces are services

Interfaces that define a contract are identified by the `org.jvnet.hk2.annotations.Contract` annotation.

    @Retention(RUNTIME)
    @Target(TYPE)
    public @interface Contract {
    }

Implementations of such contracts should be identified with an `org.jvnet.hk2.annotations.Service` annotation so that the HK2 runtime can recognize them as `@Contract` implementations.

    @Retention(RUNTIME)
    @Target(TYPE)
    public @interface Service {
        ...
    }

For more information, see `Service`.

## HK2 Runtime

Once Services are defined, the HK2 runtime can be used to instantiate or retrieve instances of services. Each service instance has a scope, specified as singleton, per thread, per application, or a custom scope.

### Scopes of Services

You can specify the scope of a service by adding an `org.jvnet.hk2.annotations.Scoped` annotation to the class-level of your `@Service` implementation class. Scopes are also services, so they can be custom defined and added to the HK2 runtime before being used by other services. Each scope is responsible for storing the service instances to which it is tied; therefore, the HK2 runtime does not rely on predefined scopes (although it comes with a few predefined ones).

    @Contract
    public abstract class Scope {
        public abstract ScopeInstance current();
    }

The following code fragment shows how to set the scope for a service to the predefined `Singleton` scope:

    @Service
    public Singleton implements Scope {
        ...
    }

    @Scope(Singleton.class)
    @Service
    public class SingletonService implements RandomContract {
        ...
    }

You can define a new `Scope` implementation and use that scope on your `@Service` implementations. You will see that the HK2 runtime uses the `Scope` instance to store and retrieve service instances tied to that scope.

### Instantiation of Components in HK2

Do not call the `new` method to instantiate components. Instead, retrieve components by using the `Habitat` instance. The simplest way to use the `Habitat` instance is through a `getComponent(Class` T `contract)` call:

    public <T> T getComponent(Class<T> clazz) throws ComponentException;

More APIs are available at `Habitat`.

### HK2 Lifecycle Interfaces

Components can attach behaviors to their construction and destruction events by implementing the `org.jvnet.hk2.component.PostConstruct` interface, the `org.jvnet.hk2.component.PreDestroy` interface, or both. These are interfaces rather than annotations for performance reasons.

The `PostConstruct` interface defines a single method, `postConstruct`, which is called after a component has been initialized and all its dependencies have been injected.

The `PreDestroy` interface defines a single method, `preDestroy`, which is called just before a component is removed from the system.

Example 2-1 Example Implementation of `PostContruct` and `PreDestroy`

    @Service(name="com.example.container.MyContainer")
    public class MyContainer implements Container, PostConstruct, PreDestroy {
        @Inject
        Logger logger;
        ...
        public void postConstruct() {
            logger.info("Starting up.");
        }

        public void preDestroy() {
            logger.info("Shutting down.");
        }
    }

## Inversion of Control

Inversion of control (IoC) refers to a style of software architecture where the behavior of a system is determined by the runtime capabilities of the individual, discrete components that make up the system. This architecture is different from traditional styles of software architecture, where all the components of a system are specified at design-time. With IoC, discrete components respond to high-level events to perform actions. While performing these actions, the components typically rely on other components to provide other actions. In an IoC system, components use injection to gain access to other components.

### Injecting HK2 Components

Services usually rely on other services to perform their tasks. The HK2 runtime identifies the `@Contract` implementations required by a service by using the `org.jvnet.hk2.annotations.Inject` annotation. `Inject` can be placed on fields or setter methods of any service instantiated by the HK2 runtime. The target service is retrieved and injected during the calling service's instantiation by the component manager.

The following example shows how to use `@Inject` at the field level:

    @Inject
    ConfigService config;

The following example shows how to use `@Inject` at the setter level:

    @Inject
    public void set(ConfigService svc) {...}

Injection can further qualify the intended injected service implementation by using a name and scope from which the service should be available:

    @Inject(Scope=Singleton.class, name="deploy")
    AdminCommand deployCommand;

### Instantiation Cascading in HK2

Injection of instances that have not been already instantiated triggers more instantiation. You can see this as a component instantiation cascade where some code requests for a high-level service will, by using the `@Inject` annotation, require more injection and instantiation of lower level services. This cascading feature keeps the implementation as private as possible while relying on interfaces and the separation of contracts and providers.

Example 2-2 Example of Instantiation Cascading

The following example shows how the instantiation of `DeploymentService` as a `Startup` contract implementation will trigger the instantiation of the `ConfigService`.

    @Contract
    public interface Startup {...}

    Iterable<Startup> startups;
    startups = habitat.getComponents(Startup.class);

    @Service
    public class DeploymentService implements Startup {
        @Inject
        ConfigService config;
    }

    @Service
    public Class ConfigService implements ... {...}

## Identifying a Class as an Add-On Component

GlassFish Server discovers add-on components by identifying Java programming language classes that are annotated with the `org.jvnet.hk2.annotation.Service` annotation.

To identify a class as an implementation of an GlassFish Server service, add the `org.jvnet.hk2.annotations.Service` annotation at the class-definition level of your Java programming language class.

    @Service
    public class SamplePlugin implements ConsoleProvider {
    ...
    }

The `@Service` annotation has the following elements. All elements are optional.

`name`

  The name of the service. The default value is an empty string.

`scope`

  The scope to which this service implementation is tied. The default value is `org.glassfish.hk2.scopes.PerLookup.class`.

`factory`

  The factory class for the service implementation, if the service is created by a factory class rather than by calling the default constructor. If this element is specified, the factory component is activated, and `Factory.getObject` is used instead of the default constructor. The default value of the `factory` element is `org.jvnet.hk2.component.Factory.class`.

Example 2-3 Example of the Optional Elements of the `@Service` Annotation

The following example shows how to use the optional elements of the `@Service` annotation:

    @Service (name="MyService",
        scope=com.example.PerRequest.class,
        factory=com.example.MyCustomFactory)
    public class SamplePlugin implements ConsoleProvider {
    ...
    }

## Using the Apache Maven Build System to Develop HK2 Components

If you are using Maven 2 to build HK2 components, invoke the `auto-depends` plug-in for Maven so that the `META-INF/services` files are generated automatically during build time.

Example 2-4 Example of the Maven Plug-In Configuration

    <plugin>
        <groupId>org.glassfish.hk2</groupId>
        <artifactId>hk2-maven-plugin</artifactId>
        <configuration>
            <includes>
                <include>com/example/**</include>
            </includes>
        </configuration>
    </plugin>

Example 2-5 Example of `META-INF/services` File Generation

This example shows how to use `@Contract` and `@Service` and the resulting `META-INF/services` files.

The interfaces and classes in this example are as follows:

    package com.example.wallaby.annotations;
    @Contract
    public interface Startup {...}

    package com.example.wombat;
    @Contract
    public interface RandomContract {...}

    package com.example.wallaby;
    @Service
    public class MyService implements Startup, RandomContract, PropertyChangeListener {
        ...
    }

These interfaces and classes generate this `META-INF/services` file with the `MyService` content:

    com.example.wallaby.annotations.Startup
    com.example.wombat.RandomContract