Java Platform, Enterprise Edition (Java EE) 8 The Java EE Tutorial

## 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.

The source files are located in the `_tut-install_/examples/cdi/decorators/src/main/java/javaeetutorial/decorators/` directory.

The following topics are addressed here:

- Overview of the decorators Example

- Components of the decorators Example

- Running the decorators Example

### Overview of the decorators Example

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;

        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>javaeetutorial.decorators.CoderDecorator</class>
        </decorators>
        <interceptors>
            <class>javaeetutorial.decorators.LoggedInterceptor</class>
        </interceptors>

### Running the decorators Example

You can use either NetBeans IDE or Maven to build, package, deploy, and run the `decorators` application.

The following topics are addressed here:

- To Build, Package, and Deploy the decorators Example Using NetBeans IDE

- To Build, Package, and Deploy the decorators Example Using Maven

- To Run the decorators Example

#### To Build, Package, and Deploy the decorators Example Using NetBeans IDE

1. Make sure that GlassFish Server has been started (see Starting and Stopping GlassFish Server).

2. From the File menu, choose Open Project.

3. In the Open Project dialog box, navigate to:

       tut-install/examples/cdi

4. Select the `decorators` folder.

5. Click Open Project.

6. In the Projects tab, right-click the `decorators` project and select Build.

   This command builds and packages the application into a WAR file, `decorators.war`, located in the `target` directory, and then deploys it to GlassFish Server.

#### To Build, Package, and Deploy the decorators Example Using Maven

1. Make sure that GlassFish Server has been started (see Starting and Stopping GlassFish Server).

2. In a terminal window, go to:

       tut-install/examples/cdi/decorators/

3. Enter the following command to deploy the application:

       mvn install

   This command builds and packages the application into a WAR file, `decorators.war`, located in the `target` directory, and then deploys it to GlassFish Server.

#### To Run the decorators Example

1. In a web browser, enter the following URL:

       http://localhost:8080/decorators

2. On the Decorated String Encoder page, enter a string and the number of letters to shift by, and then click Encode.

   The output from the decorator method appears in blue on the Result line. For example, if you entered `Java` and `4`, you would see the following:

       "Java" becomes "Neze", 4 characters in length

3. Examine the server log output.

   In NetBeans IDE, the output is visible in the GlassFish Server Output tab. Otherwise, view domain-dir`/logs/server.log`.

   The output from the interceptors appears:

       INFO: Entering method: encodeString in class javaeetutorial.decorators.CoderBean
       INFO: Entering method: codeString in class javaeetutorial.decorators.CoderImpl

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.