• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Cristian Vrabie
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Cristian Vrabie
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!