Uploaded image for project: 'Struts 2'
  1. Struts 2
  2. WW-4410

Custom configuration providers accessing servlet-context

    Details

      Description

      I'm using custom configuration providers following the suggestion of this post.

      I figured out that my custom configuration provider needed access to the servlet-context so I patched the code in the Dispatcher class to let, in case the constructor is available, instantiate a ConfigurationProvider with the servlet-context as argument

      private void init_CustomConfigurationProviders() {
      	String configProvs = initParams.get("configProviders");
      	if (configProvs != null) {
      		String[] classes = configProvs.split("\\s*[,]\\s*");
      		for (String cname : classes) {
      			try {
      				Class cls = ClassLoaderUtil.loadClass(cname, this.getClass());
      				
      				ConfigurationProvider prov;
      				try {
      					Constructor constructor = cls.getConstructor(ServletContext.class);
      					prov = (ConfigurationProvider)constructor.newInstance(servletContext);
      				} catch (Exception e) {
      					prov = (ConfigurationProvider)cls.newInstance();	
      				}
      				configurationManager.addContainerProvider(prov);
      			} catch (InstantiationException e) {
      				throw new ConfigurationException("Unable to instantiate provider: "+cname, e);
      			} catch (IllegalAccessException e) {
      				throw new ConfigurationException("Unable to access provider: "+cname, e);
      			} catch (ClassNotFoundException e) {
      				throw new ConfigurationException("Unable to locate provider class: "+cname, e);
      			}
      		}
      	}
      }
      

        Activity

        Hide
        hudson Hudson added a comment -

        SUCCESS: Integrated in Struts-JDK6-support-2.3 #981 (See https://builds.apache.org/job/Struts-JDK6-support-2.3/981/)
        WW-4410 Adds dedicated interface to allow inject ServletContext (lukaszlenart: rev 9860ef829a95428be4fd2638f984b830855e5812)

        • core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java
        • core/src/main/java/com/opensymphony/xwork2/config/ServletContextAwareConfigurationProvider.java
        Show
        hudson Hudson added a comment - SUCCESS: Integrated in Struts-JDK6-support-2.3 #981 (See https://builds.apache.org/job/Struts-JDK6-support-2.3/981/ ) WW-4410 Adds dedicated interface to allow inject ServletContext (lukaszlenart: rev 9860ef829a95428be4fd2638f984b830855e5812) core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java core/src/main/java/com/opensymphony/xwork2/config/ServletContextAwareConfigurationProvider.java
        Hide
        hudson Hudson added a comment -

        SUCCESS: Integrated in Struts-JDK7-master #431 (See https://builds.apache.org/job/Struts-JDK7-master/431/)
        WW-4410 Adds dedicated interface to allow inject ServletContext (lukaszlenart: rev 4ea8883ff9d81ab93a5360e17a37fc57453f56c4)

        • core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java
        • core/src/main/java/com/opensymphony/xwork2/config/ServletContextAwareConfigurationProvider.java
        Show
        hudson Hudson added a comment - SUCCESS: Integrated in Struts-JDK7-master #431 (See https://builds.apache.org/job/Struts-JDK7-master/431/ ) WW-4410 Adds dedicated interface to allow inject ServletContext (lukaszlenart: rev 4ea8883ff9d81ab93a5360e17a37fc57453f56c4) core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java core/src/main/java/com/opensymphony/xwork2/config/ServletContextAwareConfigurationProvider.java
        Hide
        jira-bot ASF subversion and git services added a comment -

        Commit 4ea8883ff9d81ab93a5360e17a37fc57453f56c4 in struts's branch refs/heads/master from Lukasz Lenart
        [ https://git-wip-us.apache.org/repos/asf?p=struts.git;h=4ea8883 ]

        WW-4410 Adds dedicated interface to allow inject ServletContext

        Show
        jira-bot ASF subversion and git services added a comment - Commit 4ea8883ff9d81ab93a5360e17a37fc57453f56c4 in struts's branch refs/heads/master from Lukasz Lenart [ https://git-wip-us.apache.org/repos/asf?p=struts.git;h=4ea8883 ] WW-4410 Adds dedicated interface to allow inject ServletContext
        Show
        lukaszlenart Lukasz Lenart added a comment - Done plus docs https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=27471
        Hide
        jira-bot ASF subversion and git services added a comment -

        Commit 9860ef829a95428be4fd2638f984b830855e5812 in struts's branch refs/heads/support-2-3 from Lukasz Lenart
        [ https://git-wip-us.apache.org/repos/asf?p=struts.git;h=9860ef8 ]

        WW-4410 Adds dedicated interface to allow inject ServletContext

        Show
        jira-bot ASF subversion and git services added a comment - Commit 9860ef829a95428be4fd2638f984b830855e5812 in struts's branch refs/heads/support-2-3 from Lukasz Lenart [ https://git-wip-us.apache.org/repos/asf?p=struts.git;h=9860ef8 ] WW-4410 Adds dedicated interface to allow inject ServletContext
        Hide
        fustaki Lorenzo Bernacchioni added a comment - - edited

        I agree, the try...catch solution was just a hack, the new interface sounds good!
        I didn't find clear references to the use of custom configuration providers, as far as I understand they can be added via web.xml (described in my first comment, it should be mentioned here) or programmatically via DispatcherListener (mentioned here)

        Show
        fustaki Lorenzo Bernacchioni added a comment - - edited I agree, the try...catch solution was just a hack, the new interface sounds good! I didn't find clear references to the use of custom configuration providers, as far as I understand they can be added via web.xml (described in my first comment, it should be mentioned here ) or programmatically via DispatcherListener (mentioned here )
        Hide
        lukaszlenart Lukasz Lenart added a comment -

        And can you point me to the docs where I should put such information? I mean how to programmatically create configuration?

        Show
        lukaszlenart Lukasz Lenart added a comment - And can you point me to the docs where I should put such information? I mean how to programmatically create configuration?
        Hide
        lukaszlenart Lukasz Lenart added a comment -

        Maybe instead of using the same interface, a new one should be introduced like below:

        public interface ServletContextAwareConfigurationProvider extends ConfigurationProvider {
            void initWithContext(ServletContext ctx) throws ConfigurationException;
        }
        

        and this method will be called just after creating an instance (instead of nasty try...catch solution ), wdyt?

        Show
        lukaszlenart Lukasz Lenart added a comment - Maybe instead of using the same interface, a new one should be introduced like below: public interface ServletContextAwareConfigurationProvider extends ConfigurationProvider { void initWithContext(ServletContext ctx) throws ConfigurationException; } and this method will be called just after creating an instance (instead of nasty try...catch solution ), wdyt?
        Hide
        fustaki Lorenzo Bernacchioni added a comment -

        Exactly, I think having the context it's useful (getting and putting stuff, real path for files, etc..)

        Show
        fustaki Lorenzo Bernacchioni added a comment - Exactly, I think having the context it's useful (getting and putting stuff, real path for files, etc..)
        Hide
        lukaszlenart Lukasz Lenart added a comment -

        As I understand you would like to have option to create ConfigurationProvider with ServletContext injected by Dispatcher without hacking it?

        Show
        lukaszlenart Lukasz Lenart added a comment - As I understand you would like to have option to create ConfigurationProvider with ServletContext injected by Dispatcher without hacking it?
        Hide
        fustaki Lorenzo Bernacchioni added a comment -

        I need to modify struts configuration depending on objects found in the servlet-context.
        I created my custom config provider in the web.xml

        <filter>
          <filter-name>action2</filter-name>
          <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
          <init-param>
            <param-name>configProviders</param-name>
            <param-value>com.mysite.MyConfigurationProvider</param-value>
          </init-param>
         </filter>
        

        and I need to access the servlet-context in its register() method.

        I modified the instantiation of MyConfigurationProvider in the Dispatcher class to make servlet-context available

        Show
        fustaki Lorenzo Bernacchioni added a comment - I need to modify struts configuration depending on objects found in the servlet-context. I created my custom config provider in the web.xml <filter> <filter-name>action2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>configProviders</param-name> <param-value>com.mysite.MyConfigurationProvider</param-value> </init-param> </filter> and I need to access the servlet-context in its register() method. I modified the instantiation of MyConfigurationProvider in the Dispatcher class to make servlet-context available
        Hide
        lukaszlenart Lukasz Lenart added a comment -

        What's your problem?

        Show
        lukaszlenart Lukasz Lenart added a comment - What's your problem?

          People

          • Assignee:
            lukaszlenart Lukasz Lenart
            Reporter:
            fustaki Lorenzo Bernacchioni
          • Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development