0

Using WSO2 AS 5.2.1 been unable to read a xml file in the classpath. Here is the code and the accompanying error:

try {
        InputStreamReader inputReader =new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream(xmlFile.getName())); 
        HolidayInitializer holidayInit = new HolidayInitializer();
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLStreamReader xmlStreamReader = factory.createXMLStreamReader(inputReader);
        institutionVumcHolidays = holidayInit.processHolidays(xmlStreamReader);
        log.info(getClass().getSimpleName() + " loadHolidays() Found Holidays config file in classpath");
    } catch (Exception e) {
        log.error(getClass().getSimpleName() + " loadHolidays() Holidays config file does not exist in classpath", e);
    }

And the output is:

2015-05-26 02:49:14 ERROR ExternalCallbackWebServiceImpl:343 - ExternalCallbackWebServiceImpl loadHolidays() Holidays config file does not exist in classpath

java.io.FileNotFoundException: c:\wso2as-5.2.1\Holidays.xml (No such file or directory)
    at java.io.FileInputStream.open(Native Method)

No matter where I stuff the xml file in WEB-INF/classes or in the directory of the source file, it keeps looking for that file in WSO2_HOME (installation directory). Very dire situation. Would appreciate your help.

Community
  • 1
  • 1
user2654788
  • 181
  • 2
  • 14
  • This is exception is because you are trying to create a File object for a location which does not exist. Where did you initialize xmlFile? – Eranda May 27 '15 at 02:27
  • The file has been provided under WEB-INF/classes. The absolute path of the file is c:\ws02-5.2.1\repository\deployment\server\webapps\mock\WEB-INF\classes. What I don't understand is why is WSO2 looking for the file under c:\wso2-5.2.1? – user2654788 May 27 '15 at 16:24
  • Alternately, when I copied the file over to c:\wso2as-5.2.1\Holidays.xml, the application ran as expected. I need help in figuring out how to read a classpath resource using WS02. Thanks. – user2654788 May 27 '15 at 16:38
  • can you try with getClass().getResourceAsStream() – Thusitha Thilina Dayaratne May 27 '15 at 17:24
  • Hi Thusitha: still getting this error- java.io.FileNotFoundException: C:\WSO2AS~1.1\Holidays.xml (The system cannot find the file specified) – user2654788 May 28 '15 at 00:26
  • Reported as a bug https://wso2.org/jira/browse/WSAS-1953?filter=-2 – user2654788 May 28 '15 at 17:35

1 Answers1

0

You can read the config file with the following,

context.getServletContext().getResource("/META-INF/holidays.xml")

thanks.