Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Purpose of using java.lang.reflect.Proxy ?

Reply
Thread Tools

Purpose of using java.lang.reflect.Proxy ?

 
 
Sébastien de Mapias
Guest
Posts: n/a
 
      06-11-2009
Hi,

This is a newbie question... I've just read that using a proxy inside
application code is quite unusual, and that it's mostly used in
frameworks... In an app I've inherited, I find the following lines:

DFLT_SESS_FACT = HibernateSupport.getSessionFactory
("hibernate.cfg.xml",
JdbcStuffDAO.class.getClassLoader
(),
new StuffBaseEntityInterceptor());
[...]
public IStuffService2 getStuffService(String p_user,
String p_password,
ApplicationCode
p_applicationCode,
SessionFactory
p_sessionFactory)
throws StuffException
{
JdbcStuffServiceImpl l_service =
new JdbcStuffServiceImpl(p_user,p_applicationCode,new
JdbcStuffDAO(p_sessionFactory));
IStuffService2 l_proxy =
(IStuffService2)Proxy.newProxyInstance
(JdbcStuffFactory.class.getClassLoader(),
new Class[] {IStuffService2.class},
new HibernateSessionInvocationHandler
(DFLT_SESS_FACT,l_service,l_service.getLocale()));
return l_proxy;
}

I'm not familiar with this Proxy thing... What purpose does it serve ?
I've noticed this method could return 'l_service' as well (cause
JdbcStuffServiceImpl implements IStuffService2), and thus have:
{
JdbcStuffServiceImpl l_service =
new JdbcStuffServiceImpl(p_user,p_applicationCode,new
JdbcStuffDAO(p_sessionFactory));
return l_service;
}
but I haven't tested it.
What did the guy who wrote these lines want to achieve ? (he's left,
so
I can't ask him...)

Thanks for your help...
Regards,
Sébastien


 
Reply With Quote
 
 
 
 
Joshua Cranmer
Guest
Posts: n/a
 
      06-11-2009
Sébastien de Mapias wrote:
> This is a newbie question...


Proxies are not newbie questions (nor anything to do with reflection,
for that matter).

> I'm not familiar with this Proxy thing... What purpose does it serve ?


Proxies are one of the more obscure features of reflection. The basic
idea is that the end result is an object which dynamically implements
certain interfaces.

> What did the guy who wrote these lines want to achieve ? (he's left,
> so
> I can't ask him...)


In lieu of context, I can only make assumptions. One possibility is that
the person is wrapping a generic extender around the original object.
Since you mention that the class also appears to extend the interface in
question as well, I can presume that either this was a short-term hack
intended to support something while he was working on making
JdbcStuffServiceImpl implement IService2.

The other possibility that strikes me is that the wrapper is doing some
external operations, such as (judging from Google results) wrapping the
method in a transaction device:

<wrapped method impl>:
try {
beginTransaction();
<call original method>
commitTransaction();
} catch (Throwable t) {
rollbackTransaction();
throw t;
}

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What's the purpose of using variable list arguments in error handling? Chad C Programming 9 06-17-2007 08:33 AM
What's the purpose of Download Manager in FF charleswalters@no-email.com Firefox 10 04-27-2005 08:17 PM
Visual Web Developer 2005 Express SCREWS UP YOUR CODE on PURPOSE...STOP TOUCHING MY CODE, MICROSOFT!!!! ik ASP .Net 6 07-16-2004 05:28 PM
What is the purpose of FormsAuthentication.SignOut()? Ali ASP .Net 1 01-29-2004 02:08 PM
What is the purpose of using UNION in a class...?? jonnychang C++ 4 06-27-2003 11:03 PM



Advertisments