This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Java in General and the fly likes WebApplication architecture (carrying context some other way than thought method parameters) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "WebApplication architecture (carrying context some other way than thought method parameters)" Watch "WebApplication architecture (carrying context some other way than thought method parameters)" New topic
Author

WebApplication architecture (carrying context some other way than thought method parameters)

Cristian Vrabie
Ranch Hand

Joined: Jul 09, 2008
Posts: 71
Hi all!
I have an application that is pretty much layered this way:
FacadeServlet -> Controller -> Service -> Dao -> Hibernate ->DB

The thing is that in all methods of the Service layer I need 2 strings A and B (for rights check and other stuff) that are stored in the HttpSession.

At the moment all methods in the Service layer have these 2 string as first parameters: public method C(String A, String B, otherParameters...).
The Controller has the job of getting the strings from the HttpSession and making the calls to the Service.

You can see that there's a lot of repetition. In all Controllers have the exact same 2 lines, the methods in Service don't look pretty, plus I think i use more memory as every method call has the 2 string on the stack.

I'm wondering if I can solve this more elegantly. I vaguely remember something that I've seen at one point, involving the String retrieval in FacadeServlet and setting them in ThreadLocal variables. I never worked with ThreadLocal before but if it does what I think it does it would make sense since all http requests run on different threads (?), and FacadeServlet would be on the same thread as the Service method call (?). Is it possible? If yes, I already spot some troubles in case the service adds a timer to call another service method at a later time.

Are there any other ways to do what I want?

Thanks!
Cristian
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3268

Hi there,

A couple of random things to think about (Trying to start a discussion here as much as providing answers):

1.) Could all of your controllers inherit from a parent controller which deals with the 2 strings?

2.) Could all of your controllers implement an interface which formalises dealing with the 2 strings?

3.) Have you investigated AOP for this (I assuming its security cross cutting you're trying to achieve here)?

Also google j2ee security patterns and you'll get a wealth of knowledge there.

Hopefully other ranchers weigh in


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Cristian Vrabie
Ranch Hand

Joined: Jul 09, 2008
Posts: 71
Hi Martijn, thanks for your answer!
1.) Could all of your controllers inherit from a parent controller which deals with the 2 strings?
2.) Could all of your controllers implement an interface which formalises dealing with the 2 strings?

I'm not sure how this would really help. The controllers are simple classes where more than 1 methods act as resolver for different urls. Maybe it would reduce my code from 2 lines to 1 in each controller (calling the super method), but i have to underline, the worse thing in my opinion is not that i'm doing that repetitive action in the controller but the fact that all my methods in service have to have those 2 extra parameters.
3.) Have you investigated AOP for this (I assuming its security cross cutting you're trying to achieve here)?

Very true. If would have to do only the security check with the 2 strings AOP would be right on the spot. However, I also have the particular need to send the 2 string lower, to the DAO layer (in less situations, but still a significant amount of times) and with my current AOP knowledge I can't think of a way to do this.
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3268

How about an alternative design where an authenticate/authorise method is called first before calling the actual business method? So you have the one method with parameters A & B that always gets called before executing the specific business method. This of course probably changes your design somewhat down further.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: WebApplication architecture (carrying context some other way than thought method parameters)
 
Similar Threads
Just another MVC-Question
Architectural dilemma
get applicationContext at Business layer
how to implement MVC?
Front controller and DAO, design question!