JavaScript is required to for searching.
Skip Navigation Links
Exit Print View
Oracle GlassFish Server 3.1 Add-On Component Development Guide
search filter icon
search icon

Document Information

Preface

1.   Introduction to the Development Environment for GlassFish Server Add-On Components

2.  Writing HK2 Components

HK2 Component Model

Services in the HK2 Component Model

HK2 Runtime

Scopes of Services

Instantiation of Components in HK2

HK2 Lifecycle Interfaces

Inversion of Control

Injecting HK2 Components

Extraction

Instantiation Cascading in HK2

Identifying a Class as an Add-On Component

Using the Apache Maven Build System to Develop HK2 Components

3.  Extending the Administration Console

4.  Extending the asadmin Utility

5.  Adding Monitoring Capabilities

6.  Adding Configuration Data for a Component

7.  Adding Container Capabilities

8.  Creating a Session Persistence Module

9.  Packaging, Integrating, and Delivering an Add-On Component

A.  Integration Point Reference

Index

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, and extraction to make component variables available to the system.

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;

Extraction

Although all services are automatically placed into a scope for later retrieval, a component may need to extract more than itself. One practical way of doing so is to use a factory service. For simplicity, however, the HK2 runtime extracts all fields or getter methods annotated with the org.jvnet.hk2.annotations.Extract annotation.

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

@Extract
ConfigService config;

The following example shows how to use @Extract at the getter level:

@Extract
public ConfigService getConfigService() {...}

Extraction, like injection, can also use the name and scope annotation fields to further qualify the extracted Contract implementation.

Extracted fields and properties are made available to other service instances by exporting them to the org.jvnet.hk2.component.Habitat instance. Habitat instances can be injected into other components, and the components can then extract and use the data contained in the Habitat instance.

@Inject
protected Habitat habitat;
...
public void doSomething(String name) {
    ...
    ConfigService config = habitat.getComponent(ConfigService.class);
    ...
}

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 = componentMgr.getComponents(Startup.class);
@Service
public class DeploymentService implements Startup {
    @Inject
    ConfigService config;
}
@Service
public Class ConfigService implements ... {...}