Purpose of using java.lang.reflect.Proxy ?

Discussion in 'Java' started by Sébastien de Mapias, Jun 11, 2009.

  1. 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
     
    Sébastien de Mapias, Jun 11, 2009
    #1
    1. Advertising

  2. 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
     
    Joshua Cranmer, Jun 11, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. snatchitup
    Replies:
    3
    Views:
    1,962
    Larry A. Barowski
    Dec 4, 2003
  2. David C. Partridge
    Replies:
    1
    Views:
    3,302
    Chris Smith
    Apr 13, 2004
  3. Nikita A. Visnevski

    Problem with java.lang.reflect.Proxy

    Nikita A. Visnevski, Apr 22, 2004, in forum: Java
    Replies:
    1
    Views:
    1,766
    Nikita A. Visnevski
    Apr 22, 2004
  4. Ovidiu

    java.lang.reflect.Proxy

    Ovidiu, Aug 10, 2004, in forum: Java
    Replies:
    1
    Views:
    492
    John C. Bollinger
    Aug 10, 2004
  5. Karsten Wutzke
    Replies:
    1
    Views:
    893
    Steven Simpson
    Aug 20, 2007
Loading...

Share This Page