ResourceUtils.getFile() bug in Spring Framework 2.0.8 with weblogic10.3

I cam across this weird problem which gave me really hard time for about 2-3 weeks. The issue is as below:
We use spring framework in one of our web applications. One of the java classes loads XMLs which reside in web-inf/classes folder. It uses ResourceUtils.getFile() method as below:
           File file = ResourceUtils.getFile("classpath://data-values.xml");
This method works fine when we deploy as exploded WAR in weblogic 10.3 but when we deploy as WAR, it gives FileNotFoundException. What weblogic does during WAR deployment(obviously it internally explodes) is, it puts all the files reside in web-inf/classes folder into jar called _wl_cls_gen.jar file and copies into web-inf/lib directory. This is where the problem during deployment as the java class was unable to read the xml and throwing FileNotFoundException.
        The exact error message is as below:
java.io.FileNotFoundException: class path resource [//data-values.xml] cannot be resolved to absolute file path because it does not reside in the file system: zip:C:/domains/devDomain/servers/AdminServer/tmp/_WL_user/testWebApp/8j5e1y/war/WEB-INF/lib/_wl_cls_gen.jar!/data-values.xml
           So I was looking to see if there is a way to tell weblogic to avoid generating this jar as my java classes were unable to load XMLs which resides in that jar file now. It looks like as for as my knowledge there may not be a way. Even I have asked the same question in oracle forums itself but that didn't help. Also when we googled we came across this issue in number of search results. Some say it is spring issue with ResourceUtils.getFile().
        Some suggested use different approach to load xmls. So one of our team members tried using  org.springframework.core.io.ClassPathResource which fixed the issue. The code snippet is as below:

      String xmlFile = "classpath://data-values.xml";

      final int index = StringUtils.lastIndexOf(xmlFile, "/") == -1 ? 0 : StringUtils.lastIndexOf(xmlFile, "/");
      final Resource resource = new ClassPathResource(StringUtils.substring(xmlFile, index, xmlFile.length()));
      final InputStream is = resource.getInputStream();
      try {
        final InputStreamReader reader = new InputStreamReader(is);
        try {
          final Document headerDoc = new SAXBuilder().build(reader);
        } finally {
          reader.close();
        }
      } finally {
        is.close();
      }

It looks like Spring's 2.0.8 version ResourceUtils.getFile() method is having a bug while loading xmls when you deploy as WAR in weblogic. But no issues when you deploy as exploded. But ClasspathResource seems to be working fine.

5 comments:

  1. Hi

    you have written any build scripts for weblogic portal. If you are written please share it to me.

    ReplyDelete
  2. What kind of build scripts? Could you be more specific?

    ReplyDelete
  3. We need weblogic portal build scripts ti build ear file

    ReplyDelete
  4. org.springframework.core.io.ClassPathResource
    Just edit this file
    line 175:
    change
    return ResourceUtils.getFile(url, getDescription());
    to
    return getFileForLastModifiedCheck();

    everything will be ok

    I think Spring do not want to support war in weblogic
    but getFileForLastModifiedCheck(), this method has done for everything

    ReplyDelete
  5. This bug resourceutils.getfile() is not easy to handle.you provide a great solution.Thank you sir.
    Regards,
    weblogic training.

    ReplyDelete