The decorators Example: Decorating a Bean
The decorators example, which is yet another variation on the encoder example, shows how to use a decorator to implement additional business logic for a bean. Instead of having the user choose between two alternative implementations of an interface at deployment time or runtime, a decorator adds some additional logic to a single implementation of the interface.
The example includes an interface, an implementation of it, a decorator, an interceptor, a managed bean, a Facelets page, and configuration files.
Components of the decorators Example
The decorators example is very similar to the encoder example described in The encoder Example: Using Alternatives. Instead of providing two implementations of the Coder interface, however, this example provides only the CoderImpl class. The decorator class, CoderDecorator, rather than simply return the coded string, displays the input and output strings’ values and length.
The CoderDecorator class, like CoderImpl, implements the business method of the Coder interface, codeString:
@Decorator public abstract class CoderDecorator implements Coder { @Inject @Delegate @Any Coder coder; @Override public String codeString(String s, int tval) { int len = s.length(); return "\"" + s + "\" becomes " + "\"" + coder.codeString(s, tval) + "\", " + len + " characters in length"; } }
The decorator’s codeString method calls the delegate object’s codeString method to perform the actual encoding.
The decorators example includes the Logged interceptor binding and LoggedInterceptor class from the billpayment example. For this example, the interceptor is set on the CoderBean.encodeString method and the CoderImpl.codeString method. The interceptor code is unchanged; interceptors are usually reusable for different applications.
Except for the interceptor annotations, the CoderBean and CoderImpl classes are identical to the versions in the encoder example.
The beans.xml file specifies both the decorator and the interceptor:
<decorators> <class>decorators.CoderDecorator</class> </decorators> <interceptors> <class>decorators.LoggedInterceptor</class> </interceptors>
Running the decorators Example
You can use either NetBeans IDE or Ant to build, package, deploy, and run the decorators application.
To Build, Package, and Deploy the decorators Example Using NetBeans IDE
- From the File menu, choose Open Project.
- In the Open Project dialog, navigate to:
tut-install/examples/cdi/
- Select the decorators folder.
- Select the Open as Main Project check box.
- Click Open Project.
- In the Projects tab, right-click the decorators project and select Deploy.
To Build, Package, and Deploy the decorators Example Using Ant
- In a terminal window, go to:
tut-install/examples/cdi/decorators/
- Type the following command:
ant
This command calls the default target, which builds and packages the application into a WAR file, decorators.war, located in the dist directory.
- Type the following command:
ant deploy
To Run the decorators Example
- In a web browser, type the following URL:
http://localhost:8080/decorators
The Decorated String Encoder page opens.
- Type a string and the number of letters to shift by, then
click Encode.
The output from the decorator method appears in blue on the Result line. For example, if you typed Java and 4, you would see the following:
"Java" becomes "Neze", 4 characters in length
- Examine the server log output.
In NetBeans IDE, the output is visible in the GlassFish Server 3+ output window. Otherwise, view domain-dir/logs/server.log.
The output from the interceptors appears:
INFO: Entering method: encodeString in class decorators.CoderBean INFO: Entering method: codeString in class decorators.CoderImpl