18

Click here to load reader

Clase 21 programacion ejb 3.0

Embed Size (px)

Citation preview

Page 1: Clase 21 programacion ejb 3.0

Programación de EJB 3.0

Page 2: Clase 21 programacion ejb 3.0

Crear Modulo EJB – Paso 1 1.- Abrir modulo EJB de la aplicación empresarial. 2.- Click derecho sobre proyecto EJB New Session Bean (EJB 3.x)

Page 3: Clase 21 programacion ejb 3.0

Crear Modulo EJB – Paso 3

Page 4: Clase 21 programacion ejb 3.0

Crear Modulo EJB – Paso 4

Page 5: Clase 21 programacion ejb 3.0

Crear EjemploTO Crear clase con nombre EjemploTO que implemente

interfaz serializable (ADD en Interfaces)

Page 6: Clase 21 programacion ejb 3.0

EjemploTOimport java.io.Serializable;

public class EjemploTO implements Serializable {

private String nombre;

private String apellido;

public void setNombre(String nombre) {

this.nombre = nombre;

}

public String getNombre() {

return nombre;

}

public void setApellido(String apellido) {

this.apellido = apellido;

}

public String getApellido() {

return apellido;

}

}

Page 7: Clase 21 programacion ejb 3.0

Crear interface DAO Package: com.aplicacion.ingreso.persistencia.dao.iface

import java.util.List;

import com.ingreso.negocio.to.EjemploTO;

public interface IEjemploDAO {

public List<EjemploTO> getEjemplos();

}

Page 8: Clase 21 programacion ejb 3.0

Crear EjemploDAO 1.- Crear nueva clase DAO con nombre EjemploDAO

packagecom.aplicacion.ingreso.persistencia.dao.implimport java.util.ArrayList;

import java.util.List;

import com.ingreso.negocio.to.EjemploTO;

import com.aplicacion.ingreso.persistencia.dao.iface.IEjemploDAO;

public class EjemploDAO implements IEjemploDAO {

public List<EjemploTO> getEjemplos() {

ArrayList<EjemploTO> ejemplos = new ArrayList<EjemploTO>();

EjemploTO ejemploTO = new EjemploTO();

ejemploTO.setApellido("Apellido1");

ejemploTO.setNombre("Nombre1");

ejemplos.add( ejemploTO );

ejemploTO = new EjemploTO();

ejemploTO.setApellido("Apellido1");

ejemploTO.setNombre("Nombre1");

ejemplos.add( ejemploTO);

return ejemplos;

}

}

Page 9: Clase 21 programacion ejb 3.0

Crear DAO Factory: Package : com.aplicacion.ingreso.persistencia.daoimport com.aplicacion.ingreso.persistencia.dao.iface.IEjemploDAO;

import com.aplicacion.ingreso.persistencia.dao.impl.EjemploDAO;

public class DAOFactory {

private static DAOFactory daoFactory;public static DAOFactory getInstance() {

if (daoFactory == null) {

daoFactory = new DAOFactory();

}

return daoFactory;

}

private DAOFactory() {

}

public IEjemploDAO getEjemploDAO() {

return new EjemploDAO();

}

}

Page 10: Clase 21 programacion ejb 3.0

Código ejemplo BusinessObjectpublic class IngresoBO {

private static IngresoBO ingresoBO;

private PersonaTO person ;

public static IngresoBO getInstance(){

if(ingresoBO==null) {ingresoBO = new IngresoBO();

}return ingresoBO;

}

private IngresoBO() {

}

public List<StateTO> getStates() {

IStateDAO iStateDAO;

iStateDAO =DAOFactory.getInstance().getStateDAO();

return iStateDAO.getStates();

}

Page 11: Clase 21 programacion ejb 3.0

Código Ejemplo EJB /**

* Session Bean implementation class IngresoBean

*/

@Stateless

public class IngresoBean implements IngresoBeanRemote, IngresoBeanLocal {

/**

* Default constructor.

*/

public IngresoBean() {

// TODO Auto-generated constructor stub

}

public List<StateTO> getStates() {

return IngresoBO.getInstance().getStates();

}

Page 12: Clase 21 programacion ejb 3.0

Ejemplo Interface Local

@Local

public interface IngresoBeanLocal {public List<StateTO> getStates() ;}

Page 13: Clase 21 programacion ejb 3.0

Ejemplo Interface Remote @Remote public interface IngresoBeanRemote { public List<StateTO> getStates() ; }

Page 14: Clase 21 programacion ejb 3.0

Código Service Locatorimport java.util.Properties;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

public class ServiceLocator {

private static ServiceLocator me;

InitialContext ic ;

private ServiceLocator()

throws ServiceLocatorException {

try {

ic = getInitialContext();

} catch(NamingException ne) {

ne.printStackTrace();

throw new ServiceLocatorException(ne.getMessage());

}

}

Page 15: Clase 21 programacion ejb 3.0

Código Service Locator Parte 2 // Returns the instance of ServiceLocator class

public static ServiceLocator getInstance()

throws ServiceLocatorException {

if (me == null) {

me = new ServiceLocator();

}

return me;

}

private InitialContext getInitialContext( ) throws javax.naming.NamingException {

Properties p = new Properties( );

p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");

p.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");

p.put(Context.PROVIDER_URL, "jnp://localhost:1099");

return new javax.naming.InitialContext(p);

}

Page 16: Clase 21 programacion ejb 3.0

Código Service Locator Parte 3 public Object getEjb(String ejbName)throws ServiceLocatorException {

try {

Object ref = ic.lookup("AplicacionEmpresarial/IngresoBean/remote");

return ref;

}

catch (NamingException ne) {

ne.printStackTrace();

throw new ServiceLocatorException(ne.getMessage());

}

}

}

Page 17: Clase 21 programacion ejb 3.0

Business Delegatepublic class IngresoBusinessDelegate {

IngresoBeanRemote ingresoRemote=null;

public IngresoBusinessDelegate() {

Object ref = null;

try {

ref = ServiceLocator.getInstance().getEjb("IngresoBean");

} catch (ServiceLocatorException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

ingresoRemote = (IngresoBeanRemote)ref;

}

public List<StateTO> getStates() {

return ingresoRemote.getStates();

}

}

Page 18: Clase 21 programacion ejb 3.0

Invocación mediante HelperIngresoBusinessDelegate ingresoDelegate = new IngresoBusinessDelegate();

public List<StateTO> getStates() {

return ingresoDelegate.getStates();

}