Web Components

Fabric3 supports wiring services to Servlets and JSPs in a web application by treating web applications as components. In this chapter, we cover how to implement a web component and wire it to other services in a domain.

See the Starter Applications for an example calculator web application.

Implementing Web Components

Web components are web applications (WARs) with additional SCA artifacts that allow Servlets and JSPs to be wired to services in a domain.

Web components do not need to be explicitly defined - a WAR is sufficient for Fabric3 to deploy it as a web app. However, if you need to specify configuration such as the URL path, you must create a composite with an implementation.web:

<component name="calculator">
   <implementation.web/>
</component>

WAR contribution composites are typically placed under /WEB-INF although the may be located anywhere in the contribution archive. When deployed, the web application context for the above component will be <runtime ip>:<port>/calculator.

In the previous example, a reference to the CalculatorService was made available to the web application for injection onto servlets and JSPs. To inject the reference on a Servlet, use the @Reference annotation:

public class CalculatorServlet extends HttpServlet {

   @Reference
   protected CalculatorService calculatorService;

   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // ...
   }
}

 

Configuring the Web Context Path

The web context URL is calculated by using the component URI. For example, a web component named "WebComponent" will be available from http://<server address>/WebComponent. It is good practice to specify a relative context path that differs from the component name. To do this, use the implementation.web/@uri attribute:

<component name="WebComponent">
   <implementation.web uri="webapp"/>
</component>

In the above example, the web context URL will be http://<server address>/webapp

 

Note that references configured on a web component are available to all artifacts in the web application. A reference defined in the composite may be injected into multiple Servlets and JSPS.

Integrating Web UI Frameworks

Applications may require a UI. Fabric3 supports bundling UI frameworks as part of a WAR deployment. In this case, it is often necessary to provide the UI framework with references to Fabric3 services that can be invoked during request processing. UI frameworks typically provide extension points to integrate third-party component frameworks. These extension points can be used in conjunction with the Fabric3 Node API as defined in the org.fabric3:fabric-node-api module. For example, a service resolver for a UI framework can be implemented using the Fabric3 Node API as follows:

 public T resolve(Class<T> serviceType) {
    Object proxy = CACHE.get(serviceType);
    if (proxy == null) {
    	ClassLoader loader = getClass().getClassLoader();
		// get the Fabric3 service proxy and cache it
    	proxy = Bootstrap.initialize().getDomain().getService(serviceType);
    	CACHE.put(serviceType, proxy);
    }
    return proxy;
}

 

Enabling Web Component Support

Web component support is enabled by installing the web profile in any of the Fabric3 runtimes.