Skip Navigation Links | |
Exit Print View | |
![]() |
Oracle GlassFish Server 3.1 Add-On Component Development Guide |
1. Introduction to the Development Environment for GlassFish Server Add-On 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
Creating a Container Implementation
Marking the Class With the @Service Annotation
Implementing the Container Interface
Implementing the ArchiveHandler Interface
Associating File Types With Containers by Using the Sniffer Interface
Making Sniffer Implementations Available to the GlassFish Server
8. Creating a Session Persistence Module
9. Packaging, Integrating, and Delivering an Add-On Component
This example shows a custom container and a web client of the container. The example is comprised of the following code:
Code for the container, which is shown in Container Component Code
Code for a web client of the container, which is shown in Web Client Code
Code that defines the configuration data for the container component is shown in Examples of Adding Configuration Data for a Component.
Code for an asadmin subcommand that updates the configuration data in this example is shown in Example 4-7.
The container component code is comprised of the classes and interfaces that are listed in the following table. The table also provides a cross-reference to the listing of each class or interface.
|
Example 7-3 Annotation to Denote a Container's Component
This example shows the code for defining a component of the Greeter container.
package org.glassfish.examples.extension.greeter; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; /** * Simple annotation to denote Greeter's component */ @Retention(java.lang.annotation.RetentionPolicy.RUNTIME) public @interface Greeter { /** * Name to uniquely identify different greeters * * @return a good greeter name */ public String name(); }
Example 7-4 Application Container Class
This example shows the Java language class GreeterAppContainer, which implements the ApplicationContainer interface.
package org.glassfish.examples.extension.greeter; import org.glassfish.api.deployment.ApplicationContainer; import org.glassfish.api.deployment.ApplicationContext; import org.glassfish.api.deployment.archive.ReadableArchive; import java.util.List; import java.util.ArrayList; public class GreeterAppContainer implements ApplicationContainer { final GreeterContainer ctr; final List<Class> componentClasses = new ArrayList<Class>(); public GreeterAppContainer(GreeterContainer ctr) { this.ctr = ctr; } void addComponent(Class componentClass) { componentClasses.add(componentClass); } public Object getDescriptor() { return null; } public boolean start(ApplicationContext startupContext) throws Exception { for (Class componentClass : componentClasses) { try { Object component = componentClass.newInstance(); Greeter greeter = (Greeter) componentClass.getAnnotation(Greeter.class); ctr.habitat.addComponent(greeter.name(), component); } catch(Exception e) { throw new RuntimeException(e); } } return true; } public boolean stop(ApplicationContext stopContext) { for (Class componentClass : componentClasses) { ctr.habitat.removeAllByType(componentClass); } return true; } public boolean suspend() { return false; } public boolean resume() throws Exception { return false; } public ClassLoader getClassLoader() { return null; } }
Example 7-5 Container Class
This example shows the Java language class GreeterContainer, which implements the Container interface.
package org.glassfish.examples.extension.greeter; import org.glassfish.api.container.Container; import org.glassfish.api.deployment.Deployer; import org.jvnet.hk2.annotations.Service; import org.jvnet.hk2.annotations.Inject; import org.jvnet.hk2.component.Habitat; @Service(name="org.glassfish.examples.extension.GreeterContainer") public class GreeterContainer implements Container { @Inject Habitat habitat; public Class<? extends Deployer> getDeployer() { return GreeterDeployer.class; } public String getName() { return "greeter"; } }
Example 7-6 Deployer Class
This example shows the Java language class GreeterDeployer, which implements the Deployer interface.
package org.glassfish.examples.extension.greeter; import org.glassfish.api.deployment.Deployer; import org.glassfish.api.deployment.MetaData; import org.glassfish.api.deployment.DeploymentContext; import org.glassfish.api.deployment.ApplicationContainer; import org.glassfish.api.deployment.archive.ReadableArchive; import org.glassfish.api.container.Container; import org.jvnet.hk2.annotations.Service; import java.util.Enumeration; @Service public class GreeterDeployer implements Deployer<GreeterContainer, GreeterAppContainer> { public MetaData getMetaData() { return null; } public <V> V loadMetaData(Class<V> type, DeploymentContext context) { return null; } public boolean prepare(DeploymentContext context) { return false; } public GreeterAppContainer load( GreeterContainer container, DeploymentContext context) { GreeterAppContainer appCtr = new GreeterAppContainer(container); ClassLoader cl = context.getClassLoader(); ReadableArchive ra = context.getOriginalSource(); Enumeration<String> entries = ra.entries(); while (entries.hasMoreElements()) { String entry = entries.nextElement(); if (entry.endsWith(".class")) { String className = entryToClass(entry); try { Class componentClass = cl.loadClass(className); // ensure it is one of our component if (componentClass.isAnnotationPresent(Greeter.class)) { appCtr.addComponent(componentClass); } } catch(Exception e) { throw new RuntimeException(e); } } } return appCtr; } public void unload(GreeterAppContainer appContainer, DeploymentContext context) { } public void clean(DeploymentContext context) { } private String entryToClass(String entry) { String str = entry.substring("WEB-INF/classes/".length(), entry.length()-6); return str.replaceAll("/", "."); } }
Example 7-7 Sniffer Class
This example shows the Java language class GreeterSniffer, which implements the Sniffer interface.
package org.glassfish.examples.extension.greeter; import org.glassfish.api.container.Sniffer; import org.glassfish.api.deployment.archive.ReadableArchive; import org.glassfish.api.admin.config.ConfigParser; import org.glassfish.examples.extension.greeter.config.GreeterContainerConfig; import org.jvnet.hk2.annotations.Service; import org.jvnet.hk2.annotations.Inject; import org.jvnet.hk2.component.Habitat; import com.sun.enterprise.module.Module; import java.util.logging.Logger; import java.util.Map; import java.io.IOException; import java.lang.annotation.Annotation; import java.lang.reflect.Array; import java.net.URL; /** * @author Jerome Dochez */ @Service(name="greeter") public class GreeterSniffer implements Sniffer { @Inject(optional=true) GreeterContainerConfig config=null; @Inject ConfigParser configParser; @Inject Habitat habitat; public boolean handles(ReadableArchive source, ClassLoader loader) { return false; } public String[] getURLPatterns() { return new String[0]; } public Class<? extends Annotation>[] getAnnotationTypes() { Class<? extends Annotation>[] a = (Class<? extends Annotation>[]) Array.newInstance(Class.class, 1); a[0] = Greeter.class; return a; } public String getModuleType() { return "greeter"; } public Module[] setup(String containerHome, Logger logger) throws IOException { if (config==null) { URL url = this.getClass().getClassLoader().getResource("init.xml"); if (url!=null) { configParser.parseContainerConfig( habitat, url, GreeterContainerConfig.class); } } return null; } public void tearDown() { } public String[] getContainersNames() { String[] c = { GreeterContainer.class.getName() }; return c; } public boolean isUserVisible() { return true; } public Map<String, String> getDeploymentConfigurations (ReadableArchive source) throws IOException { return null; } public String[] getIncompatibleSnifferTypes() { return new String[0]; } }
The web client code is comprised of the classes and resources that are listed in the following table. The table also provides a cross-reference to the listing of each class or resource.
|
Example 7-8 Container Client Class
import components.SimpleGreeter; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.annotation.WebServlet; import javax.servlet.*; import javax.servlet.http.*; import javax.annotation.Resource; @WebServlet(urlPatterns={"/hello"}) public class HelloWorld extends HttpServlet { @Resource(name="Simple") SimpleGreeter greeter; public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { PrintWriter pw = res.getWriter(); try { pw.println("Injected service is " + greeter); if (greeter!=null) { pw.println("SimpleService says " + greeter.saySomething()); pw.println("<br>"); } } catch(Exception e) { e.printStackTrace(); } } }
Example 7-9 Component for Container Client
package components; import org.glassfish.examples.extension.greeter.Greeter; @Greeter(name="simple") public class SimpleGreeter { public String saySomething() { return "Bonjour"; } }
Example 7-10 Deployment Descriptor for Container Client
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/Line break added for readability xml/ns/javaee/web-app_2_5.xsd"> </web-app>