Log4j 2
  1. Log4j 2
  2. LOG4J2-742

XInclude not working with relative path

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.0
    • Fix Version/s: 2.0.1
    • Component/s: None
    • Labels:
      None

      Description

      When using XInclude in a log4j2 configuration, it uses the CWD of the running application instead of the location of the log4j configuration as base.
      I.e. running the application from within eclipse, the CWD of eclipse is used as base for finding the document to be included.

      IMO, the problem is in XmlConfiguration:

                  final InputStream configStream = configSource.getInputStream();
                  try {
                      buffer = toByteArray(configStream);
                  } finally {
                      configStream.close();
                  }
                  final InputSource source = new InputSource(new ByteArrayInputStream(buffer));
                  final Document document = newDocumentBuilder().parse(source);
      

      There is no way the DOMParser can know, where the base should be, because it is just parsing an InputStream and has no file location.

      The fix would be to add source.setSystemId(configSource.getLocation()) before parsing the document.

      1. xinclude.patch
        0.8 kB
        Pascal Chollet
      2. xinclude-test.patch
        6 kB
        Pascal Chollet

        Activity

        Pascal Chollet created issue -
        Pascal Chollet made changes -
        Field Original Value New Value
        Description When using XInclude in a log4j2 configuration, it uses the CWD of the running application instead of the location of the log4j configuration as base.
        I.e. running the application from within eclipse, the CWD of eclipse is used as base for finding the document to be included.

        IMO, the problem is in XmlConfiguration:
        {code}
                    final InputStream configStream = configSource.getInputStream();
                    try {
                        buffer = toByteArray(configStream);
                    } finally {
                        configStream.close();
                    }
                    final InputSource source = new InputSource(new ByteArrayInputStream(buffer));
                    final Document document = newDocumentBuilder().parse(source);
        {code}
        There is no way the DOMParser can know, where the base should be, because it is just parsing a InputStream and has not file location.

        The fix would be to add source.setSystemId(configSource.getLocation()) before parsing the document.
        When using XInclude in a log4j2 configuration, it uses the CWD of the running application instead of the location of the log4j configuration as base.
        I.e. running the application from within eclipse, the CWD of eclipse is used as base for finding the document to be included.

        IMO, the problem is in XmlConfiguration:
        {code}
                    final InputStream configStream = configSource.getInputStream();
                    try {
                        buffer = toByteArray(configStream);
                    } finally {
                        configStream.close();
                    }
                    final InputSource source = new InputSource(new ByteArrayInputStream(buffer));
                    final Document document = newDocumentBuilder().parse(source);
        {code}
        There is no way the DOMParser can know, where the base should be, because it is just parsing an InputStream and has no file location.

        The fix would be to add source.setSystemId(configSource.getLocation()) before parsing the document.
        Hide
        Ralph Goers added a comment -

        Would you be able to provide a unit test and a patch for this?

        Show
        Ralph Goers added a comment - Would you be able to provide a unit test and a patch for this?
        Hide
        Gary Gregory added a comment -

        FWIW: When I tried the suggested code change, the tests passed but there might be unforeseen side effects, and without a failing unit test there is no proof that this fixes anything.

        Show
        Gary Gregory added a comment - FWIW: When I tried the suggested code change, the tests passed but there might be unforeseen side effects, and without a failing unit test there is no proof that this fixes anything.
        Pascal Chollet made changes -
        Attachment xinclude-test.patch [ 12657798 ]
        Attachment xinclude.patch [ 12657799 ]
        Hide
        Pascal Chollet added a comment -

        The tests pass, because there is no test for XInclude yet! There exist test resource files already (with wrong href), but they are not being used by any test.

        I did attach patch files for the test and the fix.

        Show
        Pascal Chollet added a comment - The tests pass, because there is no test for XInclude yet! There exist test resource files already (with wrong href), but they are not being used by any test. I did attach patch files for the test and the fix.
        Pascal Chollet made changes -
        Attachment xinclude.patch [ 12657799 ]
        Pascal Chollet made changes -
        Attachment xinclude.patch [ 12657801 ]
        Pascal Chollet made changes -
        Attachment xinclude-test.patch [ 12657806 ]
        Pascal Chollet made changes -
        Attachment xinclude-test.patch [ 12657798 ]
        Gary Gregory made changes -
        Assignee Gary Gregory [ garydgregory ]
        Hide
        Gary Gregory added a comment -

        Thank you for the patches! Patches applies with only one minor Javadoc change to the test class.

        commit -m "[LOG4J2-742] XInclude not working with relative path." C:/vcs/svn/apache/log4j2/trunk-alt/log4j-core/src/test/resources/log4j-xinclude.xml C:/vcs/svn/apache/log4j2/trunk-alt/log4j-core/src/test/java/org/apache/logging/log4j/core/config/XIncludeTest.java C:/vcs/svn/apache/log4j2/trunk-alt/src/changes/changes.xml C:/vcs/svn/apache/log4j2/trunk-alt/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java
            Sending        C:/vcs/svn/apache/log4j2/trunk-alt/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java
            Adding         C:/vcs/svn/apache/log4j2/trunk-alt/log4j-core/src/test/java/org/apache/logging/log4j/core/config/XIncludeTest.java
            Sending        C:/vcs/svn/apache/log4j2/trunk-alt/log4j-core/src/test/resources/log4j-xinclude.xml
            Sending        C:/vcs/svn/apache/log4j2/trunk-alt/src/changes/changes.xml
            Transmitting file data ...
            Committed revision 1613427.
        
        Show
        Gary Gregory added a comment - Thank you for the patches! Patches applies with only one minor Javadoc change to the test class. commit -m "[LOG4J2-742] XInclude not working with relative path." C:/vcs/svn/apache/log4j2/trunk-alt/log4j-core/src/test/resources/log4j-xinclude.xml C:/vcs/svn/apache/log4j2/trunk-alt/log4j-core/src/test/java/org/apache/logging/log4j/core/config/XIncludeTest.java C:/vcs/svn/apache/log4j2/trunk-alt/src/changes/changes.xml C:/vcs/svn/apache/log4j2/trunk-alt/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java Sending C:/vcs/svn/apache/log4j2/trunk-alt/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java Adding C:/vcs/svn/apache/log4j2/trunk-alt/log4j-core/src/test/java/org/apache/logging/log4j/core/config/XIncludeTest.java Sending C:/vcs/svn/apache/log4j2/trunk-alt/log4j-core/src/test/resources/log4j-xinclude.xml Sending C:/vcs/svn/apache/log4j2/trunk-alt/src/changes/changes.xml Transmitting file data ... Committed revision 1613427.
        Gary Gregory made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Fix Version/s 2.0.1 [ 12327381 ]
        Resolution Fixed [ 1 ]

          People

          • Assignee:
            Gary Gregory
            Reporter:
            Pascal Chollet
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development