when i develop servlet+javabean structure application,in order to imploement decoupling,i would like to write the config information in the properties files(key-value pair),in servlet,i use "getResourceAsStream(String relativePath)" method to retrieve the configuration information from the properties files(i even write the SQL clause in the files),it alwsys works well. now,i want to implement such function within the ejb container,that is to read a properties file from session bean,but i don't know how can i locate the file within the ejb container by using relative path,i wonder if there is the same method within ejb container as "getResourceAsStream(.......)" method within servlet container? thanks for any helps! the code snippet is appreciated!
You can use EJB environment properties from the deployment descriptor, and retrieve them by jndi lookup. Or, you can put the properties file in the ejb jar and retrieve it using this.getClass().getResourceAsStream().
Accessing the file system in EJB is against the spec but no application server actually enforces this. Using "this" in an ejb is prefectly legal for the case Dave is talking about. The important distinction is that an ejb cannot pass a reference of itself for use by other clients using "this". It must use its EJBContext to lookup and pass a reference to its EJBObject. Last note: Go with the deployment descriptor solution Dave mentioned. It is much better than using property files.
Can some one explain the first method Dave mentioned in a bit more detail please. Thanks a lot! Does Dave means binding an object to jndi and then look it up again. If so please explain more in it preferably with some code. Sorry if this sounds troublesome. Thanks! Please Help any advice is very much welcomed! [ September 29, 2002: Message edited by: rastin purr ]
Thanks Dave. In this case mentioned by sb cong, one could pass on the path trough the descriptor and get the path in the ejb as string. As ejb are not prefered to deal with io. Would it be possible to pass io related operations to a helper class? is this a good idea? Is it that ejb are not prefered to use io? if so why? could someone kindly explain it to me please. I know this applies to entity beans but for session beans, i just cannot figure it out. Thanks in advance. ras
Just because the IO activity is placed in a different class it doesn't mean that using that class from within an ejb is legal per the spec. If IO used within the context of an ejb (Entity, Session, or MDB) than it is breaking the rules. Many people think that helper classes used by an ejb are not under the same restrictions as the ejb. This is not true. The helper class is executing in the context of the ejb and hence needs to adhere the same rules. The reason IO is disallowed is for portability, transactioning, and clustering. Protability - Various file systems are accessed differently. Thus the use of the file system will tie you to the platform. For example, this could cause problems trying to move an ejb from a Windows Machine(multiple file roots) to a Solaris machine(with one file root). Transaction Support - Reading and writing of files cannot take place within a transaction. Thus rolling back changes will not undo changes made to any file during processing. Clustering - Ejbs spread across a cluster will likely not be accessing the same file system.Therefore writing changes to a file in one ejb will not cause those changes in all instances of that ejb in the cluster. This is inconsistent. No container that I know of actually enforces this restriction, so use at your own risk. Keep in mind, environment variables were created specifically to do the types of things that are being discussed here.
Thanks for the explanation Chris. Is there any way to log user activities by writing to a file in an application server? Could in be done in the web components? But then that would results back in the same situation u mention, wouldn;t it? Do you have any idea how logging could be accomplished? Thanks for the help
Hi Chris, Thanks for the reply Chris. About logging user activities, is it possible to use JAXP and log it in the form of xml. I haven't read up much about jaxp yet. Hope you could shed some light on this issue. If it is not possible then, I suppose i'll have to go with the way u suggested. Another thing i need to ask Chris. What about uploading a file to the server. Is it down to the same situation as well cause there must be some way of writting it to the server. What do you think Chris? Thanks for your time and patience Chris With lots of respect, Ras