Announcement Announcement Module
Collapse
No announcement yet.
Why doesn't exist ResourceBundle Spring PropertyEditor? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Why doesn't exist ResourceBundle Spring PropertyEditor?

    Dear members,

    For generating reports on our application, we need to load some parameters (column names, title page, etc), from a given resource bundle. Looking to the ResourceBundleMessageSource, all methods for getting the ResourceBundle its selfs, are protected, so the only solution would be to extends from a given class in order to get the ResourceBundle (for example on Jasper Report, we have to set the ResourceBundle to be used, so in some way we need to obtain such information).

    Looking into Spring Properties Editors, I don't see the ResourceBundle editors, why?, so the only way to set a property editor is with two steps: baseName (String) and locale (Locale). So for example if we define a class like this:

    Code:
     
    public class Bundle {
      private String baseName;
      private Locale locale;
      public getBaseName(){this.baseName;}
      public getLocale(){this.locale;}
      setBaseName(String baseName){this.baseName = baseName;}
      setLocale(Locale locale){this.locale = locale;}
      ....
    }
    and now we can set for a particular report generator the bundle information:

    Code:
     
     
    <bean id="AssetCustomerResumeReportGenerator"
      class="com.schinvest.lra.business.report.generator.
        xls.AssetCustomerInfo.AssetCustomerResumeReportGenerator"
        parent="xlsReportGenerator" singleton="false">
      <description>Report generator for AssetCustomerResume</description>
      <property name="reportDao">
       <ref bean="reportDao"/>
      </property>
      <property name="locale">
     <value>${AssetCustomerResumeReportGenerator.locale}</value>
    </property>
    <property name="bundleBaseName">
     <value>${AssetCustomerResumeReportGenerator.bundleBaseName}</value>
      </property>
     </bean>
    and we use placeholder in order to better mantain the applicationContext file.

    My question is, why there is no such ResourceBundle Properties Editor, for such cases? (base name before the first underscore and after the first underscore is the locale definition, so there a easy way to parse such informatin, in order to get basename and locale.). So, we can define the bean like this:

    Code:
     
     
    <bean id="AssetCustomerResumeReportGenerator"
      class="com.schinvest.lra.business.report.generator
        .xls.AssetCustomerInfo.AssetCustomerResumeReportGenerator" 
        parent="xlsReportGenerator" singleton="false">
      <description>Report generator for AssetCustomerResume</description>
      <property name="reportDao">
       <ref bean="reportDao"/>
      </property>
      <property name="resourceBundle">
     <value>AssetCustomerResumeReportGenerator_es</value>
    </property>
     </bean>
    (We are assuming there should be defined a ResourceBundle Properties Editor and properly registered)

    I am wondering if some forum members, adopt this solution or a better one?

    Did some one have already implemented the ResourceBundle Properties Editor?

    Thanks in advance,

    David Leal

  • #2
    Why don't you write a ResourceBundle Editor on your own? Below is an alternative solution all the same.

    Code:
    <bean id="resourceBundle" class="java.util.ResourceBundle"
          factory-method="getBundle">
      <constructor-arg type="java.lang.String">
        <value>AssetCustomerResumeReportGenerator</value>
      </constructor-arg>
      <constructor-arg type="java.util.Locale">
        <value>es</value>
      </constructor-arg>
    </bean>
    
    <bean id="AssetCustomerResumeReportGenerator"
        class="com.schinvest.lra.business.report.generator.
           xls.AssetCustomerInfo.AssetCustomerResumeReportGenerator" 
        parent="xlsReportGenerator" singleton="false">
      <description>Report generator for AssetCustomerResume</description>
      <property name="reportDao">
        <ref bean="reportDao"/>
      </property>
      <property name="resourceBundle">
        <ref bean="resourceBundle" />
      </property>
    </bean>
    http://www.springframework.org/docs/reference/beans.html

    greets

    Comment


    • #3
      Explosions of beans definition...:-(

      Thanks unkreativ for your post.

      For my particular case I am going to define more or less 20 types of reports on my application context, so I am going to have 40 beans because for each generator I need to define also the bundle (I am using a bundle per report).

      This of course works, but I have to pay the price of creating a lot of bundles beans.

      The solution, I guess, in order to avoid such numbers of beans is to define a ResourceBundle Property Editor.

      Thanks for your suggestion about this problem,

      David

      Comment


      • #4
        Added on Jira ResourceBundle Properties Editor

        I have added on JIRA with Issue Number: SPR-2232 as improvement to consider as one of supported Properties Editor this one for ResourceBundle.

        You can see on it, the implementation and testing classes for this Properties Editor.

        Regards,

        David

        Comment


        • #5
          As you're injecting quite a few ResourceBundles using a common approach (I assume you're using the same Locale overall the application) I'd consider using a BeanPostProcessor.

          Code:
          public class ResourceBundleProcessor implements BeanPostProcessor {
          
            private Locale locale;
          
            public void setLocale(Locale locale) {
              this.locale = locale;
            }
          
            public Object postProcessBeforeInitialization(Object bean, String beanName)
                throws BeansException {
              return bean;
            }
          
            public Object postProcessAfterInitialization(Object bean, String beanName)
                throws BeansException {
              if (bean instanceof ResourceBundleAware) {
                ResourceBundleAware awareBean = (ResourceBundleAware) bean;
                awareBean.setResourceBundle(
                  getResourceBundle(bean.getClass().getSimpleName()));
              }
          
              return bean;
            }
          
            private ResourceBundle getResourceBundle(String baseName) {
              // preventing NullPointerExceptions
              if (locale == null) {
                return ResourceBundle.getBundle(baseName);
              } else {
                return ResourceBundle.getBundle(baseName, locale);
              }
            }
          }
          Code:
          public interface ResourceBundleAware {
          
            public void setResourceBundle(ResourceBundle bundle);
          }
          Code:
          public class AssetCustomerResumeReportGenerator 
              implements ResourceBundleAware {
          
            // ...
          }
          Code:
          <bean id="resourceProcessor" class="x.y.z.ResourceBundleProcessor">
            <property name="locale">
              <value>es</value>
            </property>
          </bean>
          
          <bean id="AssetCustomerResumeReportGenerator"
              class="com.schinvest.lra.business.report.generator.
                 xls.AssetCustomerInfo.AssetCustomerResumeReportGenerator" 
              parent="xlsReportGenerator" singleton="false">
            <description>Report generator for AssetCustomerResume</description>
            <property name="reportDao">
              <ref bean="reportDao"/>
            </property>
          </bean>
          
          ...
          greets
          Last edited by unkreativ; Jul 6th, 2006, 02:52 PM.

          Comment

          Working...
          X