Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I guess this would be the perfect use case for </p> <ol> <li>Java Dynamic Proxys</li> <li>Thread locals</li> </ol> <p>To elucidate:</p> <p>First, create your Facade Interface :</p> <pre> interface UserServiceFacede { List findUsers(UserSearchParameters searchParameters, PagingParameters pagingParameters) throws AuthenticationFault, InvalidIndexFault; void removeUser(User userToRemove) throws AutheticationFault; void addUser(User newUser) throws AuthenticationFault; } </pre> <p>Next you implement your InvocationHandler :</p> <pre> public class UserHandler implements InvocationHandler{ private final UserService service; public UserHandler(UserService service) { this.service = service; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { .... // Based on the method argument, call the appropriate function on the actual service // object .... } } </pre> <p>You then create the Proxy object like so :</p> <pre> UserServiceFacade uProxy = (UserServiceFacade)Proxy.newProxyInstance(UserHandler.class.getClassLoader(), new Class[] { UserServiceFacade.class}, new UserHandler(userService)); </pre> <p>The first argument is the classloader, the second is the list of interfaces the Proxy must confirm to and the third is the handler object</p> <p>Now the question is how do you get a handle onto your loggedinUser. This is where <a href="http://download.oracle.com/javase/6/docs/api/java/lang/ThreadLocal.html" rel="nofollow">ThreadLocal</a> comes in.</p> <p>Create a ThreadLocal static variable that holds the loggedin user. Then set the current user into the ThreadLocal before invoking the proxy</p> <pre> private static final ThreadLocal loggedinUser = ...; ... ... loggedinUser.set(currentUser); // Create a user Proxy object as mentioned above.. uProxy.findUser(searchParams, ..); ... </pre> <p>Retrieve the User from the ThreadLocal inside the invoke function of the handler. Now inside the handler, you have access to both the loggedinUser (using <code>loggedinUser.get()</code>) as well as the user service object (You have passed in the service object into your handler's constructor)</p> <p>As far as how you handle exceptions are concerned, WHat I would do is generally keep throwing a subclass of RuntimeExceptions. Then catch these at the periphery layer of your app. By periphery layer I mean, entry point into your app/library. If you were developing a webbApp this would be your Servlet.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload