Interceptors: TV Channel Service Example (Enterprise JavaBeans 3.1)

 

Description

As we’ve seen with security and transactions, often we have business logic that is not a part of our core concerns. Quite the opposite; applications are likely to have rules that need to be universal across modules or EJBs, and baking this logic into our implementation mixes concerns and makes for an unmaintainable system over time.

Much like Servlet Filters or AOP aspects, the EJB Interceptor model provides a mechanism for application developers to apply logic to incoming requests. The ^Interceptors annotation quickly acts as a mapping marker for the EJB container to know to apply a particular interceptor for a given invocation.

Our example represents a television server which is capable of returning channel streams when a channel number is requested by the client. We apply a custom security policy whereby Channel 2 may be closed by an administrator; if a client requests Channel 2 while access is disallowed, the invocation will return a “Chan-nel2ClosedException”. All of this logic is separated from the core code to return the channel requested, and therefore may be applied to other modules or unplugged from the system without need to alter the existing application in any way.

Source Listing

Following is a full listing of all source code used in this runnable example.

Implementation Resources

AuditedInvocation.java

tmp99-146_thumb[1]tmp99-147_thumb[1]

CachingAuditor.java

tmp99-148_thumb[1]

tmp99-149_thumb[1]

tmp99-150_thumb[1]

tmp99-151_thumb

Channel2AccessPolicy.java

tmp99-152_thumb[1]tmp99-153_thumb

Channel2ClosedException.java

tmp99-154_thumb[1]tmp99-155_thumb[1]

Channel2Restrictor.java

tmp99-156_thumb[2]

tmp99-157_thumb[1]

tmp99-158_thumb[1]

TunerBean.java

tmp99-159_thumb[1]

tmp99-160_thumb[1]

tmp99-161_thumb

TunerLocalBusiness.java

tmp99-162_thumb[1]

Test Resources

CachingInterceptorUnitTestCase.java

tmp99-163_thumb

tmp99-164_thumb[2]

tmp99-165_thumb[1]

tmp99-166_thumb[1]

tmp99-167_thumb[1]

tmp99-168_thumb[1]

Channel2RestrictorUnitTestCase.java

tmp99-169_thumb[1]

tmp99-170_thumb[1]

tmp99-171_thumb[1]

InterceptorIntegrationTest.java

tmp99-172_thumb[1]

tmp99-173_thumb[1]

tmp99-174_thumb[1]

tmp99-175_thumb[1]

MockInvocationContext.java

tmp99-176_thumb[1]

tmp99-177_thumb[1]

tmp99-178_thumb[1]

SecurityActions.java

tmp99-179_thumb[1]

tmp99-180_thumb[1]

tmp99-181_thumb[1]

tmp99-182_thumb

Next post:

Previous post: