File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Is this correct - EJBs are allowed to invoke Class.getResource Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Is this correct - EJBs are allowed to invoke Class.getResource " Watch "Is this correct - EJBs are allowed to invoke Class.getResource " New topic
Author

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

mini mehta
Ranch Hand

Joined: Oct 22, 2000
Posts: 120
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

Joined: Aug 26, 2001
Posts: 7610
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


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
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.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is this correct - EJBs are allowed to invoke Class.getResource