Custom OgnlValueStack in Struts 2

1k views Asked by At

I want to implement a custom ValueStack in my application by extending the OgnlValueStack class of Struts 2.3.x.

Please let me know how to accomplish this. What classes do I need to extend and implement in my application and how to inject different dependencies using the @Inject annotation?

Update

I have made the changes as suggested earlier. My ValueStackFactory implementation is:

package jp.co.spectrum.insight.core.mvc.factory;

import java.util.Map;
import java.util.Set;

import jp.co.spectrum.insight.core.datamodel.InsightValueStackImpl;
import ognl.MethodAccessor;
import ognl.OgnlRuntime;
import ognl.PropertyAccessor;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.TextProvider;
import com.opensymphony.xwork2.conversion.NullHandler;
import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.ognl.OgnlNullHandlerWrapper;
import com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor;
import com.opensymphony.xwork2.util.CompoundRoot;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.ValueStackFactory;

public class InsightValueStackFactory implements ValueStackFactory {
    @Inject
    private XWorkConverter xworkConverter;

    private CompoundRootAccessor compoundRootAccessor;

    @Inject("system")
    private TextProvider textProvider;

    @Inject
    private Container container;

    private boolean allowStaticMethodAccess;

    /*
    private static ValueStackFactory instance;

    public static ValueStackFactory getInstance() {
        if (instance == null) {
            instance = new InsightValueStackFactory();
        }
        return instance;
    }
    */

    private static ValueStackFactory factory = new InsightValueStackFactory();

    public static void setFactory(ValueStackFactory factoryParam) {
        factory = factoryParam;
    }

    public static ValueStackFactory getFactory() {
        return factory;
    }


    public void setXWorkConverter(XWorkConverter conv) {
        this.xworkConverter = conv;
    }

    public void setTextProvider(TextProvider textProvider) {
        this.textProvider = textProvider;
    }

    @Inject(value="allowStaticMethodAccess", required=true)
public void  setAllowStaticMethodAccess(String allowStaticMethodAccess) {
         this.allowStaticMethodAccess = "true".equalsIgnoreCase(allowStaticMethodAccess);
     }

     public ValueStack  createValueStack() {
         ValueStack stack = new InsightValueStackImpl(xworkConverter, compoundRootAccessor, textProvider, allowStaticMethodAccess);
         container.inject(stack);
         stack.getContext().put(ActionContext.CONTAINER, container);
         return stack;
     }
     public ValueStack createValueStack(ValueStack stack) {
         ValueStack result = new InsightValueStackImpl(stack, xworkConverter, compoundRootAccessor,  allowStaticMethodAccess);
         container.inject(result);
         stack.getContext().put(ActionContext.CONTAINER, container);
         return result;
     }


     public void  setContainer(Container container) throws ClassNotFoundException {
         Set<String> names = container.getInstanceNames(PropertyAccessor.class);
         for (String name : names) {
             Class cls = Class.forName(name);
             if (cls != null) {
                 if (Map.class.isAssignableFrom(cls)) {
                     PropertyAccessor acc = container.getInstance(PropertyAccessor.class, name);
                 }
                 OgnlRuntime.setPropertyAccessor(cls, container.getInstance(PropertyAccessor.class, name));
                 if (compoundRootAccessor == null && CompoundRoot.class.isAssignableFrom(cls)) {
                     compoundRootAccessor = (CompoundRootAccessor) container.getInstance(PropertyAccessor.class, name);
                 }
             }
         }
         names = container.getInstanceNames(MethodAccessor.class);
         for (String name : names) {
             Class cls = Class.forName(name);
             if (cls != null) {
                 OgnlRuntime.setMethodAccessor(cls, container.getInstance(MethodAccessor.class, name));
             }
         }
         names = container.getInstanceNames(NullHandler.class);
         for (String name : names) {
             Class cls = Class.forName(name);
             if (cls != null) {
                 OgnlRuntime.setNullHandler(cls, new OgnlNullHandlerWrapper(container.getInstance(NullHandler.class, name)));
             }
         }
         if (compoundRootAccessor == null) {
             throw new IllegalStateException("Couldn't find the compound root accessor");
         }
         this.container = container;
     }
}

The InsightValueStackImpl class is my customized ValueStack and it extends the OgnlValueStack.

After the changes as suggested earlier, when I start the application, I get the following error:

java.lang.IllegalArgumentException: Wrapped type converter cannot be null
    at com.opensymphony.xwork2.ognl.OgnlTypeConverterWrapper.<init>(OgnlTypeConverterWrapper.java:32)
    at com.opensymphony.xwork2.ognl.OgnlValueStack.setRoot(OgnlValueStack.java:88)
    at com.opensymphony.xwork2.ognl.OgnlValueStack.<init>(OgnlValueStack.java:71)
    at jp.co.spectrum.insight.core.datamodel.InsightValueStackImpl.<init>(InsightValueStackImpl.java:86)
    at jp.co.spectrum.insight.core.mvc.factory.InsightValueStackFactory.createValueStack(InsightValueStackFactory.java:85)
    at jp.co.spectrum.insight.core.mvc.dispatcher.InsightFilterDispatcher.<init>(InsightFilterDispatcher.java:118)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at java.lang.Class.newInstance0(Class.java:355)
    at java.lang.Class.newInstance(Class.java:308)
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275)
    at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:422)
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:115)
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4072)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4726)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
    at org.apache.catalina.core.StandardService.start(StandardService.java:525)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)   

This is because of null XWorkConverter instance.

Please let me know why it is not getting injected.

Thanks in advance

Thanks


I have made the changes as suggested earlier. My ValueStackFactory implementation is:

package jp.co.spectrum.insight.core.mvc.factory;

import java.util.Map;
import java.util.Set;

import jp.co.spectrum.insight.core.datamodel.InsightValueStackImpl;
import ognl.MethodAccessor;
import ognl.OgnlRuntime;
import ognl.PropertyAccessor;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.TextProvider;
import com.opensymphony.xwork2.conversion.NullHandler;
import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.ognl.OgnlNullHandlerWrapper;
import com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor;
import com.opensymphony.xwork2.util.CompoundRoot;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.ValueStackFactory;


public class InsightValueStackFactory implements ValueStackFactory {
     @Inject
     private XWorkConverter xworkConverter ;
     private CompoundRootAccessor compoundRootAccessor  ;
     @Inject("system")
     private TextProvider textProvider ;
     @Inject
     private Container container;

     private boolean allowStaticMethodAccess;


      /*private static ValueStackFactory instance;
      public static ValueStackFactory getInstance(){
         if(instance==null){
             instance = new InsightValueStackFactory();
         }
         return instance;
     }*/

     private static ValueStackFactory factory = new InsightValueStackFactory();

     public static void setFactory(ValueStackFactory factoryParam) {
      factory = factoryParam;
     }

    public static ValueStackFactory getFactory() {
      return factory;
    }


     public void  setXWorkConverter(XWorkConverter conv) {
         this.xworkConverter = conv;

     }

     public void  setTextProvider(TextProvider textProvider) {
         this.textProvider = textProvider;
     }
     @Inject(value="allowStaticMethodAccess", required=true)
     public void  setAllowStaticMethodAccess(String allowStaticMethodAccess) {
         this.allowStaticMethodAccess = "true".equalsIgnoreCase(allowStaticMethodAccess);
     }

     public ValueStack  createValueStack() {
         ValueStack stack = new InsightValueStackImpl(xworkConverter, compoundRootAccessor, textProvider, allowStaticMethodAccess);
         container.inject(stack);
         stack.getContext().put(ActionContext.CONTAINER, container);
         return stack;
     }
     public ValueStack createValueStack(ValueStack stack) {
         ValueStack result = new InsightValueStackImpl(stack, xworkConverter, compoundRootAccessor,  allowStaticMethodAccess);
         container.inject(result);
         stack.getContext().put(ActionContext.CONTAINER, container);
         return result;
     }


     public void  setContainer(Container container) throws ClassNotFoundException {
         Set<String> names = container.getInstanceNames(PropertyAccessor.class);
         for (String name : names) {
             Class cls = Class.forName(name);
             if (cls != null) {
                 if (Map.class.isAssignableFrom(cls)) {
                     PropertyAccessor acc = container.getInstance(PropertyAccessor.class, name);
                 }
                 OgnlRuntime.setPropertyAccessor(cls, container.getInstance(PropertyAccessor.class, name));
                 if (compoundRootAccessor == null && CompoundRoot.class.isAssignableFrom(cls)) {
                     compoundRootAccessor = (CompoundRootAccessor) container.getInstance(PropertyAccessor.class, name);
                 }
             }
         }
         names = container.getInstanceNames(MethodAccessor.class);
         for (String name : names) {
             Class cls = Class.forName(name);
             if (cls != null) {
                 OgnlRuntime.setMethodAccessor(cls, container.getInstance(MethodAccessor.class, name));
             }
         }
         names = container.getInstanceNames(NullHandler.class);
         for (String name : names) {
             Class cls = Class.forName(name);
             if (cls != null) {
                 OgnlRuntime.setNullHandler(cls, new OgnlNullHandlerWrapper(container.getInstance(NullHandler.class, name)));
             }
         }
         if (compoundRootAccessor == null) {
             throw new IllegalStateException("Couldn't find the compound root accessor");
         }
         this.container = container;
     }
}

The InsightValueStackImpl class is my customized ValueStack and it extends the OgnlValueStack.

After the changes as suggested earlier, when I start the application, I get the following error:

java.lang.IllegalArgumentException: Wrapped type converter cannot be null at com.opensymphony.xwork2.ognl.OgnlTypeConverterWrapper.(OgnlTypeConverterWrapper.java:32) at com.opensymphony.xwork2.ognl.OgnlValueStack.setRoot(OgnlValueStack.java:88) at com.opensymphony.xwork2.ognl.OgnlValueStack.(OgnlValueStack.java:71) at jp.co.spectrum.insight.core.datamodel.InsightValueStackImpl.(InsightValueStackImpl.java:86) at jp.co.spectrum.insight.core.mvc.factory.InsightValueStackFactory.createValueStack(InsightValueStackFactory.java:85) at jp.co.spectrum.insight.core.mvc.dispatcher.InsightFilterDispatcher.(InsightFilterDispatcher.java:118) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at java.lang.Class.newInstance0(Class.java:355) at java.lang.Class.newInstance(Class.java:308) at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275) at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:422) at org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:115) at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4072) at org.apache.catalina.core.StandardContext.start(StandardContext.java:4726) at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057) at org.apache.catalina.core.StandardHost.start(StandardHost.java:840) at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057) at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463) at org.apache.catalina.core.StandardService.start(StandardService.java:525) at org.apache.catalina.core.StandardServer.start(StandardServer.java:754) at org.apache.catalina.startup.Catalina.start(Catalina.java:595) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)

This is because of null XWorkConverter instance.

Please let me know why it is not getting injected.

Thanks in advance

1

There are 1 answers

1
Steven Benitez On

You need to implement a ValueStackFactory and register it in your struts.xml, as follows:

<bean type="com.opensymphony.xwork2.util.ValueStackFactory"
      name="yourOgnlValueStackFactory"
      class="com.example.YourOgnlValueStackFactory" />

Then, set your implementation as the factory to use, using:

<constant name="struts.valueStackFactory" value="yourOgnlValueStackFactory"/>

Update

I'm not sure if you are able to mix field and method injection like you are doing. Try moving the @Inject annotations back to the setter methods and see if that resolves the issue.