6 Replies Latest reply on Jun 14, 2010 1:18 PM by saltnlight5

    How to get Temp directory in JBoss 5.1

    forfano

      Lads,

       

      I am migrating an application from Jboss 3.2.8 to 5.1.0GA.

      In my code I use to execute a getResource("/").getPath() and it used to return the temp directory that JBoss 3.2.8 created: JBOSS_HOME\server\default\tmp\deploy\tmp7521058916574205799MyEAR.ear-contents\MyWEBAplication.war

       

      However in JBoss 5.1.0 it returns empty because JBoss uses the VFS so getResource("/") returns a vfsmemory://3l662039-hn0ww6-g9vcm4ds-1-g9vcmyq7-2h/ which does not contains a path.

       

      My question is. How can I get the tmp folder created by Jboss in the Jboss 5.1.0? I can see that Jboss creates a tmp on JBOSS_HOME\server\default\tmp\3l662039-hn0ww6-g9vcm4ds-1-g9vcnomw-9x\myWEBAplicatiion.war .

       

       

      Thank you!

       

      Flavio

        • 1. Re: How to get Temp directory in JBoss 5.1
          saltnlight5

          JBoss temp directory is initialized during server startup and be query using System.getProperty("jboss.server.temp.dir")

           

          I wouldn't recommand you to use this directory for your own app use unless you got a good reason for it. It's a temp folder for JBoss server processing, and there is no guarantee that it will not overwrite your stuff. You better off setup your own temp directory for use instead.

          • 2. Re: How to get Temp directory in JBoss 5.1
            saltnlight5

            Also, you can't rely on getResource("/").getPath() to give you real path dir, because the resource/class it loaded might not have a real path (like if it's from a archive, or if it loaded over the network eg.). This works for you in the past perhaps you only deployed your app in exploded mode. The JBoss5 is now giving you a better view of your problem, the resource is a vfsmemory, which clearly says you shouldn't be using that as temp dir!

            • 3. Re: How to get Temp directory in JBoss 5.1
              forfano

              Thanks a million Zemian for your response.

               

              It does make sense your points but What we need is a way to get the web application context directory.

              For example,  as shown above the 3.2.8 jboss use to return the path which passes by the MyWEBAplication.war folder which is the web application context folder.

               

              The reason I need that is because our application does generate some javascripts dynamically after the start up of Jboss and it writes the js files inside the WEB-INF/js folder which is under the tmp folder previously created by Jboss.

              If we can't get the tmp folder for then we will have to save them in a fixed path which is somehow accessible.

               

              cheers,

               

              Flavio

              • 4. Re: How to get Temp directory in JBoss 5.1
                saltnlight5

                To get the web application context directory, you can obtain that from something like this:

                 

                javax.servlet.ServletContext servletContext = ...
                String webappDirectory = servletContext.getRealPath("/");
                

                 

                ServletContext is availabe in a ServletContextListener implementation.

                • 5. Re: How to get Temp directory in JBoss 5.1
                  forfano

                  Hi Zemian!

                   

                  Thanks again for you reply.

                   

                  The ServletContext did the trick for us.

                  However we have one little concern that may not be considered one at all.

                   

                  Before in Jboss 3.2.8 we used to load the path calling the following code:

                   

                   

                  static {

                  StringBuffer helper = new StringBuffer(UGOJavascriptValidatorTag.class
                                  .getResource("/").getPath());
                          helper.deleteCharAt(0);
                          helper.append("js");
                          dir = helper.toString();

                  }

                   

                  This would return the address JBOSS_HOME\server\default\tmp\deploy\tmp7521058916574205799MyEAR.ear-contents\MyWEBAplication.war

                   

                  So note that we do that in a static way in a custom tag class (a class that extends javax.servlet.jsp.tagext.BodyTagSupport). In this way this code will be executed only once during Application start up in Jboss.

                   

                  For Jboss 5.1 the code above does not return the path but a kind of pointer for VFS memory.

                   

                  Because of that, we are going to use Servlet context and we realized that it is not possible to use it in a static way and neither in a constructor or final class variable. May guess is that during the start up of the class the pageContext is not known yet so it returns null (this.pageContext == null).

                   

                  We work around this by calling it inside the doStartTag() method. At this point and moment the pageContext is known:

                   

                  public int doStartTag() throws JspException {

                   

                          System.out.println("page context "+ this.pageContext);
                          System.out.println("page context servlet context  "+ this.pageContext.getServletContext());
                          StringBuffer helper = new StringBuffer(this.pageContext.getServletContext().getRealPath("/"));
                          helper.append("js");
                          dir = helper.toString();
                          System.out.println("dir orfano " + dir);
                         ........

                        .........

                   

                  THIS WORKS FINE FOR US! BUT it executes every time the page is  called. OUR concern is: Does it cost much or would it only hurt our pride?

                   

                  If you can comment here I would be very thankful. However it already works as a solution.

                   

                  Thank you!

                   

                  Flavio

                  • 6. Re: How to get Temp directory in JBoss 5.1
                    saltnlight5

                    That seems like a BAD way to initialize a webapp code. Why not just use the standard ServletContextListener ? You just need to add your implementation into your web descriptor (WEB-INF/web.xml) like:

                     

                    <web-app>
                    ...
                    <listener>
                        <listener-class>my.WebAppListener</listener-class>
                    </listener>
                    ...
                    </web-app>
                    
                    

                     

                    Example of the listener class:

                     

                    package my;
                    public class WebAppListener implements javax.servlet.ServletContextListener {
                         public void contextInitialized(ServletContextEvent scEvent) {
                              ServletContext sc = scEvent.getServletContext();
                              String webappPath = sc.getRealPath("/");
                              // ... initialize your the path here.
                         } 
                         public void contextDestroyed(ServletContextEvent scEvent) {}
                    }
                    

                     

                    By spec, the ServletContextListener methods are only called once per your webapp during initialize and destroying as your deploy and undeploy it.

                     

                    This has nothing to do with JBoss specific, and I suggest you review the Java Servlet Spec and how a typical webap is structured and written.

                     

                    /Z