| |
Sistemi distribuiti: design
-
Forum del Corso
Messaggi del Thread
|
| Autore |
Messaggio |
m.mancioppi
|
Thread
Postato:
11 gennaio 2005
Titolo:
Accessing Files and I/O from EJBs
|
|
|
I'm writing this topic hoping to prevent you from wasting huge amounts of time searching the Web for explanations about how to deal with I/O while developing EJBs (yes, I wasted REALLY a lot of time in order to solve this issue!).
The EJB 2.0 spec restricts enterprise beans from accessing files and directories (it does this by restricting access to the java.io package). Please notice that dealing with such a limitation is required only if you want your application to be fully compliant to the EJB specifications: in fact, from a more "pratical" (or somewhat lesser philosophical and more realistic point of view), the java.io package limitation is set only on few ASs (on JBoss it can be set somewhere in the configuration files, and for default the access is enabled), so if you use FileInputStreams and other '.io gizmoes', your application *should* work properly.
It appears that there are two main "cases" of files to be accessed from an EJB. How to access them depends on their location:
1) The file is contained within the same EAR or WAR bundle containing the EBJs: the former can be accessed as a Resource through an InputStream. For more hints have a look ad java.lang.ClassLoader.getSystemResource(String name) (I suppose this way is the one all the libraries adopt to access properties files). 2) The file is stored somewhere on a File System (and cannot be accessed through the ClassLoader). I've seen that there are so opinions about how to deal with such an issue, that probably every Java developer has its own one. However, there are some workarounds that seem to be rather popular, and are the ones accessible through the <resource-ref></resource-ref> declarations in the ejb-jar:
| http://jguru.com/faq/view.jsp?EID=12638 wrote: | *) JDBC (javax.sql.DataSource) : Write I/O data in to a relational database. *) URL (java.net.URL) : Write I/O data to a custom server or post them to a web server. *) JavaMail (javax.mail.Session) : E-mail I/O data to a special account *) JMS (javax.jms.QueueConnectionFactory | javax.jms.TopicConnectionFactory) : Send the I/O data as messages. |
For more details, check those links out: http://www.mail-archive.com/ejb-interest%40java.sun.com/msg13587.html http://www.mail-archive.com/ejb-interest%40java.sun.com/msg13923.html
Ah, if you wonder why such a limitation has been set, please have a look at this weblog: Although surfing the net I found other explanations (always involving concurrency, security and similar topics), the ones presented in the linked article seemed to be the most convincing to me.
|
|
|
marco.ronchetti
|
Postato:
12 gennaio 2005
Titolo:
Re: Accessing Files and I/O from EJBs
|
|
|
|
Very interesting contribution. Thanks!
|
|
|
|
|