Programación de 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)
Crear Modulo EJB – Paso 3
Crear Modulo EJB – Paso 4
Crear EjemploTO Crear clase con nombre EjemploTO que implemente
interfaz serializable (ADD en Interfaces)
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;
}
}
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();
}
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;
}
}
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();
}
}
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();
}
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();
}
Ejemplo Interface Local
@Local
public interface IngresoBeanLocal {public List<StateTO> getStates() ;}
Ejemplo Interface Remote @Remote public interface IngresoBeanRemote { public List<StateTO> getStates() ; }
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());
}
}
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);
}
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());
}
}
}
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();
}
}
Invocación mediante HelperIngresoBusinessDelegate ingresoDelegate = new IngresoBusinessDelegate();
public List<StateTO> getStates() {
return ingresoDelegate.getStates();
}