[jboss-user] [EJB 3.0] - Re: NullPointerException in JavaEEComponentHelper with JBoss
japase
do-not-reply at jboss.com
Mon Apr 27 03:22:49 EDT 2009
An example of a bean declaration and injection with some other typical annotations (all our generated beans are more or less same):
| package ifs.application.administrateuserprofiles;
|
| import ifs.fnd.log.*;
| import ifs.fnd.record.*;
| import ifs.fnd.record.serialization.*;
| import ifs.fnd.remote.*;
| import ifs.fnd.remote.j2ee.*;
| import ifs.fnd.sf.j2ee.*;
| import ifs.fnd.sf.j2ee.meta.*;
| import ifs.fnd.base.*;
| import ifs.fnd.sf.*;
| import ifs.application.manageuserprofile.*;
|
| import java.util.Date;
| import javax.ejb.Stateless;
| import javax.ejb.EJB;
| import javax.annotation.PostConstruct;
| import javax.ejb.TransactionAttribute;
| import javax.ejb.TransactionAttributeType;
|
| @Stateless(name="AdministrateUserProfiles")
| public class AdministrateUserProfilesBean extends FndActivityBean implements AdministrateUserProfilesLocal {
|
| private AdministrateUserProfiles implementation;
|
| @EJB(mappedName="ifsapp/UserProfileService/local") private UserProfileServiceLocal userProfileService;
|
| @PostConstruct
| private void initialize() {
| // some code goes here â¦
| }
|
| @Override
| public String[] getOperations() {
| return operations;
| }
|
| @TransactionAttribute(TransactionAttributeType.REQUIRED)
| public FndRecordResultWrapper clearClientProfile(ifs.application.manageuserprofile.ClientProfileExt inRecord, FndContext ctx) throws IfsException {
| // method code hereâ¦
| }
|
| // other methods
| }
|
All the generated beans extend indirectly the following class (complete code):
| package ifs.fnd.sf.j2ee;
|
| import ifs.fnd.base.SystemException;
| import ifs.fnd.base.IfsException;
| import ifs.fnd.base.FndConstants;
| import ifs.fnd.base.FndContext;
| import ifs.fnd.base.FndFramework;
| import ifs.fnd.log.*;
| //import ifs.fnd.service.IfsProperties;
|
| import javax.annotation.Resource;
| import javax.ejb.SessionContext;
| import javax.interceptor.AroundInvoke;
| import javax.interceptor.InvocationContext;
| import javax.sql.DataSource;
| import javax.annotation.security.DeclareRoles;
| import javax.annotation.security.RolesAllowed;
|
| /**
| * Base class for all EJB classes.
| */
| @DeclareRoles({FndConstants.USER_ROLE, FndConstants.TRUSTED_MODULE_ROLE})
| @RolesAllowed({FndConstants.USER_ROLE})
| abstract class FndAbstractBean {
|
| //private static final String DATASOURCE_JNDI_PREFIX = IfsProperties.getProperty("fndext.datasource.jndi.prefix");
|
| // this logger may be used during initialization of a bean; it is then recreated by @AroundInvoke method
| protected Logger log;
|
| // logger used for debugging of EJB specific events
| protected Logger clsLog;
|
| protected FndAbstractBean() {
| log = LogMgr.getFrameworkLogger();
| clsLog = LogMgr.getClassLogger(FndAbstractBean.class);
| if(clsLog.debug)
| clsLog.debug("Created bean [&1]", getClass().getName());
| }
|
| @Resource
| protected SessionContext sessionContext;
|
| @Resource(name="fndbas_en-US", mappedName="java:jdbc/fndbas_en-US")
| private DataSource dsEn;
|
| @Resource(name="fndbas-noxa_en-US", mappedName="java:jdbc/fndbas-noxa_en-US")
| private DataSource dsNoXaEn;
|
| public DataSource getDataSource(String name) throws SystemException {
| if("fndbas_en-US".equals(name))
| return dsEn;
| else if("fndbas-noxa_en-US".equals(name))
| return dsNoXaEn;
| throw new SystemException("No dependency to DataSource " + name);
| }
|
| protected SessionContext getSessionContext() {
| return sessionContext;
| }
|
| @AroundInvoke
| private Object aroundInvoke(InvocationContext ctx) throws Exception {
|
| // recreate the bean-protected instance of logger
| log = LogMgr.getFrameworkLogger();
|
| // prepare current FndContext for invocation of a business method
| FndJ2eeContext fndctx;
| if(this instanceof FndActivityBean) {
| Object[] params = ctx.getParameters();
| Object lastParam = params[params.length - 1];
| if(lastParam instanceof FndContext) {
| // typed invoke(): set passed FndContext as current context
| fndctx = (FndJ2eeContext) lastParam;
| setCallerAsApplicationUser(fndctx);
| FndContext.setCurrentContext(fndctx);
| }
| else {
| // untyped invoke(): create new FndContext and set it as current context
| fndctx = (FndJ2eeContext) FndFramework.getFramework().newContext();
| FndContext.setCurrentContext(fndctx);
| }
| }
| else {
| // for nested beans the current FndContext has been already set
| fndctx = FndJ2eeContext.getCurrentJ2eeContext();
| }
|
| // push this bean to the stack with current bens
| fndctx.pushCurrentBean(this);
| if(clsLog.info)
| clsLog.info("Pushed current bean [&1]", getClass().getName());
|
| try {
| // invoke business method
| return ctx.proceed();
| }
| finally {
| // pop this bean from the stack with current bens
| fndctx.popCurrentBean(this);
| if(log.debug)
| log.debug("Poped current bean [&1]", getClass().getName());
|
| // clear the current context after top-level bean method invocation
| if(this instanceof FndActivityBean) {
| FndContext.setCurrentContext(null);
| if(log.debug)
| log.debug("Current context cleared on exit from bean [&1]", getClass().getName());
| }
| }
| }
|
| /**
| * Sets the authenticated user as application user in the specified context.
| */
| protected void setCallerAsApplicationUser(FndJ2eeContext ctx) throws IfsException {
| try {
| String user = sessionContext.getCallerPrincipal().getName();
| ctx.setApplicationUser(user);
| if(log.debug)
| log.debug("Current application user set to authenticated user: &1", user);
| }
| catch (IllegalStateException e) {
| throw new ifs.fnd.base.SecurityException(e,
| "FNDSESSIONBEANSEC:No security context set. Check that security has been configured for the application.");
| }
| }
|
| }
|
There are some other levels of inheritance between a generated EJB and the FndAbstractBean class above, but without anything specific for EJB (I think). But, of course, I can post the code for those classes as well, if necessary.
View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4226913#4226913
Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4226913
More information about the jboss-user
mailing list