3 Replies Latest reply on May 5, 2015 6:40 PM by jameslivingston

    How to get image path inside EJB bundled in EAR?

    valsaraj007

      Hi,

       

      How to get image path inside EJB bundled in EAR? My EAR file has ejb.jar file and web.war file. Image is in web.war file and need to get it''s path from EJB. This was working in JBoss 4.2.2GA. THe code that worked is given below:

      String logoPath = "file://"+ this.getClass().getResource("/").getPath() + "/images";

      But it is not returning correct path in WildFly-8.2.

       

      We are able to get it from Servlet running in WildFly-8.2:

      getServletContext().getRealPath("/")

       

      But we need the same path to get in EJB because we need to get this path for a remote EJB call to generate a file with logo.

       

      Is there any way to get the correct path?

       

      Thanks!

        • 1. Re: How to get image path inside EJB bundled in EAR?
          jaysensharma

          getServletContext().getRealPath()  is Servlet API.  However for Application Servers and for the Enterprise Applications the        String logoPath = "file://"+ this.getClass().getResource("/").getPath() + "/images";   approach is not good as there is no guarantee what path will be returned, because it is up to the implementation.


            However you can use the   [this.getClass().getClassLoader().getResourceAsStream("images/yourLogo.png");]    approach to get the resource and to use it.

           

                    try {
                             InputStream inStream = this.getClass().getClassLoader().getResourceAsStream("images/yourLogo.png");
                             FileOutputStream fout = new FileOutputStream (new File(System.getProperty("user.home")+"/yourLogo.png"));
                             int i=0; 
                             while((i=inStream.read())!=-1){ 
                                 fout.write((byte)i); 
                             } 
                             inStream.close(); 
                          } catch(Exception e) {
                             e.printStackTrace();
                          }
                }
          
          1 of 1 people found this helpful
          • 2. Re: How to get image path inside EJB bundled in EAR?
            valsaraj007

            Hi Jay,

             

            As per the above code, we are going to create duplicate files, right? ie, reading stream and writing in user's directory to use that path? Actually we need the path inside ear/web.war like jboss-8.2.0.Final\domain\servers\node-00\tmp\vfs\deployment\deploymenta54d265f6bf674d2\web.war88\images\test.png. This path is then set on a template file which will be used by the third party library to generate PDF. Reading stream and writes a file will take some CPU time as well. Is there any way to get this path inside web.war from EJB?

             

            Thanks!

            • 3. Re: How to get image path inside EJB bundled in EAR?
              jameslivingston

              According to the spec, the contents of a WAR is not available to other sub-deployments, so there is no portable way to do this. In addition, there is no guarantee that you can actually get a path to a file on disk, since the server does not have to explode archives for internal use.

               

              If you need access to resources (like an image) from multiple sub-deployments, you could put them in a separate jar file in the EAR. If you need to serve them as static content from the web app, you'll want to have a servlet which finds the resources and copies the data to the output stream. On some servers (including WildFly8+) this will be less efficient since it involves application code rather than Undertow copying it.

               

               

              Does the third party library accept URLs or InputStreams? If so, you should use those instead.  If it only accepts File objects, then I think the only options are to use un-portable hacks, or find a better library.