Smider lige koden så andre kan få gavn af den:
NB. der bliver returneret et LocalHome interface, idet sessionBean kalder en EntityBean lokalt på JBoss serveren
import javax.ejb.CreateException;
import javax.ejb.SessionContext;
import javax.ejb.EJBException;
import javax.rmi.PortableRemoteObject;
import javax.ejb.SessionBean;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.ArrayList;
import java.util.Iterator;
import customer.CustomerHome;
import customer.Customer;
import common.ServiceLocator;
import common.ServiceLocatorException;
/**
Sessionbean, der håndterer bruger login. Fungerer som facade for login
SessionBean der kalder en EntityBean(CustomerBean)
EntityBean: CustomerBean.java, Customer.java, CustomerHome.java
*/
public class UserManagerBean implements javax.ejb.SessionBean
{
private SessionContext ctx;
private String usernameDB;
private String passwordDB;
private String roleDB;
public void createCustomer()
{
try
{
ServiceLocator serviceLocator = ServiceLocator.getInstance();
CustomerHome customerHome = (CustomerHome)serviceLocator.getHome("customer",CustomerHome.class);
customerHome.create("sys","sys","sysadmin");
customerHome.create("cus","cus","customer");
customerHome.create("dev","dev","developer");
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
//----------------------------------------------------
// Begin EJB-required methods. The methods below are
// called by the Container, and never called by client
// code.
//----------------------------------------------------
public void ejbCreate() throws CreateException
{
System.out.println("ejbCreate() called.");
}
public void ejbRemove() throws EJBException
{
System.out.println("ejbRemove() called.");
}
public void ejbActivate() throws EJBException
{
System.out.println("ejbActivate() called.");
}
public void ejbPassivate() throws EJBException
{
System.out.println("ejbPassivate() called.");
}
public void setSessionContext(SessionContext ctx) throws EJBException
{
System.out.println("setSessionContext() called");
this.ctx = ctx;
}
//----------------------------------------------------
// End EJB-required methods
//----------------------------------------------------
}
import java.util.*;
import javax.naming.*;
import java.rmi.RemoteException;
import javax.ejb.*;
import javax.rmi.PortableRemoteObject;
import java.io.*;
import customer.CustomerHome;
/**
ServiceLocator pattern, which makes lookup on all local entity beans
The design pattern is inspired by Java.sun.com
http://java.sun.com/blueprints/corej2eepatterns/Patterns/ServiceLocator.html */
public class ServiceLocator
{
public static InitialContext ctx;
public static ServiceLocator me;
private ServiceLocator() throws ServiceLocatorException
{
}
public static ServiceLocator getInstance() throws ServiceLocatorException
{
if (me == null)
{
me = new ServiceLocator();
}
return me;
}
/**
* @param name the JNDI name
* @return The EJBLocalHome interface
*/
public EJBLocalHome getHome(String name, Class homeClass) throws ServiceLocatorException
{
try
{
// create JNDI context
ctx = new InitialContext();
Object obj = ctx.lookup(name);
(CustomerHome)PortableRemoteObject.narrow(obj,homeClass);
EJBLocalHome home = (EJBLocalHome)PortableRemoteObject.narrow(obj, homeClass);
return home;
}
catch(NamingException ex)
{
throw new ServiceLocatorException();
}
}
}