aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes ejb and file I/o 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 "ejb and file I/o" Watch "ejb and file I/o" New topic
Author

ejb and file I/o

peter brews
Greenhorn

Joined: Feb 11, 2002
Posts: 20
I am new to ejb and I came to know that usage of the java.io package is forbidden in the spec but in my method implemented in ejb bean i need to write to a external file.And a shell script is then executed which reads this file and does furhter processing.Is there some other way to achive this result ans will there be problems if I use the following code.Any suggestion in this matter is highly apprecaited. Following is the piece of code.. from my method <html> <body>


try
{
PrintWriter pw=new PrintWriter(new BufferedWriter(new FileWriter(new File("c:\\testdata.txt"))));
pw.print("aaaaaa");
pw.print("bbbbbbbbbbbbbbbb");
pw.prnt(name);
pw.print(seqid);
pw.close();
}
catch(IOException e)
{
e.getMessage();
}
and then i am using following piece of code to run shell script from that method in ejb
try
{
String command = "sh /export/home//submitData.sh " + currentfile ;
Process p = Runtime.getRuntime().exec(command);
}
catch(Exception e)
{}

</body> </html> Thanks, peter
Jim Burns
Greenhorn

Joined: May 30, 2002
Posts: 1
Peter,
I think the reason that this is not allowed is so you can't compromise the running of the container by writing huge files to the server.
Perhaps this could be overcome by providing a servlet that writes to a file and calling the servlet from the Enterprise JavaBean.
Cheers,
Originally posted by peter brews:
I am new to ejb and I came to know that usage of the java.io package is forbidden in the spec but in my method implemented in ejb bean i need to write to a external file.And a shell script is then executed which reads this file and does furhter processing.Is there some other way to achive this result ans will there be problems if I use the following code.Any suggestion in this matter is highly apprecaited. Following is the piece of code.. from my method <html> <body>


try
{
PrintWriter pw=new PrintWriter(new BufferedWriter(new FileWriter(new File("c:\\testdata.txt"))));
pw.print("aaaaaa");
pw.print("bbbbbbbbbbbbbbbb");
pw.prnt(name);
pw.print(seqid);
pw.close();
}
catch(IOException e)
{
e.getMessage();
}
and then i am using following piece of code to run shell script from that method in ejb
try
{
String command = "sh /export/home//submitData.sh " + currentfile ;
Process p = Runtime.getRuntime().exec(command);
}
catch(Exception e)
{}

</body> </html> Thanks, peter
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1859
Process p = Runtime.getRuntime().exec(command);

I am sure you are not supposed to invoke the above line of code too.
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
OK, let's get something straight here. It's not "forbidden" by the Spec. That particular restriction is in a part of the spec that says "for maximum portability you should avoid this". However, in fact, every EJB server I know of will allow you to use java.io from within an EJB. Nobody really cares about this... However, I'd still probably wrap my file access into a utility Javabean and call that from my EJB...
Kyle


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
peter brews
Greenhorn

Joined: Feb 11, 2002
Posts: 20
Thank you all for your helpful replies.I code that i had mentioned i in my EJB actually works on my local machine ,but not sure if it will work when multiple users from diffrent systems will call that method.
1. I also wanted to know if the restriction is for security reasons only.
2. Will it be better if i put all this code in seperate java class and call that class in EJB.Does method should be static .If not ,how do i instantiate a class in EJB.
for eg.
I would write a class UtilityMethods.java an will define methods in that say writeData.
Then i will call this method as UtilityMethods.writeData and i will pass the data as string parameter to this method.
3.In most of other mymethods that were defined in EJB i am using url connection to pass and get information .Will there be any problem with that too?
4.Where can i get more details about EJB's.

Thanks,
pete
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
Originally posted by peter brews:
Thank you all for your helpful replies.I code that i had mentioned i in my EJB actually works on my local machine ,but not sure if it will work when multiple users from diffrent systems will call that method.
1. I also wanted to know if the restriction is for security reasons only.
2. Will it be better if i put all this code in seperate java class and call that class in EJB.Does method should be static .If not ,how do i instantiate a class in EJB.
for eg.
I would write a class UtilityMethods.java an will define methods in that say writeData.
Then i will call this method as UtilityMethods.writeData and i will pass the data as string parameter to this method.
3.In most of other mymethods that were defined in EJB i am using url connection to pass and get information .Will there be any problem with that too?
4.Where can i get more details about EJB's.

Thanks,
pete

The restriction isn't there for security reasons, but more for portability. Some EJB servers might not (theoretically) allow file I/O, but practically all of them do.
Yes, you should move the method into a utility class. However the method doesn't have to be static. You just create an instance of the object like you would any other object. Use the new operator with a constructor for the class.
Now if may I ask -- is this your first exposure to Java? If so RUN AWAY from EJB's at this point! You need to get the basics down before tackling more advanced issues like this.
So, first, read several dozen good Java books and get your Java skills up to speed. Then, if you're ready, read Richard Monson-Haefel's EJB book, or Ed Roman's EJB book -- but only after you've had a few months to master Java!
Kyle
[ June 03, 2002: Message edited by: Kyle Brown ]
HanMing Low
Ranch Hand

Joined: Oct 18, 2001
Posts: 196
Hi Peter,
Can you show how do you manage to get the file access working?
I've tried to do a file access on a stateless session bean on J2EE-RI and it throws a
java.security.AccessControlException: access denied (java.io.FilePermission f:\temp\test.txt write)
I wonder where can I specify the policy or configuration for the FileAccess for the directory.
Thanks for your advice.
Cheers.

Han Ming
peter brews
Greenhorn

Joined: Feb 11, 2002
Posts: 20
Sorry i had not visited this site since a couple of weeks.
Definea method in the bean
and try using following code.this works for me.
PrintWriter pw=new PrintWriter(new BufferedWriter(new FileWriter(new File("mytext.txt"))));

pete
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ejb and file I/o