6 Replies Latest reply on Aug 18, 2008 7:09 PM by amontobin

    Access Resources from webapp directory

    amontobin Newbie

      Hi,

      I'm trying to deploy an ear packaged file to the jboss server. I've got some files in webapp directory which i need to read from some java classes.
      The problem is that i can't find how to reference the webapp directory cos jboss is deploying the ear in the tmp/deploy/temporary_name/....

      Is there a way to find the directory where the ear is deployed ?
      Perhaps, through JNDI ? Any ideas ?

      Thank you very much,

      Sebastien

        • 1. Re: Access Resources from webapp directory
          Peter Johnson Master

          Your best bet is to place the files you need to read into WEB-INF/classes and then access them using the class loader. For example:

          URL = myfileUrl = Thread.currentThread().getContextClassLoader().getResource("my.file");

          • 2. Re: Access Resources from webapp directory
            amontobin Newbie

            Hi Peter,

            Thank you for your response
            I've found myself another way to find the correct path :
            I will try the solution in the application this afternoon.
            Here is my possible solution :

            ObjectName name = new ObjectName("jboss.system:service=MainDeployer");
            Collection<DeploymentInfo> c = (Collection<DeploymentInfo>) server.invoke(name, "listDeployed", new Object[] {}, new String[] {});
            for(Iterator<DeploymentInfo> i = c.iterator();i.hasNext();) {
             DeploymentInfo d = i.next();
             if (d.url.toString().endsWith("my-ear.ear")) {
             String dir = d.localUrl.getPath();
             File f = new File(dir + "-contents");
             if (f.exists()) {
             System.out.println("OK");
             }
             }
             System.out.println(d.toString());
            }
            
            
            


            • 3. Re: Access Resources from webapp directory
              Peter Johnson Master

              There are two caveats to your solution:

              1) It is labor intensive and very slow
              2) It does not work in JBossAS 5.0 (the listDeployed operation stopped returning anything in beta1 and as of CR1 still does not return anything)

              • 4. Re: Access Resources from webapp directory
                amontobin Newbie

                Here comes the reason why i need to put the files in webapp and not in WEB-INF/classes :

                I've got images that may be displayable on the web site and to be use for the report manager (BIRT). As you may know, BIRT need to use paths to access resources.

                The solution works in production ; but be careful code changes a little between a directly well formed ear and playing in a development environment with a directory-based ear.

                For the caveats you say :

                1. Not a problem for me, i need to compute the path only one time (saved in a static field)
                2. Arggh. Is it a change in the JBOSS Api ? or a bug to resolve ?

                Sebastien

                • 5. Re: Access Resources from webapp directory
                  Peter Johnson Master

                  Item #2 is "that's the way its is". In JBossAS 4.x, the deplores were JMX services. In 5.0 they are microcontainer pojos. When I asked about the the listDeployed operation over 2 years ago, I got the impression that there were no plans whatsoever to provide that functionality again.

                  And based on your use case, there really is no need, What you really want is a simple mechanism to determine the full path where your app was deployed. Here's a thought - the code I provided gives the full path for a file in WEB-INF/classes - just edit the resulting URL to get back to the base directory for the war.

                  • 6. Re: Access Resources from webapp directory
                    amontobin Newbie

                    I'm so stupid ; you gave me a solution that i thought at first sight was too simple (i've not thunk on url.getPath()) and search for a more complicated solution.

                    I will try as soon as possible your solution (Need vacation !!!)

                    Thank you Peter, going to sleep.

                    Sebastien