6 Replies Latest reply on Sep 2, 2012 1:05 PM by deri dzen

    How to load custom .xml file from WEB-INF?

    deri dzen Newbie

      Ok, I've been reading threads for an hour and I can't find a solution to my problem.


      The thing is that I use JBoss AS 7.1 and deploying WAR application to JBoss. I made a custom mapping.xml file that Dozer needs but I cannot locate file inside my war. What I've tried so far is:

       

      System.out.println(this.getClass().getClassLoader().getResource("/WEB-INF/mapping.xml"));
        System.out.println(this.getClass().getClassLoader().getResource("/mapping.xml"));
        System.out.println(this.getClass().getClassLoader().getResource("/resources/mapping.xml"));
        System.out.println(Thread.currentThread().getContextClassLoader().getResource("/resources/mapping.xml"));
        System.out.println(Thread.currentThread().getContextClassLoader().getResource("/mapping.xml"));
        System.out.println(Thread.currentThread().getContextClassLoader().getResource("mapping.xml"));
        System.out.println(Thread.currentThread().getContextClassLoader().getResource("/WEB-INF/mapping.xml"));
        System.out.println(this.getClass().getClassLoader().getResource("WEB-INF/mapping.xml"));
        System.out.println(this.getClass().getClassLoader().getResource("webapp/WEB-INF/mapping.xml"));
        System.out.println(this.getClass().getClassLoader().getResource("/webapp/WEB-INF/mapping.xml"));
      
      

       

      and all of them return null. What is left to try?

        • 1. Re: How to load custom .xml file from WEB-INF?
          deri dzen Newbie

          Also tried /target/classes/WEB-INF/mapping.xml and all combinations like /target/... taget/... /classes/... classes/... etc...

          • 2. Re: How to load custom .xml file from WEB-INF?
            Stephen Coy Master

            You need to use javax.servlet.ServletContext#getResourceAsStream(java.lang.String).

             

            Otherwise, if you must use a classloader, then your resource must be somewhere in WEB-INF/classes.

            • 3. Re: How to load custom .xml file from WEB-INF?
              deri dzen Newbie

              I cannot access ServletContext from this class:

               

               

              public class MyModelMapper {
              
                        private static class SingletonHolder {
                                  private static Mapper INSTANCE= new DozerBeanMapper() {
                                            {
                                                      List<String> mappingFileUrls= new ArrayList<String>();
              //                                        InputStream resourceAsStream = this.getClass().getResourceAsStream("/WEB-INF/mapping.xml");
              //                                        InputStream resourceAsStream2 = this.getClass().getResourceAsStream("WEB-INF/mapping.xml");
              //                                        InputStream resourceAsStream3 = this.getClass().getResourceAsStream("mapping.xml");
              //                                        InputStream resourceAsStream4 = this.getClass().getResourceAsStream("/mapping.xml");
              //                                        System.out.println(this.getClass().getClassLoader().getResource("/WEB-INF/mapping.xml"));
              //                                        System.out.println(this.getClass().getClassLoader().getResource("/mapping.xml"));
              //                                        System.out.println(this.getClass().getClassLoader().getResource("classes/WEB-INF/mapping.xml"));
              //                                        System.out.println(this.getClass().getClassLoader().getResource("/classes/WEB-INF/mapping.xml"));
              //                                        System.out.println(this.getClass().getClassLoader().getResource("target/classes/WEB-INF/mapping.xml"));
              //                                        System.out.println(this.getClass().getClassLoader().getResource("/target/classes/WEB-INF/mapping.xml"));
              //                                        System.out.println(this.getClass().getClassLoader().getResource("core/target/classes/WEB-INF/mapping.xml"));
              //                                        System.out.println(this.getClass().getClassLoader().getResource("/core/target/classes/WEB-INF/mapping.xml"));
              //                                        System.out.println("a:"+ this.getClass().getClassLoader().getResource("/resources/mapping.xml"));
              //                                        System.out.println(Thread.currentThread().getContextClassLoader().getResource("/resources/mapping.xml"));
              //                                        System.out.println(Thread.currentThread().getContextClassLoader().getResource("/mapping.xml"));
              //                                        System.out.println(Thread.currentThread().getContextClassLoader().getResource("classes/WEB-INF/mapping.xml"));
              //                                        System.out.println(Thread.currentThread().getContextClassLoader().getResource("/classes/WEB-INF/mapping.xml"));
              //                                        System.out.println(Thread.currentThread().getContextClassLoader().getResource("target/classes/WEB-INF/mapping.xml"));
              //                                        System.out.println(Thread.currentThread().getContextClassLoader().getResource("/target/classes/WEB-INF/mapping.xml"));
              //                                        System.out.println(Thread.currentThread().getContextClassLoader().getResource("core/target/classes/WEB-INF/mapping.xml"));
              //                                        System.out.println(Thread.currentThread().getContextClassLoader().getResource("/core/target/classes/WEB-INF/mapping.xml"));
              //                                        System.out.println(Thread.currentThread().getContextClassLoader().getResource("mapping.xml"));
              //                                        System.out.println(Thread.currentThread().getContextClassLoader().getResource("/WEB-INF/mapping.xml"));
              //                                        System.out.println(this.getClass().getClassLoader().getResource("WEB-INF/mapping.xml"));
              //                                        System.out.println(this.getClass().getClassLoader().getResource("webapp/WEB-INF/mapping.xml"));
              //                                        System.out.println(this.getClass().getClassLoader().getResource("/webapp/WEB-INF/mapping.xml"));
              //                                        mappingFileUrls.add(this.getClass().getClassLoader().getResource("/WEB-INF/mapping.xml").getFile());
              //                                        setMappingFiles(mappingFileUrls);
                                            }
                                  };
                        }
                
              
                        public static <D> D map(Object source, Class<D> destinationType) {
                                  SingletonHolder.INSTANCE.map(source, destinationType);
                        }
              }
              
              • 4. Re: How to load custom .xml file from WEB-INF?
                deri dzen Newbie

                And if I turn this MyModelMapper to @ApplicationScoped bean, how would I access ServletContext? Via CDI? I tried and I get "null" value when I debug the class...?

                • 5. Re: How to load custom .xml file from WEB-INF?
                  Nicklas Karlsson Master

                  Tried a ServletContextListener.contextInitalized(SCE e) and stash the context away?

                  • 6. Re: How to load custom .xml file from WEB-INF?
                    deri dzen Newbie

                    Well, I solved it the following way:

                    I put the file to:

                    src/main/resources/mapping.xml

                    instead of webapp/WEB-INF

                     

                    and loaded it only by providing "mapping.