Last active
March 7, 2016 12:55
-
-
Save mbaez/a9e1807a22f25e4f9737 to your computer and use it in GitHub Desktop.
Programación Web - Dynamic Proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Facultad Politécnica - Ingeniería en Informática. | |
* Programación Web - Dynamic Proxy | |
* | |
* Ejercicio de como utilizar Dynamic Proxy | |
* | |
* @author mbaez | |
*/ | |
public class Cliente { | |
/** | |
* Se encarga de guardar un producto | |
* | |
* @param p El nuevo producto | |
*/ | |
public void guardar(Producto p){ | |
// do someting | |
ProductoDAO dao = (ProductoDAO) TransactionProxy.newInstance(new ProductoDAOImpl()); | |
dao.persit(p); | |
//do someting | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Facultad Politécnica - Ingeniería en Informática. | |
* Programación Web - Dynamic Proxy | |
* | |
* Ejercicio de como utilizar Dynamic Proxy | |
* | |
* @author mbaez | |
*/ | |
public class Producto { | |
// atributos | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Facultad Politécnica - Ingeniería en Informática. | |
* Programación Web - Dynamic Proxy | |
* | |
* Ejercicio de como utilizar Dynamic Proxy | |
* | |
* @author mbaez | |
*/ | |
public interface ProductoDAO { | |
/** | |
* Inserta en la base de datos un producto específico. | |
* | |
* @param p Nuevo producto | |
*/ | |
public void persit(Producto p); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Facultad Politécnica - Ingeniería en Informática. | |
* Programación Web - Dynamic Proxy | |
* | |
* Ejercicio de como utilizar Dynamic Proxy | |
* | |
* @author mbaez | |
*/ | |
public class ProductoDAOImpl implements ProductoDAO{ | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public void persit(Producto p) { | |
// do someting | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Facultad Politécnica - Ingeniería en Informática. | |
* Programación Web - Dynamic Proxy | |
* | |
* Ejercicio de como utilizar Dynamic Proxy | |
* | |
* @author mbaez | |
*/ | |
public class TransactionProxy implements InvocationHandler { | |
/** | |
* Referencia al Dao | |
*/ | |
private Object obj; | |
/** | |
* Método encargado de crear una nueva instancia del proxy. | |
* | |
* @param obj Referencia al DAO | |
* @return una instancia del proxy | |
*/ | |
public static Object newInstance(Object obj) { | |
return java.lang.reflect.Proxy.newProxyInstance( | |
obj.getClass().getClassLoader(), | |
obj.getClass().getInterfaces(), | |
new TransactionProxy(obj)); | |
} | |
/** | |
* Constructor privado del proxy | |
* | |
* @param obj | |
*/ | |
private TransactionProxy(Object obj) { | |
this.obj = obj; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | |
Object result; | |
Session session = null; | |
Transaction tx = null; | |
try { | |
session = HibernateUtil.getSessionFactory().openSession(); | |
//inicia la transacción | |
tx = session.beginTransaction(); | |
//se invoca al método del dao | |
result = method.invoke(obj, args); | |
//se intenta realizar el commit | |
tx.commit(); | |
} catch (RuntimeException e) { | |
//si ocurrio un error se hace rollback de la transacción | |
tx.rollback(); | |
throw e; | |
} finally { | |
//se cierra la sesión | |
if (session != null) { | |
session.close(); | |
} | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment