Session Bean
Files List:
BaseSeesionBean.java
which extnds javax.ejb.seesionbean, main include some normal method but not include ejbCreate(), which is in another specail file.
code:
package base;
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import org.apache.log4j.Logger;
public class BaseSessionBean implements SessionBean {
/** Log4j instance for actual implementation class */
public transient Logger log;
private SessionContext ctx;
/** Cached initial context to save JNDI lookups */
transient InitialContext cacheCtx = null;
/**
* Initializes logging mechanism per instance
*/
public BaseSessionBean() {
log = Logger.getLogger( this.getClass());
}
/**
* Logs a message with priority TRACE
*
* @param msg Message
*/
public void trace(String msg) {
log.trace(msg);
}
/**
* Logs a message and an exception with priority TRACE
*
* @param msg Message
* @param t Exception
*/
public void trace(String msg, Throwable t) {
log.trace(msg, t);
}
/**
* Logs a message with priority DEBUG
*
* @param msg Message
*/
public void debug(String msg) {
log.debug(msg);
}
/**
* Logs a message and an exception with priority DEBUG
*
* @param msg Message
* @param t Exception
*/
public void debug(String msg, Throwable t) {
log.debug(msg, t);
}
/**
* Logs a message with priority INFO
*
* @param msg Message
*/
public void info(String msg) {
log.info(msg);
}
/**
* Logs a message and an exception with priority INFO
*
* @param msg Message
* @param t Exception
*/
public void info(String msg, Throwable t) {
log.info(msg, t);
}
/**
* Logs a message with priority WARN
*
* @param msg Message
*/
public void warn(String msg) {
log.warn(msg);
}
/**
* Logs a message and an exception with priority WARN
*
* @param msg Message
* @param t Exception
*/
public void warn(String msg, Throwable t) {
log.warn(msg, t);
}
/**
* Logs a message with priority ERROR
*
* @param msg Message
*/
public void error(String msg) {
log.error(msg);
}
/**
* Logs a message and an exception with priority ERROR
*
* @param msg Message
* @param t Exception
*/
public void error(String msg, Throwable t) {
log.error(msg, t);
}
@Override
public void ejbActivate() throws EJBException, RemoteException {
log = Logger.getLogger( this.getClass());
}
@Override
public void ejbPassivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
@Override
public void ejbRemove() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
@Override
public void setSessionContext( final javax.ejb.SessionContext ctx) throws EJBException,
RemoteException {
this.ctx = ctx;
}
/**
* Get session contect
*
* @return current session context
*/
public SessionContext getSessionContext() {
return ctx;
}
/**
* return the environment entries locator
* @return return the environment entries locator
*/
protected ServiceLocator getLocator() {
return ServiceLocator.getInstance();
}
}
ServiceLocator.java
Tool class for gethome interface of remote or local, and some others.
package base;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.ejb.EJBHome;
import javax.ejb.EJBLocalHome;
import javax.mail.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameNotFoundException;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.sql.DataSource;
public class ServiceLocator {
/** ejb home cache */
@SuppressWarnings( "unchecked")
private transient Map ejbHomes = Collections.synchronizedMap( new HashMap());
/** the jndi context */
private transient Context ctx;
/** the singleton instance */
private static transient ServiceLocator instance;
/**
* Create a new service locator object.
* @throws ServiceLocatorException if the context failed to be initialized
*/
private ServiceLocator() throws ServiceLocatorException {
try {
this.ctx = new InitialContext();
} catch (NamingException e){
throw new ServiceLocatorException(e);
}
}
/**
* return the singleton instance
* @return the singleton instance
* @throws ServiceLocatorException if the instance could not be initialized the first time
*/
public static final ServiceLocator getInstance() throws ServiceLocatorException {
// synchronization is intentionally left out. It 'should' not have dramatic
// consequences as it is not that destructive.
if (instance == null){
instance = new ServiceLocator();
}
return instance;
}
/**
* return the ejb local home.
* clients need to cast to the type of EJBHome they desire
* @param jndiHomeName the jndi home name matching the requested local home.
* @return the Local EJB Home corresponding to the home name
*/
public EJBLocalHome getLocalHome(String jndiHomeName) throws ServiceLocatorException {
EJBLocalHome home = (EJBLocalHome)ejbHomes.get(jndiHomeName);
if (home == null) {
home = (EJBLocalHome)getObject(jndiHomeName);
ejbHomes.put(jndiHomeName, home);
}
return home;
}
/**
* return the ejb remote home.
* clients need to cast to the type of EJBHome they desire
* @param jndiHomeName the jndi home name matching the requested remote home.
* @return the Local EJB Home corresponding to the home name
*/
public EJBHome getRemoteHome(String jndiHomeName, Class className) throws ServiceLocatorException {
EJBHome home = (EJBHome)ejbHomes.get(className);
if (home == null) {
Object objref = getObject(jndiHomeName);
home = (EJBHome) PortableRemoteObject.narrow(objref, className);
ejbHomes.put(className, home);
}
return home;
}
/**
* return the datasource object corresponding the the env entry name
* @return the DataSource corresponding to the env entry name parameter
* @throws ServiceLocatorException if the lookup fails
*/
public DataSource getDataSource(String dataSourceName) throws ServiceLocatorException {
return (DataSource)getObject(dataSourceName);
}
/**
* return the URL object corresponding to the env entry name
* @param envName the env entry name
* @return the URL value corresponding to the env entry name.
* @throws ServiceLocatorException if the lookup fails
*/
public URL getUrl(String envName) throws ServiceLocatorException {
return (URL)getObject(envName);
}
/**
* return a boolean value corresponding to the env entry
* @param envName the env entry name
* @return the boolean value corresponding to the env entry.
* @throws ServiceLocatorException if the lookup fails
*/
public boolean getBoolean(String envName) throws ServiceLocatorException {
return ((Boolean)getObject(envName)).booleanValue();
}
/**
* return a string value corresponding to the env entry
* @param envName the env entry name
* @return the boolean value corresponding to the env entry.
* @throws ServiceLocatorException if the lookup fails
*/
public String getString(String envName) throws ServiceLocatorException {
String ret = null;
try {
ret = (String)getObject(envName);
} catch (ServiceLocatorException e) {
if (e.getCause() instanceof NameNotFoundException) {
// ignore this and return null, otherwise we can not have empty values in Glassfish
ret = null;
}
}
return ret;
}
/**
* return a mail session corresponding to the env entry
* @param envName the env entry name
* @return the mail session corresponding to the env entry.
* @throws ServiceLocatorException if the lookup fails
*/
public Session getMailSession(String envName) throws ServiceLocatorException {
return (Session)getObject(envName);
}
/**
* return a known java object corresponding to the env entry
* @param envName the env entry name
* @return the java object corresponding to the env entry
* @throws ServiceLocatorException if the lookup fails
*/
public Object getObject(String envName) throws ServiceLocatorException {
try {
return ctx.lookup(envName);
} catch (NamingException e) {
throw new ServiceLocatorException(e);
}
}
}
LocalUserAdminSessionBean.java
Mian Biz class, include ejbCreate emthod.
package ejbBeans;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import base.BaseSessionBean;
public class LocalUserAdminSessionBean extends BaseSessionBean{
/**
*
*/
private static final long serialVersionUID = 5028163905841527500L;
private IUserAdminSessionLocal useradminsession;
/**
* Default create for SessionBean.
*
* @throws CreateException if bean instance can't be created
* @see org.ejbca.core.model.log.Admin
*/
public void ejbCreate() throws CreateException {
// try {
//
// IUserAdminSessionLocalHome useradminsessionhome = (IUserAdminSessionLocalHome) getLocator().getLocalHome(IUserAdminSessionLocalHome.COMP_NAME);
// useradminsession = useradminsessionhome.create();
//
// } catch (Exception e) {
// error("Error creating session bean:", e);
// throw new EJBException(e);
// }
}
public void setDaniel() throws RemoteException{
// useradminsession.setString("daniel");
}
public String setString(java.lang.String str ){
System.out.println(str);
return str+str;
}
}
UserAdminSessionSession
package ejbBeans;
public class UserAdminSessionSession extends LocalUserAdminSessionBean
implements javax.ejb.SessionBean {
/**
*
*/
private static final long serialVersionUID = 1964534241710585774L;
public void ejbActivate() throws javax.ejb.EJBException,
java.rmi.RemoteException {
super.ejbActivate();
}
public void ejbPassivate() throws javax.ejb.EJBException,
java.rmi.RemoteException {
super.ejbPassivate();
}
public void setSessionContext(javax.ejb.SessionContext ctx)
throws javax.ejb.EJBException, java.rmi.RemoteException {
super.setSessionContext(ctx);
}
public void unsetSessionContext() {
}
public void ejbRemove() throws javax.ejb.EJBException,
java.rmi.RemoteException {
super.ejbRemove();
}
}
IUserAdminSessionHome.java
/**
*
*/
package ejbBeans;
/**
* Home interface for UserAdminSession.
*/
public interface IUserAdminSessionHome
extends javax.ejb.EJBHome
{
public static final String COMP_NAME= "java:comp/env/ejb/UserAdminSession";
public static final String JNDI_NAME= "UserAdminSession";
public IUserAdminSessionRemote create()
throws javax.ejb.CreateException,java.rmi.RemoteException;
}
IUserAdminSessionLocal.java
package ejbBeans;
import javax.ejb.EJBLocalObject;
public interface IUserAdminSessionLocal extends EJBLocalObject {
public String setString(java.lang.String str ) ;
}
IUserAdminSessionLocalHome.java
package ejbBeans;
import javax.ejb.EJBLocalHome;
public interface IUserAdminSessionLocalHome extends EJBLocalHome {
public static final String COMP_NAME= "java:comp/env/ejb/UserAdminSessionLocal";
public static final String JNDI_NAME= "UserAdminSessionLocal";
public IUserAdminSessionLocal create()
throws javax.ejb.CreateException;
}
IUserAdminSessionRemote.java
package ejbBeans;
import javax.ejb.EJBObject;
public interface IUserAdminSessionRemote extends EJBObject {
public String setString(java.lang.String str ) throws java.rmi.RemoteException;
}
Deploy XMl
ejb-jar.xml
<? xml version ="1.0" encoding ="UTF-8" ?>
< ejb-jar id ="ejb-jar_ID" version ="2.1" xmlns ="http://java.sun.com/xml/ns/j2ee" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd" >
< display-name >ejb21 </ display-name >
< enterprise-beans >
<!-- Session Beans -->
<!-- UserAdminSession bean -->
< session id ="Session_UserAdminSession" >
< description ><![CDATA[Administrates users in the database using UserData Entity Bean.]]> </ description >
< ejb-name >UserAdminSession </ ejb-name >
< home >ejbBeans.IUserAdminSessionHome </ home >
< remote >ejbBeans.IUserAdminSessionRemote </ remote >
< local-home >ejbBeans.IUserAdminSessionLocalHome </ local-home >
< local >ejbBeans.IUserAdminSessionLocal </ local >
< ejb-class >ejbBeans.UserAdminSessionSession </ ejb-class >
< session-type >Stateless </ session-type >
< transaction-type >Container </ transaction-type >
< env-entry >
< env-entry-name >DataSource </ env-entry-name >
< env-entry-type >java.lang.String </ env-entry-type >
< env-entry-value ><![CDATA[java:/EjbcaDS]]> </ env-entry-value >
</ env-entry >
< ejb-local-ref id ="EJBLocalRef_48" >
< ejb-ref-name >ejb/UserAdminSessionLocal </ ejb-ref-name >
< ejb-ref-type >Session </ ejb-ref-type >
< local-home >ejbBeans.IUserAdminSessionHome </ local-home >
< local >ejbBeans.IUserAdminSessionLocal </ local >
< ejb-link >UserAdminSession </ ejb-link >
</ ejb-local-ref >
</ session >
<!-- UserAdminSession bean -->
< session id ="Session_RaAdminSession" >
< description ><![CDATA[Session bean handling core CA function,signing certificates]]> </ description >
< display-name >RaAdminSB </ display-name >
< ejb-name >RaAdminSession </ ejb-name >
< home >RABeans.IRaAdminSessionHome </ home >
< remote >RABeans.IRaAdminSessionRemote </ remote >
< local-home >RABeans.IRaAdminSessionLocalHome </ local-home >
< local >RABeans.IRaAdminSessionLocal </ local >
< ejb-class >RABeans.RaAdminSessionSession </ ejb-class >
< session-type >Stateless </ session-type >
< transaction-type >Container </ transaction-type >
</ session >
</ enterprise-beans >
< assembly-descriptor >
< container-transaction id ="MethodTransaction_273" >
< method id ="MethodElement_297" >
< ejb-name >UserAdminSession </ ejb-name >
< method-name >* </ method-name >
</ method >
< trans-attribute >Required </ trans-attribute >
</ container-transaction >
< container-transaction id ="MethodTransaction_305" >
< method id ="MethodElement_329" >
< ejb-name >UserAdminSession </ ejb-name >
< method-intf >Local </ method-intf >
< method-name >setString </ method-name >
< method-params >
< method-param >java.lang.String </ method-param >
</ method-params >
</ method >
< trans-attribute >Supports </ trans-attribute >
</ container-transaction >
< container-transaction id ="MethodTransaction_104" >
< method id ="MethodElement_128" >
< ejb-name >RaAdminSession </ ejb-name >
< method-name >* </ method-name >
</ method >
< trans-attribute >Required </ trans-attribute >
</ container-transaction >
</ assembly-descriptor >
</ ejb-jar >
Jboss.xml
<? xml version ="1.0" encoding ="UTF-8" ?>
<!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 3.2//EN" "http://www.jboss.org/j2ee/dtd/jboss_3_2.dtd">
< jboss >
< unauthenticated-principal >nobody </ unauthenticated-principal >
< enterprise-beans >
< session >
< ejb-name >UserAdminSession </ ejb-name >
<!-- jdni-name 主要是处理多个ejb组件同处一个ejb-jar.xml文件时候的jndi名字到ejb名的重定向 -->
< jndi-name >UserAdminSession </ jndi-name >
< local-jndi-name >UserAdminSessionLocal </ local-jndi-name >
< method-attributes >
</ method-attributes >
</ session >
< session >
< ejb-name >RaAdminSession </ ejb-name >
< jndi-name >RaAdminSession </ jndi-name >
< local-jndi-name >RaAdminSessionLocal </ local-jndi-name >
< method-attributes >
</ method-attributes >
</ session >
</ enterprise-beans >
</ jboss >
Entity Bean
Similar with Sessionbean, main have 5 files,
BaseEntityBean.java
Include some common method except ejbCreate(), and some tool method.
UserDataBean.java
Extends BaseEntityBean, an abstract class, implments some getter and setter which have identity in UserDataLocal.java , include mian class ejbCreate().
UserDataLocal.java
Extends EJBLocalObject, an interface file, identity some method for user.
UserDataLocalHome.java
Extends EJBLocalHome, an interface , identity COMP_NAME and JDNI_NAME and include an main Create() method.