Fork me on GitHub

WoofServlet Dependency Injection Tutorial

This tutorial demonstrates using the dependency injected objects (e.g. EJBs) from the Servlet container within WoOF.

Download Tutorial Source

EJB

To illustrate using the Servlet container dependency injection the following simple EJB will be used

@Stateless
public class ExampleDependency implements ExampleDependencyLocal {

	@Override
	public Message getMessage() {
		return new Message("MESSAGE");
	}

}

with a local interface as follows

@Local
public interface ExampleDependencyLocal {

	Message getMessage();

}

and a returned object as follows.

public class Message {

	private String text;

	public Message(String text) {
		this.text = text;
	}

	public String getText() {
		return this.text;
	}

}

Making use of an EJB within the WoofServlet

To use an EJB extend WoofServlet and add the necessary EJB fields as follows.

public class ExampleServlet extends WoofServlet {

	@EJB
	ExampleDependencyLocal dependency;

}

The extended Servlet is then configured into the web.xml as follows. It is configured as a ServletContextListener as it will configure itself appropriately based on the URIs to be serviced.

<?xml version="1.0" encoding="UTF-8"?>
<web-app>

	<listener>
		<listener-class>net.officefloor.tutorial.woofservletdependencyinjection.ExampleServlet</listener-class>
	</listener>

</web-app>

On start up the WoofServlet will interrogate itself (implementing class) for JEE Servlet container dependency injected objects. WoOF will make these Servlet dependency injected objects available for dependency injection into the template logic methods.

The template logic shows the use of the Servlet dependency injected object.

public class ExampleTemplateLogic {

	public Message getTemplate(ExampleDependencyLocal dependency) {
		return dependency.getMessage();
	}

}

The template is as follows.

<html><body>${text}</body></html>

WoOF will use the JEE container thread to execute all methods using a dependency from the JEE container. This is necessary to allow EJBs to pick up the appropriate context (via ThreadLocal) for executing correctly. WoOF will however allow multi-threading all other methods that do not use dependencies from the JEE container.

Unit Test

The following unit test illustrates the ease in which to test the template logic class as a POJO.

	public void testTemplateLogic() {

		ExampleTemplateLogic logic = new ExampleTemplateLogic();

		// May mock interface to test but will use EJB for simplicity
		ExampleDependencyLocal ejb = new ExampleDependency();

		Message message = logic.getTemplate(ejb);
		assertEquals("Text for message", "MESSAGE", message.getText());
	}

The following unit tests show servicing of a request by the WoOF template utilising the dependency injected EJB.

	public void testTemplate() throws IOException {

		// Undertake the request
		HttpGet request = new HttpGet(
				"http://localhost:18181/test/template.woof");
		HttpResponse response = this.client.execute(request);
		assertEquals("Request should be successful", 200, response
				.getStatusLine().getStatusCode());

		// Ensure content is as expected
		ByteArrayOutputStream buffer = new ByteArrayOutputStream();
		response.getEntity().writeTo(buffer);
		assertEquals("Incorrect response entity",
				"<html><body>MESSAGE</body></html>", buffer.toString());
	}

Next

The next tutorial looks at embedding Servlet functionality within a WoOF Application.