This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes Access file system from enterprise bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Access file system from enterprise bean" Watch "Access file system from enterprise bean" New topic
Author

Access file system from enterprise bean

Fisher Daniel
Ranch Hand

Joined: Sep 14, 2001
Posts: 582
Dear all,
Because an enterprise bean cannot must not use java.io package to access file and directories in the file system.
How does an enterprise bean access files or directories in the file system?
thanks
daniel
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Better put the file as a part of deployment jar and read from there.


Groovy
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
It depends on what you are trying to do. I am all in favor of breaking the no java.io.* rule in some cases. For example, I see no problem in accessing the file system to get a complex configuration file and in some cases it can't be easily avoided (ie. third-party libraries that use config files). However, I would definitely recommend staying away from any read-write file IO.
Harm de Laat
Greenhorn

Joined: Sep 07, 2003
Posts: 11
Why should you not use java.io.* in EJB's?
For instance, I have a MessageDrivenBean which does some image processing.
In my JMS message to the bean I include the absolute path to the image file.
Then I do the processing, and save the image.
Is there anything wrong with that? (I'm asking because I have to use the java.io.File class).
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Why should you not use java.io.* in EJB's?

Because the EJB specification tells you not to... In practice, however, you can break this rule and many times you simply have to.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Harm de Laat
Greenhorn

Joined: Sep 07, 2003
Posts: 11
And in my specific case? Because i'm violating the spec, can I break something?
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
You can break the spec but in your case, I would implement the file/image processing stuff in such a way that a possible rollback of a transaction would not mess things up (for example, use a temp file that won't affect your program even if it is left hanging around for some reason).
norman richards
Author
Ranch Hand

Joined: Jul 21, 2003
Posts: 367
One nice trick is to bind part of the filesystem in via JNDI and access files on the filesystem that way. With JBoss this is pretty straightforward. It might be harder to do in other app
servers. (and as such really couldn't be considered a portable technique)
A Wolfe
Greenhorn

Joined: Sep 08, 2003
Posts: 23
Why don't you add a helper class that will do the file processing and give the data back to EJB that you need to use in EJB?
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Delegating the use of java.io.* to a helper class is no different from using it directly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access file system from enterprise bean