Contained WithinFind More DocumentationFeatured Support Resources | Download this book in PDF (949 KB)
Chapter 6 Adding Container CapabilitiesApplications run on Enterprise Server in containers. Enterprise Server enables you to create containers that extend or replace the existing containers of Enterprise Server. Adding container capabilities enables you to run new types of applications and to deploy new archive types in Enterprise Server. The following topics are addressed here: Creating a Container ImplementationTo implement a container that extends or replaces a service in Enterprise Server, you must create a Java programming language class that includes the following characteristics:
Marking the Class with the @Service AnnotationAdd a com.jvnet.hk2.annotations.Service annotation at the class definition level to identify your class as a service implementation. @Service
public class MyContainer implements Container {
...
}
To avoid potential name collisions with other containers, use the fully qualified class name of your container class in the @Service annotation's name element: package com.example.containers;
...
@Service(name="com.example.containers.MyContainer")
public class MyContainer implements Container {
...
}
Implementing the Container InterfaceThe org.glassfish.api.container.Container interface is the contract that defines a container implementation. Classes that implement Container can extend or replace the functionality in Enterprise Server by allowing applications to be deployed and run within the Enterprise Server runtime. The Container interface consists of two methods, getDeployer and getName. The getDeployer method returns an implementation class of the org.glassfish.api.deployment.Deployer interface capable of managing applications that run within this container. The getName method returns a human-readable name for the container, and is typically used to display messages belonging to the container. The Deployer interface defines the contract for managing a particular application that runs in the container. It consists of the following methods:
The DeploymentContext is the usual context object passed around deployer instances during deployment. Example 6–1 Example Implementation of ContainerThis example shows a Java programming language class that implements the Container interface and is capable of extending the functionality of Enterprise Server. package com.example.containers;
contains
@Service(name="com.example.containers.MyContainer")
public class MyContainer implements Container {
public String getName() {
return "MyContainer";
}
public Class<? extends org.glassfish.api.deployment.Deployer> getDeployer() {
return MyDeployer.class;
}
}
Example 6–2 Example Implementation of Deployerpackage com.example.containers;
@Service
public class MyDeployer {
public MetaData getMetaData() {
return new MetaData(...);
}
public <V> v loadMetaData(Class<V> type, DeploymentContext dc) {
...
}
public boolean prepare(DeploymentContext dc) {
// performs any actions needed to allow the application to run,
// such as generating artifacts
...
}
public MyApplication load(MyContainer container, DeploymentContext dc) {
// creates a new instance of an application
MyApplication myApp = new MyApplication (...);
...
// returns the application instance
return myApp;
}
public void unload(MyApplication myApp, DeploymentContext dc) {
// stops and removes the application
...
}
public void clean (DeploymentContext dc) {
// cleans up any artifacts generated during prepare()
...
}
}
Adding an Archive TypeAn archive type is an abstraction of the archive file format. An archive type can be implemented as a plain JAR file, as a directory layout, or a custom type. By default, Enterprise Server recognizes JAR based and directory based archive types. A new container might require a new archive type. There are two sub-interfaces of the org.glassfish.api.deployment.archive.Archive interface, org.glassfish.api.deployment.archive.ReadableArchive and org.glassfish.api.deployment.archive.WritableArchive. Typically developers of new archive types will provide separate implementations of ReadableArchive and WritableArchive, or a single implementation that implements both ReadableArchive and WritableArchive. Implementations of the ReadableArchive interface provide read access to an archive type. ReadableArchive defines the following methods:
Implementations of the WritableArchive interface provide write access to the archive type. WritableArchive defines the following methods:
Implementing the ArchiveHandler InterfaceAn archive handler is responsible for handling the particular layout of an archive. Java EE defines a set of archives (WAR, JAR, and RAR, for example), and each of these archives has an ArchiveHandler instance associated with the archive type. Each layout should have one handler associated with it. There is no extension point support at this level; the archive handler's responsibility is to give access to the classes and resources packaged in the archive, and it should not contain any container-specific code. The java.lang.ClassLoader returned by the handler is used by all the containers in which the application will be deployed. ArchiveHandler defines the following methods:
Creating Connector ModulesConnector modules are small add-on modules that consist of application “sniffers” that associate application types with containers that can run the application type. Enterprise Server connector modules are separate from the associated add-on module that delivers the container implementation to allow Enterprise Server to dynamically install and configure containers on demand. When a deployment request is received by the Enterprise Server runtime:
Associating File Types with Containers Using the Sniffer InterfaceContainers do not necessarily need to be installed on the local machine for Enterprise Server to recognize the container's application type. Enterprise Server uses a “sniffer” concept to study the artifacts in a deployment request and to choose the associated container that handles the application type that the user is trying to deploy. To create this association, create a Java programming language class that implements the org.glassfish.api.container.Sniffer interface. This implementation can be as simple as looking for a specific file in the application's archive (such as the presence of WEB-INF/web.xml), or as complicated as running an annotation scanner to determine an XML-less archive (such as enterprise bean annotations in a JAR file). A Sniffer implementation must be as small as possible and must not load any of the container's runtime classes. A simple version of a Sniffer implementation uses the handles method to check the existence of a file in the archive that denotes the application type (as WEB-INF/web.xml denotes a web application). Once a Sniffer implementation has detected that it can handle the deployment request artifact, Enterprise Server calls the setUp method. The setUp method is responsible for setting up the container, which can involve one or more of the following actions:
The setUp method returns an array of the com.sun.enterprise.module.Module objects required by the container. The Sniffer interface defines the following methods:
Making Sniffer Implementations Available to the Enterprise ServerPackage Sniffer implementation code into modules and install the modules in the as-install/modules directory. Enterprise Server will automatically discover these modules. If an administrator installs connector modules that containSniffer implementations while Enterprise Server is running, Enterprise Serverwill pick them up at the next deployment request. |