8 Replies Latest reply on Sep 15, 2003 9:00 PM by no_ejb

    How to replace a file in a war ?

    ipozeng

      hi,friends
      we are using jboss-3.2.1_tomcat-4.1.24 bundle in linux. The war is deployed successfully.

      A GIF file in /web-app/images dir needs to be changed dynamically by the user. So i use a servlet to do it as below:
      ......
      try {
      Map map = HttpUtil.getUploads(request);
      ByteArrayDataSource ds = (ByteArrayDataSource)map.get("map_file_name");
      if (ds == null || ds.getBytes() == null) { return; } ClassLoader cl = getClass().getClassLoader();

      FileOutputStream fo = new FileOutputStream(
      cl.getResource("images/map.gif").getFile()
      );
      fo.write( ds.getBytes() );
      fo.flush();
      fo.close();

      }
      catch (Exception ex) {
      throw new ServletException(ex);
      }

      but jboss reports:
      javax.servlet.ServletException:
      file:/home/tom/jboss-3.2.1_tomcat-4.1.24/server/default/tmp/deploy/server/default/deploy/omnet-web.war/57.omnet-web.war!/images/map.gif (No such file or directory)
      at com.omnet.web.LocationServlet.doPost(LocationServlet.java:180)
      ...

      any suggestion is appreciated !

        • 1. Re: How to replace a file in a war ?
          ipozeng

          Why can NOT i get any answer ?!

          donot you think it is a foolish question ? even so i still need to know the answer.


          thanks in advance !

          • 2. Re: How to replace a file in a war ?
            schmidts

            I wouldn't recommend on-the-fly replacement of files in deployed WARs, even if it worked.
            Why not store uploaded graphics in the filesystem or in a database (and maybe caching static content in filesystem)?

            • 3. Re: How to replace a file in a war ?
              ipozeng

              thanks for your answer first !

              >>Why not store uploaded graphics in the filesystem
              The reason is that we donot know the folder which will be used by our users.Of course we can force the users to create a specified folder used to put our uploaded graphics ?!

              >>i n database
              it is another choice


              regards!

              • 4. Re: How to replace a file in a war ?
                ipozeng

                try {
                Map map = HttpUtil.getUploads(request);
                ByteArrayDataSource ds = (ByteArrayDataSource)map.get("map_file_name");
                if (ds == null || ds.getBytes() == null) {
                return;
                }

                FileOutputStream fo = new FileOutputStream("/tmp/map.gif");
                fo.write( ds.getBytes() );
                fo.flush();
                fo.close();
                }
                catch (Exception ex) {
                throw new ServletException(ex);
                }

                then the file is uploaded into /tmp successfully. However the /tmp is NOT under the webapp context so that the following code will not work:(

                <image xlink:href="/tmp/map.gif" x="0" y="0" width="100%" height="100%" onmouseover="hideMenu(evt);"/>

                then how to replace map.gif in a webapp ???

                • 5. Re: How to replace a file in a war ?
                  schmidts

                  You could create a directory "images.war" in the deploy-directory and put all your images there.
                  Another possible solution is to configure the webcontainer to serve content from outside the WAR: http://www.coredevelopers.net/technology/jetty/jetty-jboss-faq.jsp#I_want_to_serve_content_from_outside_a

                  • 6. Re: How to replace a file in a war ?
                    ipozeng

                    That is great ! why donot i get it ?


                    thank you very much!

                    • 7. How to construct the FileOutputStream ?
                      ipozeng

                      Now the map.gif has been uploaded into images.war/ successfully.However i am not sure what is the best way to construct FileOutputStream ?

                      One way i think is configure the absolute path in our configuraution file as below:

                      /home/..../images.war


                      then
                      FileOutputStream fo = new FileOutputStream(imageDir);

                      Do there exsit any other way ?
                      String pathname = getServletContext().getrealPath("/") always rturn null :(

                      thanks!

                      • 8. Re: How to construct the FileOutputStream ?
                        no_ejb

                        In JBoss there is a non-portable way which is using ServerConfigLocator class to get ServerConfig class. After that you can call getServerBaseDir(), you can append "deploy" after that. It will be the directory where you dropped your war to.

                        Again, I used it only in JBoss related part of the code and I am sure it is release dependent (in my case) it is 3.0.2.