如何在WCS的处理程序类中使用getCommandContext()?

时间:2015-10-23 14:15:56

标签: java websphere-commerce wcs

我使用以下代码从使用命令上下文

的处理程序类执行命令
CheckUserInMemberGroupCmd checkGrpCmd = (CheckUserInMemberGroupCmd) 
                            CommandFactory.createCommand(
                                    CheckUserInMemberGroupCmd.Name, 
                                    Integer.valueOf(storeId));
            checkGrpCmd.setUser(memberId);
            checkGrpCmd.setMemberGroupName(mbrName);
            checkGrpCmd.setCommandContext(getCommandContext());
            checkGrpCmd.execute();

我在同一个处理程序类中显式调用方法getCommandContext(),结果为null,因此抛出了NullPointerException。

public CommandContext getCommandContext()
{
  String METHODNAME = "getCommandContext";
  if (this.viewCommandContext != null) {
    ECTrace.trace(0L, super.getClass().getName(), "getCommandContext", "use viewCommandContext");
    return this.viewCommandContext;
  }
  ECTrace.trace(0L, super.getClass().getName(), "getCommandContext", "use commandContext if any");
  return this.commandContext;
}

现在,我该怎么做才能使这段代码可执行?

2 个答案:

答案 0 :(得分:0)

我猜想你的处理程序类中的命令上下文是null。必须从使用它的命令在处理程序上设置命令上下文。如果缺少调用将其设置在调用堆栈的任何位置,您将丢失该引用。尝试调试并追逐堆栈,直到找到设置它的命令。

答案 1 :(得分:0)

    CommandContext commandContext = null;
    BusinessContextService bcs = null;
    Boolean localBinding = Boolean.valueOf(false);

    try {
        Object localBindingValue = request.getAttribute("com.ibm.commerce.foundation.inLocalBinding");
        localBinding = Boolean.valueOf(Boolean.TRUE.equals(localBindingValue));
        bcs = BusinessContextServiceFactory.getBusinessContextService();
        ActivityData activityData = getTempActivityData(businessContext);
        ActivityToken activityToken = getActivityToken(bcs, activityData,activityTokenCallbackHandler);
        bcs.startRequest(activityToken, activityData);
        commandContext = ContextHelper.createCommandContext(activityToken);
        logger.logp(Level.INFO, CLASSNAME, methodName, "Sap1 context" + commandContext.getCurrency());
        logger.logp(Level.INFO, CLASSNAME, methodName, "Sap2 context" + commandContext);
    }catch(Exception ex){
        logger.logp(Level.FINE, CLASSNAME, methodName, "command context is null");
    }finally {
        if ((!(localBinding.booleanValue())) && (bcs != null)) {
            bcs.flushCache();
        }
    }

处理方法: getCommandContext(this.businessContext,this.activityTokenCallbackHandler,this.request)