4 Replies Latest reply on Oct 30, 2007 12:09 PM by waynebaylor

    Access a file resource from EJB3.0

    skymic

      Hi all,

      I am using a jboss-4.0.5.GA app server.
      I have a SLSB from which I want to access a file resource.
      Simply using java.io.File and java.io.FileInputStream does not seem to be allowed in EJB.

      I browsed through the forum and I think I have to define a URL resource but I could not find an example.

      Can anyone provide me with a working example of how to access a file in EJB3.0?

      Thanks

        • 1. Re: Access a file resource from EJB3.0
          gmuzzillo

          Hi!!
          Inside an EJB you can have a FileStream representation using the classloader and Threadl.
          here goes a little sample:

          ClassLoader loader = Thread.currentThread().getContextClassLoader();
           InputStream stream = loader
           .getResourceAsStream("rolesAllowed.properties");
          

          here rolesAllowed.properties is a file resource located at $JBOSS_HOME/server/default/conf.
          you can use any relative path that is present on the ejb classpath.
          I hope this helps you.
          Best
          Gustavo

          • 2. Re: Access a file resource from EJB3.0
            junkie

            I am using FileInputStream inside an EJB 3 session bean. It works perfectly. What error message do you get?

            • 3. Re: Access a file resource from EJB3.0
              skymic

              Thanks gmuzzillo and JUnkie,

              I know that using java.io.File and java.io.InputStream (and subclasses) for file access work, however I get a warning from by IDE (which is IntelliJ IDEA V6.0) which says "usage of java.io.File not allowed in EJB".
              Same for FileInputStream.

              Again, the code compiles and works well.

              Does anyone know about the "correct" rules of how to access files from inside an EJB?

              • 4. Re: Access a file resource from EJB3.0
                waynebaylor

                i believe if you want to access resources that exist outside of the app. server's container you should use JCA.