• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is this correct - EJBs are allowed to invoke Class.getResource

 
mini mehta
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this correct ?

EJBs are allowed to invoke Class.getResource in order to retrieve bundled resources (images, texts, properties, etc).
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that Class.getResource() returns an URL instance which then does its work through classes of the java.io package. Since the EJB spec does not allow bean providers to play with java.io classes for the sake of portability (EJB spec 24.1.2), it might make sense not to invoke Class.getResource() from your beans.

However, this topic has been discussed on the EJB-INTEREST mailing list and it comes out that it is indeed allowed to call Class.getResourceAsStream() from your beans. Check out the discussion at http://archives.java.sun.com/cgi-bin/wa?A2=ind0002&L=ejb-interest&F=&S=&P=59870
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, despite what the java.io.FilePermission API doc says, the EJB spec does disallow the use of the java.io package.

If you've got something like a properties file that you need to read, one possibility that springs to mind is to include it in your EAR file and use ResourceBundle, eg:

ResourceBundle rb = ResourceBundle.getBundle("some.pkg.myprop");

You can then get an enumeration of the keys and so be able to load the keys and values into a HashMap or similar.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you've got something like a properties file that you need to read, one possibility that springs to mind is to include it in your EAR file and use ResourceBundle

Well, I guess that if you have a properties file or something similar the easiest way to go is to use environment entries (EJB spec 20.2) in my opinion. But as soon as you have something more "binarish" to retrieve, the getResourceAsStream() makes sense provided what has been said on the EJB-INTEREST mailing list. You can also store that binary stuff in a database as a BLOB or something which would comply with the programming restrictions.

Besides, the EJB spec is very fuzzy about the usage of java.io classes. It doesn't say much except that... (24.1.2)

...an enterprise bean must not use the java.io package to attempt to access files and directories in the file system.
The file system APIs are not well-suited for business components to access data. Business components should use a resource manager API, such as JDBC, to store data.


This sounds pretty light to me
[ June 21, 2004: Message edited by: Valentin Crettaz ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic