title: Using Dynamic Proxies to centralize JPA code
body: <p>Actually, This is not a question but really I need your opinions in a matter...
  I put his post here because I know you always active, so please don't consider this a bad question and share me your opinions.</p>
  
  <p>I've used Java dynamic proxies to Centralize The code of JPA that I used in a standalone mode, and Here's the dynamic proxy code:</p>
  
  <pre><code>package com.forat.service;
  
  import java.lang.reflect.InvocationHandler;
  import java.lang.reflect.Method;
  import java.lang.reflect.Proxy;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  
  import javax.persistence.EntityManager;
  import javax.persistence.EntityManagerFactory;
  import javax.persistence.EntityTransaction;
  import javax.persistence.Persistence;
  
  import com.forat.service.exceptions.DAOException;
  
  /**
   * Example of usage :
   * &lt;pre&gt;
   * OnlineFromService onfromService = 
   *            (OnlineFromService) DAOProxy.newInstance(new OnlineFormServiceImpl());
   *        try {
   *            Student s = new Student();
   *            s.setName("Mohammed");
   *            s.setNationalNumber("123456");
   *            onfromService.addStudent(s);    
   *        }catch (Exception ex) {
   *            System.out.println(ex.getMessage());
   *        }
   *&lt;/pre&gt;
   * @author mohammed hewedy
   *
   */
  public class DAOProxy implements InvocationHandler{
  
      private Object object;
      private Logger logger = Logger.getLogger(this.getClass().getSimpleName());
  
      private DAOProxy(Object object) {
          this.object = object;
      }
  
      public static Object newInstance(Object object) {
          return Proxy.newProxyInstance(object.getClass().getClassLoader(), 
                      object.getClass().getInterfaces(), new DAOProxy(object));
      }
  
      @Override
      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
          EntityManagerFactory emf = null;
          EntityManager em = null;
          EntityTransaction et = null;
          Object result = null;
          try {
              emf = Persistence.createEntityManagerFactory(Constants.UNIT_NAME);
              em = emf.createEntityManager();;
              Method entityManagerSetter = object.getClass().
                  getDeclaredMethod(Constants.ENTITY_MANAGER_SETTER_METHOD, EntityManager.class);
              entityManagerSetter.invoke(object, em);
              et = em.getTransaction();
              et.begin();
              result = method.invoke(object, args);
              et.commit();
              return result;
          }catch (Exception ex) {
              et.rollback();
              Throwable cause = ex.getCause();
              logger.log(Level.SEVERE, cause.getMessage());
              if (cause instanceof DAOException)
                  throw new DAOException(cause.getMessage(), cause);
              else
                  throw new RuntimeException(cause.getMessage(), cause);
          }finally {
              em.close();
              emf.close();
          }
      }
  }
  </code></pre>
  
  <p>And here's the link that contains more info (<a href="http://m-hewedy.blogspot.com/2010/04/using-dynamic-proxies-to-centralize-jpa.html" rel="nofollow noreferrer">http://m-hewedy.blogspot.com/2010/04/using-dynamic-proxies-to-centralize-jpa.html</a>)</p>
  
  <p>So, Please give me your opinions.</p>
  
  <p>Thanks.</p>