Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    LocaleMessageProvider is never called

    Hi Isomorphic,

    please take a look at this test case.
    For datasource "animals", LocaleMessageProvider is never called.
    For datasource "employees" works as expected.

    (SNAPSHOT_v12.1d_2018-11-12/PowerEdition Deployment (built 2018-11-12))

    Code:
        <servlet>
            <servlet-name>RegisterDS</servlet-name>
            <servlet-class>com.smartgwt.sample.server.listener.RegisterDS</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
    Code:
    package com.smartgwt.sample.server.listener;
    
    import java.util.Locale;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    
    import org.w3c.dom.Document;
    
    import com.isomorphic.datasource.DSRequest;
    import com.isomorphic.datasource.DataSource;
    import com.isomorphic.datasource.DynamicDSGenerator;
    import com.isomorphic.util.LocaleMessageProvider;
    import com.isomorphic.util.LocaleMessageProviderRegistry;
    import com.isomorphic.util.LocaleMessageSourceEnum;
    
    public class RegisterDS extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        @Override
        public void init() throws ServletException {
            final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    
            DynamicDSGenerator datasourceGenerator = new DynamicDSGenerator() {
    
                @Override
                public DataSource getDataSource(String id, DSRequest arg1) {
                    try {
                        if (id.startsWith("animals")) {
                            ServletContext servletContext = getServletContext();
                            String strAbsolutePath = servletContext.getRealPath("ds");
                            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    
                            Document doc = documentBuilder.parse(strAbsolutePath + "\\" + id + ".ds.xml");
    
                            return DataSource.fromXML(doc);
                        } else if (id.startsWith("employees")) {
                            return null;
                        }
                    } catch (Exception e) {
                    }
                    return null;
                }
            };
    
            LocaleMessageProvider localeMessageProvider = new LocaleMessageProvider() {
                @Override
                public String getMessage(String arg0, Locale arg1, String arg2, String arg3, LocaleMessageSourceEnum arg4, String arg5) {
                    String id = arg0;
                    try {
                        return id + "translation";
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return "";
                }
            };
    
            LocaleMessageProviderRegistry.addDSLocaleMessageProvider(localeMessageProvider);
            DataSource.addDynamicDSGenerator(datasourceGenerator);
        }
    }
    animals.ds.xml
    Code:
            <field name="information"     title="Interesting Facts"  type="text"  length="1000">
                <title><fmt:message key="title"/></title>
            </field>
    employees.ds.xml
    Code:
            <field name="EmployeeStatus"  title="Status"          type="text"     length="40">
                <title><fmt:message key="title"/></title>
            </field>
    BuiltInDS.java
    Code:
                ListGrid lg1 = new ListGrid() {
                    {
                        setDataSource("animals");
                        ListGridField informationLGF = new ListGridField("information");
                        setFields(informationLGF);
                        fetchData();
                    }
                };
    
                ListGrid lg2 = new ListGrid() {
                    {
                        setDataSource("employees");
                        ListGridField employeeStatusLGF = new ListGridField("EmployeeStatus");
                        setFields(employeeStatusLGF);
                        fetchData();
                    }
                };
    
                addItem(lg1);
                addItem(lg2);
    Best regards
    Pavo

    #2
    This doesn't make sense - there's no difference at all between these two DataSources from the perspective of the server framework.

    What we would guess is happening is that on the "animals" file, you forgot to declare the "fmt" namespace, so the file is invalid XML and loading it crashes before the LocaleMessageProvider even gets involved. See the server logs for this error.

    Comment


      #3
      Hi Isomorphic,

      that's certainly not the case.
      The reason why this is happening is: datasource "animals" is being modified by "DynamicDSGenerator" and datasource "employees" is not.
      If I don't modify datasource "animals" by DynamicDSGenerator then LocaleMessageProvider is called as expected.

      Please call directly datasource "animals"
      Code:
      http://127.0.0.1:8888/builtinds/sc/DataSourceLoader?dataSource=animals
      Within servlet from above example (RegisterDS.java)
      Code:
      @Override
          public void init() throws ServletException {
              final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
      
              DynamicDSGenerator datasourceGenerator = new DynamicDSGenerator() {
      
                  @Override
                  public DataSource getDataSource(String id, DSRequest arg1) {
                      try {
                          if (id.startsWith("animals")) {
                              ServletContext servletContext = getServletContext();
                              String strAbsolutePath = servletContext.getRealPath("ds");
                              DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
      
                              Document doc = documentBuilder.parse(strAbsolutePath + "\\" + id + ".ds.xml");
      
                              return DataSource.fromXML(doc);
                          } else if (id.startsWith("employees")) {
                              return null;
                          }
                      } catch (Exception e) {
                      }
                      return null;
                  }
              };
      
              LocaleMessageProvider localeMessageProvider = new LocaleMessageProvider() {
                  @Override
                  public String getMessage(String arg0, Locale arg1, String arg2, String arg3, LocaleMessageSourceEnum arg4, String arg5) {
                      String id = arg0;
                      try {
                          return id + "translation";
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
                      return "";
                  }
              };
      
              LocaleMessageProviderRegistry.addDSLocaleMessageProvider(localeMessageProvider);
              DataSource.addDynamicDSGenerator(datasourceGenerator);
          }
      with this code (same as in above axample), LocaleMessageProvider is never called for "animals".

      But if you just replace this line of code
      Code:
                          if (id.startsWith("animals")) {
      with this line
      Code:
                          if (id.startsWith("animals1234567890987654321")) {
      then LocaleMessageProvider is called for "animals" as expected.

      Best regards
      Pavo

      Comment


        #4
        Hi Isomorphic,

        did you manage to reproduce this case?

        Best regards
        Pavo

        Comment


          #5
          Apologies for the delay. This is fixed and available for download in latest nightly build.

          Comment


            #6
            Hi Isomorphic,

            it works now, thank you very much!

            Best regards
            Pavo

            Comment

            Working...
            X