当从具有相同参数的两个单独函数调用时,相同函数的工作方式不同

时间:2016-09-09 10:42:17

标签: java liferay

相同的函数originalStrutsPortletAction.processAction(originalStrutsPortletAction,portletConfig,actionRequest,actionResponse)在从一个函数调用时工作正常,但在从另一个函数调用时不工作。虽然两个调用中的参数值相同。代码如下: -

public class ExampleLoginAction extends BaseStrutsPortletAction implements Serializable{

    public static int flag=0;
    public  static StrutsPortletAction originalStrutsPortletAction=null;
    public  static PortletConfig portletConfig=null;
    public  static ActionRequest actionRequest=null;
    public  static ActionResponse actionResponse=null;
    String email;
    @Override
    public void processAction(StrutsPortletAction originalStrutsPortletAction,
            PortletConfig portletConfig, ActionRequest actionRequest,
            ActionResponse actionResponse) throws Exception {
        /**
         * This is the custom process action
         * Once you try to login this method will be invoked
         * We can write our own logic here
         * Invoke the original struts action at the end
         */
         ExampleLoginAction.actionRequest=actionRequest;
         ExampleLoginAction.originalStrutsPortletAction=originalStrutsPortletAction;
         ExampleLoginAction.portletConfig=portletConfig;
         ExampleLoginAction.actionResponse=actionResponse;

        System.out.println("#############ExampleLoginAction###############");
        //System.out.println(ParamUtil.getString(arg0, "login"));
        // TODO Auto-generated method stub
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        //SecurityManager security = System.getSecurityManager();
        //System.out.println("Security Manager" + security);
        Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("abhijain.cse@gmail.com","a17432475J@");
                }
            });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("adesh.pathak01@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(ParamUtil.getString(actionRequest, "login")));
            message.setSubject("Testing Subject");
            int otp=new Random().nextInt(10000);
            message.setText("Your OTP is " + otp);
            OTP otpdemo=OTPLocalServiceUtil.createOTP(CounterLocalServiceUtil.increment());
            otpdemo.setEmail(ParamUtil.getString(actionRequest, "login"));
            otpdemo.setPassword(String.valueOf(otp));
            OTPLocalServiceUtil.addOTP(otpdemo);
            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }

        //portletConfig.getPortletContext().getRequestDispatcher("/jsp/otp.jsp").forward(actionRequest, actionResponse);
        //actionResponse.setRenderParameter("email",ParamUtil.getString(actionRequest, "login"));
        //actionRequest.setAttribute("password",ParamUtil.getString(actionRequest, "password"));
        String email=ParamUtil.getString(actionRequest, "login");
        this.email=email;
        actionRequest.getPortletSession().setAttribute("email",email);
        HttpServletRequest request=PortalUtil.getHttpServletRequest(actionRequest);
        HttpSession session1=request.getSession();
        session1.setAttribute("loginAction", this);
        actionResponse.sendRedirect("/otpsample-hook/jsp/otp.jsp?email="+email+"&originalStrutsPortletAction="+originalStrutsPortletAction+"&actionRequest="+actionRequest+"&actionResponse="+actionResponse+"&portletConfig="+portletConfig+"&ExampleLoginAction="+this);
        System.out.println("In processAction"+originalStrutsPortletAction);
        System.out.println("In processAction"+portletConfig);
        System.out.println("In processAction"+actionRequest);
        System.out.println("In processAction"+actionResponse);
        System.out.println("bye:::::::::");
        //Thread.sleep(1000);
        System.out.println(flag);
        if(flag==1)
        {
            System.out.println("hello:::::::::");
        originalStrutsPortletAction.processAction(
            originalStrutsPortletAction, portletConfig, actionRequest,
           actionResponse);
        //flag=0;

        }
        //new MVCPortlet().getPortletContext().getRequestDispatcher("/jsp/otp.jsp").forward(actionRequest, actionResponse);


    }

    public String render(
            StrutsPortletAction originalStrutsPortletAction,
            PortletConfig portletConfig, RenderRequest renderRequest,
            RenderResponse renderResponse)
        throws Exception {

        /**
         * Our own render method
         * This method is for rendering the view
         * At the end call the original struts action
         */

        System.out.println("##########Rendering view############");
        renderResponse.getWriter().println("custom rendering of page");
       // return null;
        return originalStrutsPortletAction.render(
           null, portletConfig, renderRequest, renderResponse);
       // return null;

    }

    public static void check() throws Exception
    {
        System.out.println(":::::hi::::::");
        System.out.println(originalStrutsPortletAction);
        System.out.println(portletConfig);
        System.out.println(actionRequest);
        System.out.println(actionResponse);
        originalStrutsPortletAction.processAction(originalStrutsPortletAction,
                 portletConfig, actionRequest,
                        actionResponse);

    }
}

功能

originalStrutsPortletAction.processAction(originalStrutsPortletAction,
                 portletConfig, actionRequest,
                        actionResponse)

从processAction()调用时工作正常但是当它在check()方法中调用时,我得到空指针异常,尽管两者的参数值相同。我已经通过在两个函数中打印它们来检查参数值。它们是相同的。那么哪里可能是错误?我被困在其中一天但找不到解决方案。堆栈跟踪如下: -

java.lang.NullPointerException
    at com.liferay.portlet.login.action.LoginAction.login(LoginAction.java:196)
    at com.liferay.portlet.login.action.LoginAction.processAction(LoginAction.java:93)
    at com.liferay.portal.struts.StrutsPortletActionAdapter.processAction(StrutsPortletActionAdapter.java:60)
    at com.liferay.portal.kernel.struts.BaseStrutsPortletAction.processAction(BaseStrutsPortletAction.java:44)
    at com.abhishek.ExampleLoginAction.check(ExampleLoginAction.java:196)
    at org.apache.jsp.jsp.otpCheck_jsp._jspService(otpCheck_jsp.java:90)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:116)
    at sun.reflect.GeneratedMethodAccessor482.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.liferay.portal.kernel.bean.ClassLoaderBeanHandler.invoke(ClassLoaderBeanHandler.java:67)
    at com.sun.proxy.$Proxy642.doFilter(Unknown Source)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:116)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDirectCallFilter(InvokerFilterChain.java:165)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:96)
    at com.liferay.portal.kernel.servlet.PortalClassLoaderFilter.doFilter(PortalClassLoaderFilter.java:74)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDoFilter(InvokerFilterChain.java:204)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:109)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilter.doFilter(InvokerFilter.java:96)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

1 个答案:

答案 0 :(得分:0)

这可能是因为在每种情况下都会有一些不同的依赖关系被解决,具体取决于谁调用此方法。

在某些框架中,您有类似状态的东西,当请求到来时,服务器会初始化一些变量,例如特定用户的会话对象,您可以在其中存储一些信息。如果你从其他线程调用相同的方法而没有针对任何用户解析会话,那么你根本没有会话,它可能会失败。

但是这不仅限于会话,可能还有其他变量正在初始化(或不是),具体取决于谁调用该方法。